Skip to content

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.

Merge Gabriel's live backgrounds and French (Mason363/NumPlay#31)

Mason Chen6e8de5c3d2df
diff --cc docs/play.md
index aac27fbf,aac27fbf..f3958c94
--- a/docs/play.md
+++ b/docs/play.md
@@@ -7,6 -7,6 +7,7 @@@
| **Left / Right** (or 4 / 6) | Pick a game |
| **OK** or **EXE** (or 5) | Play |
| **Back** | Leave a game's main menu to come back here. On the carousel, quit NumPlay |
++| **⌫** (backspace) | A moving background behind the carousel: Aurora, Sunset Drive, Plasma, Pastel, Lava Lamp, Ocean, then none again. NumPlay remembers it |
| **Home** | Quit, from anywhere |
The last card, **Settings**, can:
diff --cc games/games.json
index 598f8bbd,598f8bbd..d8eb19c2
--- a/games/games.json
+++ b/games/games.json
@@@ -340,7 -340,7 +340,7 @@@
"shots": [
"docs/shot.png"
],
-- "colors": 64,
++ "colors": 48,
"repo": "https://github.com/Mason363/NumPlay/tree/main/games/numvisuals"
}
]
diff --cc launcher/src/home.c
index 93a868f4,90e71293..ad88acfb
--- a/launcher/src/home.c
+++ b/launcher/src/home.c
@@@ -1,8 -1,9 +1,9 @@@
/* The home screen: a carousel of game cards. The selected card is large,
* its neighbours peek in from the sides, and the card cycles through the
* game's screenshots. The last card opens the settings. */
-#include <string.h>
#include "ui.h"
+ #include "live.h"
+#include "../../games/common/np_text.h"
#define CARD_W 200
#define CARD_H 152
@@@ -120,11 -200,15 +121,15 @@@ static void scene(void *ctx)
t = NP_CLAMP(t, 0, 256);
uint32_t top = gfx_lerp888(item_color(h->item[a], 0), item_color(h->item[b], 0), t);
uint32_t bottom = gfx_lerp888(item_color(h->item[a], 1), item_color(h->item[b], 1), t);
- gfx_vgrad(0, 0, SCREEN_W, SCREEN_H, top, bottom);
- /* a faint dot grid gives the background some texture */
- for (int y = (gfx_y0 + 15) / 16 * 16 + 8 - 16; y < gfx_y1; y += 16) {
- if (y < gfx_y0) continue;
- for (int x = 8; x < SCREEN_W; x += 16) gfx_fill_alpha(x, y, 2, 1, 0xFFFF, 22);
- if (live_active()) {
++ if (live_active()) { /* a moving background (Backspace), Gabriel's port of NumVisuals' */
+ live_strip();
+ } else {
+ gfx_vgrad(0, 0, SCREEN_W, SCREEN_H, top, bottom);
+ /* a faint dot grid gives the background some texture */
+ for (int y = (gfx_y0 + 15) / 16 * 16 + 8 - 16; y < gfx_y1; y += 16) {
+ if (y < gfx_y0) continue;
+ for (int x = 8; x < SCREEN_W; x += 16) gfx_fill_alpha(x, y, 2, 1, 0xFFFF, 22);
+ }
}
float z = ui_ease_in_out(h->zoom);
@@@ -187,8 -281,14 +192,15 @@@ static int build_items(home_t *h)
return h->n;
}
++static bool bg_loaded;
++
int np_home(int *selected, bool returning) {
ui_init();
- if (!clock_loaded) { /* once per run: the time set the last time, if the counter is still running */
- clock_loaded = true;
- if (np_clock_load(&clock0)) clock_set = true;
++ if (!bg_loaded) { /* once per run: the background chosen last time */
++ bg_loaded = true;
+ int m;
+ if (np_bg_load(&m)) live_set(m);
+ }
home_t h = {0};
build_items(&h);
/* the game played last, or else the first game there is: never Settings,
@@@ -226,6 -326,17 +238,11 @@@
h.badge = 0;
}
if (pressed & K_OK) result = h.item[h.sel];
- if (pressed & K_BACKSPACE) { /* next background, then none */
++ if (pressed & K_BACKSPACE) { /* the next background, then none */
+ live_next();
+ np_bg_save(live_mode());
+ dirty = true;
+ }
- if (pressed & K_UP) {
- clock_edit();
- keys = (ui_keys_t){np_keys(), 0, 0};
- last = np_millis();
- dirty = true;
- }
if (pressed & (K_HOME | K_BACK)) return -2;
}
/* motion */
@@@ -278,7 -389,10 +295,9 @@@
}
}
}
+ if (live_active()) dirty = true; /* the background moves: every frame */
- if (clock_set && clock_secs(now) != shown_min) shown_min = clock_secs(now), dirty = true;
if (dirty) {
+ live_frame(now);
ui_frame(scene, &h);
dirty = bar_dirty = false;
} else if (bar_dirty) {
diff --cc launcher/src/live.c
index 00000000,430a25d9..62200fa2
mode 000000,100644..100644
--- a/launcher/src/live.c
+++ b/launcher/src/live.c
@@@ -1,0 -1,253 +1,253 @@@
+ /* The backgrounds are those of NumVisuals (games/numvisuals), with the same
+ * formulas. Time is the millisecond counter. */
+ #include "live.h"
+
+ #define W SCREEN_W
+ #define H SCREEN_H
+ #define RGB1(c) (color_t)((((c) >> 8) & 0xF800) | (((c) >> 5) & 0x07E0) | (((c) >> 3) & 0x1F))
+
+ enum { AURORA, SUNSET, PLASMA, PASTEL, LAVA, OCEAN };
+
+ static int16_t *sn; /* sine, -256..256 */
+ static color_t *pal; /* 256 colours */
+ static int16_t *scr; /* scratch tables, W * 6 values */
+ static bool ready;
-static int mode = PLASMA;
++static int mode = LIVE_COUNT; /* none until Backspace picks one: the home screen as it always was */
+ static int now;
+ static int plasma_k, plasma_speed;
+
+ #define S(a) sn[(a) & 255]
+ #define SCRATCH_VALUES (W * 6)
+
+ static int clamp(int v, int a, int b) { return v < a ? a : v > b ? b : v; }
+ static int iabs(int v) { return v < 0 ? -v : v; }
+
+ /* f over b; a from 0 (all b) to 32 (all f) */
+ static color_t mixf(color_t f, color_t b, int a) {
+ uint32_t x = (f | (uint32_t)f << 16) & 0x07E0F81F, y = (b | (uint32_t)b << 16) & 0x07E0F81F;
+ y = (y + ((x - y) * (uint32_t)a >> 5)) & 0x07E0F81F;
+ return (color_t)(y | y >> 16);
+ }
+
+ typedef struct {
+ const char *name;
+ uint8_t n;
+ uint32_t col[6];
+ } bg_t;
+ static const bg_t bgs[LIVE_COUNT] = {
+ {"Aurora", 5, {0x02030C, 0x071A33, 0x0E6070, 0x33E8A0, 0xD2FFEA}},
+ {"Sunset Drive", 4, {0x14002E, 0x55106E, 0xC72C79, 0xFF8A5B}},
+ {"Plasma", 6, {0x1B0B3A, 0x6B1FA8, 0xFF3E8A, 0xFFC857, 0x2EC4B6, 0x1B0B3A}},
+ {"Pastel", 6, {0xFFB3C7, 0xB9A2FF, 0x8FD8FF, 0x9EEDB6, 0xFFE08A, 0xFFB3C7}},
+ {"Lava Lamp", 5, {0x12061F, 0x4A0F3F, 0xB8233B, 0xFF7A2E, 0xFFE7A6}},
+ {"Ocean", 4, {0x0E1B45, 0x5A3C8C, 0xE9867A, 0xFFD6A0}},
+ };
+
+ /* a gradient through the colours of the current background */
+ static void palette(void) {
+ if (!pal || mode >= LIVE_COUNT) return;
+ const uint32_t *c = bgs[mode].col;
+ int n = bgs[mode].n;
+ for (int i = 0; i < 256; i++) {
+ int p = i * (n - 1), k = p >> 8, f = p & 255;
+ uint32_t o = 0;
+ for (int s = 0; s < 24; s += 8)
+ o |= (uint32_t)((((c[k] >> s) & 255) * (256 - f) + ((c[k + 1] >> s) & 255) * f) >> 8) << s;
+ pal[i] = RGB1(o);
+ }
+ if (mode == PLASMA) plasma_k = 10, plasma_speed = 16;
+ if (mode == PASTEL) plasma_k = 6, plasma_speed = 8;
+ }
+
+ void live_init(void) {
+ ready = false;
+ sn = np_alloc(256 * sizeof(int16_t));
+ pal = np_alloc(256 * sizeof(color_t));
+ scr = np_alloc(SCRATCH_VALUES * sizeof(int16_t));
+ if (!sn || !pal || !scr) return;
+ for (int i = 0; i < 128; i++) { /* Bhaskara's sine */
+ float x = i * (3.14159265f / 128), q = x * (3.14159265f - x);
+ sn[i] = (int16_t)(16 * q / (49.348022f - 4 * q) * 256 + 0.5f);
+ sn[i + 128] = (int16_t)-sn[i];
+ }
+ palette();
+ ready = true;
+ }
+
+ bool live_ready(void) { return ready; }
+ bool live_active(void) { return ready && mode < LIVE_COUNT; }
+ int live_mode(void) { return mode; }
+ void live_set(int m) {
- mode = (m >= 0 && m <= LIVE_COUNT) ? m : PLASMA;
++ mode = (m >= 0 && m <= LIVE_COUNT) ? m : LIVE_COUNT;
+ palette();
+ }
+ void live_next(void) { live_set((mode + 1) % (LIVE_COUNT + 1)); }
+ const char *live_name(int m) { return m >= 0 && m < LIVE_COUNT ? bgs[m].name : "None"; }
+
+ /* ---------------------------------------------------------------- per frame */
+ static void frame_aurora(void) {
+ int16_t *ca = scr, *cb = scr + W, *cc = scr + 2 * W;
+ int t = now;
+ for (int x = 0; x < W; x++) {
+ ca[x] = (int16_t)(104 + (S(x + t / 29) * 22 + S(x * 2 - t / 41 + S(x / 2 + t / 67) / 6) * 12) / 256);
+ cb[x] = (int16_t)(175 + S(x * 3 + t / 13) / 9 + S(x * 9 - t / 9) / 12 + S(x / 2 - t / 23) / 4);
+ cc[x] = (int16_t)(220 + S(x * 2 + 50) / 26 + S(x * 7) / 50);
+ }
+ }
+
+ static void frame_plasma(void) {
+ int16_t *ca = scr, *cb = scr + W;
+ int t = now * plasma_speed / 16, k = plasma_k;
+ for (int x = 0; x < W; x++) ca[x] = (int16_t)(S(x * k / 8 + t / 7) + S(x * k / 13 - t / 11));
+ for (int y = 0; y < H; y++) cb[y] = (int16_t)(S(y * k / 8 - t / 9) + S(y * k / 17 + t / 13));
+ }
+
+ static void frame_lava(void) {
+ int16_t *bx = scr, *by = scr + 6;
+ int t = now;
+ for (int i = 0; i < 6; i++) {
+ bx[i] = (int16_t)(160 + S(t / (41 + i * 7) + i * 43) * (110 - i * 9) / 256);
+ by[i] = (int16_t)(120 + S(t / (53 + i * 11) + i * 97 + 64) * (100 - i * 6) / 256);
+ }
+ }
+
+ static void frame_ocean(void) {
+ static const uint8_t base[6] = {121, 130, 142, 158, 180, 209}, amp[6] = {2, 3, 4, 6, 9, 13};
+ int t = now;
+ for (int k = 0; k < 6; k++)
+ for (int x = 0; x < W; x++)
+ scr[k * W + x] = (int16_t)(base[k] + (S(x * (8 - k) / 4 + t / (70 + k * 25) + k * 50) * 2 +
+ S(x * (11 - k) / 3 - t / (110 + k * 30) + k * 90)) * amp[k] / 768);
+ }
+
+ void live_frame(uint32_t now_ms) {
+ if (!live_active()) return;
+ now = (int)now_ms;
+ switch (mode) {
+ case AURORA: frame_aurora(); break;
+ case PLASMA:
+ case PASTEL: frame_plasma(); break;
+ case LAVA: frame_lava(); break;
+ case OCEAN: frame_ocean(); break;
+ default: break;
+ }
+ }
+
+ /* ---------------------------------------------------------------- per strip */
+ static void strip_aurora(int top, int sh) {
+ int16_t *ca = scr, *cb = scr + W, *cc = scr + 2 * W;
+ for (int j = 0; j < sh; j++) {
+ int y = top + j, sky = 8 + y * 52 / H;
+ color_t *p = gfx_buf + j * W;
+ for (int x = 0; x < W; x++) {
+ if (y >= cc[x]) {
+ p[x] = RGB1(0x010308);
+ continue;
+ }
+ int d = ca[x] - y, i = d >= 0 ? cb[x] - d * 3 / 2 : cb[x] * 2 / 3 + d * 3;
+ if (i < sky) {
+ uint32_t h = (uint32_t)x * 73856093u ^ (uint32_t)y * 19349663u;
+ i = (h * 0x5BD1E995u) >> 22 ? sky : 190;
+ }
+ p[x] = pal[i > 255 ? 255 : i];
+ }
+ }
+ }
+
+ static void strip_sunset(int top, int sh) {
+ int t = now;
+ for (int j = 0; j < sh; j++) {
+ int y = top + j;
+ color_t *p = gfx_buf + j * W;
+ if (y < 150) {
+ color_t sky = pal[y * 255 / 150];
+ int dy = y - 102, r2 = 3600 - dy * dy;
+ bool cut = dy > 4 && dy % 11 < (dy - 4) / 9 + 1;
+ color_t sun = mixf(RGB1(0xFF3F81), RGB1(0xFFE45E), clamp((y - 42) * 32 / 108, 0, 32));
+ for (int x = 0; x < W; x++) {
+ int dx = x - 160, d2 = dx * dx + dy * dy;
+ p[x] = dx * dx < r2 && !cut ? sun : d2 < 8100 ? mixf(RGB1(0xFF4F9A), sky, (8100 - d2) / 700) : sky;
+ }
+ } else {
+ int d = y - 146, z0 = 98304 / d, z1 = 98304 / (d + 1), off = t / 2, a = clamp(4 + d / 3, 0, 28);
+ bool row = ((z0 + off) >> 8) != ((z1 + off) >> 8) || d == 4;
+ color_t base = mixf(RGB1(0x0A0018), RGB1(0x2A0A4A), d * 32 / 94), line = mixf(RGB1(0xFF3DDB), base, d == 4 ? 32 : a);
+ int q = (-160 * z0) >> 15;
+ for (int x = 0; x < W; x++) {
+ int nq = ((x - 159) * z0) >> 15;
+ p[x] = row || nq != q ? line : base;
+ q = nq;
+ }
+ }
+ }
+ }
+
+ static void strip_plasma(int top, int sh) {
+ int16_t *ca = scr, *cb = scr + W;
+ int t = now * plasma_speed / 16, k = plasma_k;
+ for (int j = 0; j < sh; j++) {
+ int y = top + j, r = cb[y];
+ color_t *p = gfx_buf + j * W;
+ for (int x = 0; x < W; x++) p[x] = pal[(uint8_t)(((ca[x] + r + S((x + y) * k / 16 + t / 5)) >> 3) + (t >> 4))];
+ }
+ }
+
+ static void strip_lava(int top, int sh) {
+ int16_t *bx = scr, *by = scr + 6;
+ for (int j = 0; j < sh; j++) {
+ int y = top + j, dy2[6];
+ color_t *p = gfx_buf + j * W;
+ for (int i = 0; i < 6; i++) dy2[i] = (y - by[i]) * (y - by[i]) + 16;
+ for (int x = 0; x < W; x += 2) {
+ int f = 0;
+ for (int i = 0; i < 6; i++) {
+ int dx = x - bx[i], r = 26 + i * 3;
+ f += (r * r << 7) / (dx * dx + dy2[i]);
+ }
+ p[x] = p[x + 1] = pal[f < 128 ? f * 5 / 8 : 150 + (f - 128 > 315 ? 105 : (f - 128) / 3)];
+ }
+ }
+ }
+
+ static void strip_ocean(int top, int sh) {
+ static const uint32_t sea_rgb[6] = {0x7FA9CF, 0x5A8CBE, 0x3F72A8, 0x2B5B91, 0x1C4677, 0x11335E};
+ color_t sea[6];
+ for (int i = 0; i < 6; i++) sea[i] = RGB1(sea_rgb[i]);
+ int t = now;
+ for (int j = 0; j < sh; j++) {
+ int y = top + j;
+ color_t *p = gfx_buf + j * W;
+ for (int x = 0; x < W; x++) {
+ int k = 5;
+ while (k >= 0 && y < scr[k * W + x]) k--;
+ color_t c;
+ if (k < 0) {
+ int dx = x - 236, dy = y - 92, d2 = dx * dx + dy * dy;
+ c = d2 < 225 ? RGB1(0xFFF1C9) : pal[clamp(y * 2, 0, 255)];
+ if (d2 >= 225 && d2 < 2500) c = mixf(RGB1(0xFFD9A0), c, (2500 - d2) / 160);
+ } else {
+ int e = y - scr[k * W + x];
+ c = e < 2 ? mixf(0xFFFF, sea[k], 12 - e * 5) : sea[k];
+ if (iabs(x - 236) < 4 + (y - 120) / 4 && S(y * 29 + x * 3 + t / 6) > 170) c = mixf(RGB1(0xFFE2B0), c, 14);
+ }
+ p[x] = c;
+ }
+ }
+ }
+
+ void live_strip(void) {
+ if (!live_active()) return;
+ int top = gfx_y0, sh = gfx_y1 - gfx_y0;
+ switch (mode) {
+ case AURORA: strip_aurora(top, sh); break;
+ case SUNSET: strip_sunset(top, sh); break;
+ case PLASMA:
+ case PASTEL: strip_plasma(top, sh); break;
+ case LAVA: strip_lava(top, sh); break;
+ case OCEAN: strip_ocean(top, sh); break;
+ default: break;
+ }
+ /* three quarters of the brightness, so that the white text and the cards stay readable */
+ color_t *p = gfx_buf, *e = gfx_buf + sh * W;
+ for (; p < e; p++) *p = (color_t)(((*p & 0xF7DE) >> 1) + ((*p & 0xE79C) >> 2));
+ }
diff --cc launcher/src/np.h
index 2657dc1e,2f1972c2..7e861c20
--- a/launcher/src/np.h
+++ b/launcher/src/np.h
@@@ -82,9 -82,17 +82,11 @@@ typedef struct
bool disguise; /* start as Matrices, a calculator app; NumPlay opens through the secret */
uint8_t secret; /* NP_SECRET_* */
bool hint; /* Matrices names the secret in light gray, bottom left */
- bool french; /* the interface is in French (tr.c) */
} np_config_t;
-extern bool np_french;
-const char *np_t(const char *en); /* tr.c: the text in the chosen language */
-char *np_fmt(char *out, unsigned n, const char *en, const char *arg); /* the same, with %s replaced by arg */
void np_config_load(np_config_t *c);
bool np_config_save(const np_config_t *c);
-bool np_clock_load(uint32_t *clock0); /* false when no valid saved time */
-bool np_clock_save(uint32_t clock0);
-bool np_bg_load(int *mode); /* false when none was saved */
++bool np_bg_load(int *mode); /* the home screen's live background chosen last; false when none was saved */
+ bool np_bg_save(int mode);
/* A copy of every save in numplay_saves.py, a Python script: the only kind of
file the NumWorks installer keeps when NumPlay is updated. */
void np_progress_backup(void);
diff --cc launcher/src/storage.c
index 3332ab2d,5a812655..b668083d
--- a/launcher/src/storage.c
+++ b/launcher/src/storage.c
@@@ -77,6 -81,43 +77,21 @@@ bool np_config_save(const np_config_t *
return ef_write(CONFIG_NAME, d, sizeof d);
}
-/* The home-screen clock: the seconds of the day at millisecond 0, and the millisecond counter when
- * it was saved. After a restart the counter starts again from 0, so a saved time is only kept when
- * the counter has not gone back. */
-#define CLOCK_NAME "npclock.set"
-bool np_clock_load(uint32_t *clock0) {
- uint32_t len = 0, now = np_millis();
- const uint8_t *d = ef_read(CLOCK_NAME, &len);
- if (!d || len != 10 || d[0] != 'C' || d[1] != 1) return false;
- uint32_t c = (uint32_t)d[2] | (uint32_t)d[3] << 8 | (uint32_t)d[4] << 16 | (uint32_t)d[5] << 24;
- uint32_t ms = (uint32_t)d[6] | (uint32_t)d[7] << 8 | (uint32_t)d[8] << 16 | (uint32_t)d[9] << 24;
- if (c >= 86400 || now < ms) return false;
- *clock0 = c;
- return true;
-}
-
-bool np_clock_save(uint32_t clock0) {
- uint32_t now = np_millis();
- uint8_t d[10] = {'C', 1, (uint8_t)clock0, (uint8_t)(clock0 >> 8), (uint8_t)(clock0 >> 16), (uint8_t)(clock0 >> 24),
- (uint8_t)now, (uint8_t)(now >> 8), (uint8_t)(now >> 16), (uint8_t)(now >> 24)};
- return ef_write(CLOCK_NAME, d, sizeof d);
-}
-
-/* Which live background of the home screen was chosen last. */
++/* Which live background of the home screen was chosen last (launcher/src/live.c). */
+ #define BG_NAME "npbg.set"
+ bool np_bg_load(int *mode) {
+ uint32_t len = 0;
+ const uint8_t *d = ef_read(BG_NAME, &len);
+ if (!d || len != 3 || d[0] != 'B' || d[1] != 1) return false;
+ *mode = d[2];
+ return true;
+ }
+
+ bool np_bg_save(int mode) {
+ uint8_t d[3] = {'B', 1, (uint8_t)mode};
+ return ef_write(BG_NAME, d, sizeof d);
+ }
+
/* ---------------------------------------------------------------- progress copy
* Installing apps restarts the calculator, which empties its file system; the
* NumWorks installer puts back the Python scripts only. So NumPlay keeps every
@@@ -143,6 -152,7 +158,7 @@@ static bool lz_scratch(uint32_t largest
* starts with the rest, like NumBlocks' regions); false when i is past the end */
static bool save_name(int i, char out[64]) {
if (i-- == 0) return strcpy(out, CONFIG_NAME), true;
- if (i-- == 0) return strcpy(out, BG_NAME), true; /* the home-screen background chosen */
++ if (i-- == 0) return strcpy(out, BG_NAME), true; /* the home screen's background */
for (int g = 0; g < np_game_count; g++)
for (const char *const *r = np_games[g].records; r && *r; r++) {
bool seen = false;
diff --cc tools/shots.py
index 166c7067,166c7067..64e35717
--- a/tools/shots.py
+++ b/tools/shots.py
@@@ -1,7 -1,7 +1,7 @@@
#!/usr/bin/env python3
"""Packs a game's screenshots for the launcher's carousel.
--Each screenshot is scaled to 192x144 (60 %), reduced to at most 256 colours
++Each screenshot is scaled to 192x144 (60 %), reduced to at most 48 colours (by default)
and compressed with raw DEFLATE; the launcher inflates it when the card is on
screen. On the calculator everything goes in the game's block of flash, so
uninstalling the game frees its screenshots too.
@@@ -72,7 -72,7 +72,7 @@@ def main()
ap = argparse.ArgumentParser()
ap.add_argument("--id", required=True)
ap.add_argument("--index", type=int, required=True)
-- ap.add_argument("--colors", type=int, default=64)
++ ap.add_argument("--colors", type=int, default=48)
ap.add_argument("out")
ap.add_argument("shots", nargs="+")
a = ap.parse_args()