neteja tidy a source/core i encamina Texture::loadFromFile pel ResourceHelper
This commit is contained in:
+38
-49
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <algorithm> // for any_of
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, basi...
|
||||
#include <utility>
|
||||
|
||||
// Emscripten-only: SDL 3.4+ ja no casa el GUID dels mandos de Chrome Android
|
||||
// amb gamecontrollerdb (el gamepad.id d'Android no porta Vendor/Product, el
|
||||
@@ -58,12 +59,8 @@ auto Input::get() -> Input * {
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Input::Input(const std::string &file)
|
||||
: numGamepads(0),
|
||||
dbPath(file),
|
||||
verbose(true),
|
||||
disabledUntil(d_notDisabled),
|
||||
enabled(true) {
|
||||
Input::Input(std::string file)
|
||||
: dbPath(std::move(file)) {
|
||||
// Inicializa las variables
|
||||
keyBindings_t kb;
|
||||
kb.scancode = 0;
|
||||
@@ -107,7 +104,7 @@ void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button) {
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool Input::checkInput(Uint8 input, bool repeat, int device, int index) {
|
||||
auto Input::checkInput(Uint8 input, bool repeat, int device, int index) -> bool {
|
||||
if (!enabled) {
|
||||
return false;
|
||||
}
|
||||
@@ -123,11 +120,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) {
|
||||
const bool *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat) {
|
||||
if (keyStates[keyBindings[input].scancode]) {
|
||||
successKeyboard = true;
|
||||
} else {
|
||||
successKeyboard = false;
|
||||
}
|
||||
successKeyboard = keyStates[keyBindings[input].scancode];
|
||||
} else {
|
||||
if (!keyBindings[input].active) {
|
||||
if (keyStates[keyBindings[input].scancode]) {
|
||||
@@ -147,14 +140,10 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) {
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size())
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size()) {
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) {
|
||||
if (repeat) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
|
||||
successGameController = true;
|
||||
} else {
|
||||
successGameController = false;
|
||||
}
|
||||
successGameController = SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button);
|
||||
} else {
|
||||
if (!gameControllerBindings[input].active) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
|
||||
@@ -173,12 +162,13 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (successKeyboard || successGameController);
|
||||
}
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool Input::checkAnyInput(int device, int index) {
|
||||
auto Input::checkAnyInput(int device, int index) -> bool {
|
||||
if (device == INPUT_USE_ANY) {
|
||||
index = 0;
|
||||
}
|
||||
@@ -186,8 +176,8 @@ bool Input::checkAnyInput(int device, int index) {
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) {
|
||||
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < (int)keyBindings.size(); ++i) {
|
||||
if (mKeystates[keyBindings[i].scancode]) {
|
||||
for (auto &keyBinding : keyBindings) {
|
||||
if (mKeystates[keyBinding.scancode]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -195,8 +185,8 @@ bool Input::checkAnyInput(int device, int index) {
|
||||
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size()) {
|
||||
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) {
|
||||
for (int i = 0; i < (int)gameControllerBindings.size(); ++i) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[i].button)) {
|
||||
for (auto &gameControllerBinding : gameControllerBindings) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBinding.button)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -209,10 +199,10 @@ bool Input::checkAnyInput(int device, int index) {
|
||||
// Construye el nombre visible de un mando.
|
||||
// Recorta des del primer '(' o '[' (per a evitar coses tipus
|
||||
// "Retroid Controller (vendor: 1001) ...") i talla a 25 caràcters.
|
||||
std::string Input::buildControllerName(SDL_Gamepad *pad, int padIndex) {
|
||||
auto Input::buildControllerName(SDL_Gamepad *pad, int padIndex) -> std::string {
|
||||
(void)padIndex;
|
||||
const char *padName = SDL_GetGamepadName(pad);
|
||||
std::string name = padName ? padName : "Unknown";
|
||||
std::string name = (padName != nullptr) ? padName : "Unknown";
|
||||
const auto pos = name.find_first_of("([");
|
||||
if (pos != std::string::npos) {
|
||||
name.erase(pos);
|
||||
@@ -228,7 +218,7 @@ std::string Input::buildControllerName(SDL_Gamepad *pad, int padIndex) {
|
||||
|
||||
// Busca si hay un mando conectado. Cierra y limpia el estado previo para
|
||||
// que la función sea idempotente si se invoca más de una vez.
|
||||
bool Input::discoverGameController() {
|
||||
auto Input::discoverGameController() -> bool {
|
||||
// Cierra los mandos ya abiertos y limpia los vectores paralelos
|
||||
for (auto *pad : connectedControllers) {
|
||||
if (pad != nullptr) {
|
||||
@@ -248,14 +238,14 @@ bool Input::discoverGameController() {
|
||||
|
||||
if (SDL_AddGamepadMappingsFromFile(dbPath.c_str()) < 0) {
|
||||
if (verbose) {
|
||||
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int nJoysticks = 0;
|
||||
SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks);
|
||||
|
||||
if (joysticks) {
|
||||
if (joysticks != nullptr) {
|
||||
int gamepadCount = 0;
|
||||
for (int i = 0; i < nJoysticks; ++i) {
|
||||
if (SDL_IsGamepad(joysticks[i])) {
|
||||
@@ -273,7 +263,9 @@ bool Input::discoverGameController() {
|
||||
int padIndex = 0;
|
||||
|
||||
for (int i = 0; i < nJoysticks; i++) {
|
||||
if (!SDL_IsGamepad(joysticks[i])) continue;
|
||||
if (!SDL_IsGamepad(joysticks[i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
installWebStandardMapping(joysticks[i]);
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(joysticks[i]);
|
||||
@@ -285,11 +277,11 @@ bool Input::discoverGameController() {
|
||||
numGamepads++;
|
||||
padIndex++;
|
||||
if (verbose) {
|
||||
std::cout << name << std::endl;
|
||||
std::cout << name << '\n';
|
||||
}
|
||||
} else {
|
||||
if (verbose) {
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,13 +296,13 @@ bool Input::discoverGameController() {
|
||||
}
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_ADDED
|
||||
bool Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) {
|
||||
auto Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) -> bool {
|
||||
if (!SDL_IsGamepad(jid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Si el mando ya está registrado no hace nada (ej. evento retroactivo tras el scan inicial)
|
||||
if (std::any_of(connectedControllerIds.begin(), connectedControllerIds.end(), [jid](SDL_JoystickID existing) { return existing == jid; })) {
|
||||
if (std::ranges::any_of(connectedControllerIds, [jid](SDL_JoystickID existing) { return existing == jid; })) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -318,7 +310,7 @@ bool Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) {
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(jid);
|
||||
if (pad == nullptr) {
|
||||
if (verbose) {
|
||||
std::cout << "Failed to open gamepad " << jid << ": " << SDL_GetError() << std::endl;
|
||||
std::cout << "Failed to open gamepad " << jid << ": " << SDL_GetError() << '\n';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -331,7 +323,7 @@ bool Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) {
|
||||
numGamepads++;
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Gamepad connected: " << name << std::endl;
|
||||
std::cout << "Gamepad connected: " << name << '\n';
|
||||
}
|
||||
|
||||
outName = name;
|
||||
@@ -339,9 +331,11 @@ bool Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) {
|
||||
}
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED
|
||||
bool Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) {
|
||||
auto Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) -> bool {
|
||||
for (size_t i = 0; i < connectedControllerIds.size(); ++i) {
|
||||
if (connectedControllerIds[i] != jid) continue;
|
||||
if (connectedControllerIds[i] != jid) {
|
||||
continue;
|
||||
}
|
||||
|
||||
outName = controllerNames[i];
|
||||
if (connectedControllers[i] != nullptr) {
|
||||
@@ -351,10 +345,10 @@ bool Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) {
|
||||
connectedControllerIds.erase(connectedControllerIds.begin() + i);
|
||||
controllerNames.erase(controllerNames.begin() + i);
|
||||
numGamepads--;
|
||||
if (numGamepads < 0) numGamepads = 0;
|
||||
numGamepads = std::max(numGamepads, 0);
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Gamepad disconnected: " << outName << std::endl;
|
||||
std::cout << "Gamepad disconnected: " << outName << '\n';
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -363,25 +357,20 @@ bool Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) {
|
||||
}
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound() {
|
||||
if (numGamepads > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
auto Input::gameControllerFound() const -> bool {
|
||||
return numGamepads > 0;
|
||||
}
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string Input::getControllerName(int index) {
|
||||
auto Input::getControllerName(int index) -> std::string {
|
||||
if (numGamepads > 0) {
|
||||
return controllerNames[index];
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
int Input::getNumControllers() {
|
||||
auto Input::getNumControllers() const -> int {
|
||||
return numGamepads;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user