about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-10-09 14:08:50 -0400
committerCharadon <dev@iotib.net>2022-10-09 14:08:50 -0400
commit6dca71e96c5f38033d51343ff6c9ddded9f899fe (patch)
tree4353cad5435de8e35320e35497222ddc4520172f /src
parent3fc34b2f768208e86010b7fae22ad8cc55cb2e62 (diff)
downloadPong-C-6dca71e96c5f38033d51343ff6c9ddded9f899fe.tar.gz
Fixed issue where controls that were mean't to be detected once would be detected multiple times.
Diffstat (limited to 'src')
-rw-r--r--src/controls.c23
-rw-r--r--src/pong.h1
-rw-r--r--src/title.c54
-rw-r--r--src/versus.c2
4 files changed, 65 insertions, 15 deletions
diff --git a/src/controls.c b/src/controls.c
index fca64da..a3cefa1 100644
--- a/src/controls.c
+++ b/src/controls.c
@@ -1,4 +1,5 @@
 #include "pong.h"
+#include <raylib.h>
 
 /* Checks what button the player is pressing amongst all control inputs. */
 int player_controls() {
@@ -20,18 +21,26 @@ int player_controls() {
 
 /* Same as player_controls() but check if the button was pressed, rather than held down. */
 int player_controls_pressed() {
+	int ReturnVal = -1;
 	if( IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsKeyPressed(KEY_SPACE) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_RIGHT_FACE_DOWN) ) {
-		return(CONTROLLER_ACTIVATE);
+		ReturnVal = CONTROLLER_ACTIVATE;
 	} else if( IsKeyPressed(KEY_ESCAPE) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_MIDDLE_RIGHT) ) {
-		return(CONTROLLER_PAUSE);
+		ReturnVal = CONTROLLER_PAUSE;
 	} else if( IsKeyPressed(KEY_A) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_LEFT) ) {
-		return(CONTROLLER_LEFT);
+		ReturnVal = CONTROLLER_LEFT;
 	} else if( IsKeyPressed(KEY_D) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_RIGHT) ) {
-		return(CONTROLLER_RIGHT);
+		ReturnVal = CONTROLLER_RIGHT;
 	} else if( IsKeyPressed(KEY_W) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_UP) ) {
-		return(CONTROLLER_UP);
+		ReturnVal = CONTROLLER_UP;
 	} else if( IsKeyPressed(KEY_S) || IsGamepadButtonPressed(1, GAMEPAD_BUTTON_LEFT_FACE_DOWN) ) {
-		return(CONTROLLER_DOWN);
+		ReturnVal = CONTROLLER_DOWN;
 	}
-	return(-1);
+	return(ReturnVal);
+}
+
+/* Calls BeginDrawing() and EndDrawing() to clear input buffer. */
+void clear_input_buffer() {
+	BeginDrawing();
+	EndDrawing();
+	return;
 }
diff --git a/src/pong.h b/src/pong.h
index 1103475..97f4457 100644
--- a/src/pong.h
+++ b/src/pong.h
@@ -75,5 +75,6 @@ void set_screen_mode();
 bool pause_screen(Camera2D *MainCamera);
 int player_controls();
 int player_controls_pressed();
+void clear_input_buffer();
 
 #endif
diff --git a/src/title.c b/src/title.c
index 5ab0bf3..9e0d4af 100644
--- a/src/title.c
+++ b/src/title.c
@@ -116,9 +116,11 @@ static void score_screen(Camera2D *MainCamera) {
     /* Page Buttons Rectangles. */
     Rectangle PrevPage = {5, 720-50, 70, 45};
     Rectangle NextPage = {1280-70, 720-50, 70, 45};
+	
 
     int Page = 0;
     bool EndOfPages = false;
+	bool BackButtonSelected = false;
     while(LookingAtScores == true && GameGoing == true) {
         /* Update Camera */
         MainCamera->zoom = GetScreenHeight()/720.0f;
@@ -164,6 +166,7 @@ static void score_screen(Camera2D *MainCamera) {
 				/* Page Buttons */
 				if(CheckCollisionRecs(MouseCursor, PrevPage) && Page > 0) {
 					DrawRectangleRec(PrevPage, RED);
+					BackButtonSelected = false;
 					if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
 						if(Page > 0) {
 							Page--;
@@ -171,27 +174,53 @@ static void score_screen(Camera2D *MainCamera) {
 					}
 				} else if(CheckCollisionRecs(MouseCursor, NextPage) && EndOfPages == false) {
 					DrawRectangleRec(NextPage, RED);
+					BackButtonSelected = false;
 					if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
 						if(EndOfPages == false) {
 							Page++;
 						}
 					}
 				}
+				switch(player_controls_pressed()) {
+					case CONTROLLER_RIGHT:
+						if(EndOfPages == false) {
+							++Page;
+						}
+						break;
+					case CONTROLLER_LEFT:
+						if(Page > 0) {
+							--Page;
+						}
+						break;
+					case CONTROLLER_UP:
+						BackButtonSelected = true;
+						break;
+					case CONTROLLER_ACTIVATE:
+						if(BackButtonSelected == true) {
+							LookingAtScores = false;
+						}
+						break;
+					default:
+						break;
+				}
 	            DrawText("<--", 5, 720-50, 48, WHITE);
 	            DrawText("-->", 1280-70, 720-50, 48, WHITE);
 				/* Exit Button */
-				if(CheckCollisionRecs(MouseCursor, (Rectangle){0,0,42,120})) {
+				if(CheckCollisionRecs(MouseCursor, (Rectangle){0,0,42,120}) || BackButtonSelected == true) {
 					DrawRectangle(0, 0, 42, 120, RED);
+					BackButtonSelected = true;
 					if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
 						LookingAtScores = false;
 					}
 				}
+				
 				DrawText("<", 0, 0, 128, WHITE);
 				/* Cursor */
             	DrawTexture(MouseCursorSprite, MouseCursor.x, MouseCursor.y, WHITE);
             EndMode2D();
         EndDrawing();
     }
+	clear_input_buffer();
     return;
 }
 
@@ -490,12 +519,7 @@ int title_screen() {
 			default:
 				break;
 		}
-		/* Make sure choice selector doesn't go off screen */
-		if(Choice > 4) {
-			Choice = 4;
-		} else if(Choice < 0) {
-			Choice = 0;
-		}
+		
 
 		/* Unselect the Leaderboard button if Marathon is no longer selected. */
 		if(Choice != 1) {
@@ -534,25 +558,37 @@ int title_screen() {
 			default:
 				break;
         } 
-		
 		/* Activate menu item */
 		if( player_controls_pressed() == CONTROLLER_ACTIVATE ){
 			switch(Choice) {
 				case 2:
+					clear_input_buffer();
 					settings(&MainCamera, TitleMusic);
 					break;
 				case 3:
+					clear_input_buffer();
 					OpenURL("docs/index.html");
 					EnableCursor();
 					break;
 				case 4:
 					TitleScreenGoing = false;
 					GameGoing = false;
+					break;
+				case -1:
+					TitleScreenGoing = true;
+					break;
 				default:
 					TitleScreenGoing = false;
 					break;
 			}
 		}
+		
+		/* Make sure choice selector doesn't go off screen */
+		if(Choice > 4) {
+			Choice = 4;
+		} else if(Choice < 0) {
+			Choice = 0;
+		}
 
         BeginDrawing();
             ClearBackground(BLACK);
@@ -587,6 +623,7 @@ int title_screen() {
 						DrawRectangleRec(AllScores, RED);
 						if(player_controls_pressed() == CONTROLLER_ACTIVATE) {
 							score_screen(&MainCamera);
+							Choice = -1;
 						}
 					}
 					DrawText("See All Scores...", 600, 720-150, 48, WHITE);
@@ -596,6 +633,7 @@ int title_screen() {
         	DrawText(VersionString, GetScreenWidth()-400, GetScreenHeight()-32, 32, GREEN);
         EndDrawing();
     }
+	printf("%d\n", Choice);
     Mix_HaltMusic();
     Mix_FreeMusic(TitleMusic);
     return Choice;
diff --git a/src/versus.c b/src/versus.c
index bf6027e..fa92fe5 100644
--- a/src/versus.c
+++ b/src/versus.c
@@ -70,6 +70,7 @@ void versus_main() {
 				BeginDrawing();
 					EnableCursor();
 					ClearBackground(BLACK);
+					DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255});
 					DrawText("You Lose.", 0, 0, 128, RED);
 					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
 				EndDrawing();
@@ -84,6 +85,7 @@ void versus_main() {
 				BeginDrawing();
 					EnableCursor();
 					ClearBackground(BLACK);
+					DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255});
 					DrawText("You Win!", 0, 0, 128, BLUE);
 					DrawText("Press space to return to title screen.", 0, 128, 24, WHITE);
 				EndDrawing();