clang-tidy readability-function-cognitive-complexity

This commit is contained in:
2025-07-20 15:42:10 +02:00
parent cb4e4b450d
commit f2915aa4b4
6 changed files with 389 additions and 316 deletions

View File

@@ -1,5 +1,7 @@
#include "define_buttons.h"
#include <ranges>
#include "input.h" // Para Input, InputAction
#include "lang.h" // Para getText
#include "options.h" // Para OptionsController, Options, options
@@ -86,9 +88,6 @@ auto DefineButtons::enable(int index) -> bool {
return false;
}
// Comprueba si está habilitado
auto DefineButtons::isEnabled() const -> bool { return enabled_; }
// Incrementa el indice de los botones
void DefineButtons::incIndexButton() {
if (index_button_ < buttons_.size() - 1) {
@@ -100,7 +99,6 @@ void DefineButtons::incIndexButton() {
// Guarda los cambios en las opciones
void DefineButtons::saveBindingsToOptions() {
// Modifica las opciones para colocar los valores asignados
auto &controller = Options::controllers.at(index_controller_);
controller.name = input_->getControllerName(index_controller_);
for (size_t j = 0; j < controller.inputs.size(); ++j) {
@@ -110,12 +108,9 @@ void DefineButtons::saveBindingsToOptions() {
// Comprueba que un botón no esté ya asignado
auto DefineButtons::checkButtonNotInUse(SDL_GamepadButton button) -> bool {
for (const auto &b : buttons_) {
if (b.button == button) {
return false;
}
}
return true;
return std::ranges::all_of(buttons_, [button](const auto &b) {
return b.button != button;
});
}
// Limpia la asignación de botones
@@ -130,18 +125,10 @@ void DefineButtons::clearButtons() {
// Comprueba si ha finalizado
void DefineButtons::checkEnd() {
// Comprueba si ha finalizado
if (finished_) {
// Asigna los botones definidos al input_
bindButtons();
// Guarda los cambios en las opciones
saveBindingsToOptions();
// Reinicia los estados de las pulsaciones de los botones
input_->resetInputStates();
// Deshabilita
enabled_ = false;
bindButtons(); // Asigna los botones definidos al input_
saveBindingsToOptions(); // Guarda los cambios en las opciones
input_->resetInputStates(); // Reinicia los estados de las pulsaciones de los botones
enabled_ = false; // Deshabilita
}
}

View File

@@ -32,7 +32,7 @@ class DefineButtons {
void render(); // Dibuja el objeto en pantalla
void checkEvents(const SDL_Event &event); // Procesa los eventos
auto enable(int index_controller) -> bool; // Habilita la redefinición de botones
[[nodiscard]] auto isEnabled() const -> bool; // Comprueba si está habilitado
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; }; // Comprueba si está habilitado
private:
// Objetos

View File

@@ -46,69 +46,106 @@ void Notifier::render() {
// Actualiza el estado de las notificaiones
void Notifier::update() {
for (int i = 0; i < (int)notifications_.size(); ++i) {
if (!shouldProcessNotification(i)) {
break;
}
processNotification(i);
}
clearFinishedNotifications();
}
bool Notifier::shouldProcessNotification(int index) const {
// Si la notificación anterior está "saliendo", no hagas nada
if (i > 0) {
if (notifications_[i - 1].state == NotificationStatus::RISING) {
if (index > 0 && notifications_[index - 1].state == NotificationStatus::RISING) {
return false;
}
return true;
}
void Notifier::processNotification(int index) {
auto& notification = notifications_[index];
notification.counter++;
playNotificationSoundIfNeeded(notification);
updateNotificationState(index);
notification.sprite->setPosition(notification.rect);
}
void Notifier::playNotificationSoundIfNeeded(const Notification& notification) {
// Hace sonar la notificación en el primer frame
if (notification.counter == 1 &&
param.notification.sound &&
notification.state == NotificationStatus::RISING) {
Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
}
}
void Notifier::updateNotificationState(int index) {
auto& notification = notifications_[index];
switch (notification.state) {
case NotificationStatus::RISING:
handleRisingState(index);
break;
case NotificationStatus::STAY:
handleStayState(index);
break;
case NotificationStatus::VANISHING:
handleVanishingState(index);
break;
default:
break;
}
}
notifications_[i].counter++;
void Notifier::handleRisingState(int index) {
auto& notification = notifications_[index];
// Hace sonar la notificación en el primer frame
if (notifications_[i].counter == 1) {
if (param.notification.sound) {
if (notifications_[i].state == NotificationStatus::RISING) {
// Reproduce el sonido de la notificación
Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
}
const float step = (float)notification.counter / notification.travel_dist;
const int alpha = 255 * step;
moveNotificationVertically(notification, param.notification.pos_v == NotifyPosition::TOP ? 1 : -1);
notification.texture->setAlpha(alpha);
if (notification.rect.y == notification.y) {
transitionToStayState(index);
}
}
// Comprueba los estados
if (notifications_[i].state == NotificationStatus::RISING) {
const float STEP = ((float)notifications_[i].counter / notifications_[i].travel_dist);
const int ALPHA = 255 * STEP;
void Notifier::handleStayState(int index) {
auto& notification = notifications_[index];
if (param.notification.pos_v == NotifyPosition::TOP) {
notifications_[i].rect.y++;
} else {
notifications_[i].rect.y--;
}
notifications_[i].texture->setAlpha(ALPHA);
if (notifications_[i].rect.y == notifications_[i].y) {
notifications_[i].state = NotificationStatus::STAY;
notifications_[i].texture->setAlpha(255);
notifications_[i].counter = 0;
if (notification.counter == wait_time_) {
notification.state = NotificationStatus::VANISHING;
notification.counter = 0;
}
}
else if (notifications_[i].state == NotificationStatus::STAY) {
if (notifications_[i].counter == wait_time_) {
notifications_[i].state = NotificationStatus::VANISHING;
notifications_[i].counter = 0;
}
} else if (notifications_[i].state == NotificationStatus::VANISHING) {
const float STEP = (notifications_[i].counter / (float)notifications_[i].travel_dist);
const int ALPHA = 255 * (1 - STEP);
void Notifier::handleVanishingState(int index) {
auto& notification = notifications_[index];
if (param.notification.pos_v == NotifyPosition::TOP) {
notifications_[i].rect.y--;
} else {
notifications_[i].rect.y++;
}
notifications_[i].texture->setAlpha(ALPHA);
const float step = notification.counter / (float)notification.travel_dist;
const int alpha = 255 * (1 - step);
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist) {
notifications_[i].state = NotificationStatus::FINISHED;
moveNotificationVertically(notification, param.notification.pos_v == NotifyPosition::TOP ? -1 : 1);
notification.texture->setAlpha(alpha);
if (notification.rect.y == notification.y - notification.travel_dist) {
notification.state = NotificationStatus::FINISHED;
}
}
notifications_[i].sprite->setPosition(notifications_[i].rect);
void Notifier::moveNotificationVertically(Notification& notification, int direction) {
notification.rect.y += direction;
}
clearFinishedNotifications();
void Notifier::transitionToStayState(int index) {
auto& notification = notifications_[index];
notification.state = NotificationStatus::STAY;
notification.texture->setAlpha(255);
notification.counter = 0;
}
// Elimina las notificaciones finalizadas

View File

@@ -31,9 +31,6 @@ class Notifier {
auto checkCode(const std::string &code) -> bool { return stringInVector(getCodes(), code); } // Comprueba si hay alguna notificación con un código concreto
private:
// --- Singleton ---
static Notifier *instance;
// --- Tipos internos ---
enum class NotificationStatus {
RISING,
@@ -78,10 +75,22 @@ private:
bool has_icons_; // Indica si el notificador tiene textura para iconos
// --- Métodos internos ---
void clearFinishedNotifications(); // Elimina las notificaciones finalizadas
void clearAllNotifications(); // Finaliza y elimina todas las notificaciones activas
void clearFinishedNotifications(); // Elimina las notificaciones cuyo estado es FINISHED
void clearAllNotifications(); // Elimina todas las notificaciones activas, sin importar el estado
bool shouldProcessNotification(int index) const; // Determina si una notificación debe ser procesada (según su estado y posición)
void processNotification(int index); // Procesa una notificación en la posición dada: actualiza su estado y comportamiento visual
void playNotificationSoundIfNeeded(const Notification &notification); // Reproduce sonido asociado si es necesario (dependiendo del estado o contenido)
void updateNotificationState(int index); // Actualiza el estado interno de una notificación (ej. de RISING a STAY)
void handleRisingState(int index); // Lógica de animación para el estado RISING (apareciendo)
void handleStayState(int index); // Lógica para mantener una notificación visible en el estado STAY
void handleVanishingState(int index); // Lógica de animación para el estado VANISHING (desapareciendo)
void moveNotificationVertically(Notification &notification, int direction); // Mueve verticalmente una notificación en una dirección dada (útil para animación en apilamiento)
void transitionToStayState(int index); // Cambia el estado de una notificación de RISING a STAY cuando ha alcanzado su posición final
// --- Constructor y destructor ---
Notifier(std::string icon_file, std::shared_ptr<Text> text); // Constructor privado
~Notifier() = default; // Destructor privado
// --- Singleton ---
static Notifier *instance;
};

View File

@@ -153,19 +153,58 @@ void Scoreboard::fillPanelTextures() {
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 0);
SDL_RenderClear(renderer_);
switch (panel_[i].mode) {
case ScoreboardMode::SCORE: {
renderPanelContent(i);
}
// Deja el renderizador apuntando donde estaba
SDL_SetRenderTarget(renderer_, temp);
}
void Scoreboard::renderPanelContent(size_t panelIndex) {
switch (panel_[panelIndex].mode) {
case ScoreboardMode::SCORE:
renderScoreMode(panelIndex);
break;
case ScoreboardMode::DEMO:
renderDemoMode();
break;
case ScoreboardMode::WAITING:
renderWaitingMode();
break;
case ScoreboardMode::GAME_OVER:
renderGameOverMode();
break;
case ScoreboardMode::STAGE_INFO:
renderStageInfoMode();
break;
case ScoreboardMode::CONTINUE:
renderContinueMode(panelIndex);
break;
case ScoreboardMode::ENTER_NAME:
renderEnterNameMode(panelIndex);
break;
case ScoreboardMode::SHOW_NAME:
renderShowNameMode(panelIndex);
break;
case ScoreboardMode::GAME_COMPLETED:
renderGameCompletedMode(panelIndex);
break;
default:
break;
}
}
void Scoreboard::renderScoreMode(size_t panelIndex) {
// SCORE
text_scoreboard_->writeDX(TEXT_COLOR | TEXT_CENTER, slot4_1_.x, slot4_1_.y, name_[i], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_COLOR | TEXT_CENTER, slot4_2_.x, slot4_2_.y, updateScoreText(score_[i]), 1, text_color2_);
text_scoreboard_->writeDX(TEXT_COLOR | TEXT_CENTER, slot4_1_.x, slot4_1_.y, name_[panelIndex], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_COLOR | TEXT_CENTER, slot4_2_.x, slot4_2_.y, updateScoreText(score_[panelIndex]), 1, text_color2_);
// MULT
text_scoreboard_->writeDX(TEXT_COLOR | TEXT_CENTER, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 3"), 1, text_color1_);
text_scoreboard_->writeDX(TEXT_COLOR | TEXT_CENTER, slot4_4_.x, slot4_4_.y, "x" + std::to_string(mult_[i]).substr(0, 3), 1, text_color2_);
break;
text_scoreboard_->writeDX(TEXT_COLOR | TEXT_CENTER, slot4_4_.x, slot4_4_.y, "x" + std::to_string(mult_[panelIndex]).substr(0, 3), 1, text_color2_);
}
case ScoreboardMode::DEMO: {
void Scoreboard::renderDemoMode() {
// DEMO MODE
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y + 4, Lang::getText("[SCOREBOARD] 6"), 1, text_color1_);
@@ -174,10 +213,9 @@ void Scoreboard::fillPanelTextures() {
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y - 2, Lang::getText("[SCOREBOARD] 8"), 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y - 2, Lang::getText("[SCOREBOARD] 9"), 1, text_color1_);
}
break;
}
case ScoreboardMode::WAITING: {
void Scoreboard::renderWaitingMode() {
// GAME OVER
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y + 4, Lang::getText("[SCOREBOARD] 7"), 1, text_color1_);
@@ -186,10 +224,9 @@ void Scoreboard::fillPanelTextures() {
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y - 2, Lang::getText("[SCOREBOARD] 8"), 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y - 2, Lang::getText("[SCOREBOARD] 9"), 1, text_color1_);
}
break;
}
case ScoreboardMode::GAME_OVER: {
void Scoreboard::renderGameOverMode() {
// GAME OVER
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y + 4, Lang::getText("[SCOREBOARD] 7"), 1, text_color1_);
@@ -198,10 +235,9 @@ void Scoreboard::fillPanelTextures() {
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y - 2, Lang::getText("[SCOREBOARD] 12"), 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y - 2, Lang::getText("[SCOREBOARD] 13"), 1, text_color1_);
}
break;
}
case ScoreboardMode::STAGE_INFO: {
void Scoreboard::renderStageInfoMode() {
// STAGE
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y, Lang::getText("[SCOREBOARD] 5") + std::to_string(stage_), 1, text_color1_);
@@ -215,85 +251,78 @@ void Scoreboard::fillPanelTextures() {
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 4"), 1, text_color1_);
const std::string NAME = hi_score_name_.empty() ? "" : hi_score_name_ + " - ";
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y, NAME + updateScoreText(hi_score_), 1, text_color2_);
break;
}
case ScoreboardMode::CONTINUE: {
void Scoreboard::renderContinueMode(size_t panelIndex) {
// SCORE
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y, name_[i], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_[i]), 1, text_color2_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y, name_[panelIndex], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_[panelIndex]), 1, text_color2_);
// CONTINUE
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 10"), 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y, std::to_string(continue_counter_[i]), 1, text_color2_);
break;
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y, std::to_string(continue_counter_[panelIndex]), 1, text_color2_);
}
case ScoreboardMode::ENTER_NAME: {
void Scoreboard::renderEnterNameMode(size_t panelIndex) {
// SCORE
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y, name_[i], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_[i]), 1, text_color2_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y, name_[panelIndex], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_[panelIndex]), 1, text_color2_);
// ENTER NAME
{
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 11"), 1, text_color1_);
renderNameInputField(panelIndex);
}
void Scoreboard::renderNameInputField(size_t panelIndex) {
SDL_FRect rect = {enter_name_pos_.x, enter_name_pos_.y, 5.0F, 7.0F};
// Recorre todos los slots de letras del nombre
for (size_t j = 0; j < NAME_SIZE; ++j) {
// Selecciona el color
const Color COLOR = j < selector_pos_[i] ? text_color2_ : text_color1_;
const Color COLOR = j < selector_pos_[panelIndex] ? text_color2_ : text_color1_;
if (j != selector_pos_[i] || time_counter_ % 3 == 0) {
if (j != selector_pos_[panelIndex] || time_counter_ % 3 == 0) {
// Dibuja la linea
if (j >= selector_pos_[i]) {
if (j >= selector_pos_[panelIndex]) {
SDL_SetRenderDrawColor(renderer_, COLOR.r, COLOR.g, COLOR.b, 255);
SDL_RenderLine(renderer_, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h);
}
// Dibuja la letra
if (j < record_name_[i].size()) {
text_scoreboard_->writeColored(rect.x, rect.y, record_name_[i].substr(j, 1), COLOR);
if (j < record_name_[panelIndex].size()) {
text_scoreboard_->writeColored(rect.x, rect.y, record_name_[panelIndex].substr(j, 1), COLOR);
}
}
rect.x += 7;
}
}
break;
}
case ScoreboardMode::SHOW_NAME: {
void Scoreboard::renderShowNameMode(size_t panelIndex) {
// SCORE
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y, name_[i], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_[i]), 1, text_color2_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y, name_[panelIndex], 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_2_.x, slot4_2_.y, updateScoreText(score_[panelIndex]), 1, text_color2_);
// NAME
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 11"), 1, text_color1_);
/* TEXTO CENTRADO */
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y, record_name_[i], 1, getColorLikeKnightRider(name_colors_, loop_counter_ / 5));
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y, record_name_[panelIndex], 1, getColorLikeKnightRider(name_colors_, loop_counter_ / 5));
/* TEXTO A LA IZQUIERDA */
// text_scoreboard_->writeColored(enter_name_pos_.x, enter_name_pos_.y, record_name_[i], getColorLikeKnightRider(name_colors_, loop_counter_ / 5));
break;
// text_scoreboard_->writeColored(enter_name_pos_.x, enter_name_pos_.y, record_name_[panelIndex], getColorLikeKnightRider(name_colors_, loop_counter_ / 5));
}
case ScoreboardMode::GAME_COMPLETED: {
void Scoreboard::renderGameCompletedMode(size_t panelIndex) {
// GAME OVER
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_1_.x, slot4_1_.y + 4, Lang::getText("[SCOREBOARD] 7"), 1, text_color1_);
// SCORE
if (time_counter_ % 10 < 8) {
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y - 2, Lang::getText("[SCOREBOARD] 14"), 1, text_color1_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y - 2, updateScoreText(score_[i]), 1, text_color2_);
text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y - 2, updateScoreText(score_[panelIndex]), 1, text_color2_);
}
}
default:
break;
}
}
// Deja el renderizador apuntando donde estaba
SDL_SetRenderTarget(renderer_, temp);
}
// Rellena la textura de fondo
void Scoreboard::fillBackgroundTexture() {

View File

@@ -113,6 +113,17 @@ class Scoreboard {
void updateTimeCounter(); // Actualiza el contador
void renderSeparator(); // Dibuja la línea que separa la zona de juego del marcador
void iniNameColors(); // Inicializa el vector de colores para el nombre
void renderPanelContent(size_t panelIndex);
void renderScoreMode(size_t panelIndex);
void renderDemoMode();
void renderWaitingMode();
void renderGameOverMode();
void renderStageInfoMode();
void renderContinueMode(size_t panelIndex);
void renderEnterNameMode(size_t panelIndex);
void renderNameInputField(size_t panelIndex);
void renderShowNameMode(size_t panelIndex);
void renderGameCompletedMode(size_t panelIndex);
// --- Constructor y destructor privados (singleton) ---
Scoreboard();