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:
@@ -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<JD8_Surface>(calloc(1, 64000));
|
||||
main_palette = static_cast<JD8_Palette>(calloc(1, 768));
|
||||
pixel_data = static_cast<Uint32*>(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<JD8_Surface>(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<JD8_Palette>(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<JD8_Palette>(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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
class Marcador {
|
||||
public:
|
||||
Marcador(JD8_Surface gfx, Prota* sam);
|
||||
explicit Marcador(JD8_Surface gfx, Prota* sam);
|
||||
~Marcador(void);
|
||||
|
||||
void draw();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user