From 34a41ad25cca4a84957928ee2067b12575bca3f4 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 18 Apr 2026 07:48:05 +0200 Subject: [PATCH] cppcheck --- CMakeLists.txt | 1 + source/core/rendering/animatedsprite.h | 4 ++-- source/core/rendering/fade.h | 24 ++++++++++---------- source/core/rendering/movingsprite.cpp | 2 ++ source/core/rendering/movingsprite.h | 16 +++++++++----- source/core/rendering/smartsprite.cpp | 10 --------- source/core/rendering/smartsprite.h | 11 ++-------- source/core/rendering/sprite.h | 12 +++++----- source/core/resources/resource_pack.cpp | 4 ++-- source/core/system/director.h | 3 +++ source/game/entities/balloon.h | 3 +++ source/game/entities/bullet.h | 3 +++ source/game/entities/item.h | 3 +++ source/game/entities/player.h | 3 +++ source/game/game.cpp | 8 +++---- source/game/game.h | 5 ++++- source/game/scenes/instructions.h | 19 +++++++++------- source/game/scenes/intro.h | 3 +++ source/game/scenes/logo.h | 3 +++ source/game/scenes/title.cpp | 4 +++- source/game/scenes/title.h | 29 ++++++++++++++----------- source/game/ui/menu.cpp | 6 ++--- 22 files changed, 97 insertions(+), 79 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00cd0b6..5c3958c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,6 +265,7 @@ if(CPPCHECK_EXE) --inline-suppr --suppress=missingIncludeSystem --suppress=toomanyconfigs + --suppress=*:*/source/external/* --quiet -I ${CMAKE_SOURCE_DIR}/source ${CPPCHECK_SOURCES} diff --git a/source/core/rendering/animatedsprite.h b/source/core/rendering/animatedsprite.h index 62e12ad..824a1c1 100644 --- a/source/core/rendering/animatedsprite.h +++ b/source/core/rendering/animatedsprite.h @@ -42,7 +42,7 @@ class AnimatedSprite : public MovingSprite { AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation); // Destructor - ~AnimatedSprite(); + ~AnimatedSprite() override; // Calcula el frame correspondiente a la animación actual void animate(); @@ -86,7 +86,7 @@ class AnimatedSprite : public MovingSprite { void setCurrentAnimation(int index = 0); // Actualiza las variables del objeto - void update(); + void update() override; // OLD - Establece el rectangulo para un frame de una animación void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h); diff --git a/source/core/rendering/fade.h b/source/core/rendering/fade.h index 47781ef..b936fef 100644 --- a/source/core/rendering/fade.h +++ b/source/core/rendering/fade.h @@ -10,19 +10,19 @@ constexpr int FADE_RANDOM_SQUARE = 2; // Clase Fade class Fade { private: - SDL_Renderer *mRenderer = nullptr; // El renderizador de la ventana - SDL_Texture *mBackbuffer = nullptr; // Textura para usar como backbuffer - Uint8 mFadeType = FADE_FULLSCREEN; // Tipo de fade a realizar - Uint16 mCounter = 0; // Contador interno - bool mEnabled = false; // Indica si el fade está activo - bool mFinished = false; // Indica si ha terminado la transición - Uint8 mR = 0, mG = 0, mB = 0; // Colores para el fade + SDL_Renderer *mRenderer = nullptr; // El renderizador de la ventana + SDL_Texture *mBackbuffer = nullptr; // Textura para usar como backbuffer + Uint8 mFadeType = FADE_FULLSCREEN; // Tipo de fade a realizar + Uint16 mCounter = 0; // Contador interno + bool mEnabled = false; // Indica si el fade está activo + bool mFinished = false; // Indica si ha terminado la transición + Uint8 mR = 0, mG = 0, mB = 0; // Colores para el fade Uint8 mROriginal = 0, mGOriginal = 0, mBOriginal = 0; // Colores originales para FADE_RANDOM_SQUARE - Uint32 mLastSquareTicks = 0; // Ticks del último cuadrado dibujado (FADE_RANDOM_SQUARE) - Uint16 mSquaresDrawn = 0; // Número de cuadrados dibujados (FADE_RANDOM_SQUARE) - bool mFullscreenDone = false; // Indica si el fade fullscreen ha terminado la fase de fundido - SDL_Rect mRect1{}; // Rectangulo usado para crear los efectos de transición - SDL_Rect mRect2{}; // Rectangulo usado para crear los efectos de transición + Uint32 mLastSquareTicks = 0; // Ticks del último cuadrado dibujado (FADE_RANDOM_SQUARE) + Uint16 mSquaresDrawn = 0; // Número de cuadrados dibujados (FADE_RANDOM_SQUARE) + bool mFullscreenDone = false; // Indica si el fade fullscreen ha terminado la fase de fundido + SDL_Rect mRect1{}; // Rectangulo usado para crear los efectos de transición + SDL_Rect mRect2{}; // Rectangulo usado para crear los efectos de transición public: // Constructor diff --git a/source/core/rendering/movingsprite.cpp b/source/core/rendering/movingsprite.cpp index 2a9476b..7ecb3c5 100644 --- a/source/core/rendering/movingsprite.cpp +++ b/source/core/rendering/movingsprite.cpp @@ -70,11 +70,13 @@ void MovingSprite::render() { } // Obtiene el valor de la variable +// cppcheck-suppress duplInheritedMember float MovingSprite::getPosX() { return x; } // Obtiene el valor de la variable +// cppcheck-suppress duplInheritedMember float MovingSprite::getPosY() { return y; } diff --git a/source/core/rendering/movingsprite.h b/source/core/rendering/movingsprite.h index 19c1e0e..769ee1c 100644 --- a/source/core/rendering/movingsprite.h +++ b/source/core/rendering/movingsprite.h @@ -8,8 +8,10 @@ class Texture; // Clase MovingSprite. Añade posicion y velocidad en punto flotante class MovingSprite : public Sprite { protected: - float x; // Posición en el eje X - float y; // Posición en el eje Y + // cppcheck-suppress duplInheritedMember + float x; // Posición en el eje X (sub-pixel; Sprite::x es int) + // cppcheck-suppress duplInheritedMember + float y; // Posición en el eje Y (sub-pixel; Sprite::y es int) float xPrev; // Posición anterior en el eje X float yPrev; // Posición anterior en el eje Y @@ -42,18 +44,20 @@ class MovingSprite : public Sprite { void rotate(); // Actualiza las variables internas del objeto - void update(); + virtual void update(); // Reinicia todas las variables void clear(); // Muestra el sprite por pantalla - void render(); + void render() override; // Obten el valor de la variable + // cppcheck-suppress duplInheritedMember float getPosX(); // Obten el valor de la variable + // cppcheck-suppress duplInheritedMember float getPosY(); // Obten el valor de la variable @@ -84,7 +88,7 @@ class MovingSprite : public Sprite { Uint16 getRotateSpeed(); // Establece la posición y el tamaño del objeto - void setRect(SDL_Rect rect); + void setRect(SDL_Rect rect) override; // Establece el valor de la variable void setPosX(float value); @@ -144,7 +148,7 @@ class MovingSprite : public Sprite { SDL_FlipMode getFlip(); // Devuelve el rectangulo donde está el sprite - SDL_Rect getRect(); + SDL_Rect getRect() override; // Deshace el último movimiento void undoMove(); diff --git a/source/core/rendering/smartsprite.cpp b/source/core/rendering/smartsprite.cpp index 7bb8d45..0fe1fa3 100644 --- a/source/core/rendering/smartsprite.cpp +++ b/source/core/rendering/smartsprite.cpp @@ -46,16 +46,6 @@ void SmartSprite::render() { } } -// Obtiene el valor de la variable -bool SmartSprite::isEnabled() { - return enabled; -} - -// Establece el valor de la variable -void SmartSprite::setEnabled(bool enabled) { - this->enabled = enabled; -} - // Obtiene el valor de la variable int SmartSprite::getEnabledCounter() { return enabledCounter; diff --git a/source/core/rendering/smartsprite.h b/source/core/rendering/smartsprite.h index f05554d..48807d1 100644 --- a/source/core/rendering/smartsprite.h +++ b/source/core/rendering/smartsprite.h @@ -9,7 +9,6 @@ class Texture; class SmartSprite : public AnimatedSprite { private: // Variables - bool enabled; // Indica si esta habilitado bool onDestination; // Indica si está en el destino int destX; // Posicion de destino en el eje X int destY; // Posicion de destino en el eje Y @@ -30,16 +29,10 @@ class SmartSprite : public AnimatedSprite { void init(); // Actualiza la posición y comprueba si ha llegado a su destino - void update(); + void update() override; // Pinta el objeto en pantalla - void render(); - - // Obtiene el valor de la variable - bool isEnabled(); - - // Establece el valor de la variable - void setEnabled(bool enabled); + void render() override; // Obtiene el valor de la variable int getEnabledCounter(); diff --git a/source/core/rendering/sprite.h b/source/core/rendering/sprite.h index d86f711..005a99b 100644 --- a/source/core/rendering/sprite.h +++ b/source/core/rendering/sprite.h @@ -23,10 +23,10 @@ class Sprite { Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer); // Destructor - ~Sprite(); + virtual ~Sprite(); // Muestra el sprite por pantalla - void render(); + virtual void render(); // Obten el valor de la variable int getPosX(); @@ -77,14 +77,14 @@ class Sprite { void setRenderer(SDL_Renderer *renderer); // Establece el valor de la variable - void setEnabled(bool value); + virtual void setEnabled(bool value); // Comprueba si el objeto está habilitado - bool isEnabled(); + virtual bool isEnabled(); // Devuelve el rectangulo donde está el sprite - SDL_Rect getRect(); + virtual SDL_Rect getRect(); // Establece los valores de posición y tamaño del sprite - void setRect(SDL_Rect rect); + virtual void setRect(SDL_Rect rect); }; \ No newline at end of file diff --git a/source/core/resources/resource_pack.cpp b/source/core/resources/resource_pack.cpp index 34635cf..c9aecad 100644 --- a/source/core/resources/resource_pack.cpp +++ b/source/core/resources/resource_pack.cpp @@ -18,8 +18,7 @@ ResourcePack::~ResourcePack() { } uint32_t ResourcePack::calculateChecksum(const std::vector& data) { - return std::accumulate(data.begin(), data.end(), uint32_t(0x12345678), - [](uint32_t acc, uint8_t b) { return ((acc << 5) + acc) + b; }); + return std::accumulate(data.begin(), data.end(), uint32_t(0x12345678), [](uint32_t acc, uint8_t b) { return ((acc << 5) + acc) + b; }); } void ResourcePack::encryptData(std::vector& data, const std::string& key) { @@ -157,6 +156,7 @@ bool ResourcePack::addDirectory(const std::string& directory) { return false; } + // cppcheck-suppress useStlAlgorithm for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) { if (entry.is_regular_file()) { std::string filepath = entry.path().string(); diff --git a/source/core/system/director.h b/source/core/system/director.h index bec6459..1984d14 100644 --- a/source/core/system/director.h +++ b/source/core/system/director.h @@ -63,6 +63,9 @@ class Director { // Destructor ~Director(); + Director(const Director &) = delete; + Director &operator=(const Director &) = delete; + // Ejecuta un frame del juego SDL_AppResult iterate(); diff --git a/source/game/entities/balloon.h b/source/game/entities/balloon.h index 57f8a8b..568da6d 100644 --- a/source/game/entities/balloon.h +++ b/source/game/entities/balloon.h @@ -146,6 +146,9 @@ class Balloon { // Destructor ~Balloon(); + Balloon(const Balloon &) = delete; + Balloon &operator=(const Balloon &) = delete; + // Centra el globo en la posición X void allignTo(int x); diff --git a/source/game/entities/bullet.h b/source/game/entities/bullet.h index b4ae411..af6d264 100644 --- a/source/game/entities/bullet.h +++ b/source/game/entities/bullet.h @@ -42,6 +42,9 @@ class Bullet { // Destructor ~Bullet(); + Bullet(const Bullet &) = delete; + Bullet &operator=(const Bullet &) = delete; + // Pinta el objeto en pantalla void render(); diff --git a/source/game/entities/item.h b/source/game/entities/item.h index 8f1ce11..32506fb 100644 --- a/source/game/entities/item.h +++ b/source/game/entities/item.h @@ -52,6 +52,9 @@ class Item { // Destructor ~Item(); + Item(const Item &) = delete; + Item &operator=(const Item &) = delete; + // Centra el objeto en la posición X void allignTo(int x); diff --git a/source/game/entities/player.h b/source/game/entities/player.h index 6d4a014..7a213f4 100644 --- a/source/game/entities/player.h +++ b/source/game/entities/player.h @@ -86,6 +86,9 @@ class Player { // Destructor ~Player(); + Player(const Player &) = delete; + Player &operator=(const Player &) = delete; + // Iniciador void init(); diff --git a/source/game/game.cpp b/source/game/game.cpp index 43ade2a..90fb548 100644 --- a/source/game/game.cpp +++ b/source/game/game.cpp @@ -4,9 +4,9 @@ #include // for rand #include // for max, min -#include // for accumulate #include // for basic_ifstream #include // for basic_ostream, char_traits, operator<< +#include // for accumulate #include "core/audio/jail_audio.hpp" // for JA_PlaySound, JA_DeleteSound, JA_LoadSound #include "core/input/global_inputs.hpp" // for GlobalInputs::handle @@ -2184,8 +2184,7 @@ void Game::updateDeathSequence() { // Calcula y establece el valor de amenaza en funcion de los globos activos void Game::evaluateAndSetMenace() { - menaceCurrent = std::accumulate(balloons.begin(), balloons.end(), Uint8(0), - [](Uint8 acc, Balloon *b) { return b->isEnabled() ? acc + b->getMenace() : acc; }); + menaceCurrent = std::accumulate(balloons.begin(), balloons.end(), Uint8(0), [](Uint8 acc, Balloon *b) { return b->isEnabled() ? acc + b->getMenace() : acc; }); } // Obtiene el valor de la variable @@ -3048,8 +3047,7 @@ bool Game::canPowerBallBeCreated() { // Calcula el poder actual de los globos en pantalla int Game::calculateScreenPower() { - return std::accumulate(balloons.begin(), balloons.end(), 0, - [](int acc, Balloon *b) { return b->isEnabled() ? acc + b->getPower() : acc; }); + return std::accumulate(balloons.begin(), balloons.end(), 0, [](int acc, Balloon *b) { return b->isEnabled() ? acc + b->getPower() : acc; }); } // Inicializa las variables que contienen puntos de ruta para mover objetos diff --git a/source/game/game.h b/source/game/game.h index e3ef843..160ce94 100644 --- a/source/game/game.h +++ b/source/game/game.h @@ -243,7 +243,7 @@ class Game { Uint8 lastStageReached; // Contiene el numero de la última pantalla que se ha alcanzado demo_t demo; // Variable con todas las variables relacionadas con el modo demo int totalPowerToCompleteGame; // La suma del poder necesario para completar todas las fases - int cloudsSpeed; // Velocidad a la que se desplazan las nubes + int cloudsSpeed{0}; // Velocidad a la que se desplazan las nubes int pauseCounter; // Contador para salir del menu de pausa y volver al juego bool leavingPauseMenu; // Indica si esta saliendo del menu de pausa para volver al juego bool pauseInitialized; // Indica si la pausa ha sido inicializada @@ -524,6 +524,9 @@ class Game { // Destructor ~Game(); + Game(const Game &) = delete; + Game &operator=(const Game &) = delete; + // Bucle para el juego void run(); diff --git a/source/game/scenes/instructions.h b/source/game/scenes/instructions.h index 9810735..3aa7a37 100644 --- a/source/game/scenes/instructions.h +++ b/source/game/scenes/instructions.h @@ -26,14 +26,14 @@ class Instructions { section_t *section; // Estado del bucle principal para saber si continua o se sale // Variables - Uint16 counter; // Contador - Uint16 counterEnd; // Valor final para el contador - Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa - Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa - bool manualQuit; // Indica si se quiere salir del modo manual - mode_e mode; // Modo en el que se van a ejecutar las instrucciones - bool finished; // Indica si las instrucciones han terminado - bool quitRequested; // Indica si se ha solicitado salir de la aplicación + Uint16 counter; // Contador + Uint16 counterEnd; // Valor final para el contador + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + bool manualQuit; // Indica si se quiere salir del modo manual + mode_e mode{m_auto}; // Modo en el que se van a ejecutar las instrucciones + bool finished; // Indica si las instrucciones han terminado + bool quitRequested; // Indica si se ha solicitado salir de la aplicación // Comprueba las entradas void checkInput(); @@ -45,6 +45,9 @@ class Instructions { // Destructor ~Instructions(); + Instructions(const Instructions &) = delete; + Instructions &operator=(const Instructions &) = delete; + // Bucle principal void run(mode_e mode); diff --git a/source/game/scenes/intro.h b/source/game/scenes/intro.h index 133846d..6b9489f 100644 --- a/source/game/scenes/intro.h +++ b/source/game/scenes/intro.h @@ -47,6 +47,9 @@ class Intro { // Destructor ~Intro(); + Intro(const Intro &) = delete; + Intro &operator=(const Intro &) = delete; + // Bucle principal void run(); diff --git a/source/game/scenes/logo.h b/source/game/scenes/logo.h index bd37010..97be058 100644 --- a/source/game/scenes/logo.h +++ b/source/game/scenes/logo.h @@ -42,6 +42,9 @@ class Logo { // Destructor ~Logo(); + Logo(const Logo &) = delete; + Logo &operator=(const Logo &) = delete; + // Bucle principal void run(); diff --git a/source/game/scenes/title.cpp b/source/game/scenes/title.cpp index 18864f2..d407c70 100644 --- a/source/game/scenes/title.cpp +++ b/source/game/scenes/title.cpp @@ -860,11 +860,13 @@ void Title::iterate() { demoGame->iterate(); if (demoGame->hasFinished()) { - bool wasQuit = (section->name == SECTION_PROG_QUIT); + // cppcheck-suppress knownConditionTrueFalse + const bool wasQuit = (section->name == SECTION_PROG_QUIT); delete demoGame; demoGame = nullptr; demoGameActive = false; + // cppcheck-suppress knownConditionTrueFalse if (wasQuit) { section->name = SECTION_PROG_QUIT; } else if (demoThenInstructions) { diff --git a/source/game/scenes/title.h b/source/game/scenes/title.h index 6957220..6a695ac 100644 --- a/source/game/scenes/title.h +++ b/source/game/scenes/title.h @@ -38,11 +38,11 @@ class Title { }; // Objetos y punteros - SDL_Renderer *renderer; // El renderizador de la ventana - Instructions *instructions; // Objeto para la sección de las instrucciones - Game *demoGame; // Objeto para lanzar la demo del juego - SDL_Event *eventHandler; // Manejador de eventos - section_t *section; // Indicador para el bucle del titulo + SDL_Renderer *renderer; // El renderizador de la ventana + Instructions *instructions{nullptr}; // Objeto para la sección de las instrucciones + Game *demoGame{nullptr}; // Objeto para lanzar la demo del juego + SDL_Event *eventHandler; // Manejador de eventos + section_t *section; // Indicador para el bucle del titulo Texture *dustTexture; // Textura con los graficos del polvo Texture *coffeeTexture; // Textura con los graficos de la palabra coffee @@ -87,16 +87,16 @@ class Title { std::vector deviceIndex; // Indice para el jugador [i] del vector de dispositivos de entrada disponibles // Variables para la vibración del título (SUBSECTION_TITLE_2) - int vibrationStep; // Paso actual de la vibración - int vibrationCoffeeBaseX; // Posición X base del bitmap Coffee - int vibrationCrisisBaseX; // Posición X base del bitmap Crisis - bool vibrationInitialized; // Indica si se han capturado las posiciones base + int vibrationStep; // Paso actual de la vibración + int vibrationCoffeeBaseX{0}; // Posición X base del bitmap Coffee + int vibrationCrisisBaseX{0}; // Posición X base del bitmap Crisis + bool vibrationInitialized; // Indica si se han capturado las posiciones base // Variables para sub-estados delegados (instrucciones y demo) - bool instructionsActive; // Indica si las instrucciones están activas - bool demoGameActive; // Indica si el juego demo está activo - mode_e instructionsMode; // Modo de las instrucciones activas - bool demoThenInstructions; // Indica si tras la demo hay que mostrar instrucciones + bool instructionsActive; // Indica si las instrucciones están activas + bool demoGameActive; // Indica si el juego demo está activo + mode_e instructionsMode{m_auto}; // Modo de las instrucciones activas + bool demoThenInstructions; // Indica si tras la demo hay que mostrar instrucciones // Inicializa los valores void init(); @@ -147,6 +147,9 @@ class Title { // Destructor ~Title(); + Title(const Title &) = delete; + Title &operator=(const Title &) = delete; + // Bucle para el titulo del juego void run(); diff --git a/source/game/ui/menu.cpp b/source/game/ui/menu.cpp index ba6ea14..ee58579 100644 --- a/source/game/ui/menu.cpp +++ b/source/game/ui/menu.cpp @@ -430,8 +430,7 @@ void Menu::setSelectorPos(int index) { // Obtiene la anchura del elemento más ancho del menu int Menu::getWidestItem() { - return std::accumulate(item.begin(), item.end(), 0, - [](int acc, const item_t &i) { return std::max(acc, i.rect.w); }); + return std::accumulate(item.begin(), item.end(), 0, [](int acc, const item_t &i) { return std::max(acc, i.rect.w); }); } // Deja el menu apuntando al primer elemento @@ -792,8 +791,7 @@ int Menu::findWidth() { // Calcula el alto del menu int Menu::findHeight() { - const int height = std::accumulate(item.begin(), item.end(), 0, - [](int acc, const item_t &i) { return acc + i.rect.h + i.hPaddingDown; }); + const int height = std::accumulate(item.begin(), item.end(), 0, [](int acc, const item_t &i) { return acc + i.rect.h + i.hPaddingDown; }); return height - item.back().hPaddingDown; }