From e7aa2463b4984ab0aec131c253cf7de54b8da914 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 18 Apr 2026 13:37:48 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20fase=201=20=E2=80=94=20cleanup=20me?= =?UTF-8?q?c=C3=A0nic=20de=20baix=20risc=20(NULL=E2=86=92nullptr,=20typede?= =?UTF-8?q?f=E2=86=92using,=20explicit,=20enum=20class=20local)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - jdraw8.hpp: typedef → using (JD8_Surface, JD8_Palette) - jdraw8.cpp: NULL → nullptr, C-casts → static_cast/reinterpret_cast, anon enum FadeType → enum class - momia.cpp: NULL → nullptr - bola/mapa/marcador/momia/engendro: explicit als constructors Zero canvis de lògica ni ownership. Primera fase de la modernització RAII. Co-Authored-By: Claude Opus 4.7 (1M context) --- source/core/jail/jdraw8.cpp | 56 ++++++++++++++++++------------------- source/core/jail/jdraw8.hpp | 4 +-- source/game/bola.hpp | 2 +- source/game/engendro.hpp | 2 +- source/game/mapa.hpp | 2 +- source/game/marcador.hpp | 2 +- source/game/momia.cpp | 10 +++---- source/game/momia.hpp | 2 +- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/source/core/jail/jdraw8.cpp b/source/core/jail/jdraw8.cpp index 52db252..49ad4b6 100644 --- a/source/core/jail/jdraw8.cpp +++ b/source/core/jail/jdraw8.cpp @@ -19,20 +19,20 @@ #pragma GCC diagnostic pop #endif -JD8_Surface screen = NULL; -JD8_Palette main_palette = NULL; -Uint32* pixel_data = NULL; +JD8_Surface screen = nullptr; +JD8_Palette main_palette = nullptr; +Uint32* pixel_data = nullptr; void JD8_Init() { - screen = (JD8_Surface)calloc(1, 64000); - main_palette = (JD8_Palette)calloc(1, 768); - pixel_data = (Uint32*)calloc(1, 320 * 200 * 4); + screen = static_cast(calloc(1, 64000)); + main_palette = static_cast(calloc(1, 768)); + pixel_data = static_cast(calloc(1, 320 * 200 * 4)); } void JD8_Quit() { - if (screen != NULL) free(screen); - if (main_palette != NULL) free(main_palette); - if (pixel_data != NULL) free(pixel_data); + if (screen != nullptr) free(screen); + if (main_palette != nullptr) free(main_palette); + if (pixel_data != nullptr) free(pixel_data); } void JD8_ClearScreen(Uint8 color) { @@ -40,8 +40,8 @@ void JD8_ClearScreen(Uint8 color) { } JD8_Surface JD8_NewSurface() { - JD8_Surface surface = (JD8_Surface)calloc(1, 64000); - if (surface == NULL) { + JD8_Surface surface = static_cast(calloc(1, 64000)); + if (surface == nullptr) { printf("JD8_NewSurface: out of memory\n"); exit(1); } @@ -74,7 +74,7 @@ JD8_Surface JD8_LoadSurface(const char* file) { auto buffer = ResourceHelper::loadFile(file); unsigned short w, h; Uint8* pixels = LoadGif(buffer.data(), &w, &h); - if (pixels == NULL) { + if (pixels == nullptr) { printf("Unable to load bitmap: %s\n", SDL_GetError()); exit(1); } @@ -91,8 +91,8 @@ JD8_Palette JD8_LoadPalette(const char* file) { // Reservem un buffer 768 bytes (256 * RGB) que el caller ha // d'alliberar amb free() — mateixa convenció que el LoadPalette // original (retornava un malloc). - JD8_Palette palette = (JD8_Palette)malloc(768); - if (palette == NULL) { + JD8_Palette palette = static_cast(malloc(768)); + if (palette == nullptr) { printf("JD8_LoadPalette: out of memory\n"); exit(1); } @@ -104,12 +104,12 @@ JD8_Palette JD8_LoadPalette(const char* file) { } auto buffer = ResourceHelper::loadFile(file); - return (JD8_Palette)LoadPalette(buffer.data()); + return reinterpret_cast(LoadPalette(buffer.data())); } void JD8_SetScreenPalette(JD8_Palette palette) { if (main_palette == palette) return; - if (main_palette != NULL) free(main_palette); + if (main_palette != nullptr) free(main_palette); main_palette = palette; } @@ -247,26 +247,26 @@ void JD8_SetPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b) { // el caller decideix quan fer Flip. namespace { - enum FadeType { - FADE_NONE = 0, - FADE_OUT, - FADE_TO_PAL, + enum class FadeType { + None = 0, + Out, + ToPal, }; constexpr int FADE_STEPS = 32; - FadeType fade_type = FADE_NONE; + FadeType fade_type = FadeType::None; Color fade_target[256]; int fade_step = 0; void apply_fade_step() { - if (fade_type == FADE_OUT) { + if (fade_type == FadeType::Out) { for (int i = 0; i < 256; i++) { main_palette[i].r = main_palette[i].r >= 8 ? main_palette[i].r - 8 : 0; main_palette[i].g = main_palette[i].g >= 8 ? main_palette[i].g - 8 : 0; main_palette[i].b = main_palette[i].b >= 8 ? main_palette[i].b - 8 : 0; } - } else if (fade_type == FADE_TO_PAL) { + } else if (fade_type == FadeType::ToPal) { for (int i = 0; i < 256; i++) { main_palette[i].r = main_palette[i].r <= int(fade_target[i].r) - 8 ? main_palette[i].r + 8 @@ -284,28 +284,28 @@ namespace { } // namespace void JD8_FadeStartOut() { - fade_type = FADE_OUT; + fade_type = FadeType::Out; fade_step = 0; } void JD8_FadeStartToPal(JD8_Palette pal) { - fade_type = FADE_TO_PAL; + fade_type = FadeType::ToPal; memcpy(fade_target, pal, sizeof(Color) * 256); fade_step = 0; } bool JD8_FadeIsActive() { - return fade_type != FADE_NONE; + return fade_type != FadeType::None; } bool JD8_FadeTickStep() { - if (fade_type == FADE_NONE) return true; + if (fade_type == FadeType::None) return true; apply_fade_step(); fade_step++; if (fade_step >= FADE_STEPS) { - fade_type = FADE_NONE; + fade_type = FadeType::None; return true; } return false; diff --git a/source/core/jail/jdraw8.hpp b/source/core/jail/jdraw8.hpp index dc45354..2f25726 100644 --- a/source/core/jail/jdraw8.hpp +++ b/source/core/jail/jdraw8.hpp @@ -7,8 +7,8 @@ struct Color { Uint8 b; }; -typedef Uint8* JD8_Surface; -typedef Color* JD8_Palette; +using JD8_Surface = Uint8*; +using JD8_Palette = Color*; void JD8_Init(); diff --git a/source/game/bola.hpp b/source/game/bola.hpp index be19ba6..ff6873a 100644 --- a/source/game/bola.hpp +++ b/source/game/bola.hpp @@ -6,7 +6,7 @@ class Bola : public Sprite { public: - Bola(JD8_Surface gfx, Prota* sam); + explicit Bola(JD8_Surface gfx, Prota* sam); void draw() override; void update(); diff --git a/source/game/engendro.hpp b/source/game/engendro.hpp index 5c04edc..b183d82 100644 --- a/source/game/engendro.hpp +++ b/source/game/engendro.hpp @@ -4,7 +4,7 @@ class Engendro : public Sprite { public: - Engendro(JD8_Surface gfx, Uint16 x, Uint16 y); + explicit Engendro(JD8_Surface gfx, Uint16 x, Uint16 y); bool update(); diff --git a/source/game/mapa.hpp b/source/game/mapa.hpp index 5584d5e..d6e168b 100644 --- a/source/game/mapa.hpp +++ b/source/game/mapa.hpp @@ -27,7 +27,7 @@ struct Vertex { class Mapa { public: - Mapa(JD8_Surface gfx, Prota* sam); + explicit Mapa(JD8_Surface gfx, Prota* sam); ~Mapa(void); void draw(); diff --git a/source/game/marcador.hpp b/source/game/marcador.hpp index 6427c5a..620e22c 100644 --- a/source/game/marcador.hpp +++ b/source/game/marcador.hpp @@ -6,7 +6,7 @@ class Marcador { public: - Marcador(JD8_Surface gfx, Prota* sam); + explicit Marcador(JD8_Surface gfx, Prota* sam); ~Marcador(void); void draw(); diff --git a/source/game/momia.cpp b/source/game/momia.cpp index 8aa69f0..e24d470 100644 --- a/source/game/momia.cpp +++ b/source/game/momia.cpp @@ -40,7 +40,7 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam) this->cur_frame = 0; this->o = rand() % 4; this->cycles_per_frame = 4; - this->next = NULL; + this->next = nullptr; if (this->dimoni) { if (x == 0) { @@ -65,7 +65,7 @@ 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->next != nullptr) this->next->clear(); delete this->next; } @@ -83,7 +83,7 @@ void Momia::draw() { } } } - if (this->next != NULL) this->next->draw(); + if (this->next != nullptr) this->next->draw(); } bool Momia::update() { @@ -155,7 +155,7 @@ bool Momia::update() { } } - if (this->next != NULL) { + if (this->next != nullptr) { if (this->next->update()) { Momia* seguent = this->next->next; delete this->next; @@ -168,7 +168,7 @@ bool Momia::update() { } void Momia::insertar(Momia* momia) { - if (this->next != NULL) { + if (this->next != nullptr) { this->next->insertar(momia); } else { this->next = momia; diff --git a/source/game/momia.hpp b/source/game/momia.hpp index c568220..fb1a9be 100644 --- a/source/game/momia.hpp +++ b/source/game/momia.hpp @@ -9,7 +9,7 @@ class Momia : public Sprite { public: - Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam); + explicit Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam); void clear(); void draw() override;