fase 2: fades de jd8 a màquina d'estats i helper wait_frame_or_skip a les cinemàtiques

This commit is contained in:
2026-04-15 18:12:03 +02:00
parent 7f85b50c63
commit 80fa7b46e7
3 changed files with 1414 additions and 1341 deletions

View File

@@ -186,44 +186,89 @@ void JD8_SetPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b) {
main_palette[index].b = b << 2;
}
void JD8_FadeOut() {
for (int j = 0; j < 32; j++) {
// Màquina d'estats del fade. Evita que JD8_FadeOut/JD8_FadeToPal hagen de
// mantindre whiles interns. Cada pas aplica un delta a la paleta activa i
// el caller decideix quan fer Flip.
namespace {
enum FadeType {
FADE_NONE = 0,
FADE_OUT,
FADE_TO_PAL,
};
constexpr int FADE_STEPS = 32;
FadeType fade_type = FADE_NONE;
Color fade_target[256];
int fade_step = 0;
void apply_fade_step() {
if (fade_type == FADE_OUT) {
for (int i = 0; i < 256; i++) {
if (main_palette[i].r >= 8)
main_palette[i].r -= 8;
else
main_palette[i].r = 0;
if (main_palette[i].g >= 8)
main_palette[i].g -= 8;
else
main_palette[i].g = 0;
if (main_palette[i].b >= 8)
main_palette[i].b -= 8;
else
main_palette[i].b = 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].b = main_palette[i].b >= 8 ? main_palette[i].b - 8 : 0;
}
} else if (fade_type == FADE_TO_PAL) {
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
: fade_target[i].r;
main_palette[i].g = main_palette[i].g <= int(fade_target[i].g) - 8
? main_palette[i].g + 8
: fade_target[i].g;
main_palette[i].b = main_palette[i].b <= int(fade_target[i].b) - 8
? main_palette[i].b + 8
: fade_target[i].b;
}
JD8_Flip();
}
}
#define MAX(a, b) (a) > (b) ? (a) : (b)
} // namespace
void JD8_FadeStartOut() {
fade_type = FADE_OUT;
fade_step = 0;
}
void JD8_FadeStartToPal(JD8_Palette pal) {
fade_type = FADE_TO_PAL;
memcpy(fade_target, pal, sizeof(Color) * 256);
fade_step = 0;
}
bool JD8_FadeIsActive() {
return fade_type != FADE_NONE;
}
bool JD8_FadeTickStep() {
if (fade_type == FADE_NONE) return true;
apply_fade_step();
fade_step++;
if (fade_step >= FADE_STEPS) {
fade_type = FADE_NONE;
return true;
}
return false;
}
void JD8_FadeOut() {
JD8_FadeStartOut();
while (true) {
const bool done = JD8_FadeTickStep();
JD8_Flip();
if (done) break;
}
}
void JD8_FadeToPal(JD8_Palette pal) {
for (int j = 0; j < 32; j++) {
for (int i = 0; i < 256; i++) {
if (main_palette[i].r <= int(pal[i].r) - 8)
main_palette[i].r += 8;
else
main_palette[i].r = pal[i].r;
if (main_palette[i].g <= int(pal[i].g) - 8)
main_palette[i].g += 8;
else
main_palette[i].g = pal[i].g;
if (main_palette[i].b <= int(pal[i].b) - 8)
main_palette[i].b += 8;
else
main_palette[i].b = pal[i].b;
}
JD8_FadeStartToPal(pal);
while (true) {
const bool done = JD8_FadeTickStep();
JD8_Flip();
if (done) break;
}
}

View File

@@ -50,10 +50,21 @@ void JD8_PutPixel(JD8_Surface surface, int x, int y, Uint8 pixel);
void JD8_SetPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b);
// Fades legacy bloquejants (shim damunt la màquina d'estats de sota).
void JD8_FadeOut();
void JD8_FadeToPal(JD8_Palette pal);
// 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.
void JD8_FadeStartOut();
void JD8_FadeStartToPal(JD8_Palette pal);
bool JD8_FadeTickStep();
bool JD8_FadeIsActive();
// JD_Font JD_LoadFont( char *file, int width, int height);
// void JD_DrawText( int x, int y, JD_Font *source, char *text);

View File

@@ -17,6 +17,20 @@ ModuleSequence::ModuleSequence() {
ModuleSequence::~ModuleSequence(void) {
}
namespace {
// Espera fins que passe el tick de JG_ShouldUpdate o fins que el jugador
// polse qualsevol tecla. Torna `true` si la seqüència s'ha de cancel·lar.
// Centralitza el patró repetit de les cinemàtiques; a la Fase 5 (quan
// eliminem el game thread) passarà a ser un pas de màquina d'estats real.
bool wait_frame_or_skip() {
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) return true;
}
return false;
}
} // namespace
int ModuleSequence::Go() {
if (info::ctx.num_piramide == 6 && info::ctx.diners < 200) info::ctx.num_piramide = 7;
@@ -118,177 +132,177 @@ void ModuleSequence::doIntro() {
JD8_ClearScreen(0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JG_SetUpdateTicks(100);
JD8_Blit(43, 78, gfx, 43, 155, 27, 45);
JD8_Blit(68, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_Blit(43, 78, gfx, 43, 155, 53, 45);
JD8_Blit(96, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_Blit(43, 78, gfx, 43, 155, 66, 45);
JD8_Blit(109, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JG_SetUpdateTicks(200);
JD8_Blit(43, 78, gfx, 43, 155, 92, 45);
JD8_Blit(136, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_ClearScreen(0);
JD8_Blit(43, 78, gfx, 43, 155, 92, 45);
// JD8_Blit( 136, 78, gfx, 274, 155, 27, 45 );
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JG_SetUpdateTicks(100);
JD8_Blit(43, 78, gfx, 43, 155, 118, 45);
JD8_Blit(160, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_Blit(43, 78, gfx, 43, 155, 145, 45);
JD8_Blit(188, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_Blit(43, 78, gfx, 43, 155, 178, 45);
JD8_Blit(221, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_Blit(43, 78, gfx, 43, 155, 205, 45);
JD8_Blit(248, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JG_SetUpdateTicks(200);
drawIntroWordmark(gfx);
JD8_Blit(274, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_ClearScreen(0);
drawIntroWordmark(gfx);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
drawIntroWordmark(gfx);
JD8_Blit(274, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_ClearScreen(0);
drawIntroWordmark(gfx);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
drawIntroWordmark(gfx);
JD8_Blit(274, 78, gfx, 274, 155, 27, 45);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
JD8_ClearScreen(0);
drawIntroWordmark(gfx);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
for (int j = 0; j < 256; j++) {
@@ -306,12 +320,15 @@ void ModuleSequence::doIntro() {
JD8_Flip();
}
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
doIntroSprites(gfx);
@@ -372,12 +389,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(i, 150, gfx, fr_ani_1[(i / 5) % fr1], 0, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -390,12 +407,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(200, 150, gfx, fr_ani_3[minim((i / 5), fr3 - 1)], 30, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -408,12 +425,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(200, 150, gfx, fr_ani_3[minim((i / 5), fr3 - 1)], 30, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -426,12 +443,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(i, 150, gfx, fr_ani_2[(i / 5) % fr2], 15, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -444,12 +461,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(80, 150, gfx, fr_ani_4[minim((i / 5), fr4 - 1)], 45, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -464,12 +481,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(80, 150, gfx, fr_ani_4[fr4 - 1], 45, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -486,12 +503,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(80, 133, gfx, 0, interrogant, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -506,12 +523,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(95, 150, gfx, fr_ani_6[4], 60, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -526,12 +543,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(95, 150, gfx, fr_ani_6[4], 60, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -546,12 +563,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(95, 150, gfx, fr_ani_6[4], 60, 15, 15, 0);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -581,12 +598,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(i, 150, gfx, fr_ani_1[(i / 5) % fr1], 0, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -601,12 +618,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(200, 150, gfx, fr_ani_3[minim(i / 5, fr3 - 1)], 30, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -623,12 +640,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(200, 150, gfx, fr_ani_3[fr3 - 1], 30, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -647,12 +664,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
}
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -669,12 +686,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(200, i * 2, gfx, fr_ani_8[0], 75, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -689,12 +706,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(200, 150, gfx, fr_ani_8[i / 10], 75, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -713,12 +730,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(215, 150, gfx, fr_ani_8[3], 75, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
break;
@@ -733,12 +750,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(304 - i, 150, gfx, fr_ani_6[(i / 10) % fr6], 60, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -753,12 +770,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(160, 150, gfx, fr_ani_6[1], 60, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -775,12 +792,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(160, 150, gfx, fr_ani_6[1], 60, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}
@@ -797,12 +814,12 @@ void ModuleSequence::doIntroSprites(JD8_Surface gfx) {
JD8_BlitCK(125, 150, gfx, fr_ani_11[((i / 5) % 4) + 2], 90, 15, 15, 255);
JD8_Flip();
while (!JG_ShouldUpdate()) {
JI_Update();
if (JI_AnyKey() || JG_Quitting()) {
JD8_FreeSurface(gfx);
return;
}
if (wait_frame_or_skip()) {
JD8_FreeSurface(gfx);
return;
}
}