working on game controller input

This commit is contained in:
2021-08-24 20:06:34 +02:00
parent 39a59336e4
commit bf45482c10
2 changed files with 31 additions and 10 deletions

View File

@@ -125,7 +125,7 @@ bool Director::initSDL()
bool success = true; bool success = true;
// Inicializa SDL // Inicializa SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_HAPTIC) < 0) if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER| SDL_INIT_AUDIO | SDL_INIT_HAPTIC) < 0)
{ {
printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError()); printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError());
success = false; success = false;

View File

@@ -186,16 +186,37 @@ 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()
{ {
for (int i = 0; i < SDL_NumJoysticks(); ++i)
{
if (SDL_IsGameController(i))
{
char *mapping;
SDL_Log("Index \'%i\' is a compatible controller, named \'%s\'", i, SDL_GameControllerNameForIndex(i));
mGameController = SDL_GameControllerOpen(i);
mapping = SDL_GameControllerMapping(mGameController);
SDL_Log("Controller %i is mapped as \"%s\".", i, mapping);
SDL_free(mapping);
mGameControllerFound = true;
}
else
{
SDL_Log("Index \'%i\' is not a compatible controller.", i);
mGameControllerFound = false;
}
}
return mGameController;
}
/*if (SDL_NumJoysticks() < 1) /*if (SDL_NumJoysticks() < 1)
{ {
printf("Warning: No joysticks connected!\n"); printf("Warning: No joysticks connected!\n");
mGameControllerFound = false; mGameControllerFound = false;
std::cout << "Warning: No joysticks connected!\n"; std::cout << "Warning: No joysticks connected!\n";
} }
else*/ else
{ {*/
// Carga el mando // Carga el mando
mGameController = SDL_GameControllerOpen(0);
/*mGameController = SDL_GameControllerOpen(0);
if (mGameController == NULL) if (mGameController == NULL)
{ {
@@ -225,9 +246,9 @@ bool Input::discoverGameController()
{ {
printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError()); printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError());
} }
}*/ }
} }
} }
return mGameControllerFound; return mGameControllerFound;
} }*/