diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp index 134ac99..741b1dd 100644 --- a/source/code/MenuSystem.cpp +++ b/source/code/MenuSystem.cpp @@ -179,6 +179,20 @@ Menu::Menu(SDL_Renderer* screen, const string& title, bool submenu) { } } +bool isUpEvent(const SDL_Event& event) { + if ( event.type == SDL_KEYDOWN ) { + if (event.key.keysym.sym == SDLK_UP) { + return true; + } + } + if (event.type == SDL_CONTROLLERBUTTONDOWN) { + if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP || event.cbutton.button == SDL_CONTROLLERBUTTONUP ) { + return true; + } + } + return false; +} + void Menu::run() { running = true; bool bMouseUp = false; @@ -197,19 +211,19 @@ void Menu::run() { Config::getInstance()->setShuttingDown(5); running = false; } + + if (isUpEvent(event)) { + marked--; + if (marked<0) { + marked = buttons.size(); //not -1, since exit is after the last element in the list + } + } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { running = false; } - if (event.key.keysym.sym == SDLK_UP) { - marked--; - if (marked<0) { - marked = buttons.size(); //not -1, since exit is after the last element in the list - } - } - if (event.key.keysym.sym == SDLK_DOWN) { marked++; if (marked> (int)buttons.size()) { @@ -229,6 +243,27 @@ void Menu::run() { } } } + + if (event.type == SDL_CONTROLLERBUTTONDOWN) { + std::cout << "Game controller key pressed" << std::endl; + if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN || event.cbutton.button == SDL_CONTROLLERBUTTONDOWN ) { + marked++; + if (marked> (int)buttons.size()) { + marked = 0; + } + } + if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) { + if (marked < (int)buttons.size()) { + buttons.at(marked)->doAction(); + if (buttons.at(marked)->isPopOnRun()) { + running = false; + } + } + if (marked == (int)buttons.size()) { + running = false; + } + } + } } diff --git a/source/code/common.h b/source/code/common.h index a445330..cd5d48b 100644 --- a/source/code/common.h +++ b/source/code/common.h @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/ Source information and contacts persons can be found at -http://blockattack.net +http://www.blockattack.net =========================================================================== */ diff --git a/source/code/gamecontroller.cpp b/source/code/gamecontroller.cpp new file mode 100644 index 0000000..20e8aee --- /dev/null +++ b/source/code/gamecontroller.cpp @@ -0,0 +1,37 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2016 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +http://www.blockattack.net +=========================================================================== +*/ + +#include "gamecontroller.h" +#include "SDL_gamecontroller.h" +#include + +void InitGameControllers() { + std::cout << "Number of Game controllers: " << SDL_NumJoysticks() << std::endl; + SDL_GameController *controller = nullptr; + for (int i = 0; i < SDL_NumJoysticks(); ++i) { + if (SDL_IsGameController(i)) { + controller = SDL_GameControllerOpen(i); + std::cout << "Supported game controller detected: " << SDL_GameControllerName(controller) << std::endl; + } + } +} diff --git a/source/code/gamecontroller.h b/source/code/gamecontroller.h new file mode 100644 index 0000000..d6c02d6 --- /dev/null +++ b/source/code/gamecontroller.h @@ -0,0 +1,24 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2016 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +http://www.blockattack.net +=========================================================================== +*/ + +void InitGameControllers(); \ No newline at end of file diff --git a/source/code/main.cpp b/source/code/main.cpp index a139876..d32ddee 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -81,8 +81,8 @@ http://www.blockattack.net //#include "uploadReplay.h" //Takes care of everything libcurl related #include "common.h" +#include "gamecontroller.h" #include -#include /******************************************************************************* * All variables and constant has been moved to mainVars.inc for the overview. * @@ -1569,6 +1569,10 @@ int main(int argc, char* argv[]) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { sago::SagoFatalErrorF("Unable to init SDL: %s", SDL_GetError()); } + if (SDL_Init(SDL_INIT_GAMECONTROLLER ) != 0) { + cerr << "Warning: Game controller failed to initialize. Reason: " << SDL_GetError() << endl; + } + InitGameControllers(); TTF_Init(); atexit(SDL_Quit); //quits SDL when the game stops for some reason (like you hit exit or Esc)