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
Settings: reset games, start as Matrices, credits and version
Mason363ecf085b0847e
diff --git a/Makefile b/Makefileindex 28cfcafa..df407afa 100644--- a/Makefile+++ b/Makefile@@ -40,7 +40,9 @@ $(B)/games.mk: games/games.json tools/games_mk.py | $(B)LAUNCHER_SRC = $(wildcard launcher/src/*.c)LAUNCHER_H = $(wildcard launcher/src/*.h)-ARM_CFLAGS = -std=gnu11 $(EADK_CFLAGS) -Os -Wall -Wextra -Wno-unused-parameter -fno-math-errno \+# the release version, shown in NumPlay's settings (a release is tagged v$(VERSION))+VERSION := $(shell cat VERSION)+ARM_CFLAGS = -std=gnu11 $(EADK_CFLAGS) -DNP_VERSION='"$(VERSION)"' -Os -Wall -Wextra -Wno-unused-parameter -fno-math-errno \-fno-tree-loop-distribute-patterns -ffunction-sections -fdata-sections -Ilauncher/srcARM_LINK = -nostartfiles --specs=nano.specs -Wl,--relocatable -Wl,--gc-sections -Wl,-e,main \-Wl,-u,eadk_app_name -Wl,-u,eadk_app_icon -Wl,-u,eadk_api_level@@ -93,7 +95,7 @@ $(B)/gen/shots_%.c: games/games.json tools/shots.py $$(SHOTS_$$*) | $(B)ARM_OBJS = $(patsubst launcher/src/%.c,$(B)/arm/%.o,$(LAUNCHER_SRC)) $(B)/arm/gametable.o $(B)/arm/arena.o \$(B)/arm/marks.o $(foreach g,$(GAMES),$(B)/arm/shots_$(g).o)-$(B)/arm/%.o: launcher/src/%.c $(LAUNCHER_H) | $(B)+$(B)/arm/%.o: launcher/src/%.c $(LAUNCHER_H) VERSION | $(B)$(ARM_CC) $(ARM_CFLAGS) -flto -c $< -o $@$(B)/arm/gametable.o: $(B)/gen/gametable.c launcher/src/np.h$(ARM_CC) $(ARM_CFLAGS) -c $< -o $@@@ -145,7 +147,7 @@ SIM_crossyroad = games/crossyroad/output/sim-module.oSIM_numdrive = games/numdrive/output/sim-module.oSIM_chess = games/chess/output/sim-module.oSIM_tetris = $(B)/sim/tetris.o-SIM_CFLAGS = -std=gnu11 -O2 -fPIC -DNP_SIMULATOR=1 $(EADK_SIM_CFLAGS) -Ilauncher/src -Wall -Wno-unused-parameter+SIM_CFLAGS = -std=gnu11 -O2 -fPIC -DNP_SIMULATOR=1 -DNP_VERSION='"$(VERSION)"' $(EADK_SIM_CFLAGS) -Ilauncher/src -Wall -Wno-unused-parameter$(B)/sim/tetris.o: FORCE | $(B)cd games/tetris/tetris && NWLINK="$(NWLINK)" $(CARGO) build --lib --release --quiet --target aarch64-apple-darwindiff --git a/VERSION b/VERSIONnew file mode 100644index 00000000..26aaba0e--- /dev/null+++ b/VERSION@@ -0,0 +1 @@+1.2.0diff --git a/games/games.json b/games/games.jsonindex f75e9182..0e626022 100644--- a/games/games.json+++ b/games/games.json@@ -17,7 +17,10 @@"shots": ["docs/screenshots/menu.png"],- "repo": "https://github.com/Mason363/NumDash"+ "repo": "https://github.com/Mason363/NumDash",+ "reset": [+ "numdash.nds"+ ]},{"id": "crossyroad",diff --git a/games/tetris/tetris/.cargo/config.toml b/games/tetris/tetris/.cargo/config.tomlindex b3277d84..96792d9e 100644--- a/games/tetris/tetris/.cargo/config.toml+++ b/games/tetris/tetris/.cargo/config.toml@@ -7,6 +7,10 @@ rustflags = ["link-arg=-no-gc-sections","-C","target-cpu=cortex-m7",+ # addresses as plain words (no movw/movt pairs), which NumPlay can move+ # into its shared RAM (tools/npmodule.py)+ "-C",+ "target-feature=+no-movt",][build]diff --git a/launcher/src/gfx.h b/launcher/src/gfx.hindex b1dfd7a6..96644c8f 100644--- a/launcher/src/gfx.h+++ b/launcher/src/gfx.h@@ -48,6 +48,6 @@ typedef struct {const uint8_t *data;} np_icon_t;extern const np_icon_t np_icon_gear, np_icon_warning, np_icon_trash, np_icon_play, np_icon_check, np_icon_back,- np_icon_left, np_icon_right, np_icon_logo;+ np_icon_left, np_icon_right, np_icon_logo, np_icon_heart;void gfx_icon(const np_icon_t *icon, int x, int y, color_t c, int a256);#endifdiff --git a/launcher/src/icons.c b/launcher/src/icons.cindex d02d8e26..fb4fff66 100644--- a/launcher/src/icons.c+++ b/launcher/src/icons.c@@ -156,3 +156,9 @@ static const uint8_t logo_data[165] = {64,218,221,221,221,221,221,173,5,0,0,0,0,0,0,0,0,0,0,0,0,};const np_icon_t np_icon_logo = {22, 15, logo_data};++static const uint8_t heart_data[45] = {+ 16,69,0,84,1,228,255,187,255,78,253,255,255,255,223,255,255,255,255,255,251,255,255,255,+ 191,193,255,255,255,28,0,227,255,78,0,0,48,254,4,0,0,0,84,0,0,+};+const np_icon_t np_icon_heart = {10, 9, heart_data};diff --git a/launcher/src/main.c b/launcher/src/main.cindex 23f4b455..9c8c2c88 100644--- a/launcher/src/main.c+++ b/launcher/src/main.c@@ -29,6 +29,11 @@ static bool home_pressed;int main(void) {np_session_begin();np_finish_pending_uninstalls();+ /* started as Matrices (Settings): NumPlay opens only through its secret */+ np_config_t cfg;+ np_config_load(&cfg);+ if (cfg.disguise && !np_matrices(&cfg)) return np_session_end();+ np_wait_release();int selected = np_game_count ? 0 : -1;bool returning = false;for (;;) {diff --git a/launcher/src/matrices.c b/launcher/src/matrices.cnew file mode 100644index 00000000..67d8feb1--- /dev/null+++ b/launcher/src/matrices.c@@ -0,0 +1,426 @@+/* Matrices: NumPlay dressed as a calculator app, drawn like the calculator's+ * own apps (their fonts, colours and layout): a matrix to fill in, its results,+ * and a menu on the Toolbox key. The secret chosen in Settings (a key, or the+ * Examples item of the menu) opens NumPlay; Home quits. */+#include <eadk.h>+#include <string.h>+#include "np.h"+#include "sys.h"++#define C_BAR RGB(0xFF, 0xB6, 0x31)+#define C_TAB RGB(0x63, 0x69, 0x73)+#define C_BG RGB(0xE7, 0xE7, 0xEF)+#define C_WHITE 0xFFFF+#define C_SEL RGB(0xD6, 0xD7, 0xE7)+#define C_LINE RGB(0xC6, 0xC3, 0xC6)+#define C_TEXT 0x0000+#define C_GRAY RGB(0x94, 0x9A, 0xA5)++#define MAXN 4+#define TOP 40 /* under the status and tab bars */++/* calculator keys (eadk numbering) */+enum {+ KEY_LEFT = 0, KEY_UP = 1, KEY_DOWN = 2, KEY_RIGHT = 3, KEY_OK = 4, KEY_BACK = 5, KEY_HOME = 6, KEY_ONOFF = 8,+ KEY_XNT = 14, KEY_VAR = 15, KEY_TOOLBOX = 16, KEY_BACKSPACE = 17, KEY_PI = 27, KEY_SQRT = 28, KEY_MINUS = 46,+ KEY_DOT = 49, KEY_EXE = 52+};+static const int8_t digit_keys[10] = {48, 42, 43, 44, 36, 37, 38, 30, 31, 32};+static const uint8_t secret_keys[NP_SECRET_COUNT] = {KEY_XNT, KEY_VAR, KEY_TOOLBOX, KEY_PI, KEY_SQRT, 255};++static struct {+ int n, tab, row, col; /* row -1: the dimension line */+ float a[MAXN][MAXN];+ char edit[12]; /* the cell being typed, or empty */+ int menu; /* -1 closed, else the selected item */+} M;++static void fill(int x, int y, int w, int h, color_t c) {+ if (w > 0 && h > 0) eadk_display_push_rect_uniform((eadk_rect_t){x, y, w, h}, c);+}+static void text(const char *s, int x, int y, bool big, color_t fg, color_t bg) {+ eadk_display_draw_string(s, (eadk_point_t){x, y}, big, fg, bg);+}+static int width(const char *s, bool big) {+ int n = 0;+ for (; *s; s++) n += (*s & 0xC0) != 0x80; /* characters, not UTF-8 bytes */+ return n * (big ? 10 : 7);+}+static void ctext(const char *s, int cx, int y, bool big, color_t fg, color_t bg) {+ text(s, cx - width(s, big) / 2, y, big, fg, bg);+}++/* ---- numbers, without printf */+static void fmt(float v, char *o) {+ if (v != v) {+ strcpy(o, "undef");+ return;+ }+ bool neg = v < 0;+ if (neg) v = -v;+ if (v >= 1e9f) {+ strcpy(o, neg ? "-1E9+" : "1E9+");+ return;+ }+ /* three decimals at most, trailing zeros dropped */+ uint32_t ip, fp;+ if (v >= 1e6f) ip = (uint32_t)(v + 0.5f), fp = 0;+ else {+ uint32_t scaled = (uint32_t)(v * 1000 + 0.5f);+ ip = scaled / 1000, fp = scaled % 1000;+ }+ if (neg && (ip || fp)) *o++ = '-';+ char t[12];+ int n = 0;+ do t[n++] = (char)('0' + ip % 10);+ while (ip /= 10);+ while (n) *o++ = t[--n];+ if (fp) {+ *o++ = '.';+ for (int d = 100; d && fp; d /= 10) *o++ = (char)('0' + fp / d), fp %= d;+ }+ *o = 0;+}++static float parse(const char *s) {+ float sign = 1, v = 0, scale = 0;+ if (*s == '-') sign = -1, s++;+ for (; *s; s++) {+ if (*s == '.') scale = scale ? scale : 1;+ else if (*s >= '0' && *s <= '9') {+ if (scale) scale /= 10, v += (float)(*s - '0') * scale;+ else v = v * 10 + (float)(*s - '0');+ }+ }+ return sign * v;+}++/* ---- linear algebra */+static float det(float m[MAXN][MAXN], int n) {+ float a[MAXN][MAXN], d = 1;+ memcpy(a, m, sizeof a);+ for (int c = 0; c < n; c++) {+ int p = c;+ for (int r = c + 1; r < n; r++)+ if ((a[r][c] < 0 ? -a[r][c] : a[r][c]) > (a[p][c] < 0 ? -a[p][c] : a[p][c])) p = r;+ if (a[p][c] == 0) return 0;+ if (p != c) {+ for (int k = 0; k < n; k++) {+ float t = a[c][k];+ a[c][k] = a[p][k], a[p][k] = t;+ }+ d = -d;+ }+ d *= a[c][c];+ for (int r = c + 1; r < n; r++) {+ float f = a[r][c] / a[c][c];+ for (int k = c; k < n; k++) a[r][k] -= f * a[c][k];+ }+ }+ return d;+}++static bool inverse(float m[MAXN][MAXN], int n, float out[MAXN][MAXN]) {+ float a[MAXN][2 * MAXN];+ for (int r = 0; r < n; r++)+ for (int c = 0; c < 2 * n; c++) a[r][c] = c < n ? m[r][c] : (c - n == r);+ for (int c = 0; c < n; c++) {+ int p = c;+ for (int r = c + 1; r < n; r++)+ if ((a[r][c] < 0 ? -a[r][c] : a[r][c]) > (a[p][c] < 0 ? -a[p][c] : a[p][c])) p = r;+ if ((a[p][c] < 0 ? -a[p][c] : a[p][c]) < 1e-6f) return false;+ for (int k = 0; k < 2 * n; k++) {+ float t = a[c][k];+ a[c][k] = a[p][k], a[p][k] = t;+ }+ float d = a[c][c];+ for (int k = 0; k < 2 * n; k++) a[c][k] /= d;+ for (int r = 0; r < n; r++) {+ if (r == c) continue;+ float f = a[r][c];+ for (int k = 0; k < 2 * n; k++) a[r][k] -= f * a[c][k];+ }+ }+ for (int r = 0; r < n; r++)+ for (int c = 0; c < n; c++) out[r][c] = a[r][n + c];+ return true;+}++/* ---- the calculator's chrome */+static void status_bar(void) {+ fill(0, 0, SCREEN_W, 18, C_BAR);+ text("rad", 4, 2, false, C_WHITE, C_BAR);+ ctext("MATRICES", 160, 2, false, C_WHITE, C_BAR);+ /* battery */+ int level = np_battery_level();+ fill(296, 5, 15, 8, C_WHITE);+ fill(297, 6, 13, 6, C_BAR);+ fill(311, 7, 2, 4, C_WHITE);+ int bars = level < 0 ? 3 : NP_MIN(level, 3);+ fill(298, 7, bars * 11 / 3, 4, C_WHITE);+}++static void tabs(void) {+ static const char *const names[2] = {"Matrix", "Results"};+ for (int t = 0; t < 2; t++) {+ color_t bg = t == M.tab ? C_WHITE : C_TAB, fg = t == M.tab ? C_TAB : C_WHITE;+ fill(t * 160, 18, 160, 22, bg);+ ctext(names[t], t * 160 + 80, 22, false, fg, bg);+ }+}++/* ---- the Matrix tab: its dimension, then a table like the Statistics app's */+static int cell_w(void) { return M.n <= 3 ? 80 : 64; }+static int grid_x(void) { return (SCREEN_W - (24 + M.n * cell_w())) / 2 + 24; }+#define GRID_Y 92+#define CELL_H 26++static void dimension_line(void) {+ color_t bg = M.row < 0 ? C_SEL : C_WHITE;+ fill(0, 46, SCREEN_W, 30, bg);+ text("Dimension", 10, 53, true, C_TEXT, bg);+ char s[8] = "0\xC3\x97" "0"; /* n×n */+ s[0] = (char)('0' + M.n), s[3] = (char)('0' + M.n);+ text(s, SCREEN_W - 12 - width(s, true), 53, true, C_TEXT, bg);+}++static void cell(int r, int c) {+ int x = grid_x() + c * cell_w(), y = GRID_Y + 22 + r * CELL_H, w = cell_w() - 1;+ bool sel = M.tab == 0 && M.row == r && M.col == c;+ color_t bg = sel ? C_SEL : C_WHITE;+ fill(x, y, w, CELL_H - 1, bg);+ char s[14];+ if (sel && M.edit[0]) {+ strcpy(s, M.edit);+ text(s, x + w - 6 - width(s, false), y + 6, false, C_TEXT, bg);+ fill(x + w - 5, y + 5, 1, 14, C_TEXT); /* cursor */+ } else {+ fmt(M.a[r][c], s);+ text(s, x + w - 6 - width(s, false), y + 6, false, C_TEXT, bg);+ }+}++static void matrix_tab(void) {+ fill(0, TOP, SCREEN_W, SCREEN_H - TOP, C_BG);+ dimension_line();+ int gx = grid_x(), cw = cell_w();+ /* column headers */+ fill(gx - 24, GRID_Y, 24 + M.n * cw, 22, C_WHITE);+ for (int c = 0; c < M.n; c++) {+ char h[4] = "C1";+ h[1] = (char)('1' + c);+ ctext(h, gx + c * cw + cw / 2, GRID_Y + 4, false, C_GRAY, C_WHITE);+ }+ fill(gx - 24, GRID_Y + 21, 24 + M.n * cw, 1, C_LINE);+ for (int r = 0; r < M.n; r++) {+ fill(gx - 24, GRID_Y + 22 + r * CELL_H, 23, CELL_H - 1, C_WHITE);+ char h[4] = "R1";+ h[1] = (char)('1' + r);+ ctext(h, gx - 12, GRID_Y + 28 + r * CELL_H, false, C_GRAY, C_WHITE);+ fill(gx - 24, GRID_Y + 21 + (r + 1) * CELL_H, 24 + M.n * cw, 1, C_LINE);+ for (int c = 0; c < M.n; c++) {+ fill(gx + c * cw + cw - 1, GRID_Y + 22 + r * CELL_H, 1, CELL_H, C_LINE);+ cell(r, c);+ }+ }+ fill(gx - 1, GRID_Y + 22, 1, M.n * CELL_H, C_LINE);+}++/* ---- the Results tab */+static void small_matrix(float m[MAXN][MAXN], int x, int y) {+ int cw = 46;+ fill(x, y, 2, M.n * 16, C_TEXT);+ fill(x, y, 5, 1, C_TEXT);+ fill(x, y + M.n * 16 - 1, 5, 1, C_TEXT);+ int x1 = x + 8 + M.n * cw;+ fill(x1, y, 2, M.n * 16, C_TEXT);+ fill(x1 - 3, y, 5, 1, C_TEXT);+ fill(x1 - 3, y + M.n * 16 - 1, 5, 1, C_TEXT);+ for (int r = 0; r < M.n; r++)+ for (int c = 0; c < M.n; c++) {+ char s[14];+ fmt(m[r][c], s);+ text(s, x + 4 + c * cw + cw - width(s, false), y + 1 + r * 16, false, C_TEXT, C_WHITE);+ }+}++static void results_tab(void) {+ fill(0, TOP, SCREEN_W, SCREEN_H - TOP, C_BG);+ char s[16];+ int y = 46;+ fill(0, y, SCREEN_W, 28, C_WHITE);+ text("Determinant", 10, y + 7, false, C_GRAY, C_WHITE);+ fmt(det(M.a, M.n), s);+ text(s, SCREEN_W - 12 - width(s, true), y + 5, true, C_TEXT, C_WHITE);+ y += 29;+ fill(0, y, SCREEN_W, 28, C_WHITE);+ text("Trace", 10, y + 7, false, C_GRAY, C_WHITE);+ float tr = 0;+ for (int i = 0; i < M.n; i++) tr += M.a[i][i];+ fmt(tr, s);+ text(s, SCREEN_W - 12 - width(s, true), y + 5, true, C_TEXT, C_WHITE);+ y += 29;+ int h = M.n * 16 + 12;+ fill(0, y, SCREEN_W, SCREEN_H - y, C_WHITE);+ text("Inverse", 10, y + 6, false, C_GRAY, C_WHITE);+ float inv[MAXN][MAXN];+ if (inverse(M.a, M.n, inv)) small_matrix(inv, SCREEN_W - 20 - (8 + M.n * 46), y + 6);+ else text("Not invertible", SCREEN_W - 12 - width("Not invertible", false), y + 6, false, C_TEXT, C_WHITE);+ y += h;+ fill(0, y, SCREEN_W, 1, C_LINE);+ text("Transpose", 10, y + 6, false, C_GRAY, C_WHITE);+ float t[MAXN][MAXN];+ for (int r = 0; r < M.n; r++)+ for (int c = 0; c < M.n; c++) t[r][c] = M.a[c][r];+ small_matrix(t, SCREEN_W - 20 - (8 + M.n * 46), y + 6);+}++/* ---- the Toolbox menu */+static const char *const menu_items[3] = {"Identity matrix", "Clear", "Examples"};++static void menu_draw(void) {+ int x = 150, y = 60, w = 162;+ fill(x - 1, y - 1, w + 2, 1 + 22 + 3 * 30 + 1, C_LINE);+ fill(x, y, w, 22, C_TAB);+ text("Matrices", x + 8, y + 4, false, C_WHITE, C_TAB);+ for (int i = 0; i < 3; i++) {+ color_t bg = i == M.menu ? C_SEL : C_WHITE;+ fill(x, y + 22 + i * 30, w, 29, bg);+ text(menu_items[i], x + 8, y + 22 + i * 30 + 7, false, C_TEXT, bg);+ fill(x, y + 22 + i * 30 + 29, w, 1, C_LINE);+ }+}++static void redraw(void) {+ tabs();+ if (M.tab == 0) matrix_tab();+ else results_tab();+ if (M.menu >= 0) menu_draw();+}++static void commit(void) {+ if (!M.edit[0]) return;+ M.a[M.row][M.col] = parse(M.edit);+ M.edit[0] = 0;+}++static void example(void) {+ static const int8_t ex[MAXN][MAXN] = {{2, 1, 0, 3}, {1, 3, 2, 0}, {0, 2, 4, 1}, {3, 0, 1, 2}};+ for (int r = 0; r < MAXN; r++)+ for (int c = 0; c < MAXN; c++) M.a[r][c] = ex[r][c];+}++bool np_matrices(const np_config_t *cfg) {+ memset(&M, 0, sizeof M);+ M.n = 3;+ M.menu = -1;+ for (int i = 0; i < MAXN; i++) M.a[i][i] = 1;+ status_bar();+ redraw();+ uint64_t prev = eadk_keyboard_scan();+ uint32_t repeat_at = 0;+ for (int frame = 0;; frame++) {+ uint64_t k = eadk_keyboard_scan(), hit = k & ~prev;+ uint32_t now = np_millis();+ const uint64_t arrows = 0xF;+ if (hit & arrows) repeat_at = now + 400;+ else if ((k & arrows) && (int32_t)(now - repeat_at) >= 0) hit |= k & arrows, repeat_at = now + 110;+ prev = k;+#define HIT(key) ((hit >> (key)) & 1)+ if (HIT(KEY_HOME) || HIT(KEY_ONOFF)) return false;+ uint8_t sk = secret_keys[cfg->secret];+ if (sk != 255 && HIT(sk)) return true;+ if (frame % 600 == 0) status_bar(); /* the battery, now and then */+ if (!hit) {+ np_sleep(16);+ continue;+ }+ bool dirty = false;+ if (M.menu >= 0) {+ if (HIT(KEY_UP) && M.menu > 0) M.menu--, dirty = true;+ if (HIT(KEY_DOWN) && M.menu < 2) M.menu++, dirty = true;+ if (HIT(KEY_BACK) || HIT(KEY_TOOLBOX)) M.menu = -1, dirty = true;+ if (HIT(KEY_OK) || HIT(KEY_EXE)) {+ if (M.menu == 2 && cfg->secret == NP_SECRET_MENU) return true;+ if (M.menu == 0) {+ memset(M.a, 0, sizeof M.a);+ for (int i = 0; i < MAXN; i++) M.a[i][i] = 1;+ } else if (M.menu == 1) {+ memset(M.a, 0, sizeof M.a);+ } else {+ example();+ }+ M.menu = -1;+ dirty = true;+ }+ if (dirty) redraw();+ continue;+ }+ if (HIT(KEY_TOOLBOX)) {+ commit();+ M.menu = 0;+ redraw();+ continue;+ }+ /* tabs: Up from the top goes to the tab bar, like the calculator's apps */+ if (M.tab == 1) {+ if (HIT(KEY_LEFT)) M.tab = 0, dirty = true;+ if (dirty) redraw();+ continue;+ }+ if (HIT(KEY_RIGHT) && M.row < 0) {+ if (M.n < MAXN) M.n++, dirty = true;+ } else if (HIT(KEY_LEFT) && M.row < 0) {+ if (M.n > 1) M.n--, dirty = true;+ } else if (HIT(KEY_UP) || HIT(KEY_DOWN) || HIT(KEY_LEFT) || HIT(KEY_RIGHT)) {+ commit();+ int r = M.row, c = M.col;+ if (HIT(KEY_UP)) r--;+ if (HIT(KEY_DOWN)) r = NP_MIN(r + 1, M.n - 1);+ if (HIT(KEY_LEFT)) c = NP_MAX(c - 1, 0);+ if (HIT(KEY_RIGHT)) {+ if (c == M.n - 1) {+ M.tab = 1; /* past the last column: the results */+ redraw();+ continue;+ }+ c++;+ }+ r = NP_MAX(r, -1);+ int or_ = M.row, oc = M.col;+ M.row = r, M.col = c;+ if (or_ < 0 || r < 0) {+ dimension_line();+ if (or_ >= 0) cell(or_, oc);+ if (r >= 0) cell(r, c);+ } else {+ cell(or_, oc);+ cell(r, c);+ }+ continue;+ } else if (M.row >= 0) {+ size_t len = strlen(M.edit);+ for (int d = 0; d < 10; d++)+ if (HIT(digit_keys[d]) && len < 9) M.edit[len++] = (char)('0' + d), M.edit[len] = 0;+ if (HIT(KEY_DOT) && len < 9 && !strchr(M.edit, '.')) M.edit[len++] = '.', M.edit[len] = 0;+ if (HIT(KEY_MINUS) && len == 0) M.edit[len++] = '-', M.edit[len] = 0;+ if (HIT(KEY_BACKSPACE)) {+ if (len) M.edit[--len] = 0;+ else M.a[M.row][M.col] = 0;+ }+ if (HIT(KEY_OK) || HIT(KEY_EXE)) {+ commit();+ if (M.col < M.n - 1) M.col++;+ else if (M.row < M.n - 1) M.row++, M.col = 0;+ matrix_tab();+ continue;+ }+ cell(M.row, M.col);+ continue;+ }+ if (dirty) {+ M.row = NP_MIN(M.row, M.n - 1), M.col = NP_MIN(M.col, M.n - 1);+ redraw();+ }+ }+}diff --git a/launcher/src/np.h b/launcher/src/np.hindex 873fdfc2..ff6766f7 100644--- a/launcher/src/np.h+++ b/launcher/src/np.h@@ -58,6 +58,7 @@ typedef struct {const np_shot_t *shots;uint8_t nshots;const char *const *records; /* save files, deleted on uninstall */+ const char *const *progress; /* the ones Reset deletes (not levels made in an editor) */uint32_t est_size; /* code and data, for the simulator */} np_game_t;@@ -71,6 +72,19 @@ void np_game_run(int i);/* ---- storage (storage.c): Epsilon's record file system */bool np_storage_delete(const char *name); /* true if gone (or never there) */uint32_t np_storage_record_size(const char *name);+/* deletes a game's progress (np_game_t.progress); true if all of it is gone */+bool np_reset_game(int game);++/* NumPlay's own settings, in the file numplay.set */+enum { NP_SECRET_XNT, NP_SECRET_VAR, NP_SECRET_TOOLBOX, NP_SECRET_PI, NP_SECRET_SQRT, NP_SECRET_MENU, NP_SECRET_COUNT };+typedef struct {+ bool disguise; /* start as Matrices, a calculator app; NumPlay opens through the secret */+ uint8_t secret; /* NP_SECRET_* */+} np_config_t;+void np_config_load(np_config_t *c);+bool np_config_save(const np_config_t *c);+/* Matrices, the disguise: true when the secret was used (open NumPlay), false to quit */+bool np_matrices(const np_config_t *c);/* ---- uninstalling (flash.c) */enum { NP_UNINSTALL_OK, NP_UNINSTALL_UNSUPPORTED, NP_UNINSTALL_BATTERY, NP_UNINSTALL_FAILED };diff --git a/launcher/src/settings.c b/launcher/src/settings.cindex 6f467998..bb6d7dd5 100644--- a/launcher/src/settings.c+++ b/launcher/src/settings.c@@ -1,5 +1,6 @@-/* Settings: the list of installed games, and uninstalling one of them behind- * a clear warning and a hold-to-confirm button. */+/* Settings: the installed games, each with Reset (its progress) and Uninstall+ * (behind a clear warning and a hold-to-confirm button); Reset all; starting+ * as Matrices, a calculator app with a secret way into NumPlay; and credits. */#include <string.h>#include "sys.h"#include "ui.h"@@ -10,6 +11,7 @@#define DANGER_TOP 0x8C1D23#define DANGER_BOTTOM 0x2A080B#define OK_GREEN 0x2FB36B+#define ACCENT 0x7B43FF#define ROW_H 34#define ROWS_Y 50#define HOLD_MS 1400@@ -27,12 +29,35 @@ static char *fmt_kb(char *out, uint32_t bytes) {}/* ---------------------------------------------------------------- list */+enum { ROW_GAME, ROW_RESET_ALL, ROW_DISGUISE, ROW_SECRET };typedef struct {- int n, sel, top;- int8_t games[16];+ int n, sel, top, action; /* action: on a game, 0 Reset, 1 Uninstall */+ int8_t kind[20], game[20];+ int ngames;+ np_config_t cfg;float cursor; /* animated selection */} list_t;+static const char *const secret_names[NP_SECRET_COUNT] = {"x,n,t key", "var key", "Toolbox key", "Pi key",+ "Square root key", "Menu: Examples"};++static void build_rows(list_t *l) {+ l->n = l->ngames = 0;+ for (int i = 0; i < np_game_count; i++)+ if (np_game_installed(i)) l->kind[l->n] = ROW_GAME, l->game[l->n++] = (int8_t)i, l->ngames++;+ l->kind[l->n++] = ROW_RESET_ALL;+ l->kind[l->n++] = ROW_DISGUISE;+ l->kind[l->n++] = ROW_SECRET;+ l->sel = NP_MIN(l->sel, l->n - 1);+}++/* keeps the selection in view; the credits show under the last row */+static void scroll(list_t *l) {+ if (l->sel < l->top) l->top = l->sel;+ if (l->sel > l->top + 4) l->top = l->sel - 4;+ if (l->sel == l->n - 1) l->top = NP_MAX(0, l->n + 2 - 5);+}+static void draw_thumb(int game, int x, int y, int w, int h) {const np_game_t *g = &np_games[game];const uint8_t *px = ui_shot(game, 0);@@ -40,6 +65,17 @@ static void draw_thumb(int game, int x, int y, int w, int h) {else gfx_rrect(x, y, w, h, 5, gfx_rgb(g->top), 256);}+static void pill(int x, int y, int w, const char *label, bool on, uint32_t color) {+ if (on) gfx_rrect(x, y, w, 20, 10, gfx_rgb(color), 256);+ else gfx_rrect_outline(x, y, w, 20, 10, 1, 0xFFFF, 90);+ gfx_text_center(&np_font_small, x + w / 2, y + 14, label, 0xFFFF, on ? 256 : 190);+}++static void row_text(int y, const char *title, const char *sub, int a) {+ gfx_text(&np_font_body, 18, y + 15, title, 0xFFFF, a);+ gfx_text(&np_font_small, 18, y + 28, sub, 0xFFFF, a * 150 / 256);+}+static void list_scene(void *ctx) {list_t *l = ctx;gfx_vgrad(0, 0, SCREEN_W, SCREEN_H, BG_TOP, BG_BOTTOM);@@ -47,49 +83,86 @@ static void list_scene(void *ctx) {gfx_icon(&np_icon_back, 14, 17, 0xFFFF, 200);gfx_text(&np_font_title, 36, 30, "Settings", 0xFFFF, 256);uint32_t total = 0;- for (int i = 0; i < l->n; i++) total += np_game_size(l->games[i]);+ for (int i = 0; i < l->ngames; i++) total += np_game_size(l->game[i]);char kb[16];- if (l->n) {+ if (l->ngames) {gfx_text_right(&np_font_small, SCREEN_W - 14, 20, "INSTALLED GAMES", 0xFFFF, 150);gfx_text_right(&np_font_body, SCREEN_W - 14, 34, fmt_kb(kb, total), 0xFFFF, 230);}}- if (!l->n) {- gfx_icon(&np_icon_check, 145, 92, 0xFFFF, 150);- gfx_text_center(&np_font_body, 160, 146, "No games left to uninstall", 0xFFFF, 220);- gfx_text_center(&np_font_small, 160, 164, "Reinstall NumPlay to get them back", 0xFFFF, 150);- return;- }/* selection highlight slides between rows */float cy = ROWS_Y + (l->cursor - l->top) * (ROW_H + 4);gfx_rrect(10, (int)cy, SCREEN_W - 20, ROW_H, 10, 0xFFFF, 46);- for (int r = 0; r < 5 && l->top + r < l->n; r++) {- int i = l->top + r, game = l->games[i];- int y = ROWS_Y + r * (ROW_H + 4);- if (y >= gfx_y1 || y + ROW_H <= gfx_y0) continue;- const np_game_t *g = &np_games[game];- draw_thumb(game, 16, y + 4, 35, 26);- gfx_text(&np_font_body, 60, y + 15, g->title, 0xFFFF, 256);+ for (int r = 0; r < 5; r++) {+ int i = l->top + r, y = ROWS_Y + r * (ROW_H + 4);+ if (y >= gfx_y1 || y + (i >= l->n ? 60 : ROW_H + 4) <= gfx_y0) continue; /* the credits are taller */+ if (i >= l->n) {+ /* credits, under the last row */+ if (i != l->n) continue;+ int w = gfx_text_width(&np_font_body, "Made with ") + 12 + gfx_text_width(&np_font_body, " by Mason Chen");+ int x = 160 - w / 2;+ x = gfx_text(&np_font_body, x, y + 20, "Made with ", 0xFFFF, 230);+ gfx_icon(&np_icon_heart, x + 1, y + 12, gfx_rgb(DANGER), 256);+ gfx_text(&np_font_body, x + 12, y + 20, " by Mason Chen", 0xFFFF, 230);+ gfx_text_center(&np_font_small, 160, y + 36, "github.com/Mason363/NumPlay", 0xFFFF, 170);+ gfx_text_center(&np_font_small, 160, y + 50, "NumPlay " NP_VERSION, 0xFFFF, 120);+ continue;+ }+ bool on = i == l->sel;char kb[16];- gfx_text(&np_font_small, 60, y + 28, fmt_kb(kb, np_game_size(game)), 0xFFFF, 150);- if (i == l->sel) {- gfx_rrect(SCREEN_W - 104, y + 7, 86, 20, 10, gfx_rgb(DANGER), 256);- gfx_icon(&np_icon_trash, SCREEN_W - 96, y + 9, 0xFFFF, 256);- gfx_text(&np_font_small, SCREEN_W - 78, y + 21, "Uninstall", 0xFFFF, 256);+ switch (l->kind[i]) {+ case ROW_GAME: {+ int game = l->game[i];+ draw_thumb(game, 16, y + 4, 35, 26);+ gfx_text(&np_font_body, 60, y + 15, np_games[game].title, 0xFFFF, 256);+ gfx_text(&np_font_small, 60, y + 28, fmt_kb(kb, np_game_size(game)), 0xFFFF, 150);+ if (on) {+ pill(SCREEN_W - 170, y + 7, 58, "Reset", l->action == 0, ACCENT);+ pill(SCREEN_W - 104, y + 7, 86, "", l->action == 1, DANGER);+ gfx_icon(&np_icon_trash, SCREEN_W - 96, y + 9, 0xFFFF, l->action == 1 ? 256 : 190);+ gfx_text(&np_font_small, SCREEN_W - 78, y + 21, "Uninstall", 0xFFFF, l->action == 1 ? 256 : 190);+ }+ break;+ }+ case ROW_RESET_ALL:+ row_text(y, "Reset all games", "Deletes the progress of every game", 256);+ if (on) pill(SCREEN_W - 76, y + 7, 58, "Reset", true, ACCENT);+ break;+ case ROW_DISGUISE: {+ row_text(y, "Start as Matrices", "A math app first; NumPlay opens in secret", 256);+ int tx = SCREEN_W - 58, ty = y + 8;+ gfx_rrect(tx, ty, 38, 18, 9, l->cfg.disguise ? gfx_rgb(OK_GREEN) : 0xFFFF, l->cfg.disguise ? 256 : 70);+ gfx_circle(l->cfg.disguise ? tx + 29 : tx + 9, ty + 9, 7, 0xFFFF, 256);+ break;+ }+ case ROW_SECRET: {+ int a = l->cfg.disguise ? 256 : 130;+ row_text(y, "Secret way in", "Opens NumPlay from Matrices", a);+ const char *v = secret_names[l->cfg.secret];+ int w = gfx_text_width(&np_font_small, v);+ gfx_text_right(&np_font_small, SCREEN_W - 32, y + 21, v, 0xFFFF, a);+ if (on) {+ gfx_icon(&np_icon_left, SCREEN_W - 44 - w, y + 10, 0xFFFF, a);+ gfx_icon(&np_icon_right, SCREEN_W - 26, y + 10, 0xFFFF, a);+ }+ break;+ }}}}/* ---------------------------------------------------------------- dialogs */+enum { ACT_UNINSTALL, ACT_RESET, ACT_RESET_ALL };typedef struct {int game;- int choice; /* 0 cancel, 1 uninstall */+ int choice; /* 0 cancel, 1 uninstall or reset */float hold; /* 0..1 while OK is held on "Uninstall" */bool hinted;int stage; /* 0 confirm, 1 working, 2 done, 3 error */int error;float progress;uint32_t freed;+ int action; /* ACT_* */} dialog_t;static const char *error_text(int e) {@@ -104,8 +177,43 @@ static const char *error_text(int e) {}}+/* Reset and Reset all: a plain question (Cancel first), then done */+static void reset_scene(dialog_t *d) {+ const char *title = d->action == ACT_RESET_ALL ? "every game" : np_games[d->game].title;+ char line[64], body[128];+ if (d->stage == 2) {+ gfx_vgrad(0, 0, SCREEN_W, SCREEN_H, 0x1F5A3C, 0x0B1A12);+ gfx_circle(160, 74, 34, gfx_rgb(OK_GREEN), 256);+ gfx_icon(&np_icon_check, 145, 59, 0xFFFF, 256);+ strcpy(line, d->action == ACT_RESET_ALL ? "Every game" : title);+ strcat(line, d->action == ACT_RESET_ALL ? " was reset" : " was reset");+ gfx_text_center(&np_font_title, 160, 142, line, 0xFFFF, 256);+ gfx_text_center(&np_font_body, 160, 166, "Its progress starts over.", 0xFFFF, 200);+ ui_button(110, 192, 100, 28, "OK", true, OK_GREEN, 256);+ return;+ }+ gfx_vgrad(0, 0, SCREEN_W, SCREEN_H, DANGER_TOP, DANGER_BOTTOM);+ gfx_icon(&np_icon_warning, 137, 14, 0xFFFF, 256);+ strcpy(line, "Reset ");+ strcat(line, title);+ strcat(line, "?");+ gfx_text_center(&np_font_title, 160, 80, line, 0xFFFF, 256);+ strcpy(body, "This deletes the saved progress of ");+ strcat(body, title);+ strcat(body, ": best scores, unlocked levels and settings. You can't undo this.");+ int y = gfx_paragraph(&np_font_body, 160, 102, 280, 16, body, 0xFFFF, 240);+ gfx_paragraph(&np_font_small, 160, y + 4, 290, 13, "Games stay installed, and levels you made in an editor are kept.",+ 0xFFFF, 170);+ ui_button(38, 198, 110, 28, "Cancel", d->choice == 0, 0x555D70, 256);+ ui_button(172, 198, 110, 28, "Reset", d->choice == 1, DANGER, 256);+}+static void dialog_scene(void *ctx) {dialog_t *d = ctx;+ if (d->action != ACT_UNINSTALL) {+ reset_scene(d);+ return;+ }const np_game_t *g = &np_games[d->game];char line[64];if (d->stage == 2) {@@ -187,9 +295,35 @@ static void wait_ok(dialog_t *d) {}}+/* Reset (game >= 0) or Reset all (game < 0), after asking. */+static void reset_flow(int game) {+ dialog_t d = {game < 0 ? 0 : game, 0, 0, false, 0, 0, 0, 0, game < 0 ? ACT_RESET_ALL : ACT_RESET};+ ui_keys_t k = {np_keys(), 0, 0};+ ui_frame(dialog_scene, &d);+ for (;;) {+ uint32_t p = ui_poll(&k);+ if (p & (K_BACK | K_HOME)) return;+ if (p & (K_LEFT | K_RIGHT)) {+ d.choice = (p & K_RIGHT) != 0;+ ui_frame(dialog_scene, &d);+ }+ if (p & K_OK) {+ if (d.choice == 0) return;+ break;+ }+ np_sleep(16);+ }+ if (game >= 0) np_reset_game(game);+ else+ for (int i = 0; i < np_game_count; i++) np_reset_game(i);+ np_wait_release();+ d.stage = 2;+ wait_ok(&d);+}+/* Returns true if the game was uninstalled. */static bool uninstall_flow(int game) {- dialog_t d = {game, 0, 0, false, 0, 0, 0, 0};+ dialog_t d = {game, 0, 0, false, 0, 0, 0, 0, ACT_UNINSTALL};active_dialog = &d;if (!np_uninstall_supported() || !np_battery_ok()) {d.stage = 3;@@ -245,8 +379,8 @@ static bool uninstall_flow(int game) {void np_settings(void) {ui_init();list_t l = {0};- for (int i = 0; i < np_game_count; i++)- if (np_game_installed(i)) l.games[l.n++] = (int8_t)i;+ np_config_load(&l.cfg);+ build_rows(&l);ui_keys_t k = {np_keys(), 0, 0};uint32_t last = np_millis();bool dirty = true;@@ -256,26 +390,28 @@ void np_settings(void) {last = now;uint32_t p = ui_poll(&k);if (p & (K_BACK | K_HOME)) return;- if (l.n) {- if (p & K_UP) l.sel = NP_MAX(l.sel - 1, 0);- if (p & K_DOWN) l.sel = NP_MIN(l.sel + 1, l.n - 1);- if (l.sel < l.top) l.top = l.sel;- if (l.sel > l.top + 4) l.top = l.sel - 4;- if (p & (K_OK | K_BACKSPACE)) {- if (uninstall_flow(l.games[l.sel])) {- l.n = 0;- for (int i = 0; i < np_game_count; i++)- if (np_game_installed(i)) l.games[l.n++] = (int8_t)i;- l.sel = NP_MIN(l.sel, NP_MAX(l.n - 1, 0));- l.top = NP_MIN(l.top, l.sel);- l.cursor = l.sel;- }- k.held = np_keys();- last = np_millis();- dirty = true;- }- if (p) dirty = true;+ int kind = l.kind[l.sel];+ if (p & K_UP) l.sel = NP_MAX(l.sel - 1, 0), l.action = 0;+ if (p & K_DOWN) l.sel = NP_MIN(l.sel + 1, l.n - 1), l.action = 0;+ if (kind == ROW_GAME && (p & (K_LEFT | K_RIGHT))) l.action = (p & K_RIGHT) != 0;+ if (kind == ROW_SECRET && (p & (K_LEFT | K_RIGHT | K_OK))) {+ l.cfg.secret = (uint8_t)((l.cfg.secret + ((p & K_LEFT) ? NP_SECRET_COUNT - 1 : 1)) % NP_SECRET_COUNT);+ np_config_save(&l.cfg);+ }+ if (kind == ROW_DISGUISE && (p & (K_OK | K_LEFT | K_RIGHT))) {+ l.cfg.disguise = !l.cfg.disguise;+ np_config_save(&l.cfg);+ }+ bool go = (p & K_OK) || (kind == ROW_GAME && (p & K_BACKSPACE));+ if (go && (kind == ROW_GAME || kind == ROW_RESET_ALL)) {+ if (kind == ROW_RESET_ALL) reset_flow(-1);+ else if (l.action == 0 && !(p & K_BACKSPACE)) reset_flow(l.game[l.sel]);+ else if (uninstall_flow(l.game[l.sel])) build_rows(&l), l.cursor = l.sel;+ k.held = np_keys();+ last = np_millis();}+ scroll(&l);+ if (p) dirty = true;float c = ui_approach(l.cursor, (float)l.sel, dt, 0.05f);if (c != l.cursor) dirty = true;l.cursor = c;diff --git a/launcher/src/storage.c b/launcher/src/storage.cindex 41516e3e..0211d67f 100644--- a/launcher/src/storage.c+++ b/launcher/src/storage.c@@ -1,5 +1,6 @@-/* Deleting a game's save files from Epsilon's file system, with the careful- * record handling shared with the games (games/common/epsilon_files.h). */+/* Save files in Epsilon's file system, with the careful record handling shared+ * with the games (games/common/epsilon_files.h): deleting a game's saves, and+ * NumPlay's own settings. */#include "np.h"#include "../../games/common/epsilon_files.h"@@ -9,3 +10,27 @@ uint32_t np_storage_record_size(const char *name) {uint32_t len = 0;return ef_read(name, &len) ? len : 0;}++bool np_reset_game(int game) {+ bool ok = true;+ for (const char *const *r = np_games[game].progress; r && *r; r++) ok &= np_storage_delete(*r);+ return ok;+}++#define CONFIG_NAME "numplay.set"+#define CONFIG_MAGIC 0x4E /* 'N' */++void np_config_load(np_config_t *c) {+ c->disguise = false;+ c->secret = NP_SECRET_XNT;+ uint32_t len = 0;+ const uint8_t *d = ef_read(CONFIG_NAME, &len);+ if (!d || len != 4 || d[0] != CONFIG_MAGIC || d[1] != 1) return;+ c->disguise = d[2] != 0;+ if (d[3] < NP_SECRET_COUNT) c->secret = d[3];+}++bool np_config_save(const np_config_t *c) {+ uint8_t d[4] = {CONFIG_MAGIC, 1, c->disguise, c->secret};+ return ef_write(CONFIG_NAME, d, sizeof d);+}diff --git a/tools/gen_games.py b/tools/gen_games.pyindex c0b0078b..6667b0e2 100644--- a/tools/gen_games.py+++ b/tools/gen_games.py@@ -59,6 +59,8 @@ def main():L.append(f"int np_{i}_main(void);")recs = ", ".join(json.dumps(r) for r in g["records"])L.append(f"static const char *const {i}_records[] = {{{recs + ', ' if recs else ''}NULL}};")+ prog = ", ".join(json.dumps(r) for r in g.get("reset", g["records"]))+ L.append(f"static const char *const {i}_progress[] = {{{prog + ', ' if prog else ''}NULL}};")L.append("")if a.simulator:L.append("/* The simulator has no flash blocks and gives each game its own RAM. */")@@ -83,7 +85,7 @@ def main():L.append(f" {{{c_str(i)}, {c_str(g['title'])}, {c_str(g['tagline'])}, {c_str(g.get('credit'))},")L.append(f" {g['top']}, {g['bottom']}, {g['accent']}, np_{i}_begin, np_{i}_end, np_{i}_main,")L.append(f" RAM(np_{i}_data), INIT(np_{i}_init), SIZE({s['data']}), RAM(np_{i}_bss), SIZE({s['bss']}),")- L.append(f" np_{i}_shots, {len(g['shots'])}, {i}_records, {est}}},")+ L.append(f" np_{i}_shots, {len(g['shots'])}, {i}_records, {i}_progress, {est}}},")L.append("};")L.append(f"const int np_game_count = {len(games)};")open(os.path.join(a.out, "gametable.c"), "w").write("\n".join(L) + "\n")diff --git a/tools/icongen.py b/tools/icongen.pyindex 71656ef5..828d64b9 100644--- a/tools/icongen.py+++ b/tools/icongen.py@@ -129,6 +129,16 @@ def logo(w, h):return finish(im, w, h)+def heart(w, h):+ im, d = canvas(w, h)+ W, H = w * S, h * S+ r = W * 0.27+ for cx in (W * 0.28, W * 0.72):+ d.ellipse([cx - r, H * 0.08, cx + r, H * 0.08 + 2 * r], fill=255)+ d.polygon([(W * 0.04, H * 0.42), (W * 0.96, H * 0.42), (W * 0.5, H * 0.96)], fill=255)+ return finish(im, w, h)++ICONS = [("gear", lambda: gear(42)),("warning", lambda: warning(46, 40)),@@ -139,6 +149,7 @@ ICONS = [("left", lambda: chevron(8, 14, True)),("right", lambda: chevron(8, 14, False)),("logo", lambda: logo(22, 15)),+ ("heart", lambda: heart(10, 9)),]