refactor: fase 2 — elimina malloc/free a jdraw8 i paletes d'escenes
- JD8_Init/Quit: new[]/delete[] per a screen, main_palette, pixel_data
- JD8_NewSurface/FreeSurface: new Uint8[64000]{}/delete[]
- JD8_LoadPalette: uniforme — sempre retorna `new Color[256]`, copiant del
LoadPalette extern al path no-cached (l'intermedi raw es frees amb free()
perquè gif.h el malloca)
- JD8_SetScreenPalette: delete[] la paleta reemplaçada
- slides/secreta/menu/banner/mort scenes: std::free/std::malloc → delete[]/new Color[256]
Ownership uniforme: tot el cicle de vida de surface/palette usa new[]/delete[].
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -24,15 +24,18 @@ JD8_Palette main_palette = nullptr;
|
||||
Uint32* pixel_data = nullptr;
|
||||
|
||||
void JD8_Init() {
|
||||
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));
|
||||
screen = new Uint8[64000]{};
|
||||
main_palette = new Color[256]{};
|
||||
pixel_data = new Uint32[320 * 200]{};
|
||||
}
|
||||
|
||||
void JD8_Quit() {
|
||||
if (screen != nullptr) free(screen);
|
||||
if (main_palette != nullptr) free(main_palette);
|
||||
if (pixel_data != nullptr) free(pixel_data);
|
||||
delete[] screen;
|
||||
delete[] main_palette;
|
||||
delete[] pixel_data;
|
||||
screen = nullptr;
|
||||
main_palette = nullptr;
|
||||
pixel_data = nullptr;
|
||||
}
|
||||
|
||||
void JD8_ClearScreen(Uint8 color) {
|
||||
@@ -40,12 +43,7 @@ void JD8_ClearScreen(Uint8 color) {
|
||||
}
|
||||
|
||||
JD8_Surface JD8_NewSurface() {
|
||||
JD8_Surface surface = static_cast<JD8_Surface>(calloc(1, 64000));
|
||||
if (surface == nullptr) {
|
||||
printf("JD8_NewSurface: out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
return surface;
|
||||
return new Uint8[64000]{};
|
||||
}
|
||||
|
||||
// Helper intern: deriva el basename d'una ruta per a buscar al Cache.
|
||||
@@ -85,31 +83,31 @@ JD8_Surface JD8_LoadSurface(const char* file) {
|
||||
}
|
||||
|
||||
JD8_Palette JD8_LoadPalette(const char* file) {
|
||||
// Sempre retorna un buffer de 256 colors reservat amb `new Color[256]`
|
||||
// — el caller és responsable d'alliberar-lo amb `delete[]` (o lliurar-ne
|
||||
// l'ownership a `JD8_SetScreenPalette`).
|
||||
JD8_Palette palette = new Color[256];
|
||||
|
||||
if (Resource::Cache::get() != nullptr) {
|
||||
try {
|
||||
const auto& cached = Resource::Cache::get()->getPaletteBytes(jd8_basename(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 = static_cast<JD8_Palette>(malloc(768));
|
||||
if (palette == nullptr) {
|
||||
printf("JD8_LoadPalette: out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
memcpy(palette, cached.data(), 768);
|
||||
return palette;
|
||||
} catch (const std::exception&) {
|
||||
// No està al cache.
|
||||
// No està al cache — fallback a lectura + LoadPalette.
|
||||
}
|
||||
}
|
||||
|
||||
auto buffer = ResourceHelper::loadFile(file);
|
||||
return reinterpret_cast<JD8_Palette>(LoadPalette(buffer.data()));
|
||||
Uint8* raw = LoadPalette(buffer.data()); // external malloc
|
||||
memcpy(palette, raw, 768);
|
||||
free(raw);
|
||||
return palette;
|
||||
}
|
||||
|
||||
void JD8_SetScreenPalette(JD8_Palette palette) {
|
||||
if (main_palette == palette) return;
|
||||
if (main_palette != nullptr) free(main_palette);
|
||||
delete[] main_palette;
|
||||
main_palette = palette;
|
||||
}
|
||||
|
||||
@@ -225,7 +223,7 @@ Uint32* JD8_GetFramebuffer() {
|
||||
}
|
||||
|
||||
void JD8_FreeSurface(JD8_Surface surface) {
|
||||
free(surface);
|
||||
delete[] surface;
|
||||
}
|
||||
|
||||
Uint8 JD8_GetPixel(JD8_Surface surface, int x, int y) {
|
||||
|
||||
Reference in New Issue
Block a user