about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-07-14 04:50:04 -0400
committerCharadon <dev@iotib.net>2022-07-14 04:50:04 -0400
commit170c83ae7f87676274508a0974737a0a39dec08b (patch)
treef7113756c0ca89d27ca2687455d1ce5a58360175
parent69e0f6c8180c1d0cffd2f719b8c1219c999d34bb (diff)
downloadPong-C-170c83ae7f87676274508a0974737a0a39dec08b.tar.gz
Added gamepad and keyboard support to versus mode
-rw-r--r--Pong.make2
-rw-r--r--src/versus.c27
2 files changed, 20 insertions, 9 deletions
diff --git a/Pong.make b/Pong.make
index 50abfbf..ffc4c0e 100644
--- a/Pong.make
+++ b/Pong.make
@@ -26,7 +26,7 @@ DEFINES +=
 INCLUDES +=
 FORCE_INCLUDE +=
 ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
-ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -Og -std=c89 -g -ggdb `pkg-config --cflags raylib` `pkg-config --cflags glfw3` `pkg-config --cflags gl` `pkg-config --cflags sdl2` `pkg-config --cflags SDL2_mixer` `pkg-config --cflags glu`
+ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -Og -std=c99 -g -ggdb `pkg-config --cflags raylib` `pkg-config --cflags glfw3` `pkg-config --cflags gl` `pkg-config --cflags sdl2` `pkg-config --cflags SDL2_mixer` `pkg-config --cflags glu`
 ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -Og -g -ggdb `pkg-config --cflags raylib` `pkg-config --cflags glfw3` `pkg-config --cflags gl` `pkg-config --cflags sdl2` `pkg-config --cflags SDL2_mixer` `pkg-config --cflags glu`
 ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
 LIBS +=
diff --git a/src/versus.c b/src/versus.c
index 30bccd1..d808b80 100644
--- a/src/versus.c
+++ b/src/versus.c
@@ -1,4 +1,5 @@
 #include "pong.h"
+#include <raylib.h>
 
 void versus_main() {
     /* Init Player Variables */
@@ -45,12 +46,16 @@ void versus_main() {
 	bool VersusGoing = true;
 	Ball.NextTick = SDL_AtomicGet(&Ticks);
 	Enemy.NextTick = SDL_AtomicGet(&Ticks);
+
+	Vector2 MouseLastPosition = GetMousePosition();
+	Vector2 MouseCurrentPosition = GetMousePosition();
+	bool MouseMoved = false;
+
     while(VersusGoing == true && GameGoing == true) {
 		
 		if (WindowShouldClose()) { /* Quit Game if the window is closed. */
             GameGoing = false;
         }
-
 		MainCamera.zoom = GetScreenHeight()/720.0f;
 		MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f};
 		MainCamera.target = (Vector2){1280/2.0f, 720/2.0f};
@@ -87,11 +92,17 @@ void versus_main() {
 		}
 
 		/* Controls */
-		if(IsKeyDown(KEY_UP)) {
-			Player.Y -= 10;
-		} else if (IsKeyDown(KEY_DOWN)) {
-			Player.Y += 10;
-		} else if(IsKeyPressed(KEY_ESCAPE)) {
+		MouseMoved = false;
+		MouseCurrentPosition = GetMousePosition();
+		if (MouseCurrentPosition.y != MouseLastPosition.y) {
+			MouseMoved = true;
+		}
+		MouseLastPosition = MouseCurrentPosition;
+		if(IsKeyDown(KEY_W) || IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_UP)) {
+			Player.Y -= 20;
+		} else if (IsKeyDown(KEY_S) || IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) {
+			Player.Y += 20;
+		} else if (IsKeyPressed(KEY_ESCAPE)) {
 				Mix_PauseMusic();
 				BeginDrawing();
 				EndDrawing();
@@ -99,10 +110,10 @@ void versus_main() {
 				Mix_ResumeMusic();
 				Ball.NextTick = SDL_AtomicGet(&Ticks)+1;
 				Enemy.NextTick = SDL_AtomicGet(&Ticks)+1;
-		} else if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsCursorHidden() == true) {
+		} else if (MouseMoved == true) {
 			Player.Y = GetMouseY()-PaddleSprite.height/2.0f;
 			DisableCursor();
-		} 
+		}
 		
 		/* Leave Game */
 		if(IsKeyPressed(KEY_Q)) {