working on game controller

This commit is contained in:
2021-08-25 12:02:18 +02:00
parent c3d560a9b6
commit 36eaf1de4f
3 changed files with 76 additions and 27 deletions

View File

@@ -133,20 +133,6 @@ bool Director::initSDL()
} }
else else
{ {
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
int nJoysticks = SDL_NumJoysticks();
int numGamepads = 0;
// Count how many controllers there are
for (int i = 0; i < nJoysticks; i++)
if (SDL_IsGameController(i))
numGamepads++;
printf("%i nJoysticks, %i numGamepads\n", nJoysticks, numGamepads);
//SDL_JoystickEventState(SDL_ENABLE);
// Establece el filtro de la textura a nearest // Establece el filtro de la textura a nearest
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0")) if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"))
{ {

View File

@@ -15,17 +15,43 @@ Input::Input(int source)
mGameControllerBindings[i].active = false; mGameControllerBindings[i].active = false;
} }
mGameControllerFound = false; mSource = source;
if (mSource == USE_GAMECONTROLLER)
{
/*
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
int nJoysticks = SDL_NumJoysticks();
int numGamepads = 0;
// Count how many controllers there are
for (int i = 0; i < nJoysticks; i++)
if (SDL_IsGameController(i))
numGamepads++;
printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads);
//SDL_JoystickEventState(SDL_ENABLE);
*/
if (!discoverGameController())
mSource = USE_KEYBOARD;
}
// Comprueba si hay algún mando conectado // Comprueba si hay algún mando conectado
//discoverGameController(); //discoverGameController();
// En caso de haber un mando, el objeto se puede utilizar con entradas de mando // En caso de haber un mando, el objeto se puede utilizar con entradas de mando
if (mGameControllerFound) //if (mGameControllerFound)
mSource = source; // mSource = source;
// Si no hay un mando, el objeto se configura como teclado // Si no hay un mando, el objeto se configura como teclado
//else
// mSource = USE_KEYBOARD;
if (mSource == USE_KEYBOARD)
printf("Input using KEYBOARD\n");
else else
mSource = USE_KEYBOARD; printf("Input using GAMECONTROLLER\n");
} }
// Destructor // Destructor
@@ -188,27 +214,64 @@ bool Input::checkInput(Uint8 input, bool repeat)
// Comprueba si hay un mando conectado // Comprueba si hay un mando conectado
bool Input::discoverGameController() bool Input::discoverGameController()
{ {
bool found = false;
printf("%i joystics found", SDL_NumJoysticks()); /*
printf("%i joystics found\n", SDL_NumJoysticks());
for (int i = 0; i < SDL_NumJoysticks(); ++i) for (int i = 0; i < SDL_NumJoysticks(); ++i)
{ {
if (SDL_IsGameController(i)) if (SDL_IsGameController(i))
{ {
char *mapping; char *mapping;
printf("Index \'%i\' is a compatible controller, named \'%s\'", i, SDL_GameControllerNameForIndex(i)); printf("Index %i is a compatible controller, named %s\n", i, SDL_GameControllerNameForIndex(i));
mGameController = SDL_GameControllerOpen(i); mGameController = SDL_GameControllerOpen(i);
mapping = SDL_GameControllerMapping(mGameController); mapping = SDL_GameControllerMapping(mGameController);
printf("Controller %i is mapped as \"%s\".", i, mapping); printf("Controller %i is mapped as %s\n", i, mapping);
SDL_free(mapping); SDL_free(mapping);
mGameControllerFound = true; found = true;
} }
else else
{ {
printf("Index \'%i\' is not a compatible controller.", i); printf("Index %i is not a compatible controller.\n", i);
mGameControllerFound = false; found = false;
} }
} }
return mGameController; */
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
int nJoysticks = SDL_NumJoysticks();
int numGamepads = 0;
// Count how many controllers there are
for (int i = 0; i < nJoysticks; i++)
if (SDL_IsGameController(i))
numGamepads++;
printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads);
//if (numGamepads > 0)
// found = true;
//SDL_JoystickEventState(SDL_ENABLE);
if (numGamepads > 0)
{
for (int i = 0; i < numGamepads; i++)
{
// Open the controller and add it to our list
mGameController = SDL_GameControllerOpen(i);
if (SDL_GameControllerGetAttached(mGameController) == 1)
//connectedControllers.push_back(pad);
found = true;
else
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
SDL_GameControllerEventState(SDL_ENABLE);
}
//SDL_GameControllerGetButton(mGameController, SDL_CONTROLLER_BUTTON_A);
//printf("hola");
return found;
} }
/*if (SDL_NumJoysticks() < 1) /*if (SDL_NumJoysticks() < 1)
{ {

View File

@@ -46,7 +46,7 @@ private:
SDL_GameController *mGameController; // Manejador para el mando SDL_GameController *mGameController; // Manejador para el mando
//SDL_Haptic *mControllerHaptic; // Manejador para el mando con vibración //SDL_Haptic *mControllerHaptic; // Manejador para el mando con vibración
bool mGameControllerFound; // Variable para saber si hay un mando conectado //bool mGameControllerFound; // Variable para saber si hay un mando conectado
int mSource; // Indica si el objeto usará un mando o un teclado int mSource; // Indica si el objeto usará un mando o un teclado