diff --git a/sdl-game-controller.cc b/sdl-game-controller.cc index dc55aa3..2b960c5 100644 --- a/sdl-game-controller.cc +++ b/sdl-game-controller.cc @@ -3,6 +3,7 @@ #include "GL3/gl3w.h" #include #include +#include using namespace std; @@ -78,7 +79,7 @@ void display(SDL_Window * window) int main(int argc, char *argv[]) { - if (SDL_Init(SDL_INIT_VIDEO)) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS)) { printf("Failed to initialize SDL!\n"); return 1; @@ -118,6 +119,23 @@ int main(int argc, char *argv[]) return 2; } + int n_joysticks = SDL_NumJoysticks(); + std::list game_controllers; + for (int i = 0; i < n_joysticks; i++) + { + if (SDL_IsGameController(i)) + { + SDL_GameController * game_controller = SDL_GameControllerOpen(i); + if (game_controller == nullptr) + { + cerr << "Failed to open game controller " << i << ": " << SDL_GetError() << endl; + return 2; + } + game_controllers.push_back(game_controller); + } + } + cout << "Found " << n_joysticks << " joystick devices, " << game_controllers.size() << " of which are game controllers" << endl; + display(window); SDL_Event event; bool exit = false; @@ -132,11 +150,24 @@ int main(int argc, char *argv[]) if (event.key.keysym.sym == SDLK_ESCAPE) exit = true; } + else if (event.type == SDL_CONTROLLERAXISMOTION) + { + std::cerr << "motion!" << std::endl; + } + else if (event.type == SDL_CONTROLLERBUTTONDOWN) + { + std::cerr << "button!" << std::endl; + } } if (exit) break; display(window); } + for (auto game_controller : game_controllers) + { + SDL_GameControllerClose(game_controller); + } + return 0; }