open game controller devices and begin receiving events
This commit is contained in:
parent
c2c36d4be4
commit
0fffdfb38f
@ -3,6 +3,7 @@
|
||||
#include "GL3/gl3w.h"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <list>
|
||||
|
||||
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<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);
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user