open game controller devices and begin receiving events

This commit is contained in:
Josh Holtrop 2018-01-15 21:26:54 -05:00
parent c2c36d4be4
commit 0fffdfb38f

View File

@ -3,6 +3,7 @@
#include "GL3/gl3w.h" #include "GL3/gl3w.h"
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <list>
using namespace std; using namespace std;
@ -78,7 +79,7 @@ void display(SDL_Window * window)
int main(int argc, char *argv[]) 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"); printf("Failed to initialize SDL!\n");
return 1; return 1;
@ -118,6 +119,23 @@ int main(int argc, char *argv[])
return 2; return 2;
} }
int n_joysticks = SDL_NumJoysticks();
std::list<SDL_GameController *> 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); display(window);
SDL_Event event; SDL_Event event;
bool exit = false; bool exit = false;
@ -132,11 +150,24 @@ int main(int argc, char *argv[])
if (event.key.keysym.sym == SDLK_ESCAPE) if (event.key.keysym.sym == SDLK_ESCAPE)
exit = true; 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) if (exit)
break; break;
display(window); display(window);
} }
for (auto game_controller : game_controllers)
{
SDL_GameControllerClose(game_controller);
}
return 0; return 0;
} }