Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Sonic: the SEGA and title screens, demos, pause menu and saves
Mason3630bf461c83bb7
diff --git a/games/sonic/src/game.c b/games/sonic/src/game.cindex 3bc4eaa8..a122a312 100644--- a/games/sonic/src/game.c+++ b/games/sonic/src/game.c@@ -179,6 +179,7 @@ void GM_Level(void) {AddPLC(plcid_GHZAnimals + V.zone);}V.gamemode &= 0x7F;+ if (!V.f_demo) save_progress(); /* (each act kept as it starts) */for (;;) {PauseGame();V.vblank_routine = id_VBlank_Levels;diff --git a/games/sonic/src/game.h b/games/sonic/src/game.hindex 31cd22c9..39c1e06b 100644--- a/games/sonic/src/game.h+++ b/games/sonic/src/game.h@@ -21,6 +21,10 @@ void game_save_on_quit(void);void save_progress(void);bool save_load_progress(void);bool save_exists(void);+void save_clear_progress(void);+void save_peek(int *zone, int *act, int *lives);+bool save_seen_keys(void);+void save_set_seen_keys(void);/* Sonic (sonic.c, react.c) */void SonicPlayer(Obj *o);diff --git a/games/sonic/src/level.c b/games/sonic/src/level.cindex 6f641551..02bddff8 100644--- a/games/sonic/src/level.c+++ b/games/sonic/src/level.c@@ -160,10 +160,15 @@ static const struct {int level_plc1(void) { return headers[V.zone].plc1; }int level_nchunks(void) { return nchunks; }-void LevelDataLoad(void) {+/* the zone's blocks and chunks */+void level_blocks_load(void) {blocks16 = DATAW(headers[V.zone].blocks);chunks256 = DATAW(headers[V.zone].chunks);nchunks = headers[V.zone].nchunks;+}++void LevelDataLoad(void) {+ level_blocks_load();LevelLayoutLoad();int pal = headers[V.zone].pal;if (V.zone == id_LZ && V.act == 3) pal = palid_SBZ3;@@ -320,6 +325,9 @@ static void draw_chunks(uint16_t (*pl)[64], int plane, const Cam *cam) {for (int i = 0; i < 32; i++) draw_at(pl, plane, cam, -16 + r * 16, i * 16);}+/* the title's background: the whole of plane B from the background camera (DrawChunks) */+void DrawChunks_BG(void) { draw_chunks(plane_b, 1, &V.cam[1]); }+static void bg_rows_by_map(const uint8_t *map, int ybase, int ymask, int from) {for (int r = 0; r < 16; r++) {int dy = from + r * 16;diff --git a/games/sonic/src/menus.c b/games/sonic/src/menus.cnew file mode 100644index 00000000..d60c47d6--- /dev/null+++ b/games/sonic/src/menus.c@@ -0,0 +1,119 @@+/* The screens that are the calculator's, not the original's: the keys the first time, Continue or+ * New game on the title when a game was left, and the pause menu (Back pauses as Start did, with+ * Resume and Quit game, which asks first; quitting keeps the game, as Home does). */+#include "s1.h"+#include "game.h"+#include "ui.h"++void keys_screen(void) {+ ui_clear();+ ui_box(0, 0, 320, 224, UI_BLUE);+ ui_text_centred(10, UI_YELLOW, 2, "SONIC THE HEDGEHOG");+ ui_text_centred(34, UI_WHITE, 1, "HOW TO PLAY");+ ui_text(16, 54, UI_YELLOW, 1, "ARROWS");+ ui_text(136, 54, UI_WHITE, 1, "RUN, LOOK UP, DUCK");+ ui_text(16, 66, UI_YELLOW, 1, "DOWN, RUNNING");+ ui_text(136, 66, UI_WHITE, 1, "ROLL");+ ui_text(16, 78, UI_YELLOW, 1, "OK, EXE, 5, 0");+ ui_text(136, 78, UI_WHITE, 1, "JUMP");+ ui_text(16, 90, UI_YELLOW, 1, "BACK");+ ui_text(136, 90, UI_WHITE, 1, "START, PAUSE");+ ui_text(16, 102, UI_YELLOW, 1, "HOME");+ ui_text(136, 102, UI_WHITE, 1, "SAVE AND QUIT");+ ui_text(16, 124, UI_WHITE, 1, "COLLECT RINGS: HIT WITH RINGS, YOU");+ ui_text(16, 136, UI_WHITE, 1, "ONLY LOSE THEM. 100 RINGS: 1 UP.");+ ui_text(16, 148, UI_WHITE, 1, "END AN ACT WITH 50 RINGS AND JUMP");+ ui_text(16, 160, UI_WHITE, 1, "INTO THE GIANT RING: SPECIAL STAGE.");+ ui_text(16, 172, UI_WHITE, 1, "FIND THE 6 CHAOS EMERALDS!");+ ui_text_centred(198, UI_YELLOW, 1, "PRESS OK");+ vdp_display = false;+ do {+ V.vblank_routine = id_VBlank_Sega;+ WaitForVBlank();+ } while (!(V.jpadpress1 & (btnABC | btnStart)));+ ui_clear();+ save_set_seen_keys();+}++static const char *const zone_names[7] = {"GREEN HILL", "LABYRINTH", "MARBLE", "STAR LIGHT",+ "SPRING YARD", "SCRAP BRAIN", "FINAL"};++/* the title's menu, drawn; sel: the line chosen */+static char cont_line[40];+void continue_menu_draw(int sel) {+ int z, a, l;+ save_peek(&z, &a, &l);+ if (z > 6) z = 6;+ /* (Scrap Brain's third act is in Labyrinth Zone) */+ const char *name = z == 1 && a == 3 ? "SCRAP BRAIN" : zone_names[z];+ int act = z == 1 && a == 3 ? 3 : a + 1;+ char *p = cont_line;+ for (const char *s = name; *s;) *p++ = *s++;+ for (const char *s = " ZONE"; *s;) *p++ = *s++;+ if (z != 6) {+ for (const char *s = " ACT "; *s;) *p++ = *s++;+ *p++ = (char)('0' + act);+ }+ *p = 0;+ ui_clear();+ ui_box(40, 150, 240, 64, UI_DARK);+ ui_text(56, 158, sel == 0 ? UI_YELLOW : UI_WHITE, 1, sel == 0 ? "> CONTINUE" : " CONTINUE");+ ui_text(72, 170, sel == 0 ? UI_YELLOW : UI_WHITE, 1, cont_line);+ ui_text(56, 190, sel == 1 ? UI_YELLOW : UI_WHITE, 1, sel == 1 ? "> NEW GAME" : " NEW GAME");+}++/* ------------------------------------------------------------------ pause */+static void pause_draw(int sel, bool confirm, int csel) {+ ui_clear();+ if (!confirm) {+ ui_box(96, 72, 128, 76, UI_DARK);+ ui_text_centred(82, UI_WHITE, 2, "PAUSE");+ ui_text(120, 110, sel == 0 ? UI_YELLOW : UI_WHITE, 1, sel == 0 ? "> RESUME" : " RESUME");+ ui_text(120, 126, sel == 1 ? UI_YELLOW : UI_WHITE, 1, sel == 1 ? "> QUIT GAME" : " QUIT GAME");+ } else {+ ui_box(64, 72, 192, 76, UI_DARK);+ ui_text_centred(84, UI_WHITE, 1, "QUIT THE GAME?");+ ui_text_centred(98, UI_WHITE, 1, "IT IS SAVED AT THIS ACT.");+ ui_text(104, 124, csel == 0 ? UI_YELLOW : UI_WHITE, 1, csel == 0 ? "> YES" : " YES");+ ui_text(176, 124, csel == 1 ? UI_YELLOW : UI_WHITE, 1, csel == 1 ? "> NO" : " NO");+ }+}++void PauseGame(void) {+ if (!V.lives) {+ V.f_pause = 0;+ return;+ }+ if (!V.f_pause && !(V.jpadpress1 & btnStart)) return;+ V.f_pause = 1;+ int sel = 0, csel = 1;+ bool confirm = false;+ for (;;) {+ pause_draw(sel, confirm, csel);+ V.vblank_routine = id_VBlank_Paused;+ WaitForVBlank();+ uint8_t p = V.jpadpress1;+ if (!confirm) {+ if (p & btnStart) break;+ if (p & (btnUp | btnDn)) sel ^= 1;+ if (p & btnABC) {+ if (sel == 0) break;+ confirm = true, csel = 1;+ }+ } else {+ if (p & btnStart) confirm = false;+ if (p & (btnL | btnR | btnUp | btnDn)) csel ^= 1;+ if (p & btnABC) {+ if (csel == 0) {+ ui_clear();+ game_quit();+ }+ confirm = false;+ }+ }+ }+ ui_clear();+ V.f_pause = 0;+}++void pause_overlay_draw(void) {}diff --git a/games/sonic/src/obj.c b/games/sonic/src/obj.cindex d8c96ac4..866956ac 100644--- a/games/sonic/src/obj.c+++ b/games/sonic/src/obj.c@@ -132,6 +132,8 @@ void BuildSprites(void) {for (int k = 0; k < q->n; k++) {Obj *o = q->o[k];if (!o->id) continue;+ /* (no mappings: the title's PRESS START BUTTON, never set up there in the original, nor here) */+ if (!o->map) continue;o->render &= 0x7F;uint8_t r = o->render;int16_t x, y;diff --git a/games/sonic/src/obj_cards.c b/games/sonic/src/obj_cards.cindex fd852150..e646b60c 100644--- a/games/sonic/src/obj_cards.c+++ b/games/sonic/src/obj_cards.c@@ -122,7 +122,7 @@ void GameOverCard(Obj *o) {V.f_restart = 1;} else {V.gamemode = id_Continue;- if (!V.continues) V.gamemode = id_Sega;+ if (!V.continues) V.gamemode = id_Sega, save_clear_progress();}DisplaySprite(o);}diff --git a/games/sonic/src/objects.c b/games/sonic/src/objects.cindex fc65686f..0cccb8ac 100644--- a/games/sonic/src/objects.c+++ b/games/sonic/src/objects.c@@ -48,4 +48,7 @@ const ObjFn obj_index[0x8D + 1] = {[id_Newtron] = Newtron,[id_BossGreenHill] = BossGreenHill,[id_BossBall] = BossBall,+ [id_TitleSonic] = TitleSonic,+ [id_PSBTM] = PSBTM,+ [id_CreditsText] = CreditsText,};diff --git a/games/sonic/src/objs.h b/games/sonic/src/objs.hindex 20c211a4..4808d5b5 100644--- a/games/sonic/src/objs.h+++ b/games/sonic/src/objs.h@@ -122,4 +122,9 @@ void Newtron(Obj *o);void BossGreenHill(Obj *o);void BossBall(Obj *o);void boss_hit_flash(Obj *o, uint8_t restore);++/* screens.c */+void TitleSonic(Obj *o);+void PSBTM(Obj *o);+void CreditsText(Obj *o);#endifdiff --git a/games/sonic/src/s1.h b/games/sonic/src/s1.hindex c248e5d2..0d9ce26d 100644--- a/games/sonic/src/s1.h+++ b/games/sonic/src/s1.h@@ -277,7 +277,6 @@ void FadeOut_ToBlack(void);void WhiteIn_FromWhite(void);void WhiteOut_ToWhite(void);void PaletteCycle(void);-void PalCycle_Sega(void);void PalCycle_Title(void);enum {palid_SegaBG, palid_Title, palid_LevelSel, palid_Sonic, palid_GHZ, palid_LZ, palid_MZ, palid_SLZ, palid_SYZ,@@ -289,6 +288,8 @@ enum {void LevelSizeLoad(void);void LevelDataLoad(void);void LevelLayoutLoad(void);+void level_blocks_load(void);+void DrawChunks_BG(void);void LoadTilesFromStart(void);void LoadTilesAsYouMove(void);void LoadTilesAsYouMove_BGOnly(void);diff --git a/games/sonic/src/save.c b/games/sonic/src/save.cnew file mode 100644index 00000000..7a5fa73c--- /dev/null+++ b/games/sonic/src/save.c@@ -0,0 +1,170 @@+/* Saves: where the game was left (the act, lives, continues, emeralds, score; Home, On/Off and Quit+ * game keep it, and so does each act as it starts), and whether the keys screen was seen, in+ * sonic.sav; and a copy in a Python script, sonic_saves.py: installing apps from the NumWorks+ * website keeps only Python scripts, so the save comes back from it when the file itself is gone.+ * The copy is a comment line "#>sonic.sav:base64" (the way NumPlay and its big games keep theirs). */+#include "s1.h"+#include "game.h"+#include "plat.h"++#define SAVE_NAME "sonic.sav"+#define COPY_NAME "sonic_saves.py"+#define MAGIC 0x56533153u /* "S1SV" */++typedef struct {+ uint32_t magic;+ uint8_t version, seen_keys, has_game, zone, act, lives, continues, emeralds;+ uint8_t emldlist[6], lastspecial, pad;+ uint32_t score, scorelife;+ uint32_t frames; /* time played */+} SaveFile;++static SaveFile sv;+static bool loaded;+static uint32_t counted; /* the frames already added to sv.frames */++static const char copy_head[] =+ "# Sonic the Hedgehog keeps a copy of your game here, so that\n"+ "# reinstalling it doesn't erase it. If you delete this file,\n"+ "# the game writes it again: your game stays either way.\n";+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";++static void copy_write(const uint8_t *d, uint32_t len) {+ uint32_t total = 1 + (sizeof copy_head - 1) + 2 + (uint32_t)strlen(SAVE_NAME) + 1 + (len + 2) / 3 * 4 + 1 + 1;+ plat_remove(COPY_NAME);+ uint8_t *o = plat_reserve(COPY_NAME, total), *p = o;+ if (!o) return;+ *p++ = 0; /* (the script's status byte: not imported) */+ memcpy(p, copy_head, sizeof copy_head - 1), p += sizeof copy_head - 1;+ *p++ = '#', *p++ = '>';+ memcpy(p, SAVE_NAME, strlen(SAVE_NAME)), p += strlen(SAVE_NAME);+ *p++ = ':';+ for (uint32_t i = 0; i < len; i += 3, p += 4) {+ /* (written out: GCC 15.2 for the calculator got a loop over the four wrong, see NumPlay's+ * launcher/src/storage.c) */+ uint32_t left = len - i, v = (uint32_t)d[i] << 16 | (left > 1 ? d[i + 1] << 8 : 0) | (left > 2 ? d[i + 2] : 0);+ p[0] = (uint8_t)b64[v >> 18 & 63];+ p[1] = (uint8_t)b64[v >> 12 & 63];+ p[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';+ p[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';+ }+ *p++ = '\n';+ *p++ = 0;+ plat_reserved(COPY_NAME, (uint32_t)(p - o));+}++static int b64_value(uint8_t c) {+ for (int i = 0; i < 64; i++)+ if ((uint8_t)b64[i] == c) return i;+ return -1;+}++/* the save back from the script, when the file is gone */+void save_restore_copy(void) {+ uint32_t n, have;+ if (plat_load(SAVE_NAME, &have)) return;+ const uint8_t *c = plat_load(COPY_NAME, &n);+ if (!c) return;+ static const char tag[] = "#>" SAVE_NAME ":";+ for (uint32_t i = 0; i + sizeof tag - 1 <= n; i++) {+ if (memcmp(c + i, tag, sizeof tag - 1)) continue;+ const uint8_t *l = c + i + sizeof tag - 1;+ uint32_t m = 0;+ while (l + m < c + n && l[m] != '\n' && l[m]) m++;+ if (!m || m % 4 || m / 4 * 3 < sizeof(SaveFile)) return;+ uint8_t out[sizeof(SaveFile) + 3];+ uint32_t o = 0;+ for (uint32_t k = 0; k < m && o < sizeof out; k += 4) {+ int v0 = l[k] == '=' ? 0 : b64_value(l[k]), v1 = l[k + 1] == '=' ? 0 : b64_value(l[k + 1]);+ int v2 = l[k + 2] == '=' ? 0 : b64_value(l[k + 2]), v3 = l[k + 3] == '=' ? 0 : b64_value(l[k + 3]);+ if (v0 < 0 || v1 < 0 || v2 < 0 || v3 < 0) return;+ uint32_t wd = (uint32_t)(v0 << 18 | v1 << 12 | v2 << 6 | v3);+ if (o < sizeof out) out[o++] = (uint8_t)(wd >> 16);+ if (o < sizeof out) out[o++] = (uint8_t)(wd >> 8);+ if (o < sizeof out) out[o++] = (uint8_t)wd;+ }+ SaveFile f;+ memcpy(&f, out, sizeof f);+ if (f.magic == MAGIC) plat_save(SAVE_NAME, &f, sizeof f);+ return;+ }+}++static void load(void) {+ if (loaded) return;+ loaded = true;+ uint32_t len;+ const uint8_t *d = plat_load(SAVE_NAME, &len);+ memset(&sv, 0, sizeof sv);+ if (d && len == sizeof sv) memcpy(&sv, d, sizeof sv);+ if (sv.magic != MAGIC) memset(&sv, 0, sizeof sv);+ sv.magic = MAGIC, sv.version = 1;+}++static void write(void) {+ plat_save(SAVE_NAME, &sv, sizeof sv);+ copy_write((const uint8_t *)&sv, sizeof sv);+}++bool save_seen_keys(void) {+ load();+ return sv.seen_keys;+}+void save_set_seen_keys(void) {+ load();+ sv.seen_keys = 1;+ write();+}++bool save_exists(void) {+ load();+ return sv.has_game;+}+void save_peek(int *zone, int *act, int *lives) {+ load();+ *zone = sv.zone, *act = sv.act, *lives = sv.lives;+}++/* the game as it is: in a level (or its special stage), the act it is at */+void save_progress(void) {+ load();+ if (V.f_demo || (V.gamemode != id_Level && V.gamemode != id_Special && V.gamemode != id_Continue)) return;+ if (V.gamemode == id_Level && !V.lives) return;+ sv.has_game = 1;+ sv.zone = V.zone, sv.act = V.act;+ sv.lives = V.lives ? V.lives : 1;+ sv.continues = V.continues;+ if (V.gamemode == id_Continue) sv.lives = 3, sv.continues = V.continues ? V.continues - 1 : 0;+ sv.emeralds = V.emeralds;+ memcpy(sv.emldlist, V.emldlist, 6);+ sv.lastspecial = V.lastspecial;+ sv.score = V.score;+ sv.scorelife = V.scorelife;+ sv.frames += V.vblank_count - counted;+ counted = V.vblank_count;+ write();+}++bool save_load_progress(void) {+ load();+ if (!sv.has_game) return false;+ V.zone = sv.zone, V.act = sv.act;+ V.lives = sv.lives;+ V.continues = sv.continues;+ V.emeralds = sv.emeralds;+ memcpy(V.emldlist, sv.emldlist, 6);+ V.lastspecial = sv.lastspecial;+ V.score = sv.score;+ V.scorelife = sv.scorelife;+ return true;+}++/* the game over, or the game finished: nothing to continue */+void save_clear_progress(void) {+ load();+ if (!sv.has_game) return;+ sv.has_game = 0;+ write();+}++void game_save_on_quit(void) { save_progress(); }diff --git a/games/sonic/src/screens.c b/games/sonic/src/screens.cnew file mode 100644index 00000000..351293db--- /dev/null+++ b/games/sonic/src/screens.c@@ -0,0 +1,326 @@+/* The SEGA screen (its light sweeping across the logo), and the title screen: SONIC TEAM PRESENTS,+ * then Sonic rising out of the emblem over Green Hill's scrolling background, PRESS START BUTTON;+ * left alone it plays a demo of Green Hill, Marble, Spring Yard or the special stage in turn.+ * Start (or OK) plays from the beginning, or offers to continue a game left with Home. */+#include "s1.h"+#include "game.h"+#include "objs.h"+#include "ui.h"++/* ------------------------------------------------------------------ SEGA */+/* the light across the logo, then the logo fading in; false when done */+static bool PalCycle_Sega(void) {+ if (!(V.pcyc_time & 0xFF)) {+ uint16_t *a1 = &V.palette[16];+ const uint16_t *a0 = DATAW(D_PAL_SEGA1);+ int d1 = N_PAL_SEGA1 - 1;+ int16_t d0 = (int16_t)V.pcyc_num;+ while (d0 < 0) a0++, d1--, d0 = (int16_t)(d0 + 2);+ for (; d1 >= 0; d1--) {+ if (!(d0 & 0x1E)) d0 = (int16_t)(d0 + 2);+ if ((uint16_t)d0 < 0x60) a1[d0 >> 1] = *a0++;+ d0 = (int16_t)(d0 + 2);+ }+ d0 = (int16_t)(V.pcyc_num + 2);+ if (!(d0 & 0x1E)) d0 = (int16_t)(d0 + 2);+ if (d0 >= 0x60 + 4) {+ V.pcyc_time = 0x401;+ d0 = -6 * 2;+ }+ V.pcyc_num = (uint16_t)d0;+ return true;+ }+ uint8_t t = (uint8_t)(V.pcyc_time >> 8);+ if ((int8_t)--t >= 0) {+ V.pcyc_time = (uint16_t)(t << 8 | (V.pcyc_time & 0xFF));+ return true;+ }+ V.pcyc_time = (uint16_t)(4 << 8 | (V.pcyc_time & 0xFF));+ int16_t d0 = (int16_t)(V.pcyc_num + 6 * 2);+ if ((uint16_t)d0 >= 6 * 2 * 4) return false;+ V.pcyc_num = (uint16_t)d0;+ const uint16_t *a0 = DATAW(D_PAL_SEGA2) + d0 / 2;+ for (int i = 0; i < 5; i++) V.palette[2 + i] = *a0++;+ uint16_t *a1 = &V.palette[16];+ d0 = 0;+ for (int i = 0; i < 0x30 - 3; i++) {+ if (!(d0 & 0x1E)) d0 = (int16_t)(d0 + 2);+ a1[d0 >> 1] = *a0;+ d0 = (int16_t)(d0 + 2);+ }+ return true;+}++void GM_Sega(void) {+ ClearPLC();+ PaletteFadeOut();+ V.f_wtr_state = 0;+ vdp_display = false;+ ClearScreen();+ vdp_backdrop = 0;+ vdp_load_art(ART_Nem_SegaLogo, ArtTile_Sega_Tiles);+ const uint16_t *eni = DATAW(D_ENI_SEGALOGO);+ vdp_fill_plane(plane_b, 8, 10, 24, 8, eni, ArtTile_Sega_Tiles);+ vdp_fill_plane(plane_a, 0, 0, 40, 28, eni + 24 * 8, ArtTile_Sega_Tiles);+ PalLoad(palid_SegaBG);+ V.pcyc_num = (uint16_t)-0xA;+ V.pcyc_time = 0;+ V.pal_buffer[9] = V.pal_buffer[8] = 0;+ vdp_display = true;+ do {+ V.vblank_routine = id_VBlank_Sega;+ WaitForVBlank();+ } while (PalCycle_Sega());+ V.vblank_routine = id_VBlank_SegaPCM;+ WaitForVBlank();+ V.generictimer = 30;+ do {+ V.vblank_routine = id_VBlank_Sega;+ WaitForVBlank();+ } while (V.generictimer && !(V.jpadpress1 & btnStart));+ V.gamemode = id_Title;+}++/* ------------------------------------------------------------------ the title's objects */+void TitleSonic(Obj *o) {+ switch (o->routine) {+ case 0:+ o->routine += 2;+ o->x.w.i = 0x80 + 0x70;+ obScreenY(o) = 0x80 + 0x5E;+ o->map = map_get(MAP_TSon);+ o->gfx = ArtTile_Title_Sonic | Tile_Pal2;+ o->priority = 1;+ o->delayani = 30 - 1;+ AnimateSprite(o, ani_get(ANI_TSon));+ /* fall through */+ case 2:+ if ((int8_t)--o->delayani < 0) {+ o->routine += 2;+ DisplaySprite(o);+ }+ return;+ case 4:+ obScreenY(o) = (int16_t)(obScreenY(o) - 8);+ if (obScreenY(o) == 0x80 + 0x16) o->routine += 2;+ DisplaySprite(o);+ return;+ default:+ AnimateSprite(o, ani_get(ANI_TSon));+ DisplaySprite(o);+ return;+ }+}++void PSBTM(Obj *o) {+ switch (o->routine) {+ case 0:+ o->routine += 2;+ o->x.w.i = 0x80 + 0x50;+ obScreenY(o) = 0x80 + 0xB0;+ o->map = map_get(MAP_PSB);+ o->gfx = ArtTile_Title_Foreground;+ if (o->frame < 2) goto press;+ o->routine += 2;+ if (o->frame == 3) {+ o->gfx = ArtTile_Title_Trademark | Tile_Pal2;+ o->x.w.i = 0x80 + 0xF0;+ obScreenY(o) = 0x80 + 0x78;+ }+ break;+ case 2:+ press:+ AnimateSprite(o, ani_get(ANI_PSBTM));+ break;+ }+ DisplaySprite(o);+}++void CreditsText(Obj *o) {+ if (o->routine == 0) {+ o->routine += 2;+ o->x.w.i = 320 / 2 + 0x80;+ obScreenY(o) = 224 / 2 + 0x80;+ o->map = map_get(MAP_Cred);+ o->gfx = ArtTile_Credits_Font;+ o->frame = (uint8_t)V.creditsnum;+ o->render = sprite_cam_screen;+ o->priority = 0;+ if (V.gamemode == id_Title) {+ o->gfx = ArtTile_Sonic_Team_Font;+ o->frame = 0xA;+ }+ }+ DisplaySprite(o);+}++/* ------------------------------------------------------------------ the title screen */+static void new_game(void) {+ V.gamemode = id_Level;+ V.lives = 3;+ V.rings = 0;+ V.timemin = V.timesec = V.timecent = 0;+ V.score = 0;+ V.lastspecial = 0;+ V.emeralds = 0;+ memset(V.emldlist, 0, sizeof V.emldlist);+ V.continues = 0;+ V.scorelife = 5000;+}++static bool demo_tick(void) {+ player->x.w.i = (int16_t)(player->x.w.i + 2);+ if ((uint16_t)player->x.w.i < 0x1C00) return false;+ V.gamemode = id_Sega;+ return true;+}++/* Continue or New game, while the title goes on; then the game starts */+static void choose_game(void) {+ int sel = 0;+ for (;;) {+ continue_menu_draw(sel);+ V.vblank_routine = id_VBlank_Title;+ WaitForVBlank();+ ExecuteObjects();+ DeformLayers();+ BuildSprites();+ PalCycle_Title();+ uint8_t p = V.jpadpress1;+ if (p & (btnUp | btnDn)) sel ^= 1;+ if (p & (btnABC | btnStart)) break;+ }+ ui_clear();+ new_game();+ if (sel == 0) save_load_progress();+}++static void play(void) {+ if (save_exists()) choose_game();+ else new_game();+}++void GM_Title(void) {+ static const uint16_t demo_levels[4] = {id_GHZ_act1, id_MZ_act1, id_SYZ_act1, 0x600};+ ClearPLC();+ PaletteFadeOut();+ V.f_wtr_state = 0;+ ClearScreen();+ vdp_backdrop = 0x20;+ memset(objs, 0, sizeof(Obj) * NOBJ);+ vdp_load_art(ART_Nem_JapNames, ArtTile_Title_Japanese_Text);+ vdp_load_art(ART_Nem_CreditText, ArtTile_Sonic_Team_Font);+ vdp_fill_plane(plane_a, 0, 0, 40, 28, DATAW(D_ENI_JAPNAMES), ArtTile_Title_Japanese_Text);+ memset(V.palette_fading, 0, sizeof V.palette_fading);+ PalLoad_Fade(palid_Sonic);+ OBJ(2)->id = id_CreditsText;+ ExecuteObjects();+ BuildSprites();+ PaletteFadeIn();+ /* (SONIC TEAM PRESENTS stays while the original unpacks the title's pictures) */+ for (int i = 0; i < 100; i++) {+ V.vblank_routine = id_VBlank_PaletteFade;+ WaitForVBlank();+ }+ vdp_load_art(ART_Nem_TitleFg, ArtTile_Title_Foreground);+ vdp_load_art(ART_Nem_TitleSonic, ArtTile_Title_Sonic);+ vdp_load_art(ART_Nem_TitleTM, ArtTile_Title_Trademark);+ vdp_load_art(ART_Art_Text, ArtTile_Level_Select_Font);+ V.lastlamp = 0;+ V.debuguse = 0;+ V.f_demo = 0;+ V.zone = id_GHZ, V.act = 0;+ V.pcyc_time = 0;+ LevelSizeLoad();+ DeformLayers();+ level_blocks_load();+ LevelLayoutLoad();+ PaletteFadeOut();+ ClearScreen();+ DrawChunks_BG();+ vdp_fill_plane(plane_a, 3, 4, 34, 22, DATAW(D_ENI_TITLE), ArtTile_Level);+ vdp_load_art(ART_Nem_GHZ_1st, ArtTile_Level);+ PalLoad_Fade(palid_Title);+ V.f_debugmode = 0;+ V.generictimer = 376;+ memset(OBJ(2), 0, sizeof(Obj) / 2);+ OBJ(1)->id = id_TitleSonic;+ OBJ(2)->id = id_PSBTM;+ OBJ(3)->id = id_PSBTM;+ OBJ(3)->frame = 3;+ OBJ(4)->id = id_PSBTM;+ OBJ(4)->frame = 2;+ ExecuteObjects();+ DeformLayers();+ BuildSprites();+ NewPLC(plcid_Main);+ V.title_dcount = V.title_ccount = 0;+ vdp_display = true;+ PaletteFadeIn();+ for (;;) {+ V.vblank_routine = id_VBlank_Title;+ WaitForVBlank();+ ExecuteObjects();+ DeformLayers();+ BuildSprites();+ PalCycle_Title();+ RunPLC();+ if (demo_tick()) return;+ if (!V.generictimer) break;+ if (V.jpadpress1 & (btnStart | btnABC)) {+ play();+ return;+ }+ }+ /* the demo, after half a second */+ V.generictimer = 30;+ for (;;) {+ V.vblank_routine = id_VBlank_Title;+ WaitForVBlank();+ DeformLayers();+ PaletteCycle();+ RunPLC();+ if (demo_tick()) return;+ if (V.jpadpress1 & (btnStart | btnABC)) {+ play();+ return;+ }+ if (!V.generictimer) break;+ }+ uint16_t za = demo_levels[V.demonum & 3];+ set_zone_act(za);+ if (++V.demonum >= 4) V.demonum = 0;+ V.f_demo = 1;+ V.gamemode = id_Demo;+ if (za == 0x600) {+ V.gamemode = id_Special;+ set_zone_act(0);+ V.lastspecial = 0;+ }+ V.lives = 3;+ V.rings = 0;+ V.timemin = V.timesec = V.timecent = 0;+ V.score = 0;+ V.scorelife = 5000;+}++void game_init(void) {+#ifdef HOST+ /* (tests: straight to an act, or to special stage n with --level 7.n) */+ extern int host_level, host_act, host_lives;+ if (host_level >= 0) {+ new_game();+ AddPLC(plcid_Main);+ if (host_lives > 0) V.lives = (uint8_t)host_lives;+ if (host_level == 7) {+ V.gamemode = id_Special;+ V.lastspecial = (uint8_t)host_act;+ return;+ }+ V.zone = (uint8_t)host_level, V.act = (uint8_t)host_act;+ return;+ }+#endif+ V.gamemode = id_Sega;+ if (!save_seen_keys()) keys_screen();+}diff --git a/games/sonic/src/stubs.c b/games/sonic/src/stubs.cindex ea816842..9d7967d2 100644--- a/games/sonic/src/stubs.c+++ b/games/sonic/src/stubs.c@@ -7,21 +7,7 @@ void ResumeMusic(void) { V.air = 30; }void AnimateLevelGfx(void) {}void LZWaterFeatures(void) {}void PalCycle_SS(void) {}-void pause_overlay_draw(void) {}-void PauseGame(void) {}-void save_restore_copy(void) {}-void game_save_on_quit(void) {}-extern int host_level, host_act, host_lives;-void game_init(void) {- V.gamemode = id_Level;- V.lives = 3;-#ifdef HOST- if (host_level >= 0) V.zone = (uint8_t)host_level, V.act = (uint8_t)host_act, AddPLC(plcid_Main);-#endif-}-void GM_Sega(void) {}-void GM_Title(void) {}-void GM_Special(void) {}-void GM_Continue(void) {}-void GM_Ending(void) {}-void GM_Credits(void) {}+void GM_Special(void) { V.gamemode = id_Level; }+void GM_Continue(void) { V.gamemode = id_Sega; }+void GM_Ending(void) { V.gamemode = id_Sega; }+void GM_Credits(void) { V.gamemode = id_Sega; }diff --git a/games/sonic/src/ui.c b/games/sonic/src/ui.cnew file mode 100644index 00000000..dd08ba66--- /dev/null+++ b/games/sonic/src/ui.c@@ -0,0 +1,109 @@+/* What is drawn over the game's picture: the pause menu, the title's Continue / New game, the keys+ * screen. Text is the original's level select font (8x8: digits, capitals, $ - = >, and a few signs+ * drawn here in its style), at 1x or 2x, on boxes; it goes into each 16-line band of the picture as+ * vdp_render() pushes it, so nothing flickers. */+#include "s1.h"+#include "ui.h"++#define NBOX 6+#define NTEXT 28+static struct {+ int16_t x, y, w, h;+ uint16_t c;+} boxes[NBOX];+static struct {+ int16_t x, y;+ uint16_t c;+ uint8_t scale;+ const char *s;+} texts[NTEXT];+static int nbox, ntext;+bool ui_active;++void ui_clear(void) { nbox = ntext = 0, ui_active = false; }+void ui_box(int x, int y, int w, int h, uint16_t c) {+ if (nbox < NBOX) boxes[nbox].x = (int16_t)x, boxes[nbox].y = (int16_t)y, boxes[nbox].w = (int16_t)w,+ boxes[nbox].h = (int16_t)h, boxes[nbox].c = c, nbox++;+ ui_active = true;+}+void ui_text(int x, int y, uint16_t c, int scale, const char *s) {+ if (ntext < NTEXT) texts[ntext].x = (int16_t)x, texts[ntext].y = (int16_t)y, texts[ntext].c = c,+ texts[ntext].scale = (uint8_t)scale, texts[ntext].s = s, ntext++;+ ui_active = true;+}+int ui_width(const char *s, int scale) { return (int)strlen(s) * 8 * scale; }+void ui_text_centred(int y, uint16_t c, int scale, const char *s) { ui_text((320 - ui_width(s, scale)) / 2, y, c, scale, s); }++/* signs the font lacks, one byte a row (the first pixel the high bit) */+static const struct {+ char ch;+ uint8_t rows[8];+} extra[] = {+ {'.', {0, 0, 0, 0, 0, 0, 0x60, 0x60}}, {',', {0, 0, 0, 0, 0, 0x30, 0x30, 0x60}},+ {':', {0, 0, 0x30, 0x30, 0, 0x60, 0x60, 0}}, {'!', {0, 0x0C, 0x18, 0x18, 0x30, 0x30, 0, 0x60}},+ {'?', {0, 0x3C, 0x66, 0x0C, 0x18, 0x30, 0, 0x30}}, {'/', {0, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0}},+ {'\'', {0, 0x18, 0x18, 0x30, 0, 0, 0, 0}}, {'+', {0, 0, 0x18, 0x18, 0x7E, 0x30, 0x30, 0}},+};++/* the glyph's row r as 8 bits (the first pixel the high bit) */+static uint8_t glyph_row(char ch, int r) {+ int t = -1;+ if (ch >= '0' && ch <= '9') t = ch - '0';+ else if (ch == '$') t = 0xA;+ else if (ch == '-') t = 0xB;+ else if (ch == '=') t = 0xC;+ else if (ch == '>') t = 0xD;+ else if (ch == 'Y' || ch == 'Z') t = 0xF + ch - 'Y';+ else if (ch >= 'A' && ch <= 'X') t = 0x11 + ch - 'A';+ else if (ch >= 'a' && ch <= 'z') return glyph_row((char)(ch - 32), r);+ if (t < 0) {+ for (unsigned i = 0; i < sizeof extra / sizeof extra[0]; i++)+ if (extra[i].ch == ch) return extra[i].rows[r];+ return 0;+ }+ const uint8_t *p = (const uint8_t *)DATA(D_TILES) + ((uint32_t)DATAW(D_ARTS)[ART_Art_Text * 2] + t) * 32 + r * 4;+ uint8_t b = 0;+ for (int i = 0; i < 4; i++) {+ if (p[i] >> 4) b |= (uint8_t)(0x80 >> (i * 2));+ if (p[i] & 15) b |= (uint8_t)(0x40 >> (i * 2));+ }+ return b;+}++static void text_band(uint16_t *band, int y0, int x, int y, uint16_t c, int scale, const char *s, bool shadow) {+ int h = 8 * scale;+ if (y + h <= y0 || y >= y0 + 16) return;+ for (; *s; s++, x += 8 * scale) {+ if (*s == ' ') continue;+ for (int py = 0; py < h; py++) {+ int sy = y + py;+ if (sy < y0 || sy >= y0 + 16) continue;+ uint8_t bits = glyph_row(*s, py / scale);+ if (!bits) continue;+ uint16_t *row = band + (sy - y0) * 320;+ for (int px = 0; px < 8 * scale; px++) {+ if (!(bits & (0x80 >> (px / scale)))) continue;+ int sx = x + px;+ if (shadow) {+ if (sx + 1 < 320 && sy + 1 < y0 + 16) row[320 + sx + 1] = 0;+ continue;+ }+ if ((unsigned)sx < 320) row[sx] = c;+ }+ }+ }+}++/* into the band of the picture's lines y0..y0+15 */+void ui_draw_band(uint16_t *band, int y0) {+ for (int i = 0; i < nbox; i++) {+ int ya = boxes[i].y > y0 ? boxes[i].y : y0, yb = boxes[i].y + boxes[i].h < y0 + 16 ? boxes[i].y + boxes[i].h : y0 + 16;+ for (int y = ya; y < yb; y++)+ for (int x = boxes[i].x; x < boxes[i].x + boxes[i].w; x++)+ if ((unsigned)x < 320) band[(y - y0) * 320 + x] = boxes[i].c;+ }+ for (int i = 0; i < ntext; i++)+ text_band(band, y0, texts[i].x, texts[i].y, 0, texts[i].scale, texts[i].s, true);+ for (int i = 0; i < ntext; i++)+ text_band(band, y0, texts[i].x, texts[i].y, texts[i].c, texts[i].scale, texts[i].s, false);+}diff --git a/games/sonic/src/ui.h b/games/sonic/src/ui.hnew file mode 100644index 00000000..5253e6a4--- /dev/null+++ b/games/sonic/src/ui.h@@ -0,0 +1,23 @@+/* Text and boxes over the picture (ui.c), and the screens that use them (menus.c). */+#ifndef S1_UI_H+#define S1_UI_H+#include <stdbool.h>+#include <stdint.h>++#define UI_WHITE 0xFFFF+#define UI_YELLOW 0xFFE0+#define UI_BLUE 0x0010+#define UI_DARK 0x0841++extern bool ui_active;+void ui_clear(void);+void ui_box(int x, int y, int w, int h, uint16_t c);+void ui_text(int x, int y, uint16_t c, int scale, const char *s);+void ui_text_centred(int y, uint16_t c, int scale, const char *s);+int ui_width(const char *s, int scale);+void ui_draw_band(uint16_t *band, int y0);++/* menus.c */+void keys_screen(void); /* how to play, the first time */+void continue_menu_draw(int sel); /* on the title: Continue (0) or New game (1) */+#endifdiff --git a/games/sonic/src/vdp.c b/games/sonic/src/vdp.cindex dc0da411..86b3ae36 100644--- a/games/sonic/src/vdp.c+++ b/games/sonic/src/vdp.c@@ -9,6 +9,7 @@* then the colours, 16 lines at a time to the screen, 8 lines below its top. */#include "s1.h"#include "plat.h"+#include "ui.h"const uint8_t *vram[2048];uint16_t plane_a[32][64], plane_b[32][64];@@ -146,6 +147,9 @@ void vdp_render(void) {uint16_t back = l[vdp_backdrop & 63];for (int x = 0; x < 320; x++) dst[x] = out[x] ? l[out[x] & 63] : back;}- if ((y & 15) == 15) plat_push(0, 8 + y - 15, 320, 16, band);+ if ((y & 15) == 15) {+ if (ui_active) ui_draw_band(band, y - 15);+ plat_push(0, 8 + y - 15, 320, 16, band);+ }}}