migracio a SDL3
This commit is contained in:
131
source/input.cpp
131
source/input.cpp
@@ -1,9 +1,5 @@
|
||||
#include "input.h"
|
||||
#include <SDL2/SDL.h> // for SDL_INIT_GAMECONTROLLER, SDL_InitSubS...
|
||||
#include <SDL2/SDL_error.h> // for SDL_GetError
|
||||
#include <SDL2/SDL_events.h> // for SDL_ENABLE
|
||||
#include <SDL2/SDL_joystick.h> // for SDL_NumJoysticks
|
||||
#include <SDL2/SDL_keyboard.h> // for SDL_GetKeyboardState
|
||||
#include <SDL3/SDL.h>
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, basi...
|
||||
|
||||
// Constructor
|
||||
@@ -19,7 +15,7 @@ Input::Input(std::string file)
|
||||
keyBindings.resize(input_number_of_inputs, kb);
|
||||
|
||||
GameControllerBindings_t gcb;
|
||||
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
gcb.button = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
gameControllerBindings.resize(input_number_of_inputs, gcb);
|
||||
|
||||
@@ -43,7 +39,7 @@ void Input::bindKey(Uint8 input, SDL_Scancode code)
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button)
|
||||
void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button)
|
||||
{
|
||||
gameControllerBindings[input].button = button;
|
||||
}
|
||||
@@ -66,11 +62,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||
{
|
||||
const Uint8 *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
const bool *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[keyBindings[input].scancode])
|
||||
{
|
||||
successKeyboard = true;
|
||||
}
|
||||
@@ -83,7 +79,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
{
|
||||
if (!keyBindings[input].active)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[keyBindings[input].scancode])
|
||||
{
|
||||
keyBindings[input].active = true;
|
||||
successKeyboard = true;
|
||||
@@ -95,7 +91,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] == 0)
|
||||
if (!keyStates[keyBindings[input].scancode])
|
||||
{
|
||||
keyBindings[input].active = false;
|
||||
successKeyboard = false;
|
||||
@@ -113,7 +109,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button))
|
||||
{
|
||||
successGameController = true;
|
||||
}
|
||||
@@ -126,7 +122,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
{
|
||||
if (!gameControllerBindings[input].active)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button))
|
||||
{
|
||||
gameControllerBindings[input].active = true;
|
||||
successGameController = true;
|
||||
@@ -138,7 +134,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0)
|
||||
if (!SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button))
|
||||
{
|
||||
gameControllerBindings[input].active = false;
|
||||
successGameController = false;
|
||||
@@ -164,11 +160,11 @@ bool Input::checkAnyInput(int device, int index)
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||
{
|
||||
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < (int)keyBindings.size(); ++i)
|
||||
{
|
||||
if (mKeystates[keyBindings[i].scancode] != 0)
|
||||
if (mKeystates[keyBindings[i].scancode])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -181,7 +177,7 @@ bool Input::checkAnyInput(int device, int index)
|
||||
{
|
||||
for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0)
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[i].button))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -197,12 +193,12 @@ bool Input::discoverGameController()
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
|
||||
if (SDL_WasInit(SDL_INIT_GAMEPAD) != SDL_INIT_GAMEPAD)
|
||||
{
|
||||
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
||||
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
|
||||
}
|
||||
|
||||
if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0)
|
||||
if (SDL_AddGamepadMappingsFromFile(dbPath.c_str()) < 0)
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
@@ -210,55 +206,66 @@ bool Input::discoverGameController()
|
||||
}
|
||||
}
|
||||
|
||||
const int nJoysticks = SDL_NumJoysticks();
|
||||
int nJoysticks = 0;
|
||||
SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks);
|
||||
numGamepads = 0;
|
||||
|
||||
// Cuenta el numero de mandos
|
||||
for (int i = 0; i < nJoysticks; ++i)
|
||||
if (joysticks)
|
||||
{
|
||||
if (SDL_IsGameController(i))
|
||||
// Cuenta el numero de mandos
|
||||
for (int i = 0; i < nJoysticks; ++i)
|
||||
{
|
||||
numGamepads++;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "\nChecking for game controllers...\n";
|
||||
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
|
||||
}
|
||||
|
||||
if (numGamepads > 0)
|
||||
{
|
||||
found = true;
|
||||
|
||||
for (int i = 0; i < numGamepads; i++)
|
||||
{
|
||||
// Abre el mando y lo añade a la lista
|
||||
SDL_GameController *pad = SDL_GameControllerOpen(i);
|
||||
if (SDL_GameControllerGetAttached(pad) == 1)
|
||||
if (SDL_IsGamepad(joysticks[i]))
|
||||
{
|
||||
connectedControllers.push_back(pad);
|
||||
const std::string separator(" #");
|
||||
std::string name = SDL_GameControllerNameForIndex(i);
|
||||
name.resize(25);
|
||||
name = name + separator + std::to_string(i);
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
controllerNames.push_back(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
numGamepads++;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_GameControllerEventState(SDL_ENABLE);
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "\nChecking for game controllers...\n";
|
||||
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
|
||||
}
|
||||
|
||||
if (numGamepads > 0)
|
||||
{
|
||||
found = true;
|
||||
int padIndex = 0;
|
||||
|
||||
for (int i = 0; i < nJoysticks; i++)
|
||||
{
|
||||
if (!SDL_IsGamepad(joysticks[i])) continue;
|
||||
|
||||
// Abre el mando y lo añade a la lista
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(joysticks[i]);
|
||||
if (pad != nullptr)
|
||||
{
|
||||
connectedControllers.push_back(pad);
|
||||
const std::string separator(" #");
|
||||
const char *padName = SDL_GetGamepadName(pad);
|
||||
std::string name = padName ? padName : "Unknown";
|
||||
name.resize(25);
|
||||
name = name + separator + std::to_string(padIndex);
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
controllerNames.push_back(name);
|
||||
padIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetGamepadEventsEnabled(true);
|
||||
}
|
||||
|
||||
SDL_free(joysticks);
|
||||
}
|
||||
|
||||
return found;
|
||||
@@ -314,4 +321,4 @@ void Input::enable()
|
||||
{
|
||||
enabled = true;
|
||||
disabledUntil = d_notDisabled;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user