refactor: JD8_* a namespace Jd8::

This commit is contained in:
2026-05-16 14:24:22 +02:00
parent 1ce0d9c56c
commit f37308a5f0
41 changed files with 394 additions and 396 deletions
+39 -39
View File
@@ -21,17 +21,17 @@
#pragma GCC diagnostic pop
#endif
JD8_Surface screen = nullptr;
JD8_Palette main_palette = nullptr;
Jd8::Surface screen = nullptr;
Jd8::Palette main_palette = nullptr;
Uint32* pixel_data = nullptr;
void JD8_Init() {
void Jd8::init() {
screen = new Uint8[64000]{};
main_palette = new Color[256]{};
pixel_data = new Uint32[std::size_t{320} * 200]{};
}
void JD8_Quit() {
void Jd8::quit() {
delete[] screen;
delete[] main_palette;
delete[] pixel_data;
@@ -40,30 +40,30 @@ void JD8_Quit() {
pixel_data = nullptr;
}
void JD8_ClearScreen(Uint8 color) {
void Jd8::clearScreen(Uint8 color) {
memset(screen, color, 64000);
}
auto JD8_NewSurface() -> JD8_Surface {
auto Jd8::newSurface() -> Jd8::Surface {
return new Uint8[64000]{};
}
// Helper intern: deriva el basename d'una ruta per a buscar al Cache.
static auto jd8_basename(const char* file) -> std::string {
static auto pathBasename(const char* file) -> std::string {
std::string s = file;
auto pos = s.find_last_of("/\\");
return pos == std::string::npos ? s : s.substr(pos + 1);
}
auto JD8_LoadSurface(const char* file) -> JD8_Surface {
auto Jd8::loadSurface(const char* file) -> Jd8::Surface {
// Prova primer el Resource::Cache. Si l'asset és precarregat, copiem
// els 64KB des del cache (microsegons) i ens estalviem la decodificació
// GIF. Mantenim el contracte de la funció: el caller rep un buffer
// fresc que ha d'alliberar amb JD8_FreeSurface.
// fresc que ha d'alliberar amb Jd8::freeSurface.
if (Resource::Cache::get() != nullptr) {
try {
const auto& cached = Resource::Cache::get()->getSurfacePixels(jd8_basename(file));
JD8_Surface image = JD8_NewSurface();
const auto& cached = Resource::Cache::get()->getSurfacePixels(pathBasename(file));
Jd8::Surface image = Jd8::newSurface();
memcpy(image, cached.data(), 64000);
return image;
} catch (const std::exception&) {
@@ -79,21 +79,21 @@ auto JD8_LoadSurface(const char* file) -> JD8_Surface {
printf("Unable to load bitmap: %s\n", SDL_GetError());
exit(1);
}
JD8_Surface image = JD8_NewSurface();
Jd8::Surface image = Jd8::newSurface();
memcpy(image, pixels, 64000);
free(pixels);
return image;
}
auto JD8_LoadPalette(const char* file) -> JD8_Palette {
auto Jd8::loadPalette(const char* file) -> Jd8::Palette {
// 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`).
// l'ownership a `Jd8::setScreenPalette`).
auto* palette = new Color[256];
if (Resource::Cache::get() != nullptr) {
try {
const auto& cached = Resource::Cache::get()->getPaletteBytes(jd8_basename(file));
const auto& cached = Resource::Cache::get()->getPaletteBytes(pathBasename(file));
memcpy(palette, cached.data(), 768);
return palette;
} catch (const std::exception&) {
@@ -108,7 +108,7 @@ auto JD8_LoadPalette(const char* file) -> JD8_Palette {
return palette;
}
void JD8_SetScreenPalette(JD8_Palette palette) {
void Jd8::setScreenPalette(Jd8::Palette palette) {
if (main_palette == palette) {
return;
}
@@ -116,13 +116,13 @@ void JD8_SetScreenPalette(JD8_Palette palette) {
main_palette = palette;
}
void JD8_FillSquare(int ini, int height, Uint8 color) {
void Jd8::fillSquare(int ini, int height, Uint8 color) {
const int offset = ini * 320;
const int size = height * 320;
memset(&screen[offset], color, size);
}
void JD8_FillRect(int x, int y, int w, int h, Uint8 color) {
void Jd8::fillRect(int x, int y, int w, int h, Uint8 color) {
if (x < 0) {
w += x;
x = 0;
@@ -145,11 +145,11 @@ void JD8_FillRect(int x, int y, int w, int h, Uint8 color) {
}
}
void JD8_Blit(const Uint8* surface) {
void Jd8::blit(const Uint8* surface) {
memcpy(screen, surface, 64000);
}
void JD8_Blit(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh) {
void Jd8::blit(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh) {
int src_pointer = sx + (sy * 320);
int dst_pointer = x + (y * 320);
for (int i = 0; i < sh; i++) {
@@ -159,7 +159,7 @@ void JD8_Blit(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh
}
}
void JD8_BlitToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, JD8_Surface dest) {
void Jd8::blitToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Jd8::Surface dest) {
int src_pointer = sx + (sy * 320);
int dst_pointer = x + (y * 320);
for (int i = 0; i < sh; i++) {
@@ -169,7 +169,7 @@ void JD8_BlitToSurface(int x, int y, const Uint8* surface, int sx, int sy, int s
}
}
void JD8_BlitCK(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey) {
void Jd8::blitCK(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey) {
int src_pointer = sx + (sy * 320);
int dst_pointer = x + (y * 320);
for (int j = 0; j < sh; j++) {
@@ -183,7 +183,7 @@ void JD8_BlitCK(int x, int y, const Uint8* surface, int sx, int sy, int sw, int
}
}
void JD8_BlitCKCut(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey) {
void Jd8::blitCKCut(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey) {
int src_pointer = sx + (sy * 320);
int dst_pointer = x + (y * 320);
for (int j = 0; j < sh; j++) {
@@ -197,7 +197,7 @@ void JD8_BlitCKCut(int x, int y, const Uint8* surface, int sx, int sy, int sw, i
}
}
void JD8_BlitCKScroll(int y, const Uint8* surface, int sx, int sy, int sh, Uint8 colorkey) {
void Jd8::blitCKScroll(int y, const Uint8* surface, int sx, int sy, int sh, Uint8 colorkey) {
int dst_pointer = y * 320;
for (int j = sy; j < sy + sh; j++) {
for (int i = 0; i < 320; i++) {
@@ -210,7 +210,7 @@ void JD8_BlitCKScroll(int y, const Uint8* surface, int sx, int sy, int sh, Uint8
}
}
void JD8_BlitCKToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, JD8_Surface dest, Uint8 colorkey) {
void Jd8::blitCKToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Jd8::Surface dest, Uint8 colorkey) {
int src_pointer = sx + (sy * 320);
int dst_pointer = x + (y * 320);
for (int j = 0; j < sh; j++) {
@@ -224,7 +224,7 @@ void JD8_BlitCKToSurface(int x, int y, const Uint8* surface, int sx, int sy, int
}
}
void JD8_Flip() {
void Jd8::flip() {
// Converteix el framebuffer indexat (paletted) a ARGB (pixel_data).
// El Director crida aquesta funció després del tick de cada escena
// per preparar el frame abans de presentar-lo. Ja no fa yield —
@@ -237,23 +237,23 @@ void JD8_Flip() {
}
}
auto JD8_GetFramebuffer() -> Uint32* {
auto Jd8::getFramebuffer() -> Uint32* {
return pixel_data;
}
void JD8_FreeSurface(JD8_Surface surface) { // NOLINT(readability-non-const-parameter): allibera memòria, no pot ser const
void Jd8::freeSurface(Jd8::Surface surface) { // NOLINT(readability-non-const-parameter): allibera memòria, no pot ser const
delete[] surface;
}
auto JD8_GetPixel(const Uint8* surface, int x, int y) -> Uint8 {
auto Jd8::getPixel(const Uint8* surface, int x, int y) -> Uint8 {
return surface[x + (y * 320)];
}
void JD8_PutPixel(JD8_Surface surface, int x, int y, Uint8 pixel) {
void Jd8::putPixel(Jd8::Surface surface, int x, int y, Uint8 pixel) {
surface[x + (y * 320)] = pixel;
}
void JD8_SetPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b) {
void Jd8::setPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b) {
main_palette[index].r = r << 2;
main_palette[index].g = g << 2;
main_palette[index].b = b << 2;
@@ -300,22 +300,22 @@ namespace {
} // namespace
void JD8_FadeStartOut() {
void Jd8::fadeStartOut() {
fade_type = FadeType::Out;
fade_step = 0;
}
void JD8_FadeStartToPal(const Color* pal) {
void Jd8::fadeStartToPal(const Color* pal) {
fade_type = FadeType::ToPal;
memcpy(fade_target, pal, sizeof(Color) * 256);
fade_step = 0;
}
auto JD8_FadeIsActive() -> bool {
auto Jd8::fadeIsActive() -> bool {
return fade_type != FadeType::None;
}
auto JD8_FadeTickStep() -> bool {
auto Jd8::fadeTickStep() -> bool {
if (fade_type == FadeType::None) {
return true;
}
@@ -331,8 +331,8 @@ auto JD8_FadeTickStep() -> bool {
}
// Els shims bloquejants `JD8_FadeOut` i `JD8_FadeToPal` han estat
// eliminats a Phase B.2: feien un bucle de 32 iteracions amb `JD8_Flip`
// eliminats a Phase B.2: feien un bucle de 32 iteracions amb `Jd8::flip`
// entre cada una que només funcionava mentre l'entorn tenia fibers i
// `JD8_Flip` cedia el control al Director. Ara tot fade es fa tick a
// tick via `scenes::PaletteFade` (que encapsula `JD8_FadeStartOut` /
// `JD8_FadeStartToPal` + `JD8_FadeTickStep`).
// `Jd8::flip` cedia el control al Director. Ara tot fade es fa tick a
// tick via `scenes::PaletteFade` (que encapsula `Jd8::fadeStartOut` /
// `Jd8::fadeStartToPal` + `Jd8::fadeTickStep`).
+78 -80
View File
@@ -1,80 +1,78 @@
#pragma once
#include <SDL3/SDL.h>
struct Color {
Uint8 r;
Uint8 g;
Uint8 b;
};
using JD8_Surface = Uint8*;
using JD8_Palette = Color*;
void JD8_Init();
void JD8_Quit();
void JD8_ClearScreen(Uint8 color);
auto JD8_NewSurface() -> JD8_Surface;
auto JD8_LoadSurface(const char* file) -> JD8_Surface;
auto JD8_LoadPalette(const char* file) -> JD8_Palette;
void JD8_SetScreenPalette(JD8_Palette palette);
void JD8_FillSquare(int ini, int height, Uint8 color);
// Omple un rectangle arbitrari de la pantalla amb un color paletat.
// Pensat per a UI senzilla (barra de progrés del BootLoader, etc.).
void JD8_FillRect(int x, int y, int w, int h, Uint8 color);
void JD8_Blit(const Uint8* surface);
void JD8_Blit(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh);
void JD8_BlitToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, JD8_Surface dest);
void JD8_BlitCK(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey);
void JD8_BlitCKCut(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey);
void JD8_BlitCKScroll(int y, const Uint8* surface, int sx, int sy, int sh, Uint8 colorkey);
void JD8_BlitCKToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, JD8_Surface dest, Uint8 colorkey);
// Converteix la pantalla indexada a ARGB. El Director crida aquesta
// funció al final de cada tick i després llegeix el framebuffer via
// JD8_GetFramebuffer() per presentar-lo.
void JD8_Flip();
// Accés al framebuffer ARGB de 320x200 actualitzat per l'última crida a
// JD8_Flip(). Propietat de jdraw8 — el caller no ha de lliberar-lo.
auto JD8_GetFramebuffer() -> Uint32*;
void JD8_FreeSurface(JD8_Surface surface);
auto JD8_GetPixel(const Uint8* surface, int x, int y) -> Uint8;
void JD8_PutPixel(JD8_Surface surface, int x, int y, Uint8 pixel);
void JD8_SetPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b);
// API de fade no bloquejant (màquina d'estats). `FadeStart*` inicia el
// fade; `FadeTickStep` aplica un pas i retorna `true` quan el fade ha
// acabat. Un pas correspon visualment a una iteració del fade original
// (32 passos en total). El caller és responsable de fer el Flip entre
// passos si el vol veure animat. `FadeIsActive` permet saber si hi ha
// un fade en curs per a enllaçar-lo amb un altre subsistema.
// L'embolcall `scenes::PaletteFade` ho fa més idiomàtic per a escenes.
void JD8_FadeStartOut();
void JD8_FadeStartToPal(const Color* pal);
auto JD8_FadeTickStep() -> bool;
auto JD8_FadeIsActive() -> bool;
// JD_Font JD_LoadFont( char *file, int width, int height);
// void JD_DrawText( int x, int y, JD_Font *source, char *text);
// char *JD_GetFPS();
#pragma once
#include <SDL3/SDL.h>
struct Color {
Uint8 r;
Uint8 g;
Uint8 b;
};
namespace Jd8 {
using Surface = Uint8*;
using Palette = Color*;
void init();
void quit();
void clearScreen(Uint8 color);
auto newSurface() -> Surface;
auto loadSurface(const char* file) -> Surface;
auto loadPalette(const char* file) -> Palette;
void setScreenPalette(Palette palette);
void fillSquare(int ini, int height, Uint8 color);
// Omple un rectangle arbitrari de la pantalla amb un color paletat.
// Pensat per a UI senzilla (barra de progrés del BootLoader, etc.).
void fillRect(int x, int y, int w, int h, Uint8 color);
void blit(const Uint8* surface);
void blit(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh);
void blitToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Surface dest);
void blitCK(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey);
void blitCKCut(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Uint8 colorkey);
void blitCKScroll(int y, const Uint8* surface, int sx, int sy, int sh, Uint8 colorkey);
void blitCKToSurface(int x, int y, const Uint8* surface, int sx, int sy, int sw, int sh, Surface dest, Uint8 colorkey);
// Converteix la pantalla indexada a ARGB. El Director crida aquesta
// funció al final de cada tick i després llegeix el framebuffer via
// getFramebuffer() per presentar-lo.
void flip();
// Accés al framebuffer ARGB de 320x200 actualitzat per l'última crida a
// flip(). Propietat de jdraw8 — el caller no ha de lliberar-lo.
auto getFramebuffer() -> Uint32*;
void freeSurface(Surface surface);
auto getPixel(const Uint8* surface, int x, int y) -> Uint8;
void putPixel(Surface surface, int x, int y, Uint8 pixel);
void setPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b);
// API de fade no bloquejant (màquina d'estats). `fadeStart*` inicia el
// fade; `fadeTickStep` aplica un pas i retorna `true` quan el fade ha
// acabat. Un pas correspon visualment a una iteració del fade original
// (32 passos en total). El caller és responsable de fer el Flip entre
// passos si el vol veure animat. `fadeIsActive` permet saber si hi ha
// un fade en curs per a enllaçar-lo amb un altre subsistema.
// L'embolcall `scenes::PaletteFade` ho fa més idiomàtic per a escenes.
void fadeStartOut();
void fadeStartToPal(const Color* pal);
auto fadeTickStep() -> bool;
auto fadeIsActive() -> bool;
} // namespace Jd8