refactor: fase 1 — cleanup mecànic de baix risc (NULL→nullptr, typedef→using, explicit, enum class local)

- 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) <noreply@anthropic.com>
This commit is contained in:
2026-04-18 13:37:48 +02:00
parent 27f8b0ae36
commit e7aa2463b4
8 changed files with 40 additions and 40 deletions

View File

@@ -19,20 +19,20 @@
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
JD8_Surface screen = NULL; JD8_Surface screen = nullptr;
JD8_Palette main_palette = NULL; JD8_Palette main_palette = nullptr;
Uint32* pixel_data = NULL; Uint32* pixel_data = nullptr;
void JD8_Init() { void JD8_Init() {
screen = (JD8_Surface)calloc(1, 64000); screen = static_cast<JD8_Surface>(calloc(1, 64000));
main_palette = (JD8_Palette)calloc(1, 768); main_palette = static_cast<JD8_Palette>(calloc(1, 768));
pixel_data = (Uint32*)calloc(1, 320 * 200 * 4); pixel_data = static_cast<Uint32*>(calloc(1, 320 * 200 * 4));
} }
void JD8_Quit() { void JD8_Quit() {
if (screen != NULL) free(screen); if (screen != nullptr) free(screen);
if (main_palette != NULL) free(main_palette); if (main_palette != nullptr) free(main_palette);
if (pixel_data != NULL) free(pixel_data); if (pixel_data != nullptr) free(pixel_data);
} }
void JD8_ClearScreen(Uint8 color) { void JD8_ClearScreen(Uint8 color) {
@@ -40,8 +40,8 @@ void JD8_ClearScreen(Uint8 color) {
} }
JD8_Surface JD8_NewSurface() { JD8_Surface JD8_NewSurface() {
JD8_Surface surface = (JD8_Surface)calloc(1, 64000); JD8_Surface surface = static_cast<JD8_Surface>(calloc(1, 64000));
if (surface == NULL) { if (surface == nullptr) {
printf("JD8_NewSurface: out of memory\n"); printf("JD8_NewSurface: out of memory\n");
exit(1); exit(1);
} }
@@ -74,7 +74,7 @@ JD8_Surface JD8_LoadSurface(const char* file) {
auto buffer = ResourceHelper::loadFile(file); auto buffer = ResourceHelper::loadFile(file);
unsigned short w, h; unsigned short w, h;
Uint8* pixels = LoadGif(buffer.data(), &w, &h); Uint8* pixels = LoadGif(buffer.data(), &w, &h);
if (pixels == NULL) { if (pixels == nullptr) {
printf("Unable to load bitmap: %s\n", SDL_GetError()); printf("Unable to load bitmap: %s\n", SDL_GetError());
exit(1); exit(1);
} }
@@ -91,8 +91,8 @@ JD8_Palette JD8_LoadPalette(const char* file) {
// Reservem un buffer 768 bytes (256 * RGB) que el caller ha // Reservem un buffer 768 bytes (256 * RGB) que el caller ha
// d'alliberar amb free() — mateixa convenció que el LoadPalette // d'alliberar amb free() — mateixa convenció que el LoadPalette
// original (retornava un malloc). // original (retornava un malloc).
JD8_Palette palette = (JD8_Palette)malloc(768); JD8_Palette palette = static_cast<JD8_Palette>(malloc(768));
if (palette == NULL) { if (palette == nullptr) {
printf("JD8_LoadPalette: out of memory\n"); printf("JD8_LoadPalette: out of memory\n");
exit(1); exit(1);
} }
@@ -104,12 +104,12 @@ JD8_Palette JD8_LoadPalette(const char* file) {
} }
auto buffer = ResourceHelper::loadFile(file); auto buffer = ResourceHelper::loadFile(file);
return (JD8_Palette)LoadPalette(buffer.data()); return reinterpret_cast<JD8_Palette>(LoadPalette(buffer.data()));
} }
void JD8_SetScreenPalette(JD8_Palette palette) { void JD8_SetScreenPalette(JD8_Palette palette) {
if (main_palette == palette) return; if (main_palette == palette) return;
if (main_palette != NULL) free(main_palette); if (main_palette != nullptr) free(main_palette);
main_palette = 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. // el caller decideix quan fer Flip.
namespace { namespace {
enum FadeType { enum class FadeType {
FADE_NONE = 0, None = 0,
FADE_OUT, Out,
FADE_TO_PAL, ToPal,
}; };
constexpr int FADE_STEPS = 32; constexpr int FADE_STEPS = 32;
FadeType fade_type = FADE_NONE; FadeType fade_type = FadeType::None;
Color fade_target[256]; Color fade_target[256];
int fade_step = 0; int fade_step = 0;
void apply_fade_step() { void apply_fade_step() {
if (fade_type == FADE_OUT) { if (fade_type == FadeType::Out) {
for (int i = 0; i < 256; i++) { 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].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].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; 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++) { 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 = main_palette[i].r <= int(fade_target[i].r) - 8
? main_palette[i].r + 8 ? main_palette[i].r + 8
@@ -284,28 +284,28 @@ namespace {
} // namespace } // namespace
void JD8_FadeStartOut() { void JD8_FadeStartOut() {
fade_type = FADE_OUT; fade_type = FadeType::Out;
fade_step = 0; fade_step = 0;
} }
void JD8_FadeStartToPal(JD8_Palette pal) { void JD8_FadeStartToPal(JD8_Palette pal) {
fade_type = FADE_TO_PAL; fade_type = FadeType::ToPal;
memcpy(fade_target, pal, sizeof(Color) * 256); memcpy(fade_target, pal, sizeof(Color) * 256);
fade_step = 0; fade_step = 0;
} }
bool JD8_FadeIsActive() { bool JD8_FadeIsActive() {
return fade_type != FADE_NONE; return fade_type != FadeType::None;
} }
bool JD8_FadeTickStep() { bool JD8_FadeTickStep() {
if (fade_type == FADE_NONE) return true; if (fade_type == FadeType::None) return true;
apply_fade_step(); apply_fade_step();
fade_step++; fade_step++;
if (fade_step >= FADE_STEPS) { if (fade_step >= FADE_STEPS) {
fade_type = FADE_NONE; fade_type = FadeType::None;
return true; return true;
} }
return false; return false;

View File

@@ -7,8 +7,8 @@ struct Color {
Uint8 b; Uint8 b;
}; };
typedef Uint8* JD8_Surface; using JD8_Surface = Uint8*;
typedef Color* JD8_Palette; using JD8_Palette = Color*;
void JD8_Init(); void JD8_Init();

View File

@@ -6,7 +6,7 @@
class Bola : public Sprite { class Bola : public Sprite {
public: public:
Bola(JD8_Surface gfx, Prota* sam); explicit Bola(JD8_Surface gfx, Prota* sam);
void draw() override; void draw() override;
void update(); void update();

View File

@@ -4,7 +4,7 @@
class Engendro : public Sprite { class Engendro : public Sprite {
public: public:
Engendro(JD8_Surface gfx, Uint16 x, Uint16 y); explicit Engendro(JD8_Surface gfx, Uint16 x, Uint16 y);
bool update(); bool update();

View File

@@ -27,7 +27,7 @@ struct Vertex {
class Mapa { class Mapa {
public: public:
Mapa(JD8_Surface gfx, Prota* sam); explicit Mapa(JD8_Surface gfx, Prota* sam);
~Mapa(void); ~Mapa(void);
void draw(); void draw();

View File

@@ -6,7 +6,7 @@
class Marcador { class Marcador {
public: public:
Marcador(JD8_Surface gfx, Prota* sam); explicit Marcador(JD8_Surface gfx, Prota* sam);
~Marcador(void); ~Marcador(void);
void draw(); void draw();

View File

@@ -40,7 +40,7 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
this->cur_frame = 0; this->cur_frame = 0;
this->o = rand() % 4; this->o = rand() % 4;
this->cycles_per_frame = 4; this->cycles_per_frame = 4;
this->next = NULL; this->next = nullptr;
if (this->dimoni) { if (this->dimoni) {
if (x == 0) { if (x == 0) {
@@ -65,7 +65,7 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
} }
void Momia::clear() { void Momia::clear() {
if (this->next != NULL) this->next->clear(); if (this->next != nullptr) this->next->clear();
delete this->next; 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() { bool Momia::update() {
@@ -155,7 +155,7 @@ bool Momia::update() {
} }
} }
if (this->next != NULL) { if (this->next != nullptr) {
if (this->next->update()) { if (this->next->update()) {
Momia* seguent = this->next->next; Momia* seguent = this->next->next;
delete this->next; delete this->next;
@@ -168,7 +168,7 @@ bool Momia::update() {
} }
void Momia::insertar(Momia* momia) { void Momia::insertar(Momia* momia) {
if (this->next != NULL) { if (this->next != nullptr) {
this->next->insertar(momia); this->next->insertar(momia);
} else { } else {
this->next = momia; this->next = momia;

View File

@@ -9,7 +9,7 @@
class Momia : public Sprite { class Momia : public Sprite {
public: 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 clear();
void draw() override; void draw() override;