about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-06-02 12:09:14 -0400
committerCharadon <dev@iotib.net>2022-06-02 12:09:14 -0400
commite372341eb2d92ee7df6fcaf43f4dc2a27967e390 (patch)
tree0454572596e26e5c94bdb0ffba88e0e6edc08a76 /src
parent66881a0a3136c871fb0c58aa31be174c60bcf118 (diff)
downloadPong-C-e372341eb2d92ee7df6fcaf43f4dc2a27967e390.tar.gz
Seperated versus mode from main.c
Diffstat (limited to 'src')
-rw-r--r--src/main.c134
-rw-r--r--src/pong.h3
-rw-r--r--src/title.c9
-rw-r--r--src/versus.c121
4 files changed, 138 insertions, 129 deletions
diff --git a/src/main.c b/src/main.c
index de4dcb0..11c69b7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -98,144 +98,30 @@ int main() {
 	SetWindowState(FLAG_WINDOW_RESIZABLE);
 	SetWindowMinSize(1280, 720);
 	
-	//Initialize Variables
-	Camera2D MainCamera;
-	MainCamera.target = (Vector2){0, 0};
-	MainCamera.offset = (Vector2){0, 0};
-	MainCamera.rotation = 0;
 	//Populate Audio Queue
 	for(unsigned int i = 0; i < 20; i++) {
 		AudioQueue[i] = -1;
 	}
 	mtx_init(&AudioQueueBeingModified, mtx_plain);
 
-	// Init Player Variables
-	struct Players Player;
-	Player.Y = 0;
-	Player.Score = 0;
-	char PlayerScore[50]; // Used later to display score on screen.
-
-	// Init Enemy Variables
-	struct Players Enemy;
-	Enemy.Y = 0;
-	Enemy.Score = 0;
-	Enemy.Direction = 0;
-	char EnemyScore[50];
-
-	struct Balls Ball;
-	Ball.X = 1280/2.0f;
-	Ball.Y = 720/2.0f;
-	Ball.Direction = LEFT;
-	Ball.Speed = 3.0f;
-	Ball.Angle = 0.0f;
-	
-	
-	// Set Sprites
-	Texture2D PaddleSprite = LoadTexture("resources/paddle.png");
-	Texture2D BallSprite = LoadTexture("resources/ball.png");
-	
-	// Set Collision Boxes
-	Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height};
-	Enemy.HitBox = (Rectangle){1200, Enemy.Y, 5, PaddleSprite.height};
-	Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height};
-	Enemy.BallDetector = (Rectangle){0, Enemy.Y+120, 1280, PaddleSprite.height/5.0f};
-
 	// Initialize Internal Clock
 	thrd_t InternalClock;
 	thrd_create(&InternalClock, internal_clock, NULL);
 	thrd_t AudioThread;
 	thrd_create(&AudioThread, audio, NULL);
 	
-	// Launch Title Screen
-	jmp_buf RestartGame;
-	setjmp(RestartGame);
-	title_screen();
-	play_audio(99);
-	Enemy.Score = 0;
-	Player.Score = 0;
-	
-	while(!WindowShouldClose() && GameGoing == true) {
-		MainCamera.zoom = GetScreenHeight()/720.0f;
-		MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f};
-		MainCamera.target = (Vector2){1280/2.0f, 720/2.0f};
-
-
-		// Who won?
-		if (Enemy.Score >= 5) {
-			StopSoundMulti();
-			play_audio(MUSIC_DEFEAT);
-			while(!IsKeyDown(KEY_SPACE)) {
-				BeginDrawing();
-					EnableCursor();
-					ClearBackground(BLACK);
-					DrawText("You Lose.", 0, 0, 128, RED);
-					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
-				EndDrawing();
-			}
-			longjmp(RestartGame, 0);
-		} else if (Player.Score >= 5) {
-			StopSoundMulti();
-			play_audio(MUSIC_VICTORY);
-			while(!IsKeyDown(KEY_SPACE)) {
-				BeginDrawing();
-					EnableCursor();
-					ClearBackground(BLACK);
-					DrawText("You Win!", 0, 0, 128, BLUE);
-					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
-				EndDrawing();
-			}
-			longjmp(RestartGame, 0);
-		}
-
-		//Controls
-		if(IsKeyDown(KEY_UP)) {
-			Player.Y -= 10;
-		} else if (IsKeyDown(KEY_DOWN)) {
-			Player.Y += 10;
-		} else if(IsKeyPressed(KEY_ESCAPE)) {
-			EnableCursor();
-		} else if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsCursorHidden() == true) {
-			Player.Y = GetMouseY()-PaddleSprite.height/2.0f;
-			DisableCursor();
-		}
-		
-		if(GetMouseY() < 0) {
-			SetMousePosition(0, 0);
-		} else if(GetMouseY() > 720) {
-			SetMousePosition(0, 720);
-		}
-
-		//Check if players are off-screen
-		if (Player.Y < 0) {
-			Player.Y = 0;
-		} else if (Player.Y > 480) {
-			Player.Y = 480;
+	// Launch Game
+	while (GameGoing == true) {
+		switch(title_screen()) {
+			case 0: //Versus
+				play_audio(STOP_ALL_SOUNDS);
+				versus_main();
+				break;
+			default:
+				break;
 		}
-
-		enemy(&Enemy, Ball);
-		
-		// Collision
-		ball(&Player.HitBox, &Enemy.HitBox, &Ball, &Player.Score, &Enemy.Score);
-
-		//Updates hitbox with player's position.
-		Player.HitBox.y = Player.Y;
-
-		//Turn Scores into strings.
-		snprintf(PlayerScore, 50, "Player: %d", Player.Score);
-		snprintf(EnemyScore, 50, "Enemy: %d", Enemy.Score);
-		
-		BeginDrawing();
-			ClearBackground(BLACK);
-			BeginMode2D(MainCamera);
-				DrawTexture(PaddleSprite, 0, Player.Y, WHITE);
-				DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE);
-				DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE);
-				//DrawRectangleRec(Enemy.BallDetector, RED);
-				DrawText(PlayerScore, 0, 0, 32, GREEN);
-				DrawText(EnemyScore, 1130, 688, 32, RED);
-			EndMode2D();
-		EndDrawing();
 	}
+	
 	GameGoing = false;
 	thrd_join(AudioThread, NULL);
 	CloseWindow();
diff --git a/src/pong.h b/src/pong.h
index 70e9215..26ec9c2 100644
--- a/src/pong.h
+++ b/src/pong.h
@@ -38,7 +38,8 @@ extern atomic_int Ticks;
 void enemy(struct Players *Enemy, struct Balls ball);
 void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerScore, int *EnemyScore);
 bool play_audio(int SoundEffect);
-void title_screen();
+int title_screen();
+void versus_main();
 
 //Sounds
 extern const int SOUND_BOUNCE;
diff --git a/src/title.c b/src/title.c
index 1f2a51d..56e382c 100644
--- a/src/title.c
+++ b/src/title.c
@@ -4,7 +4,8 @@ void help_text() {
 
 }
 
-void title_screen() {
+int title_screen() {
+    // Init Camera
     Camera2D MainCamera;
     MainCamera.offset = (Vector2){0,0};
     MainCamera.target = (Vector2){0,0};
@@ -38,7 +39,7 @@ void title_screen() {
         if (CheckCollisionRecs(Mouse, Versus)) {
             Selected = &Versus;
             if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
-                return; 
+                return 0; 
             }
         } else if (CheckCollisionRecs(Mouse, Marathon)) {
             Selected = &Marathon;
@@ -50,7 +51,7 @@ void title_screen() {
             Selected = &Exit;
             if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
                 GameGoing = false;
-                return;
+                return -1;
             }
         }
         BeginDrawing();
@@ -67,5 +68,5 @@ void title_screen() {
             EndMode2D();
         EndDrawing();
     }
-    return;
+    return -1;
 }
diff --git a/src/versus.c b/src/versus.c
new file mode 100644
index 0000000..a5c1060
--- /dev/null
+++ b/src/versus.c
@@ -0,0 +1,121 @@
+#include "pong.h"
+
+void versus_main() {
+    // Init Player Variables
+	struct Players Player;
+	Player.Y = 0;
+	Player.Score = 0;
+
+	// Init Enemy Variables
+	struct Players Enemy;
+	Enemy.Y = 0;
+	Enemy.Score = 0;
+	Enemy.Direction = 0;
+
+    // Init Ball Variables
+	struct Balls Ball;
+	Ball.X = 1280/2.0f;
+	Ball.Y = 720/2.0f;
+	Ball.Direction = LEFT;
+	Ball.Speed = 3.0f;
+	Ball.Angle = 0.0f;
+    
+    // Set Sprites
+	Texture2D PaddleSprite = LoadTexture("resources/paddle.png");
+	Texture2D BallSprite = LoadTexture("resources/ball.png");
+
+	// Set Collision Boxes
+	Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height};
+	Enemy.HitBox = (Rectangle){1200, Enemy.Y, 5, PaddleSprite.height};
+	Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height};
+	Enemy.BallDetector = (Rectangle){0, Enemy.Y+120, 1280, PaddleSprite.height/5.0f};
+    char EnemyScore[50];
+    char PlayerScore[50]; // Used later to display score on screen.
+        // Init Camera
+    Camera2D MainCamera;
+        MainCamera.target = (Vector2){0, 0};
+        MainCamera.offset = (Vector2){0, 0};
+        MainCamera.rotation = 0;
+    while(!WindowShouldClose() && GameGoing == true) {
+		MainCamera.zoom = GetScreenHeight()/720.0f;
+		MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f};
+		MainCamera.target = (Vector2){1280/2.0f, 720/2.0f};
+
+		// Who won?
+		if (Enemy.Score >= 5) {
+			StopSoundMulti();
+			play_audio(MUSIC_DEFEAT);
+			while(!IsKeyDown(KEY_SPACE)) {
+				BeginDrawing();
+					EnableCursor();
+					ClearBackground(BLACK);
+					DrawText("You Lose.", 0, 0, 128, RED);
+					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
+				EndDrawing();
+			}
+			return;
+		} else if (Player.Score >= 5) {
+			StopSoundMulti();
+			play_audio(MUSIC_VICTORY);
+			while(!IsKeyDown(KEY_SPACE)) {
+				BeginDrawing();
+					EnableCursor();
+					ClearBackground(BLACK);
+					DrawText("You Win!", 0, 0, 128, BLUE);
+					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
+				EndDrawing();
+			}
+			return;
+		}
+
+		//Controls
+		if(IsKeyDown(KEY_UP)) {
+			Player.Y -= 10;
+		} else if (IsKeyDown(KEY_DOWN)) {
+			Player.Y += 10;
+		} else if(IsKeyPressed(KEY_ESCAPE)) {
+			EnableCursor();
+		} else if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsCursorHidden() == true) {
+			Player.Y = GetMouseY()-PaddleSprite.height/2.0f;
+			DisableCursor();
+		}
+		
+		if(GetMouseY() < 0) {
+			SetMousePosition(0, 0);
+		} else if(GetMouseY() > 720) {
+			SetMousePosition(0, 720);
+		}
+
+		//Check if players are off-screen
+		if (Player.Y < 0) {
+			Player.Y = 0;
+		} else if (Player.Y > 480) {
+			Player.Y = 480;
+		}
+
+		enemy(&Enemy, Ball);
+		
+		// Collision
+		ball(&Player.HitBox, &Enemy.HitBox, &Ball, &Player.Score, &Enemy.Score);
+
+		//Updates hitbox with player's position.
+		Player.HitBox.y = Player.Y;
+
+		//Turn Scores into strings.
+		snprintf(PlayerScore, 50, "Player: %d", Player.Score);
+		snprintf(EnemyScore, 50, "Enemy: %d", Enemy.Score);
+		
+		BeginDrawing();
+			ClearBackground(BLACK);
+			BeginMode2D(MainCamera);
+				DrawTexture(PaddleSprite, 0, Player.Y, WHITE);
+				DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE);
+				DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE);
+				//DrawRectangleRec(Enemy.BallDetector, RED);
+				DrawText(PlayerScore, 0, 0, 32, GREEN);
+				DrawText(EnemyScore, 1130, 688, 32, RED);
+			EndMode2D();
+		EndDrawing();
+	}
+    return;
+}
16 ^
d326f24d ^
e4409c40 ^

2cd8e80b ^
8da648ea ^
10f6c5df ^
8da648ea ^
d4a8ec3c ^
244b955f ^



2b52e9e6 ^
e4409c40 ^
2b52e9e6 ^


e4409c40 ^
















945e8eb6 ^
e4409c40 ^


945e8eb6 ^



e4409c40 ^








e4409c40 ^
10f6c5df ^
e4409c40 ^






d1349dd6 ^

a8251776 ^






affaa4f7 ^







d31fb3c4 ^







d3f9d547 ^

02f28514 ^

2b52e9e6 ^
d326f24d ^
2b52e9e6 ^

86b4d41a ^
d326f24d ^
86b4d41a ^

45f1aadc ^




a2a1ab1e ^

2b52e9e6 ^
50f9f94b ^
c8a4110f ^



50f9f94b ^
a2a1ab1e ^

04a31662 ^





38fbf2f2 ^
04a31662 ^

a2a1ab1e ^
04a31662 ^

e087f6d4
04a31662 ^
9a7862a6 ^
e4409c40 ^
9a7862a6 ^
04a31662 ^
a2a1ab1e ^

d326f24d ^
e4409c40 ^

a2a1ab1e ^




e4409c40 ^
08fc6e5c ^
1fc1d8af ^




9c512bba ^




03413d1c ^
4bb52da7 ^
e087f6d4

d1349dd6 ^



0cb11571 ^




b9e930ec ^
0cb11571 ^





e087f6d4


08fc6e5c ^
e087f6d4
08fc6e5c ^
e087f6d4
08fc6e5c ^
e087f6d4
d326f24d ^
e087f6d4





244b955f ^
e087f6d4
244b955f ^










d326f24d ^
244b955f ^


e087f6d4


d326f24d ^
e087f6d4






d326f24d ^
e087f6d4
e087f6d4




e087f6d4

d326f24d ^
e087f6d4
e087f6d4
e087f6d4









e087f6d4



f6e997b7 ^

0cb11571 ^
4525eb4b ^

ab1d1ae5 ^


4525eb4b ^

ab1d1ae5 ^
1fc1d8af ^

f6e997b7 ^
1fc1d8af ^
f6e997b7 ^
e087f6d4

c7bfda90 ^


a2a1ab1e ^
c7bfda90 ^






e087f6d4








d326f24d ^

e087f6d4




d326f24d ^

e087f6d4
d326f24d ^
e087f6d4
04a31662 ^


e087f6d4


04a31662 ^
03413d1c ^
e087f6d4




c3ec4181 ^

d326f24d ^



e087f6d4



5b288495 ^
e087f6d4
aa7067e1 ^
d326f24d ^
aa7067e1 ^
5b288495 ^
e087f6d4


aa7067e1 ^
e087f6d4
87434eaa ^


e087f6d4

bed9a2c7 ^




fbcb509d ^
bed9a2c7 ^
87434eaa ^
bed9a2c7 ^




08fc6e5c ^
d326f24d ^


d326f24d ^
d326f24d ^
bed9a2c7 ^

e087f6d4
e087f6d4
6cb04c09 ^
d4a8ec3c ^





2a256611 ^

6cb04c09 ^
2a256611 ^
2a256611 ^
d3f9d547 ^
e087f6d4
2a256611 ^




6cb04c09 ^
0cb11571 ^
2a256611 ^

d326f24d ^
e087f6d4
6cb04c09 ^
e087f6d4
d326f24d ^
2a256611 ^
e087f6d4









af9be28b ^
e087f6d4
e087f6d4
50f9f94b ^






e087f6d4




50f9f94b ^






50f9f94b ^

5b288495 ^

50f9f94b ^



86b4d41a ^
e087f6d4
533c7482 ^
d3f9d547 ^
e087f6d4




08fc6e5c ^
e087f6d4
08fc6e5c ^
e087f6d4
d326f24d ^

e087f6d4
4bb52da7 ^





8c68f693 ^
4bb52da7 ^

d1349dd6 ^
4bb52da7 ^



17f50de9 ^
4bb52da7 ^

f78e2771 ^
03413d1c ^
fa5da88c ^



4bb52da7 ^
bff0a2c0 ^



8c68f693 ^
d1349dd6 ^
8c68f693 ^
d326f24d ^
8c68f693 ^

d1349dd6 ^

d3f9d547 ^

4bb52da7 ^
d3f9d547 ^
a2a1ab1e ^
0eae931d ^

d3f9d547 ^





d326f24d ^
d3f9d547 ^
0eae931d ^
d3f9d547 ^
08fc6e5c ^
d3f9d547 ^










d31fb3c4 ^




















d3f9d547 ^

d326f24d ^
d3f9d547 ^
d31fb3c4 ^
00405b6d ^
d31fb3c4 ^
03413d1c ^
d31fb3c4 ^




03413d1c ^
d31fb3c4 ^
03413d1c ^









00405b6d ^



03413d1c ^
d326f24d ^
d31fb3c4 ^




00405b6d ^
03413d1c ^

d31fb3c4 ^
d31fb3c4 ^

0cb11571 ^
d326f24d ^
d83d4af1 ^
d83d4af1 ^

0cb11571 ^
d326f24d ^
d83d4af1 ^
d83d4af1 ^


d326f24d ^
d83d4af1 ^

0654c702 ^

1fc1d8af ^






















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680