From 27f8b0ae360cad1ed99d4b92dd6649186ee28769 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 18 Apr 2026 13:22:13 +0200 Subject: [PATCH] cppcheck --- .claude/scheduled_tasks.lock | 1 + source/core/audio/jail_audio.hpp | 6 +++--- source/core/jail/jdraw8.cpp | 11 +++++++++-- source/core/jail/jfile.cpp | 4 +--- source/core/rendering/text.cpp | 12 ++++++------ source/game/bola.hpp | 2 +- source/game/engendro.cpp | 4 ---- source/game/engendro.hpp | 1 - source/game/modulegame.cpp | 27 ++++++++++---------------- source/game/modulegame.hpp | 12 +++++++----- source/game/momia.cpp | 19 ++++++++---------- source/game/momia.hpp | 6 ++++-- source/game/prota.hpp | 4 ++-- source/game/sprite.hpp | 4 ++-- source/scenes/boot_loader_scene.hpp | 1 - source/scenes/credits_scene.hpp | 1 - source/scenes/intro_new_logo_scene.hpp | 1 - source/scenes/intro_scene.hpp | 1 - source/scenes/intro_sprites_scene.hpp | 1 - source/scenes/menu_scene.hpp | 1 - source/scenes/mort_scene.hpp | 1 - 21 files changed, 54 insertions(+), 66 deletions(-) create mode 100644 .claude/scheduled_tasks.lock diff --git a/.claude/scheduled_tasks.lock b/.claude/scheduled_tasks.lock new file mode 100644 index 0000000..ff296c9 --- /dev/null +++ b/.claude/scheduled_tasks.lock @@ -0,0 +1 @@ +{"sessionId":"7b0c9c32-3dd4-48a3-ba06-c2303dc08243","pid":123890,"acquiredAt":1776510185734} \ No newline at end of file diff --git a/source/core/audio/jail_audio.hpp b/source/core/audio/jail_audio.hpp index 63308e0..a6289ac 100644 --- a/source/core/audio/jail_audio.hpp +++ b/source/core/audio/jail_audio.hpp @@ -265,13 +265,13 @@ inline JA_Music_t* JA_LoadMusic(const Uint8* buffer, Uint32 length) { auto* music = new JA_Music_t(); music->ogg_data.assign(buffer, buffer + length); - int error = 0; + int vorbis_error = 0; music->vorbis = stb_vorbis_open_memory(music->ogg_data.data(), static_cast(length), - &error, + &vorbis_error, nullptr); if (!music->vorbis) { - std::cout << "JA_LoadMusic: stb_vorbis_open_memory failed (error " << error << ")" << '\n'; + std::cout << "JA_LoadMusic: stb_vorbis_open_memory failed (error " << vorbis_error << ")" << '\n'; delete music; return nullptr; } diff --git a/source/core/jail/jdraw8.cpp b/source/core/jail/jdraw8.cpp index 99d796e..52db252 100644 --- a/source/core/jail/jdraw8.cpp +++ b/source/core/jail/jdraw8.cpp @@ -40,8 +40,11 @@ void JD8_ClearScreen(Uint8 color) { } JD8_Surface JD8_NewSurface() { - JD8_Surface surface = (JD8_Surface)malloc(64000); - memset(surface, 0, 64000); + JD8_Surface surface = (JD8_Surface)calloc(1, 64000); + if (surface == NULL) { + printf("JD8_NewSurface: out of memory\n"); + exit(1); + } return surface; } @@ -89,6 +92,10 @@ JD8_Palette JD8_LoadPalette(const char* file) { // d'alliberar amb free() — mateixa convenció que el LoadPalette // original (retornava un malloc). JD8_Palette palette = (JD8_Palette)malloc(768); + if (palette == NULL) { + printf("JD8_LoadPalette: out of memory\n"); + exit(1); + } memcpy(palette, cached.data(), 768); return palette; } catch (const std::exception&) { diff --git a/source/core/jail/jfile.cpp b/source/core/jail/jfile.cpp index 8efe30b..499fd87 100644 --- a/source/core/jail/jfile.cpp +++ b/source/core/jail/jfile.cpp @@ -82,9 +82,7 @@ void file_setconfigfolder(const char* foldername) { config_folder = std::string(homedir) + "/.config/" + foldername; #endif - if (!config_folder.empty()) { - std::filesystem::create_directories(config_folder); - } + std::filesystem::create_directories(config_folder); } const char* file_getconfigfolder() { diff --git a/source/core/rendering/text.cpp b/source/core/rendering/text.cpp index 89e836e..0e936fd 100644 --- a/source/core/rendering/text.cpp +++ b/source/core/rendering/text.cpp @@ -80,27 +80,27 @@ void Text::loadFont(const char* fnt_file) { // Elimina comentaris inline auto comment_pos = line.find('#'); if (comment_pos != std::string::npos) { - line = line.substr(0, comment_pos); + line.resize(comment_pos); } // Parseja directives - if (line.find("box_width") == 0) { + if (line.starts_with("box_width")) { sscanf(line.c_str(), "box_width %d", &box_width_); continue; } - if (line.find("box_height") == 0) { + if (line.starts_with("box_height")) { sscanf(line.c_str(), "box_height %d", &box_height_); continue; } - if (line.find("columns") == 0) { + if (line.starts_with("columns")) { sscanf(line.c_str(), "columns %d", &columns_); continue; } - if (line.find("cell_spacing") == 0) { + if (line.starts_with("cell_spacing")) { sscanf(line.c_str(), "cell_spacing %d", &cell_spacing_); continue; } - if (line.find("row_spacing") == 0) { + if (line.starts_with("row_spacing")) { sscanf(line.c_str(), "row_spacing %d", &row_spacing_); continue; } diff --git a/source/game/bola.hpp b/source/game/bola.hpp index f5f3c5d..be19ba6 100644 --- a/source/game/bola.hpp +++ b/source/game/bola.hpp @@ -8,7 +8,7 @@ class Bola : public Sprite { public: Bola(JD8_Surface gfx, Prota* sam); - void draw(); + void draw() override; void update(); protected: diff --git a/source/game/engendro.cpp b/source/game/engendro.cpp index 48e37d5..7bd7262 100644 --- a/source/game/engendro.cpp +++ b/source/game/engendro.cpp @@ -29,10 +29,6 @@ Engendro::Engendro(JD8_Surface gfx, Uint16 x, Uint16 y) this->cycles_per_frame = 30; } -void Engendro::draw() { - Sprite::draw(); -} - bool Engendro::update() { bool mort = false; diff --git a/source/game/engendro.hpp b/source/game/engendro.hpp index 20adb40..5c04edc 100644 --- a/source/game/engendro.hpp +++ b/source/game/engendro.hpp @@ -6,7 +6,6 @@ class Engendro : public Sprite { public: Engendro(JD8_Surface gfx, Uint16 x, Uint16 y); - void draw(); bool update(); protected: diff --git a/source/game/modulegame.cpp b/source/game/modulegame.cpp index 5946944..6b256e0 100644 --- a/source/game/modulegame.cpp +++ b/source/game/modulegame.cpp @@ -9,28 +9,21 @@ ModuleGame::ModuleGame() { this->gfx = JD8_LoadSurface(info::ctx.pepe_activat ? "gfx/frames2.gif" : "gfx/frames.gif"); JG_SetUpdateTicks(10); - this->sam = new Prota(this->gfx); - this->mapa = new Mapa(this->gfx, this->sam); - this->marcador = new Marcador(this->gfx, this->sam); + this->sam = std::make_unique(this->gfx); + this->mapa = std::make_unique(this->gfx, this->sam.get()); + this->marcador = std::make_unique(this->gfx, this->sam.get()); if (info::ctx.num_piramide == 2) { - this->bola = new Bola(this->gfx, this->sam); - } else { - this->bola = nullptr; + this->bola = std::make_unique(this->gfx, this->sam.get()); } - this->momies = nullptr; this->iniciarMomies(); } ModuleGame::~ModuleGame() { - if (this->bola != nullptr) delete this->bola; if (this->momies != nullptr) { this->momies->clear(); delete this->momies; } - delete this->marcador; - delete this->mapa; - delete this->sam; JD8_FreeSurface(this->gfx); } @@ -123,7 +116,7 @@ void ModuleGame::Draw() { this->marcador->draw(); this->sam->draw(); if (this->momies != nullptr) this->momies->draw(); - if (this->bola != nullptr) this->bola->draw(); + if (this->bola) this->bola->draw(); } void ModuleGame::Update() { @@ -137,14 +130,14 @@ void ModuleGame::Update() { this->momies = seguent; info::ctx.momies--; } - if (this->bola != nullptr) this->bola->update(); + if (this->bola) this->bola->update(); this->mapa->update(); if (this->mapa->novaMomia()) { if (this->momies != nullptr) { - this->momies->insertar(new Momia(this->gfx, true, 0, 0, this->sam)); + this->momies->insertar(new Momia(this->gfx, true, 0, 0, this->sam.get())); info::ctx.momies++; } else { - this->momies = new Momia(this->gfx, true, 0, 0, this->sam); + this->momies = new Momia(this->gfx, true, 0, 0, this->sam.get()); info::ctx.momies++; } } @@ -187,9 +180,9 @@ void ModuleGame::iniciarMomies() { bool dimonis = info::ctx.num_piramide == 6; for (int i = 0; i < info::ctx.momies; i++) { if (this->momies == nullptr) { - this->momies = new Momia(this->gfx, dimonis, x, y, this->sam); + this->momies = new Momia(this->gfx, dimonis, x, y, this->sam.get()); } else { - this->momies->insertar(new Momia(this->gfx, dimonis, x, y, this->sam)); + this->momies->insertar(new Momia(this->gfx, dimonis, x, y, this->sam.get())); } x += 65; if (x == 345) { diff --git a/source/game/modulegame.hpp b/source/game/modulegame.hpp index 338b92d..f931aff 100644 --- a/source/game/modulegame.hpp +++ b/source/game/modulegame.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "game/bola.hpp" #include "game/info.hpp" #include "game/mapa.hpp" @@ -52,9 +54,9 @@ class ModuleGame : public scenes::Scene { Uint8 final_{0}; JD8_Surface gfx{nullptr}; - Mapa* mapa{nullptr}; - Prota* sam{nullptr}; - Marcador* marcador{nullptr}; - Momia* momies{nullptr}; - Bola* bola{nullptr}; + std::unique_ptr mapa; + std::unique_ptr sam; + std::unique_ptr marcador; + Momia* momies{nullptr}; // llista enllaçada, ownership enredat amb `next` + std::unique_ptr bola; }; diff --git a/source/game/momia.cpp b/source/game/momia.cpp index f76a08c..8aa69f0 100644 --- a/source/game/momia.cpp +++ b/source/game/momia.cpp @@ -10,15 +10,15 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam) this->sam = sam; entitat.frames.reserve(20); - for (int y = 0; y < 4; y++) { - for (int x = 0; x < 5; x++) { + for (int row = 0; row < 4; row++) { + for (int col = 0; col < 5; col++) { Frame f; f.w = 15; f.h = 15; if (info::ctx.num_piramide == 4) f.h -= 5; - f.x = (x * 15) + 75; + f.x = (col * 15) + 75; if (this->dimoni) f.x += 75; - f.y = 20 + (y * 15); + f.y = 20 + (row * 15); entitat.frames.push_back(f); } } @@ -57,9 +57,8 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam) } else { this->y = y; } - this->engendro = new Engendro(gfx, this->x, this->y); + this->engendro = std::make_unique(gfx, this->x, this->y); } else { - this->engendro = NULL; this->x = x; this->y = y; } @@ -67,12 +66,11 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam) void Momia::clear() { if (this->next != NULL) this->next->clear(); - if (this->engendro != NULL) delete this->engendro; delete this->next; } void Momia::draw() { - if (this->engendro != NULL) { + if (this->engendro) { this->engendro->draw(); } else { Sprite::draw(); @@ -91,10 +89,9 @@ void Momia::draw() { bool Momia::update() { bool morta = false; - if (this->engendro != NULL) { + if (this->engendro) { if (this->engendro->update()) { - delete this->engendro; - this->engendro = NULL; + this->engendro.reset(); } } else { if (this->sam->o < 4 && (this->dimoni || info::ctx.num_piramide == 5 || JG_GetCycleCounter() % 2 == 0)) { diff --git a/source/game/momia.hpp b/source/game/momia.hpp index 4b3b124..c568220 100644 --- a/source/game/momia.hpp +++ b/source/game/momia.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "game/engendro.hpp" #include "game/info.hpp" #include "game/prota.hpp" @@ -10,7 +12,7 @@ class Momia : public Sprite { Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam); void clear(); - void draw(); + void draw() override; bool update(); void insertar(Momia* momia); @@ -19,5 +21,5 @@ class Momia : public Sprite { protected: Prota* sam; - Engendro* engendro; + std::unique_ptr engendro; }; diff --git a/source/game/prota.hpp b/source/game/prota.hpp index fe7b08c..4ad0c0c 100644 --- a/source/game/prota.hpp +++ b/source/game/prota.hpp @@ -5,9 +5,9 @@ class Prota : public Sprite { public: - Prota(JD8_Surface gfx); + explicit Prota(JD8_Surface gfx); - void draw(); + void draw() override; Uint8 update(); Uint8 frame_pejades; diff --git a/source/game/sprite.hpp b/source/game/sprite.hpp index 149ae4a..04a7916 100644 --- a/source/game/sprite.hpp +++ b/source/game/sprite.hpp @@ -22,10 +22,10 @@ struct Entitat { class Sprite { public: - Sprite(JD8_Surface gfx); + explicit Sprite(JD8_Surface gfx); virtual ~Sprite() = default; - void draw(); + virtual void draw(); Entitat entitat; Uint8 cur_frame = 0; diff --git a/source/scenes/boot_loader_scene.hpp b/source/scenes/boot_loader_scene.hpp index 94a05a7..f39e02e 100644 --- a/source/scenes/boot_loader_scene.hpp +++ b/source/scenes/boot_loader_scene.hpp @@ -16,7 +16,6 @@ namespace scenes { void onEnter() override; void tick(int delta_ms) override; bool done() const override { return done_; } - int nextState() const override { return 1; } // 1 → SceneRegistry::tryCreate(num_piramide=255 → intro) private: void render() const; diff --git a/source/scenes/credits_scene.hpp b/source/scenes/credits_scene.hpp index 6862f95..19d528b 100644 --- a/source/scenes/credits_scene.hpp +++ b/source/scenes/credits_scene.hpp @@ -29,7 +29,6 @@ namespace scenes { void onEnter() override; void tick(int delta_ms) override; bool done() const override { return phase_ == Phase::Done; } - int nextState() const override { return 1; } private: enum class Phase { Rolling, diff --git a/source/scenes/intro_new_logo_scene.hpp b/source/scenes/intro_new_logo_scene.hpp index 11926ab..6a3b32c 100644 --- a/source/scenes/intro_new_logo_scene.hpp +++ b/source/scenes/intro_new_logo_scene.hpp @@ -37,7 +37,6 @@ namespace scenes { void onEnter() override; void tick(int delta_ms) override; bool done() const override { return phase_ == Phase::Done; } - int nextState() const override { return 1; } private: enum class Phase { diff --git a/source/scenes/intro_scene.hpp b/source/scenes/intro_scene.hpp index 9e29382..1486bdc 100644 --- a/source/scenes/intro_scene.hpp +++ b/source/scenes/intro_scene.hpp @@ -38,7 +38,6 @@ namespace scenes { void onEnter() override; void tick(int delta_ms) override; bool done() const override { return phase_ == Phase::Done; } - int nextState() const override { return 1; } private: enum class Phase { diff --git a/source/scenes/intro_sprites_scene.hpp b/source/scenes/intro_sprites_scene.hpp index 176ed9e..523223f 100644 --- a/source/scenes/intro_sprites_scene.hpp +++ b/source/scenes/intro_sprites_scene.hpp @@ -29,7 +29,6 @@ namespace scenes { void onEnter() override; void tick(int delta_ms) override; bool done() const override { return done_; } - int nextState() const override { return 1; } private: SurfaceHandle gfx_; diff --git a/source/scenes/menu_scene.hpp b/source/scenes/menu_scene.hpp index 64eca4e..64dd4b0 100644 --- a/source/scenes/menu_scene.hpp +++ b/source/scenes/menu_scene.hpp @@ -27,7 +27,6 @@ namespace scenes { void onEnter() override; void tick(int delta_ms) override; bool done() const override { return phase_ == Phase::Done; } - int nextState() const override { return 1; } private: enum class Phase { FadingIn, diff --git a/source/scenes/mort_scene.hpp b/source/scenes/mort_scene.hpp index e2e1ed6..63a389b 100644 --- a/source/scenes/mort_scene.hpp +++ b/source/scenes/mort_scene.hpp @@ -19,7 +19,6 @@ namespace scenes { void onEnter() override; void tick(int delta_ms) override; bool done() const override { return phase_ == Phase::Done; } - int nextState() const override { return 1; } private: enum class Phase { FadingIn,