commit ce5aa7d3
Controllers now wotk to play the game. Only player 1 is supported at the moment.
Changed files
| M | source/code/gamecontroller.cpp before |
| M | source/code/gamecontroller.h before |
| M | source/code/main.cpp before |
diff --git a/source/code/gamecontroller.cpp b/source/code/gamecontroller.cpp
index 20e8aee..1162d0d 100644
--- a/source/code/gamecontroller.cpp
+++ b/source/code/gamecontroller.cpp
@@ -35,3 +35,75 @@ void InitGameControllers() {
}
}
}
+
+bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) {
+ if (playerNumber != 1) {
+ return false;
+ }
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) {
+ if (playerNumber != 1) {
+ return false;
+ }
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) {
+ if (playerNumber != 1) {
+ return false;
+ }
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool isPlayerRightEvent(int playerNumber, const SDL_Event& event) {
+ if (playerNumber != 1) {
+ return false;
+ }
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool isPlayerSwitchEvent(int playerNumber, const SDL_Event& event) {
+ if (playerNumber != 1) {
+ return false;
+ }
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool isPlayerPushEvent(int playerNumber, const SDL_Event& event) {
+ if (playerNumber != 1) {
+ return false;
+ }
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER || event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
+ return true;
+ }
+ }
+ return false;
+}
\ No newline at end of file
diff --git a/source/code/gamecontroller.h b/source/code/gamecontroller.h
index d6c02d6..90c7c43 100644
--- a/source/code/gamecontroller.h
+++ b/source/code/gamecontroller.h
@@ -21,4 +21,13 @@ http://www.blockattack.net
===========================================================================
*/
-void InitGameControllers();
\ No newline at end of file
+#include "SDL.h"
+
+void InitGameControllers();
+
+bool isPlayerDownEvent(int playerNumber, const SDL_Event& event);
+bool isPlayerUpEvent(int playerNumber, const SDL_Event& event);
+bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event);
+bool isPlayerRightEvent(int playerNumber, const SDL_Event& event);
+bool isPlayerSwitchEvent(int playerNumber, const SDL_Event& event);
+bool isPlayerPushEvent(int playerNumber, const SDL_Event& event);
\ No newline at end of file
diff --git a/source/code/main.cpp b/source/code/main.cpp
index 3b8fc4a..17b100c 100644
--- a/source/code/main.cpp
+++ b/source/code/main.cpp
@@ -1922,6 +1922,10 @@ int runGame(int gametype, int level) {
Config::getInstance()->setShuttingDown(5);
done = 1;
}
+
+ if (theGame.isGameOver() && isEscapeEvent(event)) {
+ done = 1;
+ }
if ( event.type == SDL_KEYDOWN ) {
if ( event.key.keysym.sym == SDLK_ESCAPE || ( event.key.keysym.sym == SDLK_RETURN && theGame.isGameOver() ) ) {
@@ -1999,6 +2003,25 @@ int runGame(int gametype, int level) {
done=1;
}
}
+
+ if (isPlayerUpEvent(1, event)) {
+ theGame.MoveCursor('N');
+ }
+ if (isPlayerDownEvent(1, event)) {
+ theGame.MoveCursor('S');
+ }
+ if (isPlayerLeftEvent(1, event)) {
+ theGame.MoveCursor('W');
+ }
+ if (isPlayerRightEvent (1, event)) {
+ theGame.MoveCursor('E');
+ }
+ if (isPlayerSwitchEvent(1, event)) {
+ theGame.SwitchAtCursor();
+ }
+ if (isPlayerPushEvent(1, event)) {
+ theGame.PushLine();
+ }
} //while event PollEvent - read keys
SDL_GetMouseState(&mousex,&mousey);