Working on support for various controllers

This commit is contained in:
2021-08-31 21:05:04 +02:00
parent 8d1cabb288
commit 42c0f19c68
6 changed files with 101 additions and 32 deletions

View File

@@ -141,6 +141,11 @@ void Title::init(bool demo, Uint8 subsection)
mOptions->input[0].deviceType = INPUT_USE_KEYBOARD;
mOptions->input[1].deviceType = INPUT_USE_GAMECONTROLLER;
}
checkInputDevices();
mDeviceIndex[0] = 0;
mDeviceIndex[1] = 0;
// Inicializa el bitmap de Coffee
mCoffeeBitmap->init(mTitleTexture, mRenderer);
@@ -363,7 +368,8 @@ void Title::updateMenuLabels()
else
{
mMenu.options->setGreyed(i, false);
mMenu.options->setItemCaption(i, mInput->getControllerName(0));
//mMenu.options->setItemCaption(i, mInput->getControllerName(0));
mMenu.options->setItemCaption(i, mOptions->input[0].name);
}
break;
@@ -392,7 +398,8 @@ void Title::updateMenuLabels()
else
{
mMenu.options->setGreyed(i, false);
mMenu.options->setItemCaption(i, mInput->getControllerName(0));
//mMenu.options->setItemCaption(i, mInput->getControllerName(0));
mMenu.options->setItemCaption(i, mOptions->input[1].name);
}
break;
@@ -755,11 +762,11 @@ section_t Title::run(Uint8 subsection)
updateMenuLabels();
break;
case 1: // PLAYER 1 CONTROLS
switchInputs(1);
updatePlayerInputs(1);
updateMenuLabels();
break;
case 3: // PLAYER 2 CONTROLS
switchInputs(2);
updatePlayerInputs(2);
updateMenuLabels();
break;
case 5: // Language
@@ -908,17 +915,45 @@ void Title::runDemoGame()
}
// Modifica las opciones para los controles de los jugadores
void Title::switchInputs(int value)
bool Title::updatePlayerInputs(int numPlayer)
{
Uint8 temp;
temp = mOptions->input[0].deviceType;
mOptions->input[0].deviceType = mOptions->input[1].deviceType;
mOptions->input[1].deviceType = temp;
const int numDevices = mInput->getNumControllers();
if (!mInput->gameControllerFound())
// Si no hay mandos se deja todo de manera prefijada
if (numDevices == 0)
{
mDeviceIndex[0] = 0;
mDeviceIndex[1] = 0;
mOptions->input[0].id = -1;
mOptions->input[0].name = "KEYBOARD";
mOptions->input[0].deviceType = INPUT_USE_KEYBOARD;
mOptions->input[1].id = 0;
mOptions->input[0].name = "GAME CONTROLLER";
mOptions->input[1].deviceType = INPUT_USE_GAMECONTROLLER;
return true;
}
else // Si hay mas de un dispositivo, se recorre el vector
{
// Incrementa el indice
if (mDeviceIndex[numPlayer] < numDevices - 1)
mDeviceIndex[numPlayer]++;
else
mDeviceIndex[numPlayer] = 0;
// Si coincide con el del otro jugador, se lo intercambian
if (mDeviceIndex[0] == mDeviceIndex[1])
{
const int temp = mDeviceIndex[0];
mDeviceIndex[0] = mDeviceIndex[1];
mDeviceIndex[1] = temp;
}
// Copia el dispositivo marcado por el indice a la variable de opciones de cada jugador
mOptions->input[0] = mAvailableInputDevices[mDeviceIndex[0]];
mOptions->input[1] = mAvailableInputDevices[mDeviceIndex[1]];
return true;
}
}
@@ -940,4 +975,31 @@ void Title::createTiledBackground()
}
SDL_SetRenderTarget(mRenderer, nullptr);
}
// Comprueba cuantos mandos hay conectados para gestionar el menu de opciones
void Title::checkInputDevices()
{
printf("Filling devices for options menu...\n");
int numControllers = mInput->getNumControllers();
mAvailableInputDevices.clear();
input_t temp;
// Añade todos los mandos
if (numControllers > 0)
for (int i = 0; i < numControllers; i++)
{
temp.id = i;
temp.name = mInput->getControllerName(i);
temp.deviceType = INPUT_USE_GAMECONTROLLER;
mAvailableInputDevices.push_back(temp);
printf("Device %i:\t%s\n", mAvailableInputDevices.size(), temp.name.c_str());
}
// Añade el teclado al final
temp.id = -1;
temp.name = "KEYBOARD";
temp.deviceType = INPUT_USE_KEYBOARD;
mAvailableInputDevices.push_back(temp);
printf("Device %i:\t%s\n\n", mAvailableInputDevices.size(), temp.name.c_str());
}