clang-tidy modernize

This commit is contained in:
2025-07-20 12:51:24 +02:00
parent bfda842d3c
commit 1f0184fde2
74 changed files with 658 additions and 665 deletions

View File

@@ -1,9 +1,9 @@
#include "input.h"
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_GetGamepa...
#include <stddef.h> // Para size_t
#include <algorithm> // Para find
#include <cstddef> // Para size_t
#include <iterator> // Para distance
#include <unordered_map> // Para unordered_map, _Node_const_iterator, operat...
#include <utility> // Para pair
@@ -18,11 +18,11 @@ void Input::init(const std::string &game_controller_db_path) { Input::instance =
void Input::destroy() { delete Input::instance; }
// Obtiene la instancia
Input *Input::get() { return Input::instance; }
auto Input::get() -> Input * { return Input::instance; }
// Constructor
Input::Input(const std::string &game_controller_db_path)
: game_controller_db_path_(game_controller_db_path) {
Input::Input(std::string game_controller_db_path)
: game_controller_db_path_(std::move(game_controller_db_path)) {
// Inicializa el subsistema SDL_INIT_GAMEPAD
initSDLGamePad();
@@ -54,7 +54,7 @@ void Input::bindGameControllerButton(int controller_index, InputAction input_tar
}
// Comprueba si un input esta activo
bool Input::checkInput(InputAction input, bool repeat, InputDevice device, int controller_index) {
auto Input::checkInput(InputAction input, bool repeat, InputDevice device, int controller_index) -> bool {
bool success_keyboard = false;
bool success_controller = false;
const int INPUT_INDEX = static_cast<int>(input);
@@ -85,7 +85,7 @@ bool Input::checkInput(InputAction input, bool repeat, InputDevice device, int c
}
// Comprueba si hay almenos un input activo
bool Input::checkAnyInput(InputDevice device, int controller_index) {
auto Input::checkAnyInput(InputDevice device, int controller_index) -> bool {
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
const int NUM_ACTIONS = static_cast<int>(InputAction::SIZE);
@@ -119,7 +119,7 @@ bool Input::checkAnyInput(InputDevice device, int controller_index) {
}
// Comprueba si hay algún botón pulsado. Devuelve 0 en caso de no encontrar nada o el indice del dispositivo + 1. Se hace así para poder gastar el valor devuelto como un valor "booleano"
int Input::checkAnyButton(bool repeat) {
auto Input::checkAnyButton(bool repeat) -> int {
// Solo comprueba los botones definidos previamente
for (auto bi : button_inputs_) {
// Comprueba el teclado
@@ -139,7 +139,7 @@ int Input::checkAnyButton(bool repeat) {
}
// Busca si hay mandos conectados
bool Input::discoverGameControllers() {
auto Input::discoverGameControllers() -> bool {
bool found = false;
if (SDL_AddGamepadMappingsFromFile(game_controller_db_path_.c_str()) < 0) {
@@ -207,16 +207,16 @@ bool Input::discoverGameControllers() {
}
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound() { return num_gamepads_ > 0 ? true : false; }
auto Input::gameControllerFound() -> bool { return num_gamepads_ > 0 ? true : false; }
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int controller_index) const { return num_gamepads_ > 0 ? controller_names_.at(controller_index) : std::string(); }
auto Input::getControllerName(int controller_index) const -> std::string { return num_gamepads_ > 0 ? controller_names_.at(controller_index) : std::string(); }
// Obten el número de mandos conectados
int Input::getNumControllers() const { return num_gamepads_; }
auto Input::getNumControllers() const -> int { return num_gamepads_; }
// Obtiene el indice del controlador a partir de un event.id
int Input::getJoyIndex(SDL_JoystickID id) const {
auto Input::getJoyIndex(SDL_JoystickID id) const -> int {
for (int i = 0; i < num_joysticks_; ++i) {
if (SDL_GetJoystickID(joysticks_[i]) == id) {
return i;
@@ -247,18 +247,18 @@ void Input::printBindings(InputDevice device, int controller_index) const {
}
// Obtiene el SDL_GamepadButton asignado a un input
SDL_GamepadButton Input::getControllerBinding(int controller_index, InputAction input) const {
auto Input::getControllerBinding(int controller_index, InputAction input) const -> SDL_GamepadButton {
return controller_bindings_[controller_index][static_cast<int>(input)].button;
}
// Obtiene el indice a partir del nombre del mando
int Input::getIndexByName(const std::string &name) const {
auto Input::getIndexByName(const std::string &name) const -> int {
auto it = std::find(controller_names_.begin(), controller_names_.end(), name);
return it != controller_names_.end() ? std::distance(controller_names_.begin(), it) : -1;
}
// Convierte un InputAction a std::string
std::string Input::inputToString(InputAction input) const {
auto Input::inputToString(InputAction input) const -> std::string {
switch (input) {
case InputAction::FIRE_LEFT:
return "input_fire_left";
@@ -276,7 +276,7 @@ std::string Input::inputToString(InputAction input) const {
}
// Convierte un std::string a InputAction
InputAction Input::stringToInput(const std::string &name) const {
auto Input::stringToInput(const std::string &name) const -> InputAction {
static const std::unordered_map<std::string, InputAction> INPUT_MAP = {
{"input_fire_left", InputAction::FIRE_LEFT},
{"input_fire_center", InputAction::FIRE_CENTER},
@@ -289,7 +289,7 @@ InputAction Input::stringToInput(const std::string &name) const {
}
// Comprueba el eje del mando
bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat) {
auto Input::checkAxisInput(InputAction input, int controller_index, bool repeat) -> bool {
// Umbral para considerar el eje como activo
bool axis_active_now = false;
@@ -362,12 +362,12 @@ void Input::update() {
// --- TECLADO ---
const bool *key_states = SDL_GetKeyboardState(nullptr);
for (size_t i = 0; i < key_bindings_.size(); ++i) {
bool key_is_down_now = key_states[key_bindings_[i].scancode];
for (auto &key_binding : key_bindings_) {
bool key_is_down_now = key_states[key_binding.scancode];
// El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo
key_bindings_[i].just_pressed = key_is_down_now && !key_bindings_[i].is_held;
key_bindings_[i].is_held = key_is_down_now;
key_binding.just_pressed = key_is_down_now && !key_binding.is_held;
key_binding.is_held = key_is_down_now;
}
// --- MANDOS ---