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.

TI-84 CE: gfx draws only what changed, Block Blast, Doodle Jump, Snake and Pac-Man faster

Mason3635c7e7eb021f6
diff --git a/games/blockblast/src/main.c b/games/blockblast/src/main.c
index 841af7bd..f76d7aaf 100644
--- a/games/blockblast/src/main.c
+++ b/games/blockblast/src/main.c
@@ -11,6 +11,9 @@
#include <eadk.h>
#include "../../../launcher/src/gfx.h"
#include "../../common/np_text.h"
+#ifdef __TICE__
+#include <string.h>
+#endif
#if NP_MODULE /* (np_file_read and np_file_write: NumPlay's, launcher/src/np.h) */
#define np_app_begin()
@@ -70,9 +73,16 @@ static const uint32_t pieces[] = {
};
#define NP (int)(sizeof pieces / sizeof pieces[0])
/* every cell of block p, row r and column c (shifted up, its first cell is the top bit) */
+#ifdef __TICE__ /* (the TI-84 Plus CE: no division) */
+#define EACH(p) \
+ for (int r = 0, c = 0, o_ = 1; o_; o_ = 0) \
+ for (uint32_t m_ = pieces[p] << 7; m_; m_ <<= 1, ++c == 5 ? (c = 0, r++) : 0) \
+ if ((int32_t)m_ < 0)
+#else
#define EACH(p) \
for (uint32_t m_ = pieces[p] << 7, b_ = 0; m_; m_ <<= 1, b_++) \
for (int r = (int)b_ / 5, c = (int)b_ % 5, o_ = (int32_t)m_ < 0; o_; o_ = 0)
+#endif
/* blue, orange, green, purple, yellow, cyan, red, and grey for what can't go anywhere */
static const uint32_t faces[9] = {0, 0x345CEF, 0xF35C17, 0x28CB30, 0x9655E5, 0xFBD008, 0x19C4F0, 0xD53D38, 0x737A9C};
@@ -235,7 +245,17 @@ static void place(void) {
/* ------------------------------------------------------------ drawing */
/* a block, s pixels a side: its face framed by bevels, the top lightest, then the left, the
right, and the bottom darkest, meeting on the diagonals */
+#ifdef __TICE__ /* (the TI-84 Plus CE: one call in the strips' lists) */
+static void block_at(const int *a);
+static void block(int x, int y, int s, int c) {
+ const int a[] = {x, y, s, c};
+ gfx_call(block_at, a, 4, x, y, s, s);
+}
+static void block_at(const int *a) {
+ int x = a[0], y = a[1], s = a[2], c = a[3];
+#else
static void block(int x, int y, int s, int c) {
+#endif
const color_t *k = shade[c];
gfx_fill(x, y, s, s, k[4]);
for (int i = 0, b = s > 14 ? 4 : 2; i < b; i++) {
@@ -398,6 +418,9 @@ int main(void) {
np_app_begin();
color_t *their_strip = gfx_buf;
gfx_buf = strip;
+#ifdef __TICE__
+ gfx_pure = true; /* (draws only with gfx_*) */
+#endif
for (int c = 1; c < 9; c++) {
static const uint8_t by[5] = {115, 60, 50, 120, 0}; /* towards black, then white */
for (int i = 0; i < 5; i++) shade[c][i] = gfx_rgb(gfx_lerp888(faces[c], i < 2 ? 0 : 0xFFFFFF, by[i]));
@@ -457,7 +480,16 @@ int main(void) {
EACH(p) b[(sv.cy + r) * N + sv.cx + c] = 1;
hl = full(b);
}
+#ifdef __TICE__ /* (only when the board or the blocks changed) */
+ static uint8_t seen[N * N + 3];
+ static bool known;
+ if (!known || memcmp(seen, sv.board, N * N) || memcmp(seen + N * N, sv.tray, 3)) {
+ known = true, memcpy(seen, sv.board, N * N), memcpy(seen + N * N, sv.tray, 3);
+ for (int i = 0; i < 3; i++) can[i] = sv.tray[i] >= 0 && placeable(sv.tray[i]);
+ }
+#else
for (int i = 0; i < 3; i++) can[i] = sv.tray[i] >= 0 && placeable(sv.tray[i]);
+#endif
if (shown != sv.score) shown += (sv.score - shown + 5) / 6;
eadk_display_wait_for_vblank();
gfx_render(scene, 0, 0, SCREEN_H);
diff --git a/games/doodlejump/src/main.c b/games/doodlejump/src/main.c
index f565d718..2d788b9d 100644
--- a/games/doodlejump/src/main.c
+++ b/games/doodlejump/src/main.c
@@ -470,6 +470,9 @@ int main(void) {
np_app_begin();
color_t *their_strip = gfx_buf;
gfx_buf = strip;
+#ifdef __TICE__
+ gfx_pure = true; /* (draws only with gfx_*) */
+#endif
now = (uint32_t)eadk_timing_millis();
load();
if (sv.flags & 1) frozen = true, best0 = sv.best;
diff --git a/games/pacman/src/main.c b/games/pacman/src/main.c
index c23de66f..507e941b 100644
--- a/games/pacman/src/main.c
+++ b/games/pacman/src/main.c
@@ -16,6 +16,10 @@
#include "../../common/epsilon_app.h"
#include "../../common/epsilon_files.h"
#include "../../common/np_text.h"
+#ifdef __TICE__
+#include <string.h>
+#include <wchar.h>
+#endif
#ifdef __ELF__ /* app name and API level, for the calculator's installer */
#ifndef NP_PORT /* (another target: platform/port.mk names the app) */
@@ -69,12 +73,24 @@ static void (*scene)(void); /* draws everything in the clip */
static void fill(int x, int y, int w, int h, color c) {
int x0 = x > cx0 ? x : cx0, x1 = x + w < cx1 ? x + w : cx1;
int y0 = y > cy0 ? y : cy0, y1 = y + h < cy1 ? y + h : cy1;
+#ifdef __TICE__ /* (the TI-84 Plus CE: the C library's fill, a row at a time) */
+ if (x1 > x0)
+ for (color *p = buf + (y0 - by) * bw + x0 - bx; y0 < y1; y0++, p += bw) wmemset((wchar_t *)p, (wchar_t)c, (size_t)(x1 - x0));
+ return;
+#endif
for (; y0 < y1; y0++)
for (color *p = buf + (y0 - by) * bw + x0 - bx, *e = p + (x1 - x0); p < e; p++) *p = c;
}
+#ifdef __TICE__ /* (the TI-84 Plus CE: each row of the buffer where refresh() set it, no multiply a pixel) */
+static color *rowp[240];
+static void pset(int x, int y, color c) {
+ if (x >= cx0 && x < cx1 && y >= cy0 && y < cy1) rowp[y][x] = c;
+}
+#else
static void pset(int x, int y, color c) {
if (x >= cx0 && x < cx1 && y >= cy0 && y < cy1) buf[(y - by) * bw + x - bx] = c;
}
+#endif
static bool hits(int x, int y, int w, int h) { return x < cx1 && y < cy1 && x + w > cx0 && y + h > cy0; }
/* redraws a rectangle of the screen, in bands that fit the buffer */
@@ -87,6 +103,9 @@ static void refresh(int x, int y, int w, int h) {
for (int band = BUFPX / w, j = y; j < y + h; j += band) {
int n = y + h - j < band ? y + h - j : band;
bx = cx0 = x, by = cy0 = j, bw = w, cx1 = x + w, cy1 = j + n;
+#ifdef __TICE__
+ for (int k = 0; k < n; k++) rowp[j + k] = buf + k * w - x;
+#endif
scene();
eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)j, (uint16_t)w, (uint16_t)n}, buf);
}
@@ -204,6 +223,29 @@ static void mini(uint32_t v, int cx, int y, color c) {
mouth opening from the top until nothing is left (die up to 32) */
static void pac_r(int x, int y, int d, int open, int die, int r) {
if (!hits(x - r, y - r, 2 * r + 1, 2 * r + 1)) return;
+#ifdef __TICE__ /* (the TI-84 Plus CE: his shapes in the maze worked out once, rows of bits) */
+ static uint16_t shape[4][5][13];
+ static uint32_t made;
+ if (!die && r == 6 && open >= 0 && open <= 4 && d >= 0 && d < 4) {
+ uint16_t *m = shape[d][open];
+ uint32_t bitm = (uint32_t)1 << (d * 5 + open);
+ if (!(made & bitm)) {
+ made |= bitm;
+ for (int j = -6; j <= 6; j++) {
+ unsigned row = 0;
+ for (int i = -6; i <= 6; i++) {
+ int a = i * DX[d] + j * DY[d], p = i * DY[d] - j * DX[d];
+ if (i * i + j * j <= 42 && !(a > 0 && 4 * (p < 0 ? -p : p) < open * a)) row |= 1u << (i + 6);
+ }
+ m[j + 6] = (uint16_t)row;
+ }
+ }
+ for (int j = 0; j < 13; j++)
+ for (unsigned b = m[j], i = 0; b; i++, b >>= 1)
+ if (b & 1) pset(x - 6 + (int)i, y - 6 + j, YELLOW);
+ return;
+ }
+#endif
for (int j = -r; j <= r; j++)
for (int i = -r; i <= r; i++) {
if (i * i + j * j > r * r + r) continue;
@@ -294,13 +336,82 @@ static bool solid(int tx, int ty) {
}
static int tile(int v) { return (v + 21) / 7 - 3; } /* floor(v / 7), for v >= -21 */
+#ifndef __TICE__
static uint64_t wpix[31][28]; /* the wall pixels of each tile */
+#endif
static float hyp(float a, float b) { return __builtin_sqrtf(a * a + b * b); }
/* The walls run half a tile inside the wall tiles: a pixel is on a line when
its distance to the nearest corridor is 3 to 4 pixels (and 5 to 6 for the
second line of the outer wall and the ghost house). Concave corners come
round by themselves; convex ones are rounded around a point inside. */
+#ifdef __TICE__
+/* the TI-84 Plus CE: the same walls in whole numbers (half pixels, squared: no float, no square root), and each
+ tile's rows of wall pixels as bytes for drawing */
+static uint8_t wrow[31][28][7];
+/* a wall tile's rows of pixels, from the corridors around it (o: a bit each, 3 x 3) and the second line: tiles
+ alike are worked out once */
+static void wall_tile(uint8_t *rows, int o, bool dbl) {
+ static struct {
+ uint16_t key;
+ uint8_t rows[7];
+ } memo[64];
+ static int nmemo;
+ uint16_t key = (uint16_t)(o | dbl << 9);
+ for (int i = 0; i < nmemo; i++)
+ if (memo[i].key == key) {
+ memcpy(rows, memo[i].rows, 7);
+ return;
+ }
+ for (int py = 0; py < 7; py++) {
+ int r = 0;
+ for (int px = 0, b = 1; px < 7; px++, b += b) {
+ int u = 2 * px + 1, v = 2 * py + 1, U = 14 - u, V = 14 - v, d = 1 << 20; /* d: the distance squared */
+ int uu = u * u, vv = v * v, UU = U * U, VV = V * V;
+ if (o & 8 && uu < d) d = uu;
+ if (o & 32 && UU < d) d = UU;
+ if (o & 2 && vv < d) d = vv;
+ if (o & 128 && VV < d) d = VV;
+ if (o & 1 && uu + vv < d) d = uu + vv;
+ if (o & 4 && UU + vv < d) d = UU + vv;
+ if (o & 64 && uu + VV < d) d = uu + VV;
+ if (o & 256 && UU + VV < d) d = UU + VV;
+ int a = o & 8 ? u : o & 32 ? U : 14, c = o & 2 ? v : o & 128 ? V : 14;
+ bool on;
+ if (a < 14 && c < 14) { /* round, around a point inside: R minus the distance to it */
+ int q = (14 - a) * (14 - a) + (14 - c) * (14 - c);
+ on = (q > 36 && q <= 64) || (dbl && q > 4 && q <= 16);
+ } else {
+ on = (d >= 36 && d < 64) || (dbl && d >= 100 && d < 144);
+ }
+ if (on) r |= b;
+ }
+ rows[py] = (uint8_t)r;
+ }
+ if (nmemo < 64) memo[nmemo].key = key, memcpy(memo[nmemo++].rows, rows, 7);
+}
+static void build_walls(void) {
+ for (int ty = 0; ty < 31; ty++)
+ for (int tx = 0; tx < 28; tx++) {
+ int c = tx < 14 ? tx : 27 - tx;
+ bool out = ((ty >= 10 && ty <= 12) || (ty >= 16 && ty <= 18)) && c < 5;
+ bool house = ty >= 12 && ty <= 16 && c >= 10, inside = house && ty != 12 && ty != 16 && c > 10;
+ bool dbl = !c || !ty || ty == 30 || (ty >= 9 && ty <= 19 && c <= 5) || (ty >= 24 && ty <= 25 && c <= 2) ||
+ (c == 13 && ty <= 4) || house;
+ memset(wrow[ty][tx], 0, 7);
+ if (solid(tx, ty) && !out && !inside && !(ty == 12 && c == 13)) {
+ static const int8_t kx[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}, ky[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
+ int o = 0;
+ for (int k = 0, b = 1; k < 9; k++, b += b) o |= solid(tx + kx[k], ty + ky[k]) ? 0 : b;
+ wall_tile(wrow[ty][tx], o, dbl);
+ }
+ }
+ /* the pink door, and the ends of the walls beside it */
+ memset(wrow[12][13], 0, 7), memset(wrow[12][14], 0, 7);
+ wrow[12][13][4] = wrow[12][13][5] = wrow[12][14][4] = wrow[12][14][5] = 127;
+ for (int py = 3; py <= 5; py++) wrow[12][12][py] |= 64, wrow[12][15][py] |= 1;
+}
+#else
static void build_walls(void) {
const float R = 7;
for (int ty = 0; ty < 31; ty++)
@@ -337,6 +448,7 @@ static void build_walls(void) {
wpix[12][12] |= 0x4081ull << 27;
wpix[12][15] |= 0x4081ull << 21;
}
+#endif
/* ------------------------------------------------------------------ the game's state */
enum { PH_START, PH_READY, PH_PLAY, PH_EAT, PH_DIE, PH_CLEAR, PH_OVER, PH_CUT };
@@ -681,6 +793,28 @@ static bool energizers_on;
static void maze_draw(void) {
int x0 = cx0 > MX ? cx0 : MX, x1 = cx1 < MX + MW ? cx1 : MX + MW;
int y0 = cy0 > MY ? cy0 : MY, y1 = cy1 < MY + MH ? cy1 : MY + MH;
+#ifdef __TICE__ /* (the TI-84 Plus CE: the tiles' rows of wall pixels, no 64-bit shift) */
+ static const uint8_t bit[7] = {1, 2, 4, 8, 16, 32, 64};
+ static const uint8_t big[7] = {0x1C, 0x3E, 0x7F, 0x7F, 0x7F, 0x3E, 0x1C}; /* an energizer: a * a + b * b <= 10 */
+ for (int y = y0; y < y1; y++) {
+ int my = y - MY, ty = my / 7, py = my - ty * 7, mx = x0 - MX, tx = mx / 7, px = mx - tx * 7;
+ const uint8_t *w = &wrow[ty][tx][py], *k = &cell[ty][tx];
+ bool dots = py == 3 || py == 4;
+ color *p = buf + (y - by) * bw + x0 - bx;
+ for (int x = x0; x < x1; x++, p++) {
+ color c = BLACK;
+ if (*w & bit[px]) {
+ c = ty == 12 && (tx == 13 || tx == 14) ? PINK : wallc;
+ } else if (*k == 1) {
+ if (dots && (px == 3 || px == (tx < 14 ? 4 : 2))) c = PEACH;
+ } else if (*k == 2 && energizers_on) {
+ if (big[py] & bit[px]) c = PEACH;
+ }
+ *p = c;
+ if (++px == 7) px = 0, tx++, w += 7, k++;
+ }
+ }
+#else
for (int y = y0; y < y1; y++) {
int my = y - MY, ty = my / 7, py = my - ty * 7, mx = x0 - MX, tx = mx / 7, px = mx - tx * 7;
color *p = buf + (y - by) * bw + x0 - bx;
@@ -699,6 +833,7 @@ static void maze_draw(void) {
if (++px == 7) px = 0, tx++;
}
}
+#endif
}
/* The intermissions, as in the arcade. 0: Blinky chases Pac-Man away, and a
@@ -1147,6 +1282,83 @@ static bool logo_in(int x, int y) {
}
return false;
}
+#ifdef __TICE__
+/* The TI-84 Plus CE: the same logo from bits worked out once for each opening of the mouth: its yellow (the
+ letters' blocks with their rounded corners, then the C), and the orange ring, the yellow spread by the ring's
+ offsets. Rows of pixels -2..237 (a bit each), -2..36. */
+#define LW 30
+static uint8_t logo_l[39][LW], logo_y[3][39][LW], logo_r[3][39][LW];
+static uint8_t logo_have;
+static void logo_set(uint8_t (*m)[LW], int x, int y) {
+ static const uint8_t bit[8] = {1, 2, 4, 8, 16, 32, 64, 128};
+ if (x >= -2 && x < 238 && y >= -2 && y < 37) m[y + 2][(x + 2) >> 3] |= bit[(x + 2) & 7];
+}
+static void logo_make(int o) {
+ static const uint8_t at[6] = {0, 35, 110, 130, 165, 200}, opens[3] = {0, 2, 4};
+ static const int8_t ring[12][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {2, 0}, {-2, 0},
+ {0, 2}, {0, -2}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
+ if (!logo_have) /* the letters: each block, less the rounded corners where two sides are open */
+ for (int i = 0; i < 6; i++) {
+ int w = i == 2 ? 3 : 6;
+ const uint8_t *g = font["PA-MAN"[i] - 32];
+ for (int by = 0; by < 7; by++)
+ for (int bx = 0; bx < w; bx++) {
+ if (!logo_blk(g, w, bx, by)) continue;
+ int round = 0;
+ for (int q = 0; q < 4; q++)
+ if (!logo_blk(g, w, bx + (q & 1 ? 1 : -1), by) && !logo_blk(g, w, bx, by + (q & 2 ? 1 : -1))) round |= 1 << q;
+ for (int py = -2; py <= 2; py++)
+ for (int px = -2; px <= 2; px++) {
+ bool in = true;
+ for (int q = 0; q < 4; q++) {
+ int qx = q & 1 ? 1 : -1, qy = q & 2 ? 1 : -1, ax = 2 * px + 5 * qx, ay = 2 * py + 5 * qy;
+ if (round >> q & 1 && px * qx >= 0 && py * qy >= 0 && ax * ax + ay * ay > 100) in = false;
+ }
+ int x = at[i] + bx * 5 + px + 2, y = by * 5 + py + 2;
+ if (in && !((x - 87) * (x - 87) + (y - 17) * (y - 17) <= 17 * 18)) logo_set(logo_l, x, y);
+ }
+ }
+ }
+ uint8_t (*Y)[LW] = logo_y[o], (*R)[LW] = logo_r[o];
+ memcpy(Y, logo_l, sizeof logo_l);
+ for (int dy = -18; dy <= 18; dy++) /* Pac-Man, as the C */
+ for (int dx = -18; dx <= 18; dx++)
+ if (dx * dx + dy * dy <= 17 * 18 && !(dx > 0 && 4 * (dy < 0 ? -dy : dy) < opens[o] * dx)) logo_set(Y, 87 + dx, 17 + dy);
+ memset(R, 0, sizeof logo_l);
+ for (int k = 0; k < 12; k++) { /* R |= Y moved by -ring[k] */
+ int dx = ring[k][0], dy = ring[k][1];
+ for (int y = 0; y < 39; y++) {
+ if (y + dy < 0 || y + dy >= 39) continue;
+ const uint8_t *s = Y[y + dy];
+ uint8_t *d = R[y];
+ for (int i = 0; i < LW; i++) {
+ unsigned prev = i ? s[i - 1] : 0, next = i + 1 < LW ? s[i + 1] : 0;
+ d[i] |= (uint8_t)(dx >= 0 ? (s[i] | next << 8) >> dx : (prev | (unsigned)s[i] << 8) >> (8 + dx));
+ }
+ }
+ }
+ for (int y = 0; y < 39; y++)
+ for (int i = 0; i < LW; i++) R[y][i] &= (uint8_t)~Y[y][i];
+ logo_have |= (uint8_t)(1 << o);
+}
+static void logo_draw(void) {
+ static const uint8_t bit[8] = {1, 2, 4, 8, 16, 32, 64, 128};
+ if (!hits(LOGO_X - 2, LOGO_Y - 2, 236, 39)) return;
+ int f = tt >> 3 & 3, o = f == 3 ? 1 : f;
+ if (!(logo_have >> o & 1)) logo_make(o);
+ int x0 = cx0 > LOGO_X - 2 ? cx0 : LOGO_X - 2, x1 = cx1 < LOGO_X + 234 ? cx1 : LOGO_X + 234;
+ int y0 = cy0 > LOGO_Y - 2 ? cy0 : LOGO_Y - 2, y1 = cy1 < LOGO_Y + 37 ? cy1 : LOGO_Y + 37;
+ for (int y = y0; y < y1; y++) {
+ const uint8_t *yr = logo_y[o][y - LOGO_Y + 2], *rr = logo_r[o][y - LOGO_Y + 2];
+ color *p = buf + (y - by) * bw + x0 - bx;
+ for (int x = x0, j = x0 - LOGO_X + 2; x < x1; x++, j++, p++) {
+ uint8_t b = bit[j & 7];
+ if (yr[j >> 3] & b) *p = YELLOW;
+ else if (rr[j >> 3] & b) *p = RGB(0xDE5A00);
+ }
+ }
+}
+#else
static void logo_draw(void) {
static const int8_t ring[12][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {2, 0}, {-2, 0},
{0, 2}, {0, -2}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
@@ -1164,6 +1376,7 @@ static void logo_draw(void) {
buf[(y - by) * bw + x - bx] = c;
}
}
+#endif
static void title_scene(void) {
static const char *const names[4] = {T("-SHADOW \"BLINKY\""), T("-SPEEDY \"PINKY\""), T("-BASHFUL \"INKY\""),
diff --git a/games/snake/src/main.c b/games/snake/src/main.c
index 9b769d39..719431ed 100644
--- a/games/snake/src/main.c
+++ b/games/snake/src/main.c
@@ -68,19 +68,47 @@ static void clip(int x, int y, int w, int h) {
}
static void unclip(void) { clip(0, 0, 320, 240); }
+#ifdef __TICE__ /* the TI-84 Plus CE: its own mixing (the same colours), rows filled by the C library */
+#include <wchar.h>
+#include "../../../platform/ti84ce/ce_draw.h"
+__attribute__((naked)) static color ce_mix(color a, color b, unsigned t) { __asm__(CE_MIX_ASM); }
+__attribute__((naked)) static void ce_blend(color *p, unsigned n, const uint8_t *t) { __asm__(CE_BLEND_ASM); }
+static uint8_t tab_mem[CE_TABLES + 255];
+static void blend_rows(int x0, int x1, int y0, int y1, color c, int a) { /* the pixels there a of the way to c */
+ if (x1 <= x0 || y1 <= y0) return;
+ uint8_t *m = (uint8_t *)(((unsigned)tab_mem + 255) & ~255u);
+ ce_tables(m, c, (unsigned)a);
+ for (int j = y0; j < y1; j++)
+ for (int x = x0; x < x1; x += 256) ce_blend(buf + (j - ry) * rw + x - rx, (unsigned)imin(x1 - x, 256) & 255, m);
+}
+#endif
+
/* f over b; a from 0 (all b) to 32 (all f) */
static color mix(color f, color b, int a) {
+#ifdef __TICE__
+ return ce_mix(b, f, (unsigned)a);
+#endif
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)(y | y >> 16);
}
static void fill(int x, int y, int w, int h, color c) {
+#ifdef __TICE__
+ int i0 = imax(x, kx0), i1 = imin(x + w, kx1);
+ if (i1 > i0)
+ for (int j = imax(y, ky0), j1 = imin(y + h, ky1); j < j1; j++) wmemset((wchar_t *)buf + (j - ry) * rw + i0 - rx, (wchar_t)c, (size_t)(i1 - i0));
+ return;
+#endif
for (int j = imax(y, ky0), j1 = imin(y + h, ky1); j < j1; j++)
for (int i = imax(x, kx0), i1 = imin(x + w, kx1); i < i1; i++) buf[(j - ry) * rw + i - rx] = c;
}
/* the pixels of a rectangle, a of the way to c */
static void wash(int x, int y, int w, int h, color c, int a) {
+#ifdef __TICE__
+ blend_rows(imax(x, kx0), imin(x + w, kx1), imax(y, ky0), imin(y + h, ky1), c, a);
+ return;
+#endif
for (int j = imax(y, ky0), j1 = imin(y + h, ky1); j < j1; j++)
for (int i = imax(x, kx0), i1 = imin(x + w, kx1); i < i1; i++) {
color *p = buf + (j - ry) * rw + i - rx;
@@ -88,9 +116,104 @@ static void wash(int x, int y, int w, int h, color c, int a) {
}
}
+#ifdef __TICE__
+/* The TI-84 Plus CE: a capsule along a row or a column (most of them: the snake, discs) in 16ths of a pixel,
+ no float per pixel: each row's inside is worked out once, a run filled, then its anti-aliased ends. */
+static int isqrt(int v) { /* (rounded down) */
+ int r = 0, b = 1 << 22;
+ while (b > v) b >>= 2;
+ for (; b; b >>= 2)
+ if (v >= r + b) v -= r + b, r = (r >> 1) + b;
+ else r >>= 1;
+ return r;
+}
+static int fx16(float v) { return (int)((v + 512) * 16 + 0.5f) - 8192; }
+/* a pixel d2 (256ths) from the middle, under a half-width e (16ths, with the half pixel): blob()'s colour */
+static void dot(color *p, int d2, int e, color c) {
+ int e2 = e * e;
+ if (d2 >= e2) return;
+ if (shade) {
+ *p = *p == c_light ? c_slight : *p == c_dark ? c_sdark : *p;
+ return;
+ }
+ if (e >= 16 && d2 <= (e - 16) * (e - 16)) {
+ *p = c;
+ return;
+ }
+ int a = (e2 - d2) / e; /* 32 (e - sqrt(d2)), to the second order */
+ a += a * a / (4 * e);
+ *p = mix(c, *p, a > 31 ? 31 : a);
+}
+static void run(color *p, int n, color c) {
+ for (; n > 0; n--, p++) *p = shade ? (*p == c_light ? c_slight : *p == c_dark ? c_sdark : *p) : c;
+}
+/* the row's pixels [x0, x1) whose middles are v = 16 x + 8 - k across the shape, along at the distance a2
+ (256ths): solid where v^2 + a2 <= (e - 16)^2 */
+static void across(color *p, int x0, int x1, int k, int a2, int e, color c) {
+ int ws = e >= 16 && a2 <= (e - 16) * (e - 16) ? isqrt((e - 16) * (e - 16) - a2) : -1;
+ int s0 = ws < 0 ? x1 : imax(x0, (k - 8 - ws + 15) >> 4), s1 = ws < 0 ? x1 : imin(x1, ((k - 8 + ws) >> 4) + 1);
+ if (s1 < s0) s1 = s0;
+ for (int x = x0; x < s0; x++) dot(p + x, (16 * x + 8 - k) * (16 * x + 8 - k) + a2, e, c);
+ run(p + s0, s1 - s0, c);
+ for (int x = s1; x < x1; x++) dot(p + x, (16 * x + 8 - k) * (16 * x + 8 - k) + a2, e, c);
+}
+static void blob_ce(float ax, float ay, float bx, float by, float ra, float rb, color c) {
+ bool col = ax == bx && ay != by; /* along a column */
+ int s0 = fx16(col ? ay : ax), s1 = fx16(col ? by : bx), k = fx16(col ? ax : ay);
+ int e0 = fx16(ra) + 8, e1 = fx16(rb) + 8;
+ if (s0 > s1) {
+ int t = s0;
+ s0 = s1, s1 = t, t = e0, e0 = e1, e1 = t;
+ }
+ int L = s1 - s0, r = imax(e0, e1) + 24;
+ int lo = (s0 - r) >> 4, hi = ((s1 + r) >> 4) + 1, klo = (k - r) >> 4, khi = ((k + r) >> 4) + 1;
+ int x0 = imax(kx0, col ? klo : lo), x1 = imin(kx1, col ? khi : hi);
+ int y0 = imax(ky0, col ? lo : klo), y1 = imin(ky1, col ? hi : khi);
+ if (x0 >= x1) return;
+ for (int y = y0; y < y1; y++) {
+ color *p = buf + (y - ry) * rw - rx;
+ int py = 16 * y + 8;
+ if (col) { /* the row crosses the shape: at u along it */
+ int u = py - s0, a = u < 0 ? -u : u > L ? u - L : 0, e = u <= 0 ? e0 : u >= L ? e1 : e0 + (e1 - e0) * u / L;
+ if (a * a >= e * e) continue;
+ int w = isqrt(e * e - a * a);
+ across(p, imax(x0, (k - 8 - w + 15) >> 4), imin(x1, ((k - 8 + w) >> 4) + 1), k, a * a, e, c);
+ } else if (e0 == e1) { /* the row runs along it, v from its middle line */
+ int v = py - k, v2 = v * v, e = e0;
+ if (v2 >= e * e) continue;
+ int w = isqrt(e * e - v2), ws = e >= 16 && v2 <= (e - 16) * (e - 16) ? isqrt((e - 16) * (e - 16) - v2) : -1;
+ int i0 = imax(x0, (s0 - 8 - w + 15) >> 4), i1 = imin(x1, ((s1 - 8 + w) >> 4) + 1);
+ int f0 = ws < 0 ? i1 : imax(i0, (s0 - 8 - ws + 15) >> 4), f1 = ws < 0 ? i1 : imin(i1, ((s1 - 8 + ws) >> 4) + 1);
+ if (f1 < f0) f1 = f0;
+ for (int x = i0; x < i1; x++) {
+ if (x == f0 && f1 > f0) {
+ run(p + f0, f1 - f0, c);
+ x = f1 - 1;
+ continue;
+ }
+ int u = 16 * x + 8 - s0, a = u < 0 ? -u : u > L ? u - L : 0;
+ dot(p + x, a * a + v2, e, c);
+ }
+ } else { /* (narrowing along a row: each pixel) */
+ int v = py - k, v2 = v * v;
+ for (int x = x0; x < x1; x++) {
+ int u = 16 * x + 8 - s0, a = u < 0 ? -u : u > L ? u - L : 0;
+ dot(p + x, a * a + v2, u <= 0 ? e0 : u >= L ? e1 : e0 + (e1 - e0) * u / L, c);
+ }
+ }
+ }
+}
+#endif
+
/* A capsule from a to b, its radius going from ra to rb, with soft edges.
Everything round is one: discs, the snake, fruit, icons. */
static void blob(float ax, float ay, float bx, float by, float ra, float rb, color c) {
+#ifdef __TICE__
+ if (ax == bx || ay == by) {
+ blob_ce(ax, ay, bx, by, ra, rb, c);
+ return;
+ }
+#endif
float dx = bx - ax, dy = by - ay, l = dx * dx + dy * dy, inv = l > 0 ? 1 / l : 0, r = (ra > rb ? ra : rb) + 2;
int x0 = imax(kx0, (int)((ax < bx ? ax : bx) - r)), x1 = imin(kx1, (int)((ax > bx ? ax : bx) + r));
int y0 = imax(ky0, (int)((ay < by ? ay : by) - r)), y1 = imin(ky1, (int)((ay > by ? ay : by) + r));
@@ -129,6 +252,11 @@ static void tri(int x, int y, int dx, int dy, int a, int s, color c) {
/* darkens everything under the band on top, behind a card */
static void dim(void) {
clip(0, HDR, 320, 240);
+#ifdef __TICE__
+ blend_rows(kx0, kx1, ky0, ky1, 0, 18);
+ unclip();
+ return;
+#endif
for (int j = ky0; j < ky1; j++)
for (color *p = buf + (j - ry) * rw, *e = p + rw; p < e; p++) *p = mix(*p, 0, 14);
unclip();
@@ -443,6 +571,22 @@ static void fieldclip(void) { clip(fx, fy, bw * cs, bh * cs); }
static color checker(int p) { return ((p & 255) + (p >> 8)) & 1 ? c_dark : c_light; }
static void ground(void) {
+#ifdef __TICE__ /* (a run a cell) */
+ for (int j = 0; j < rh; j++) {
+ int y = ry + j, cy = fl(y - fy), cx = fl(rx - fx), e = fx + cx * cs + cs;
+ wchar_t *p = (wchar_t *)buf + j * rw;
+ if (y < HDR || cy < 0 || cy >= bh) {
+ wmemset(p, (wchar_t)(y < HDR ? c_bar : c_edge), (size_t)rw);
+ continue;
+ }
+ for (int x = rx, end = rx + rw; x < end; cx++, e += cs) {
+ int n = imin(e, end) - x;
+ wmemset(p + x - rx, (wchar_t)(cx >= 0 && cx < bw ? ((cx + cy) & 1 ? c_dark : c_light) : c_edge), (size_t)n);
+ x += n;
+ }
+ }
+ return;
+#endif
for (int j = 0; j < rh; j++) {
int y = ry + j, cy = fl(y - fy), cx = fl(rx - fx), e = fx + cx * cs + cs;
bool in = y >= HDR && cy >= 0 && cy < bh;
@@ -1077,10 +1221,28 @@ static void cells(void) {
}
int n = ncur;
for (int i = 0; i < nprev; i++) mark(dprev[i]);
+#ifdef __TICE__ /* (the TI-84 Plus CE: cells next to each other in one go, most of a drawing's work being its own) */
+ static uint16_t left[MAXN];
+ for (int i = 0; i < ncur; i++) left[i] = dcur[i];
+ for (int i = 0; i < ncur; i++) {
+ if (left[i] == 0xFFFF) continue;
+ int x0 = left[i] & 255, y0 = left[i] >> 8, x1 = x0, y1 = y0;
+ for (bool more = true; more;) {
+ more = false;
+ for (int j = i + 1; j < ncur; j++) {
+ int q = left[j], qx = q & 255, qy = q >> 8;
+ if (q == 0xFFFF || qx < x0 - 1 || qx > x1 + 1 || qy < y0 - 1 || qy > y1 + 1) continue;
+ x0 = imin(x0, qx), x1 = imax(x1, qx), y0 = imin(y0, qy), y1 = imax(y1, qy), left[j] = 0xFFFF, more = true;
+ }
+ }
+ area(fx + x0 * cs, fy + y0 * cs, (x1 - x0 + 1) * cs, (y1 - y0 + 1) * cs);
+ }
+#else
for (int i = 0; i < ncur; i++) {
int p = dcur[i];
area(fx + (p & 255) * cs, fy + (p >> 8) * cs, cs, cs);
}
+#endif
for (nprev = 0; nprev < n; nprev++) dprev[nprev] = dcur[nprev]; /* only this frame's own */
}
diff --git a/launcher/src/gfx.c b/launcher/src/gfx.c
index aabbfe2b..e62d6337 100644
--- a/launcher/src/gfx.c
+++ b/launcher/src/gfx.c
@@ -8,6 +8,7 @@ int gfx_clip_x0 = 0, gfx_clip_x1 = SCREEN_W;
void gfx_init(void) { gfx_buf = np_alloc(SCREEN_W * STRIP_H * sizeof(color_t)); }
+#ifndef __TICE__
void gfx_render(gfx_scene_t scene, void *ctx, int y0, int y1) {
for (int y = y0; y < y1; y += STRIP_H) {
gfx_y0 = y;
@@ -25,6 +26,8 @@ color_t gfx_mix(color_t a, color_t b, int t) {
return (color_t)(M | M >> 16);
}
+#endif
+
color_t gfx_rgb(uint32_t c) { return RGB((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF); }
uint32_t gfx_lerp888(uint32_t a, uint32_t b, int t) {
@@ -36,6 +39,9 @@ uint32_t gfx_lerp888(uint32_t a, uint32_t b, int t) {
return out;
}
+#ifdef __TICE__
+#include "../../platform/ti84ce/gfx_ce.c" /* the TI-84 Plus CE's own */
+#else
static bool clip(int *x, int *y, int *w, int *h) {
int x0 = NP_MAX(*x, 0), y0 = NP_MAX(*y, gfx_y0);
int x1 = NP_MIN(*x + *w, SCREEN_W), y1 = NP_MIN(*y + *h, gfx_y1);
@@ -271,6 +277,8 @@ void gfx_corners(int x, int y, int w, int h, int r, color_t c) {
}
}
+#endif
+
void gfx_icon(const np_icon_t *icon, int x, int y, color_t c, int a) { gfx_mask(icon->data, icon->w, icon->h, x, y, c, a); }
static const np_glyph_t *glyph(const np_font_t *f, char ch) {
@@ -306,6 +314,12 @@ static void xplot(int x, int y, void *ctx) {
int gfx_text(const np_font_t *f, int x, int baseline, const char *s, color_t c, int a) {
int top = baseline - f->ascent, bottom = baseline + f->descent;
bool visible = bottom > gfx_y0 && top < gfx_y1;
+#ifdef __TICE__
+ if (gfx_listing) { /* in the strips' lists, then only its width */
+ gfx_ce_text(L_TEXT, f, x, baseline, s, c, a);
+ return x + gfx_text_width(f, s);
+ }
+#endif
while (*s) {
uint32_t cp;
const np_glyph_t *g = glyph_at(f, &s, &cp);
@@ -334,10 +348,22 @@ int gfx_text_width(const np_font_t *f, const char *s) {
}
void gfx_text_center(const np_font_t *f, int cx, int baseline, const char *s, color_t c, int a) {
+#ifdef __TICE__
+ if (gfx_listing) {
+ gfx_ce_text(L_TEXTC, f, cx, baseline, s, c, a);
+ return;
+ }
+#endif
gfx_text(f, cx - gfx_text_width(f, s) / 2, baseline, s, c, a);
}
void gfx_text_right(const np_font_t *f, int rx, int baseline, const char *s, color_t c, int a) {
+#ifdef __TICE__
+ if (gfx_listing) {
+ gfx_ce_text(L_TEXTR, f, rx, baseline, s, c, a);
+ return;
+ }
+#endif
gfx_text(f, rx - gfx_text_width(f, s), baseline, s, c, a);
}
diff --git a/launcher/src/gfx.h b/launcher/src/gfx.h
index 96644c8f..2b0e1cee 100644
--- a/launcher/src/gfx.h
+++ b/launcher/src/gfx.h
@@ -13,6 +13,16 @@ extern int gfx_y0, gfx_y1; /* rows of the strip being drawn */
typedef void (*gfx_scene_t)(void *ctx);
void gfx_init(void); /* takes the strip buffer from the arena */
void gfx_render(gfx_scene_t scene, void *ctx, int y0, int y1);
+#ifdef __TICE__
+/* The TI-84 Plus CE draws a strip only when its list of gfx_* calls changed: the scene runs with gfx_listing
+ * set to list them, once for each strip, or once for all when gfx_pure says it draws only with gfx_*. Drawing of
+ * a scene's own adds what it depends on, or makes its strips drawn every time (platform/ti84ce/gfx_ce.c). */
+extern bool gfx_listing, gfx_pure;
+void gfx_list(const void *p, unsigned bytes);
+void gfx_list_always(void);
+/* fn(a) draws (with gfx_*) in the box x, y, w, h what a[0..n) say (8 at most): one call in the strips' lists */
+void gfx_call(void (*fn)(const int *a), const int *a, int n, int x, int y, int w, int h);
+#endif
color_t gfx_mix(color_t a, color_t b, int t32); /* t32 = 0..32 */
color_t gfx_rgb(uint32_t rgb888);
diff --git a/platform/port.mk b/platform/port.mk
index 435df3d9..b7264c70 100644
--- a/platform/port.mk
+++ b/platform/port.mk
@@ -60,7 +60,7 @@ port: $(PORT_DEPS)
else
port: $(CE_DIR)/bin/$(CE_NAME).8xp
endif
-$(CE_DIR)/bin/$(CE_NAME).8xp: $(PORT_SRC) $(PORT_DEPS) $(wildcard $(NP_PLATFORM)/include/*.h) $(NP_PLATFORM)/ti84ce/np_ce.c \
+$(CE_DIR)/bin/$(CE_NAME).8xp: $(PORT_SRC) $(PORT_DEPS) $(wildcard $(NP_PLATFORM)/include/*.h) $(wildcard $(NP_PLATFORM)/ti84ce/*.c) \
$(NP_PLATFORM)/ti84ce/check.py
@rm -rf $(CE_DIR)/src && mkdir -p $(CE_DIR)/src
@n=0; for f in $(CE_ALL); do n=$$((n+1)); echo "#include \"$$f\"" > $(CE_DIR)/src/f$$n.c; done
diff --git a/platform/ti84ce/ce_draw.h b/platform/ti84ce/ce_draw.h
new file mode 100644
index 00000000..adbc89ec
--- /dev/null
+++ b/platform/ti84ce/ce_draw.h
@@ -0,0 +1,169 @@
+/* The TI-84 Plus CE's pixel mixing, in the eZ80's own instructions (a C loop costs a few hundred cycles a pixel
+ * there): for launcher/src/gfx.c's drawing (gfx_ce.c) and the games' own on the CE.
+ *
+ * CE_MIX_ASM the body of a naked color_t f(color_t a, color_t b, unsigned t): (a (32 - t) + b t) / 32 for each
+ * of red, green and blue, t 0..32 (the NumWorks's 32-bit mixing, the same results)
+ * CE_BLEND_ASM the body of a naked void f(color_t *p, unsigned n, const uint8_t *tables): n pixels (1..255, 0 for
+ * 256) at p mixed with one colour, through tables ce_tables made (several times faster)
+ * ce_tables(m, c, t) those tables at m (CE_TABLES bytes, 256-aligned) for colour c and level t */
+#ifndef NP_CE_DRAW_H
+#define NP_CE_DRAW_H
+#include <stdint.h>
+#include <string.h>
+
+#define CE_MIX_ASM \
+ "ld iy, 0\n" \
+ "add iy, sp\n" \
+ "ld d, (iy + 9)\n" \
+ "ld a, 32\n" \
+ "sub a, d\n" \
+ "ld e, a\n" \
+ "ld a, (iy + 4)\n" \
+ "rrca\n" \
+ "rrca\n" \
+ "rrca\n" \
+ "and a, 31\n" \
+ "ld b, a\n" \
+ "ld c, e\n" \
+ "mlt bc\n" \
+ "ld a, (iy + 7)\n" \
+ "rrca\n" \
+ "rrca\n" \
+ "rrca\n" \
+ "and a, 31\n" \
+ "ld h, a\n" \
+ "ld l, d\n" \
+ "mlt hl\n" \
+ "add hl, bc\n" \
+ "add hl, hl\n" \
+ "add hl, hl\n" \
+ "add hl, hl\n" /* h = the sum / 32 */ \
+ "ld a, h\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "push af\n" /* red << 3 */ \
+ "ld a, (iy + 3)\n" /* green: 3 bits of each byte */ \
+ "rlca\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "and a, 7\n" \
+ "ld b, a\n" \
+ "ld a, (iy + 4)\n" \
+ "and a, 7\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "or a, b\n" \
+ "ld b, a\n" \
+ "ld c, e\n" \
+ "mlt bc\n" \
+ "ld a, (iy + 6)\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "and a, 7\n" \
+ "ld h, a\n" \
+ "ld a, (iy + 7)\n" \
+ "and a, 7\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "rlca\n" \
+ "or a, h\n" \
+ "ld h, a\n" \
+ "ld l, d\n" \
+ "mlt hl\n" \
+ "add hl, bc\n" \
+ "add hl, hl\n" \
+ "add hl, hl\n" \
+ "add hl, hl\n" /* h = green */ \
+ "ld a, h\n" \
+ "rrca\n" \
+ "rrca\n" \
+ "rrca\n" /* bits 0-2: green >> 3, bits 5-7: green & 7 */ \
+ "ld c, a\n" \
+ "and a, 7\n" \
+ "ld b, a\n" \
+ "pop af\n" \
+ "or a, b\n" \
+ "ld b, a\n" /* b: the high byte */ \
+ "ld a, c\n" \
+ "and a, 224\n" \
+ "ld c, a\n" /* c: green's part of the low byte */ \
+ "push bc\n" \
+ "ld a, (iy + 3)\n" /* blue: the low byte's 5 bits */ \
+ "and a, 31\n" \
+ "ld b, a\n" \
+ "ld c, e\n" \
+ "mlt bc\n" \
+ "ld a, (iy + 6)\n" \
+ "and a, 31\n" \
+ "ld h, a\n" \
+ "ld l, d\n" \
+ "mlt hl\n" \
+ "add hl, bc\n" \
+ "add hl, hl\n" \
+ "add hl, hl\n" \
+ "add hl, hl\n" \
+ "pop bc\n" \
+ "ld a, h\n" \
+ "or a, c\n" \
+ "ld l, a\n" \
+ "ld h, b\n" \
+ "ret\n"
+
+/* five pages: the red of a high byte, the blue of a low byte, the green from each byte (the same for every
+ * colour), then the green mixed (its high byte's part, and at +64 its low byte's) */
+enum { CE_T_R = 0, CE_T_B = 256, CE_T_GHI = 512, CE_T_GLO = 768, CE_T_GH = 1024, CE_T_GL = 1088, CE_TABLES = 1280 };
+
+#define CE_BLEND_ASM \
+ "ld iy, 0\n" \
+ "add iy, sp\n" \
+ "push ix\n" \
+ "ld ix, (iy + 3)\n" \
+ "ld b, (iy + 6)\n" \
+ "ld hl, (iy + 9)\n" \
+ "1:\n" \
+ "ld e, (ix + 0)\n" /* e: the low byte, d: the high byte */ \
+ "ld d, (ix + 1)\n" \
+ "inc h\n" \
+ "inc h\n" \
+ "ld l, d\n" \
+ "ld a, (hl)\n" \
+ "inc h\n" \
+ "ld l, e\n" \
+ "or a, (hl)\n" /* the pixel's green */ \
+ "inc h\n" \
+ "ld l, a\n" \
+ "ld c, (hl)\n" \
+ "set 6, l\n" \
+ "ld a, (hl)\n" \
+ "dec h\n" \
+ "dec h\n" \
+ "dec h\n" \
+ "ld l, e\n" \
+ "or a, (hl)\n" \
+ "ld (ix + 0), a\n" \
+ "dec h\n" \
+ "ld l, d\n" \
+ "ld a, (hl)\n" \
+ "or a, c\n" \
+ "ld (ix + 1), a\n" \
+ "lea ix, ix + 2\n" \
+ "djnz 1b\n" \
+ "pop ix\n" \
+ "ret\n"
+
+static void ce_tables(uint8_t *m, uint16_t c, unsigned t) {
+ if (m[CE_T_GHI + 255] != 56) /* (the green pages, the first time) */
+ for (unsigned v = 0; v < 256; v++) m[CE_T_GHI + v] = (uint8_t)((v & 7) << 3), m[CE_T_GLO + v] = (uint8_t)(v >> 5);
+ unsigned u = 32 - t, cr = (unsigned)(c >> 11) * t, cg = (unsigned)(c >> 5 & 63) * t, cb = (unsigned)(c & 31) * t;
+ for (unsigned v = 0; v < 32; v++, cr += u, cb += u) {
+ memset(m + CE_T_R + v * 8, (int)((cr >> 5) << 3), 8);
+ m[CE_T_B + v] = (uint8_t)(cb >> 5);
+ }
+ for (int k = 32; k < 256; k += 32) memcpy(m + CE_T_B + k, m + CE_T_B, 32);
+ for (unsigned v = 0; v < 64; v++, cg += u)
+ m[CE_T_GH + v] = (uint8_t)(cg >> 5 >> 3), m[CE_T_GL + v] = (uint8_t)((cg >> 5 & 7) << 5);
+}
+#endif
diff --git a/platform/ti84ce/gfx_ce.c b/platform/ti84ce/gfx_ce.c
new file mode 100644
index 00000000..9dd1c012
--- /dev/null
+++ b/platform/ti84ce/gfx_ce.c
@@ -0,0 +1,842 @@
+/* launcher/src/gfx.c's drawing on the TI-84 Plus CE (gfx.c includes this file there).
+ *
+ * The CE runs about two million instructions a second, so a frame can't afford C work on every pixel:
+ * - a strip is drawn and sent only when what the scene draws in it changed. The scene runs once to list its
+ * calls (their arguments, the texts' letters), compared with the strip's list from the frame before; the screen's
+ * rows say which strip they show (np_ce_rows, cleared by anything else drawing there);
+ * - rows are filled by the C library's LDIR copies (wmemset, an overlapping memcpy for patterns);
+ * - colours mix with the eZ80's 8-bit multiplies (ce_mix) or tables, never 32-bit math;
+ * - rounded corners, outlines and shadows measure their distances in a table instead of with sqrtf.
+ * The pictures are the NumWorks's, to a rounding of their anti-aliased edges. */
+#include <stdlib.h>
+#include <sys/lcd.h>
+#include <wchar.h>
+#include "ce_draw.h"
+
+extern uint8_t np_ce_rows[SCREEN_H];
+
+/* ------------------------------------------------------------ colours */
+/* (a * (32 - t) + b * t) / 32 for each of red, green and blue, t = 0..32: gfx_mix's own result */
+#define ce_mix gfx_ce_mix
+__attribute__((naked, used)) color_t gfx_ce_mix(color_t a, color_t b, unsigned t) { __asm__(CE_MIX_ASM); }
+
+color_t gfx_mix(color_t a, color_t b, int t) { return ce_mix(a, b, (unsigned)t); }
+
+/* a run of pixels mixed with one colour: five pages of tables (256-aligned) for each of the last two colours and
+ * levels: the red of a high byte, the blue of a low byte, the green from each byte, then the green mixed (its
+ * high byte's part, and at +64 its low byte's) */
+#define DMAX 48 /* (corners' distances: rounded_box below) */
+static uint8_t *mixtabs[2];
+static unsigned mix_key[2];
+static uint8_t mix_next;
+
+__attribute__((naked)) static void ce_blend(color_t *p, unsigned n, const uint8_t *t) { __asm__(CE_BLEND_ASM); }
+
+static void setup(void);
+static const uint8_t *mixtab(color_t c, unsigned t) {
+ unsigned key = c | t << 16;
+ for (int i = 0; i < 2; i++)
+ if (mixtabs[i] && mix_key[i] == key) return mixtabs[i];
+ int i = mix_next;
+ if (!mixtabs[i]) setup();
+ if (!mixtabs[i]) return 0;
+ mix_next ^= 1;
+ uint8_t *m = mixtabs[i];
+ mix_key[i] = key;
+ ce_tables(m, c, t);
+ return m;
+}
+
+static void span(color_t *p, int n, color_t c) {
+ if (n > 0) wmemset((wchar_t *)p, (wchar_t)c, (size_t)n);
+}
+
+static void span_alpha(color_t *p, int n, color_t c, int t) {
+ if (t >= 32) {
+ span(p, n, c);
+ return;
+ }
+ if (t <= 0 || n <= 0) return;
+ const uint8_t *m = n < 12 ? 0 : mixtab(c, (unsigned)t);
+ if (!m) {
+ for (; n; n--, p++) *p = ce_mix(*p, c, (unsigned)t);
+ return;
+ }
+ for (; n > 0; n -= 256, p += 256) ce_blend(p, (unsigned)(n >= 256 ? 0 : n), m);
+}
+
+/* ------------------------------------------------------------ the strip cache */
+/* Each strip has a list of the calls drawing it (unsigned values: the call's kind and length, its arguments; a
+ * text's letters after them), kept from one frame to the next. A scene lists its calls either for one strip at a
+ * time, then draws the strips whose list changed, or (gfx_pure) for all of them at once: drawing a strip is then
+ * going through its list. When only some calls changed, in place, only the columns they cover (before and now)
+ * are drawn again. */
+#define SLOTS 15 /* one for each 16 rows, by where a strip starts */
+bool gfx_listing, gfx_pure;
+static uint8_t *lists; /* every strip's list in turn: off and cap bytes each (a list longer than its cap is drawn
+ by its scene every time, until the lists are laid out again) */
+static unsigned pool, off[SLOTS], cap[SLOTS], need[SLOTS], list_len[SLOTS];
+typedef struct {
+ uint8_t *at, *end, *old_end;
+ bool changed, whole, full; /* full: every column changed */
+ int x0, x1; /* else the columns that did */
+} cursor_t;
+static cursor_t cur[SLOTS];
+static int list_base = -1; /* the first strip's row */
+static cursor_t *strip_of[SCREEN_H]; /* each row's strip, from there */
+static int gx0, gx1 = SCREEN_W; /* the columns drawn */
+
+enum { L_FILL = 1, L_ALPHA, L_VGRAD, L_RRECT, L_OUTLINE, L_SHADOW, L_IMAGE, L_MASK, L_CORNERS, L_TEXT, L_TEXTC, L_TEXTR, L_CALL };
+
+/* the columns a call draws in (a little more for a text) */
+static void widen(cursor_t *k, const uint8_t *e) {
+ const int *v = (const int *)e + 1;
+ int a, b;
+ switch (e[0]) {
+ case L_FILL:
+ case L_ALPHA:
+ case L_VGRAD:
+ case L_RRECT:
+ case L_OUTLINE:
+ case L_CORNERS:
+ case L_CALL: a = v[0], b = v[0] + v[2]; break;
+ case L_SHADOW: a = v[0] - v[5], b = v[0] + v[2] + v[5]; break;
+ case L_IMAGE: a = v[5], b = v[5] + v[7]; break;
+ case L_MASK: a = v[3], b = v[3] + v[1]; break;
+ case L_TEXT:
+ case L_TEXTC:
+ case L_TEXTR: {
+ char t[256];
+ memcpy(t, v + 6, (size_t)v[5]);
+ t[v[5]] = 0;
+ int w = gfx_text_width((const np_font_t *)v[0], t), kind = e[0];
+ a = kind == L_TEXT ? v[1] : kind == L_TEXTC ? v[1] - w / 2 : v[1] - w;
+ b = a + w + 8, a -= 8;
+ break;
+ }
+ default: k->full = true; return;
+ }
+ if (a < k->x0) k->x0 = a;
+ if (b > k->x1) k->x1 = b;
+}
+
+static void put(cursor_t *k, const void *v, unsigned n) {
+ if (!k->whole) {
+ k->at += n;
+ return;
+ }
+ if ((unsigned)(k->end - k->at) < n) {
+ k->whole = false, k->changed = k->full = true;
+ k->at += n; /* (to know how much it needs) */
+ return;
+ }
+ if (!k->full || !k->changed) {
+ if (memcmp(k->at, v, n)) {
+ if (k->at + n > k->old_end || *(const unsigned *)k->at != *(const unsigned *)v) k->full = true; /* (not in place) */
+ else widen(k, k->at), widen(k, v);
+ k->changed = true;
+ memcpy(k->at, v, n);
+ }
+ } else {
+ memcpy(k->at, v, n);
+ }
+ k->at += n;
+}
+/* a call drawing rows top to bottom: in the list of each strip there (n bytes) */
+static void add(int top, int bottom, const void *v, unsigned n) {
+ top = NP_MAX(top, gfx_y0), bottom = NP_MIN(bottom, gfx_y1);
+ if (top >= bottom) return;
+ cursor_t *k = strip_of[top], *e = strip_of[bottom - 1];
+ for (; k <= e; k++) put(k, v, n);
+}
+void gfx_list(const void *p, unsigned bytes) {
+ unsigned v[16];
+ for (; bytes; p = (const uint8_t *)p + 45) {
+ unsigned n = bytes < 45 ? bytes : 45;
+ v[(n + 2) / 3] = 0;
+ memcpy(v + 1, p, n);
+ v[0] = (unsigned)(3 + (n + 2) / 3 * 3) << 8; /* (kind 0: drawn by the scene, never gone through) */
+ add(gfx_y0, gfx_y1, v, 3 + (n + 2) / 3 * 3);
+ bytes -= n;
+ }
+}
+void gfx_list_always(void) {
+ if (!gfx_listing) return;
+ for (cursor_t *k = strip_of[gfx_y0], *e = strip_of[gfx_y1 - 1]; k <= e; k++)
+ k->whole = false, k->changed = k->full = true;
+}
+#define LIST(top, bottom, kind, ...) \
+ do { \
+ if (gfx_listing) { \
+ if ((top) < gfx_y1 && (bottom) > gfx_y0) { \
+ unsigned v_[] = {0, __VA_ARGS__}; \
+ v_[0] = (unsigned)(kind) | (unsigned)sizeof v_ << 8; \
+ add((top), (bottom), v_, sizeof v_); \
+ } \
+ return; \
+ } \
+ } while (0)
+
+/* fn(a) draws in the box x, y, w, h what a[0..n) say: one call in the lists */
+void gfx_call(void (*fn)(const int *a), const int *a, int n, int x, int y, int w, int h) {
+ if (!gfx_listing || n > 8) {
+ fn(a);
+ return;
+ }
+ if (y >= gfx_y1 || y + h <= gfx_y0) return;
+ unsigned v[7 + 8];
+ v[0] = L_CALL | (unsigned)(7 + n) * 3 << 8, v[1] = (unsigned)x, v[2] = (unsigned)y, v[3] = (unsigned)w;
+ v[4] = (unsigned)h, v[5] = (unsigned)fn, v[6] = (unsigned)n;
+ memcpy(v + 7, a, (size_t)n * sizeof(int));
+ add(y, y + h, v, (unsigned)(7 + n) * 3);
+}
+
+/* a text (gfx_text, gfx_text_center, gfx_text_right) in the lists */
+void gfx_ce_text(int kind, const np_font_t *f, int x, int baseline, const char *s, color_t c, int a) {
+ int top = baseline - f->ascent, bottom = baseline + f->descent;
+ if (top >= gfx_y1 || bottom <= gfx_y0) return;
+ unsigned v[7 + 86], n = (unsigned)strlen(s);
+ if (n > 255) {
+ int y0 = gfx_y0, y1 = gfx_y1;
+ gfx_y0 = NP_MAX(top, y0), gfx_y1 = NP_MIN(bottom, y1);
+ gfx_list_always();
+ gfx_y0 = y0, gfx_y1 = y1;
+ return;
+ }
+ unsigned words = 7 + (n + 2) / 3;
+ v[0] = (unsigned)kind | words * 3 << 8, v[1] = (unsigned)f, v[2] = (unsigned)x, v[3] = (unsigned)baseline, v[4] = c;
+ v[5] = (unsigned)a, v[6] = n;
+ v[words - 1] = 0;
+ memcpy(v + 7, s, n);
+ add(top, bottom, v, words * 3);
+}
+
+/* the strip's list began (y its first row, h its rows) */
+static void begin(int s, int y, int h, gfx_scene_t scene, void *ctx) {
+ cursor_t *k = cur + s;
+ k->at = lists + off[s], k->end = k->at + cap[s];
+ k->old_end = list_len[s] == ~0u ? k->at : k->at + list_len[s];
+ k->changed = k->full = false, k->whole = true, k->x0 = SCREEN_W, k->x1 = 0;
+ const unsigned head[] = {18u << 8, (unsigned)y, (unsigned)h, (unsigned)scene, (unsigned)ctx, (unsigned)gfx_pure};
+ put(k, head, sizeof head);
+}
+
+static void replay(const uint8_t *e, const uint8_t *end);
+/* the strip's list ended: it's drawn again where it changed */
+static void end(gfx_scene_t scene, void *ctx, int s, int y, int h) {
+ cursor_t *k = cur + s;
+ unsigned len = (unsigned)(k->at - (lists + off[s]));
+ need[s] = len;
+ if (!k->whole) len = ~0u;
+ if (len != list_len[s]) k->changed = k->full = true;
+ list_len[s] = len;
+ int r = 0;
+ while (r < h && np_ce_rows[y + r] == s + 1) r++;
+ if (r < h) k->changed = k->full = true; /* (the screen there isn't the strip's) */
+ if (!k->changed) return;
+ int x0 = k->full ? 0 : NP_MAX(k->x0, 0), x1 = k->full ? SCREEN_W : NP_MIN(k->x1, SCREEN_W);
+ if (x0 >= x1) return;
+ gfx_y0 = y, gfx_y1 = y + h, gx0 = x0, gx1 = x1;
+ if (gfx_pure && k->whole) replay(lists + off[s], lists + off[s] + len);
+ else scene(ctx);
+ gx0 = 0, gx1 = SCREEN_W;
+ if (x1 - x0 == SCREEN_W) np_push(0, y, SCREEN_W, h, gfx_buf);
+ else
+ for (color_t *q = (color_t *)lcd_Ram + y * SCREEN_W + x0, *b = gfx_buf + x0, *e = b + h * SCREEN_W; b < e;
+ q += SCREEN_W, b += SCREEN_W)
+ memcpy(q, b, (size_t)(x1 - x0) * 2); /* (as np_push would, a row at a time) */
+ memset(np_ce_rows + y, k->whole ? s + 1 : 0, (size_t)h);
+}
+
+/* a strip's list didn't fit: the room shared again by what each needs, when there's enough */
+static void layout(void) {
+ unsigned total = 0;
+ bool short_ = false;
+ for (int s = 0; s < SLOTS; s++) total += need[s] + need[s] / 8 + 8, short_ |= need[s] > cap[s];
+ if (!short_ || total > pool) return;
+ unsigned spare = (pool - total) / SLOTS, at = 0;
+ for (int s = 0; s < SLOTS; s++) {
+ off[s] = at, cap[s] = need[s] + need[s] / 8 + 8 + spare, at += cap[s];
+ list_len[s] = ~0u; /* (drawn again) */
+ }
+}
+
+static bool dist_upto(int n);
+static void grads_alloc(void);
+/* the memory it takes, the first time: the tables, then the lists, as much as there is up to 1152 bytes a strip
+ * but 6 KB left for the game (none: every strip is drawn every time) */
+static void setup(void) {
+ static bool ready;
+ if (ready) return;
+ ready = true;
+ for (int i = 0; i < 2; i++) {
+ uint8_t *raw = malloc(CE_TABLES + 255);
+ if (!raw) break;
+ uint8_t *m = (uint8_t *)(((unsigned)raw + 255) & ~255u);
+ m[CE_T_GHI + 255] = 0;
+ mixtabs[i] = m, mix_key[i] = ~0u;
+ }
+ dist_upto(DMAX);
+ grads_alloc();
+ for (pool = SLOTS * 1152; pool >= SLOTS * 144; pool /= 2)
+ if ((lists = malloc(pool))) {
+ void *rest = malloc(6144);
+ if (rest) {
+ free(rest);
+ break;
+ }
+ free(lists), lists = 0;
+ }
+ for (int s = 0; s < SLOTS; s++) off[s] = s * (pool / SLOTS), cap[s] = pool / SLOTS, list_len[s] = ~0u;
+}
+
+void gfx_render(gfx_scene_t scene, void *ctx, int y0, int y1) {
+ setup();
+ if (!lists) {
+ for (int y = y0; y < y1; y += STRIP_H) {
+ gfx_y0 = y, gfx_y1 = NP_MIN(y + STRIP_H, y1);
+ scene(ctx);
+ np_push(0, y, SCREEN_W, gfx_y1 - y, gfx_buf);
+ }
+ return;
+ }
+ if (list_base != y0) {
+ list_base = y0;
+ for (int y = y0; y < SCREEN_H; y++) strip_of[y] = cur + (y0 >> 4) + ((y - y0) >> 4);
+ }
+ if (gfx_pure) { /* the scene once for every strip, then each strip that changed from its list */
+ for (int y = y0; y < y1; y += STRIP_H) begin(y >> 4, y, NP_MIN(STRIP_H, y1 - y), scene, ctx);
+ gfx_y0 = y0, gfx_y1 = y1;
+ gfx_listing = true;
+ scene(ctx);
+ gfx_listing = false;
+ for (int y = y0; y < y1; y += STRIP_H) end(scene, ctx, y >> 4, y, NP_MIN(STRIP_H, y1 - y));
+ layout();
+ return;
+ }
+ for (int y = y0; y < y1; y += STRIP_H) {
+ int s = y >> 4, h = NP_MIN(STRIP_H, y1 - y);
+ begin(s, y, h, scene, ctx);
+ gfx_y0 = y, gfx_y1 = y + h;
+ gfx_listing = true;
+ scene(ctx);
+ gfx_listing = false;
+ end(scene, ctx, s, y, h);
+ }
+ layout();
+}
+
+/* ------------------------------------------------------------ shapes */
+static const unsigned row_at[STRIP_H] = {0, 320, 640, 960, 1280, 1600, 1920, 2240, 2560, 2880, 3200, 3520, 3840,
+ 4160, 4480, 4800};
+static inline color_t *px_at(int x, int y) { return gfx_buf + row_at[y - gfx_y0] + x; }
+
+static bool clip(int *x, int *y, int *w, int *h) {
+ int x0 = NP_MAX(*x, gx0), y0 = NP_MAX(*y, gfx_y0);
+ int x1 = NP_MIN(*x + *w, gx1), y1 = NP_MIN(*y + *h, gfx_y1);
+ if (x1 <= x0 || y1 <= y0) return false;
+ *x = x0, *y = y0, *w = x1 - x0, *h = y1 - y0;
+ return true;
+}
+
+void gfx_fill(int x, int y, int w, int h, color_t c) {
+ LIST(y, y + h, L_FILL, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, c);
+ if (!clip(&x, &y, &w, &h)) return;
+ color_t *p = px_at(x, y);
+ if (w == SCREEN_W) {
+ span(p, w * h, c);
+ return;
+ }
+ if (w == 1)
+ for (; h; h--, p += SCREEN_W) *p = c;
+ else
+ for (; h; h--, p += SCREEN_W) span(p, w, c);
+}
+
+void gfx_fill_alpha(int x, int y, int w, int h, color_t c, int a) {
+ LIST(y, y + h, L_ALPHA, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, c, (unsigned)a);
+ if (!clip(&x, &y, &w, &h)) return;
+ int t = (a + 4) >> 3;
+ for (color_t *p = px_at(x, y); h; h--, p += SCREEN_W) span_alpha(p, w, c, t);
+}
+
+/* Ordered dithering hides the banding of 16-bit colour in soft gradients. */
+static const uint8_t bayer[4][4] = {{0, 8, 2, 10}, {12, 4, 14, 6}, {3, 11, 1, 9}, {15, 7, 13, 5}};
+
+/* a gradient's rows, each 4 pixels (its dither): kept for the last two gradients drawn */
+typedef struct {
+ uint32_t top, bottom;
+ int y0, h0;
+ color_t pat[SCREEN_H][4];
+} grad_t;
+static grad_t *grads[2];
+static void grads_alloc(void) { grads[0] = malloc(sizeof(grad_t)), grads[1] = malloc(sizeof(grad_t)); }
+static uint8_t grad_next;
+
+static void grad_row(color_t *pat, int row, int y0, int h0, uint32_t top, uint32_t bottom) {
+ unsigned t = h0 > 1 ? (unsigned)((row - y0) * 256 / (h0 - 1)) : 0, u = 256 - t;
+ unsigned r = ((unsigned)(top >> 16 & 0xFF) * u + (unsigned)(bottom >> 16 & 0xFF) * t) >> 8;
+ unsigned g = ((unsigned)(top >> 8 & 0xFF) * u + (unsigned)(bottom >> 8 & 0xFF) * t) >> 8;
+ unsigned b = ((unsigned)(top & 0xFF) * u + (unsigned)(bottom & 0xFF) * t) >> 8;
+ for (int i = 0; i < 4; i++) {
+ unsigned d = bayer[row & 3][i];
+ unsigned r5 = NP_MIN(31u, (r + (d >> 1)) >> 3), g6 = NP_MIN(63u, (g + (d >> 2)) >> 2), b5 = NP_MIN(31u, (b + (d >> 1)) >> 3);
+ pat[i] = (color_t)(r5 << 11 | g6 << 5 | b5);
+ }
+}
+
+void gfx_vgrad(int x, int y, int w, int h, uint32_t top, uint32_t bottom) {
+ int x0 = x, y0 = y, h0 = h;
+ LIST(y, y + h, L_VGRAD, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, (unsigned)top, (unsigned)bottom);
+ if (!clip(&x, &y, &w, &h)) return;
+ (void)x0;
+ grad_t *gr = 0;
+ for (int i = 0; i < 2; i++)
+ if (grads[i] && grads[i]->top == top && grads[i]->bottom == bottom && grads[i]->y0 == y0 && grads[i]->h0 == h0)
+ gr = grads[i];
+ if (!gr && y0 >= 0 && y0 + h0 <= SCREEN_H) {
+ if (!grads[grad_next]) grads[grad_next] = malloc(sizeof(grad_t));
+ gr = grads[grad_next];
+ grad_next ^= 1;
+ if (gr) {
+ gr->top = top, gr->bottom = bottom, gr->y0 = y0, gr->h0 = h0;
+ for (int row = y0; row < y0 + h0; row++) grad_row(gr->pat[row], row, y0, h0, top, bottom);
+ }
+ }
+ for (int j = 0; j < h; j++) {
+ int row = y + j;
+ color_t local[4], *pat = local;
+ if (gr) pat = gr->pat[row];
+ else grad_row(local, row, y0, h0, top, bottom);
+ color_t *p = px_at(x, row);
+ for (int i = 0; i < 4 && i < w; i++) p[i] = pat[(x + i) & 3];
+ if (w > 4) memcpy(p + 4, p, (size_t)(w - 4) * 2); /* (the C library's memcpy is a forward LDIR: it repeats them) */
+ }
+}
+
+/* round(256 * the distance from a corner's centre to the middle of the pixel (a, b) from it), a table up to
+ * DMAX pixels from it (made as far as it was needed) */
+static uint16_t *dist;
+static int dist_n;
+static unsigned dist_f(int a, int b) {
+ float fa = a + 0.5f, fb = b + 0.5f;
+ return (unsigned)(256 * __builtin_sqrtf(fa * fa + fb * fb) + 0.5f);
+}
+/* the table up to n, or false (then dist_f) */
+static bool dist_upto(int n) {
+ if (n <= dist_n) return true;
+ if (n > DMAX) return false;
+ if (!dist && !(dist = malloc(DMAX * DMAX * sizeof(uint16_t)))) return false;
+ n = n < 16 ? 16 : n;
+ for (int i = 0; i < n; i++)
+ for (int j = 0; j < n; j++)
+ if (i >= dist_n || j >= dist_n) dist[i * DMAX + j] = (uint16_t)dist_f(i, j);
+ dist_n = n;
+ return true;
+}
+static unsigned dist_at(int a, int b) { return dist_upto(NP_MAX(a, b) + 1) ? dist[a * DMAX + b] : dist_f(a, b); }
+
+/* Coverage (0..32) of a rounded corner's pixel: d its distance from the centre (dist_at), r the radius */
+static int corner_cov(unsigned d, int r) {
+ int c = 128 + 256 * r - (int)d; /* (in 256ths of a pixel) */
+ c = c < 0 ? 0 : c > 256 ? 256 : c;
+ return (c + 4) >> 3;
+}
+
+/* the row of a rounded box's corner a row is in: rows from the top or bottom edge, or -1 between the corners */
+static int corner_row(int j, int h, int r) { return j < r ? j : j >= h - r ? h - 1 - j : -1; }
+
+void gfx_rrect(int x, int y, int w, int h, int r, color_t c, int a) {
+ LIST(y, y + h, L_RRECT, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, (unsigned)r, c, (unsigned)a);
+ if (w <= 0 || h <= 0 || x >= gx1 || x + w <= gx0) return;
+ if (2 * r > w) r = w / 2;
+ if (2 * r > h) r = h / 2;
+ int ya = NP_MAX(y, gfx_y0), yb = NP_MIN(y + h, gfx_y1);
+ int t = (a + 4) >> 3;
+ bool table = dist_upto(r);
+ for (int row = ya; row < yb; row++) {
+ int jj = corner_row(row - y, h, r);
+ int solid0 = x, solid1 = x + w;
+ color_t *p = px_at(0, row);
+ if (jj >= 0) {
+ const uint16_t *d = table ? dist + (r - 1 - jj) * DMAX + r - 1 : 0; /* d[-i]: pixel i of the corner */
+ for (int i = 0; i < r; i++) {
+ int cov = corner_cov(d ? d[-i] : dist_f(r - 1 - jj, r - 1 - i), r);
+ if (cov >= 32) break;
+ int k = cov * t >> 5;
+ if (k > 0) {
+ if (x + i >= gx0 && x + i < gx1) p[x + i] = ce_mix(p[x + i], c, (unsigned)k);
+ int xr = x + w - 1 - i;
+ if (xr >= gx0 && xr < gx1) p[xr] = ce_mix(p[xr], c, (unsigned)k);
+ }
+ solid0 = x + i + 1;
+ solid1 = x + w - i - 1;
+ }
+ }
+ int s0 = NP_MAX(solid0, gx0), s1 = NP_MIN(solid1, gx1);
+ if (s1 > s0) span_alpha(p + s0, s1 - s0, c, t);
+ }
+}
+
+/* Signed distance (256ths of a pixel, negative inside) from the middle of a pixel to a rounded box: qa and qb
+ * are how far the pixel is past the corner's centre, in half pixels (odd numbers) */
+static int box_d(int qa, int qb, int r) {
+ int l;
+ if (qa > 0 && qb > 0) l = (int)dist_at((qa - 1) / 2, (qb - 1) / 2);
+ else l = (qa > qb ? qa : qb) * 128;
+ return l - 256 * r;
+}
+
+/* an outline's level (0..32) at qa, qb (box_d), and its inner box's distance */
+static int ring(int qa, int qb, int r, int ri, int shift, int t, int *d_in) {
+ *d_in = box_d(qa + shift, qb + shift, ri);
+ int co = 128 - box_d(qa, qb, r), ci = 128 - *d_in;
+ co = co < 0 ? 0 : co > 256 ? 256 : co;
+ ci = ci < 0 ? 0 : ci > 256 ? 256 : ci;
+ return ((co - ci) * t + 128) >> 8;
+}
+static void ring_px(color_t *p, int sx, color_t c, int k) {
+ if (k > 0 && sx >= gx0 && sx < gx1) p[sx] = k >= 32 ? c : ce_mix(p[sx], c, (unsigned)k);
+}
+
+void gfx_rrect_outline(int x, int y, int w, int h, int r, int th, color_t c, int a) {
+ LIST(y, y + h, L_OUTLINE, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, (unsigned)r, (unsigned)th, c,
+ (unsigned)a);
+ if (w <= 0 || h <= 0 || x >= gx1 || x + w <= gx0) return;
+ int ya = NP_MAX(y, gfx_y0), yb = NP_MIN(y + h, gfx_y1);
+ int t = (a + 4) >> 3, d_in;
+ int ri = r > th ? r - th : 0, shift = r > th ? 0 : 2 * (th - r); /* the inner box's corner, from the outer's */
+ int m0 = r > th ? r : th; /* from there to its mirror, a row near the top or bottom is the same all along */
+ for (int row = ya; row < yb; row++) {
+ int j = row - y, jj = j < h - 1 - j ? j : h - 1 - j;
+ int qb = 2 * (r - jj) - 1;
+ color_t *p = px_at(0, row);
+ if (jj <= r && w > 2 * m0) {
+ for (int i = 0; i < m0; i++) {
+ int k = ring(2 * (r - i) - 1, qb, r, ri, shift, t, &d_in);
+ ring_px(p, x + i, c, k);
+ ring_px(p, x + w - 1 - i, c, k);
+ }
+ int k = ring(2 * (r - m0) - 1, qb, r, ri, shift, t, &d_in);
+ int s0 = NP_MAX(x + m0, gx0), s1 = NP_MIN(x + w - m0, gx1);
+ if (k > 0 && s1 > s0) span_alpha(p + s0, s1 - s0, c, k);
+ continue;
+ }
+ for (int i = 0; i < w; i++) {
+ int ii = i < w - 1 - i ? i : w - 1 - i;
+ int k = ring(2 * (r - ii) - 1, qb, r, ri, shift, t, &d_in);
+ if (d_in < -128) {
+ if (i < w / 2) i = w - 1 - i - 1; /* the rest up to the mirror pixel is inside */
+ continue;
+ }
+ ring_px(p, x + i, c, k);
+ }
+ }
+}
+
+void gfx_shadow(int x, int y, int w, int h, int r, int blur, int a) {
+ LIST(y - blur, y + h + blur, L_SHADOW, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, (unsigned)r,
+ (unsigned)blur, (unsigned)a);
+ if (blur <= 0 || x - blur >= gx1 || x + w + blur <= gx0) return;
+ int ya = NP_MAX(y - blur, gfx_y0), yb = NP_MIN(y + h + blur, gfx_y1);
+ /* how dark, by the distance in eighths of a pixel */
+ uint8_t dark[256];
+ int n8 = NP_MIN(blur * 8, 256);
+ for (int d = 0; d < n8; d++) {
+ float f = 1 - (d + 0.5f) / 8 / blur;
+ dark[d] = (uint8_t)((int)(f * f * a) >> 3);
+ }
+ int xa = x - blur, xb = x + w + blur;
+ for (int row = ya; row < yb; row++) {
+ int j = row - y, jj = j < h - 1 - j ? j : h - 1 - j; /* (negative outside the box) */
+ int qb = 2 * (r - jj) - 1;
+ color_t *p = px_at(0, row);
+ for (int sx = xa; sx < xb; sx++) {
+ int i = sx - x, ii = i < w - 1 - i ? i : w - 1 - i, qa = 2 * (r - ii) - 1;
+ int d = box_d(qa, qb, r);
+ if (d <= 0) {
+ int mirror = 2 * x + w - 1 - sx; /* inside: the card will cover it */
+ if (mirror > sx) sx = mirror - 1;
+ continue;
+ }
+ if (d >= blur * 256 || (d >> 5) >= n8 || sx < gx0 || sx >= gx1) continue;
+ int k = dark[d >> 5];
+ if (k > 0) p[sx] = ce_mix(p[sx], 0, (unsigned)NP_MIN(k, 32));
+ }
+ }
+}
+
+void gfx_circle(int cx, int cy, int r, color_t c, int a) { gfx_rrect(cx - r, cy - r, 2 * r, 2 * r, r, c, a); }
+
+void gfx_image(const uint8_t *idx, const color_t *pal, int ncolors, int sw, int sh, int dx, int dy, int dw, int dh,
+ int radius, int dim) {
+ LIST(dy, dy + dh, L_IMAGE, (unsigned)idx, (unsigned)pal, (unsigned)ncolors, (unsigned)sw, (unsigned)sh,
+ (unsigned)dx, (unsigned)dy, (unsigned)dw, (unsigned)dh, (unsigned)radius, (unsigned)dim, (unsigned)gfx_clip_x0,
+ (unsigned)gfx_clip_x1);
+ if (dw <= 0 || dh <= 0) return;
+ int ya = NP_MAX(dy, gfx_y0), yb = NP_MIN(dy + dh, gfx_y1);
+ if (ya >= yb || dx >= gx1 || dx + dw <= gx0) return;
+ color_t dimmed[256];
+ if (dim > 0) {
+ int t = (dim + 4) >> 3;
+ for (int i = 0; i < ncolors && i < 256; i++) dimmed[i] = ce_mix(pal[i], 0, (unsigned)t);
+ pal = dimmed;
+ }
+ if (2 * radius > dw) radius = dw / 2;
+ if (2 * radius > dh) radius = dh / 2;
+ uint32_t step = ((uint32_t)sw << 16) / (uint32_t)dw;
+ int x0 = NP_MAX(NP_MAX(dx, gfx_clip_x0), gx0), x1 = NP_MIN(NP_MIN(dx + dw, gfx_clip_x1), gx1);
+ for (int row = ya; row < yb; row++) {
+ int j = row - dy;
+ const uint8_t *src = idx + (uint32_t)(j * sh / dh) * sw;
+ int jj = corner_row(j, dh, radius);
+ int in0 = 0, in1 = dw;
+ color_t *p = px_at(0, row);
+ if (jj >= 0) {
+ for (int i = 0; i < radius; i++) {
+ int cov = corner_cov(dist_at(radius - 1 - i, radius - 1 - jj), radius);
+ if (cov >= 32) break;
+ in0 = i + 1;
+ in1 = dw - i - 1;
+ if (cov <= 0) continue;
+ int xl = dx + i, xr = dx + dw - 1 - i;
+ if (xl >= x0 && xl < x1) p[xl] = ce_mix(p[xl], pal[src[(uint32_t)i * step >> 16]], (unsigned)cov);
+ if (xr >= x0 && xr < x1) p[xr] = ce_mix(p[xr], pal[src[(uint32_t)(dw - 1 - i) * step >> 16]], (unsigned)cov);
+ }
+ }
+ int a0 = NP_MAX(x0, dx + in0), a1 = NP_MIN(x1, dx + in1);
+ uint32_t fx = (uint32_t)(a0 - dx) * step;
+ for (int x = a0; x < a1; x++, fx += step) p[x] = pal[src[fx >> 16]];
+ }
+}
+
+/* A mask (4-bit coverage) in one colour, drawn by ce_mask: its coverage as a mixing level (0..32) for the last
+ * alpha, the colour, and the mixes it made, by level (a generation, the pixel, the mix), kept while the colour is
+ * the same: letters lie on few colours */
+__attribute__((used, aligned(16))) uint8_t gfx_ce_lvl[16];
+__attribute__((used, aligned(256))) uint8_t gfx_ce_mixc[256];
+__attribute__((used)) uint8_t gfx_ce_gen = 1;
+__attribute__((used)) color_t gfx_ce_color;
+static int lvl_a = -1;
+
+/* rows of cols pixels from p (dest_skip bytes to the next row), the coverage from m's nibble odd on (skip nibbles
+ * to the next row) */
+__attribute__((naked)) static void ce_mask(color_t *p, const uint8_t *m, unsigned odd, unsigned cols, unsigned rows,
+ unsigned skip, unsigned dest_skip) {
+ __asm__(
+ "ld iy, 0\n"
+ "add iy, sp\n"
+ "push ix\n"
+ "ld ix, (iy + 3)\n"
+ "ld de, (iy + 6)\n"
+ "ld c, (iy + 9)\n"
+ ".Lce_mask_row:\n"
+ "ld b, (iy + 12)\n"
+ ".Lce_mask_px:\n"
+ "ld a, (de)\n"
+ "bit 0, c\n"
+ "jr z, .Lce_mask_lo\n"
+ "rrca\n"
+ "rrca\n"
+ "rrca\n"
+ "rrca\n"
+ "inc de\n"
+ ".Lce_mask_lo:\n"
+ "and a, 15\n"
+ "jp z, .Lce_mask_next\n"
+ "ld hl, _gfx_ce_lvl\n"
+ "or a, l\n"
+ "ld l, a\n"
+ "ld a, (hl)\n"
+ "or a, a\n"
+ "jp z, .Lce_mask_next\n"
+ "cp a, 32\n"
+ "jp nc, .Lce_mask_full\n"
+ "push bc\n"
+ "push de\n"
+ "add a, a\n"
+ "add a, a\n"
+ "add a, a\n"
+ "ld hl, _gfx_ce_mixc\n"
+ "ld l, a\n"
+ "ld a, (_gfx_ce_gen)\n"
+ "cp a, (hl)\n"
+ "jr nz, .Lce_mask_miss\n"
+ "inc hl\n"
+ "ld a, (ix + 0)\n"
+ "cp a, (hl)\n"
+ "jr nz, .Lce_mask_miss1\n"
+ "inc hl\n"
+ "ld a, (ix + 1)\n"
+ "cp a, (hl)\n"
+ "jr nz, .Lce_mask_miss2\n"
+ "inc hl\n"
+ "ld a, (hl)\n"
+ "ld (ix + 0), a\n"
+ "inc hl\n"
+ "ld a, (hl)\n"
+ "ld (ix + 1), a\n"
+ "jr .Lce_mask_done\n"
+ ".Lce_mask_miss2:\n"
+ "dec hl\n"
+ ".Lce_mask_miss1:\n"
+ "dec hl\n"
+ ".Lce_mask_miss:\n"
+ "push hl\n"
+ "push iy\n"
+ "ld a, l\n"
+ "rrca\n"
+ "rrca\n"
+ "rrca\n"
+ "and a, 31\n"
+ "ld e, a\n"
+ "push de\n"
+ "ld hl, (_gfx_ce_color)\n"
+ "push hl\n"
+ "ld hl, (ix + 0)\n"
+ "push hl\n"
+ "call _gfx_ce_mix\n"
+ "pop de\n"
+ "pop de\n"
+ "pop de\n"
+ "pop iy\n"
+ "ex de, hl\n"
+ "pop hl\n"
+ "ld a, (_gfx_ce_gen)\n"
+ "ld (hl), a\n"
+ "inc hl\n"
+ "ld a, (ix + 0)\n"
+ "ld (hl), a\n"
+ "inc hl\n"
+ "ld a, (ix + 1)\n"
+ "ld (hl), a\n"
+ "inc hl\n"
+ "ld (hl), e\n"
+ "inc hl\n"
+ "ld (hl), d\n"
+ "ld (ix + 0), e\n"
+ "ld (ix + 1), d\n"
+ ".Lce_mask_done:\n"
+ "pop de\n"
+ "pop bc\n"
+ "jp .Lce_mask_next\n"
+ ".Lce_mask_full:\n"
+ "ld hl, (_gfx_ce_color)\n"
+ "ld (ix + 0), l\n"
+ "ld (ix + 1), h\n"
+ ".Lce_mask_next:\n"
+ "ld a, c\n"
+ "xor a, 1\n"
+ "ld c, a\n"
+ "lea ix, ix + 2\n"
+ "dec b\n"
+ "jp nz, .Lce_mask_px\n"
+ "ld a, (iy + 18)\n" /* the next row: skip nibbles on */
+ "add a, c\n"
+ "ld c, a\n"
+ "srl a\n"
+ "or a, a\n"
+ "sbc hl, hl\n"
+ "ld l, a\n"
+ "add hl, de\n"
+ "ex de, hl\n"
+ "ld a, c\n"
+ "and a, 1\n"
+ "ld c, a\n"
+ "push de\n"
+ "ld de, (iy + 21)\n"
+ "add ix, de\n"
+ "pop de\n"
+ "dec (iy + 15)\n"
+ "jp nz, .Lce_mask_row\n"
+ "pop ix\n"
+ "ret\n");
+}
+
+void gfx_mask(const uint8_t *mask, int w, int h, int x, int y, color_t c, int a) {
+ LIST(y, y + h, L_MASK, (unsigned)mask, (unsigned)w, (unsigned)h, (unsigned)x, (unsigned)y, c, (unsigned)a);
+ int ya = NP_MAX(y, gfx_y0), yb = NP_MIN(y + h, gfx_y1);
+ if (ya >= yb) return;
+ int i0 = NP_MAX(x, gx0) - x, i1 = NP_MIN(x + w, gx1) - x;
+ if (i1 <= i0) return;
+ if (lvl_a != a) {
+ lvl_a = a;
+ for (int i = 0; i < 16; i++) gfx_ce_lvl[i] = (uint8_t)NP_MIN((i * a + 60) / 120, 32);
+ }
+ if (gfx_ce_color != c) {
+ gfx_ce_color = c;
+ if (!++gfx_ce_gen) memset(gfx_ce_mixc, 0, sizeof gfx_ce_mixc), gfx_ce_gen = 1;
+ }
+ unsigned n = (unsigned)((ya - y) * w + i0);
+ int cols = i1 - i0;
+ if (cols <= 255 && w - cols <= 254) {
+ ce_mask(px_at(x + i0, ya), mask + (n >> 1), n & 1, (unsigned)cols, (unsigned)(yb - ya), (unsigned)(w - cols),
+ (unsigned)(SCREEN_W - cols) * 2);
+ return;
+ }
+ for (int row = ya; row < yb; row++, n += (unsigned)w) {
+ color_t *p = px_at(x + i0, row);
+ for (int i = 0; i < cols; i++, p++) {
+ unsigned k = n + (unsigned)i, cov = mask[k >> 1] >> (k & 1 ? 4 : 0) & 15, l = gfx_ce_lvl[cov];
+ if (l) *p = l >= 32 ? c : ce_mix(*p, c, l);
+ }
+ }
+}
+
+void gfx_corners(int x, int y, int w, int h, int r, color_t c) {
+ LIST(y, y + h, L_CORNERS, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, (unsigned)r, c);
+ if (x >= gx1 || x + w <= gx0) return;
+ if (2 * r > w) r = w / 2;
+ if (2 * r > h) r = h / 2;
+ int ya = NP_MAX(y, gfx_y0), yb = NP_MIN(y + h, gfx_y1);
+ for (int row = ya; row < yb; row++) {
+ int jj = corner_row(row - y, h, r);
+ if (jj < 0) continue;
+ color_t *p = px_at(0, row);
+ for (int i = 0; i < r; i++) {
+ int cov = corner_cov(dist_at(r - 1 - i, r - 1 - jj), r);
+ if (cov >= 32) break;
+ int xl = x + i, xr = x + w - 1 - i;
+ if (xl >= gx0 && xl < gx1) p[xl] = ce_mix(p[xl], c, (unsigned)(32 - cov));
+ if (xr >= gx0 && xr < gx1) p[xr] = ce_mix(p[xr], c, (unsigned)(32 - cov));
+ }
+ }
+}
+
+/* a strip drawn from its list */
+static void replay(const uint8_t *e, const uint8_t *end) {
+ for (; e < end; e += *(const unsigned *)e >> 8) {
+ const int *v = (const int *)e + 1;
+ if ((e[0] <= L_OUTLINE || e[0] == L_CALL) && (v[0] >= gx1 || v[0] + v[2] <= gx0)) continue; /* (outside) */
+ switch (e[0]) {
+ case L_FILL: gfx_fill(v[0], v[1], v[2], v[3], (color_t)v[4]); break;
+ case L_ALPHA: gfx_fill_alpha(v[0], v[1], v[2], v[3], (color_t)v[4], v[5]); break;
+ case L_VGRAD: gfx_vgrad(v[0], v[1], v[2], v[3], (unsigned)v[4], (unsigned)v[5]); break;
+ case L_RRECT: gfx_rrect(v[0], v[1], v[2], v[3], v[4], (color_t)v[5], v[6]); break;
+ case L_OUTLINE: gfx_rrect_outline(v[0], v[1], v[2], v[3], v[4], v[5], (color_t)v[6], v[7]); break;
+ case L_SHADOW: gfx_shadow(v[0], v[1], v[2], v[3], v[4], v[5], v[6]); break;
+ case L_IMAGE: {
+ int x0 = gfx_clip_x0, x1 = gfx_clip_x1;
+ gfx_clip_x0 = v[11], gfx_clip_x1 = v[12];
+ gfx_image((const uint8_t *)v[0], (const color_t *)v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9], v[10]);
+ gfx_clip_x0 = x0, gfx_clip_x1 = x1;
+ break;
+ }
+ case L_MASK: gfx_mask((const uint8_t *)v[0], v[1], v[2], v[3], v[4], (color_t)v[5], v[6]); break;
+ case L_CORNERS: gfx_corners(v[0], v[1], v[2], v[3], v[4], (color_t)v[5]); break;
+ case L_CALL: ((void (*)(const int *))v[4])(v + 6); break;
+ case L_TEXT:
+ case L_TEXTC:
+ case L_TEXTR: {
+ char t[256];
+ memcpy(t, v + 6, (size_t)v[5]);
+ t[v[5]] = 0;
+ const np_font_t *f = (const np_font_t *)v[0];
+ if (e[0] == L_TEXT) gfx_text(f, v[1], v[2], t, (color_t)v[3], v[4]);
+ else if (e[0] == L_TEXTC) gfx_text_center(f, v[1], v[2], t, (color_t)v[3], v[4]);
+ else gfx_text_right(f, v[1], v[2], t, (color_t)v[3], v[4]);
+ break;
+ }
+ }
+ }
+}
diff --git a/platform/ti84ce/np_ce.c b/platform/ti84ce/np_ce.c
index fda0f3f3..6c8f98cd 100644
--- a/platform/ti84ce/np_ce.c
+++ b/platform/ti84ce/np_ce.c
@@ -16,6 +16,7 @@
#include <sys/timers.h>
#include <ti/screen.h>
#include <time.h>
+#include <wchar.h>
#include "np_port.h"
#ifndef NP_APP_ID
@@ -24,6 +25,10 @@
static uint16_t *const lcd = (uint16_t *)lcd_Ram;
+/* for each row of the screen, the strip of launcher/src/gfx.c's cache it shows (gfx_ce.c), 0 once anything else
+ * drew there */
+uint8_t np_ce_rows[LCD_HEIGHT];
+
/* the part of r on the screen: its width and height (0: none) */
static unsigned clip(eadk_rect_t r, unsigned *h) {
if (r.x >= LCD_WIDTH || r.y >= LCD_HEIGHT) return *h = 0;
@@ -33,6 +38,7 @@ static unsigned clip(eadk_rect_t r, unsigned *h) {
void eadk_display_push_rect(eadk_rect_t r, const eadk_color_t *px) {
unsigned h, w = clip(r, &h);
+ if (w) memset(np_ce_rows + r.y, 0, h);
uint16_t *row = lcd + r.y * LCD_WIDTH + r.x;
if (w == LCD_WIDTH && r.width == LCD_WIDTH) w *= h, h = 1; /* rows of the screen's width: one copy */
for (; w && h; h--, row += LCD_WIDTH, px += r.width) memcpy(row, px, w * 2);
@@ -41,11 +47,12 @@ void eadk_display_push_rect_uniform(eadk_rect_t r, eadk_color_t c) {
unsigned h, w = clip(r, &h);
uint16_t *row = lcd + r.y * LCD_WIDTH + r.x;
if (!w || !h) return;
+ memset(np_ce_rows + r.y, 0, h);
if ((c >> 8) == (c & 0xFF)) { /* black, white...: both bytes alike */
for (; h; h--, row += LCD_WIDTH) memset(row, c & 0xFF, w * 2);
return;
}
- for (unsigned i = 0; i < w; i++) row[i] = c; /* the first row, then copies of it */
+ wmemset((wchar_t *)row, (wchar_t)c, w); /* the first row, then copies of it */
for (uint16_t *p = row + LCD_WIDTH; --h; p += LCD_WIDTH) memcpy(p, row, w * 2);
}
void eadk_display_pull_rect(eadk_rect_t r, eadk_color_t *px) {
@@ -60,7 +67,10 @@ void eadk_display_pull_rect(eadk_rect_t r, eadk_color_t *px) {
}
}
/* the LCD shows its memory as it is drawn (as the NumWorks's does): no wait, every frame counts on the CE */
-__attribute__((noinline)) bool eadk_display_wait_for_vblank(void) { return true; } /* (kept: tools/ce_emu counts frames by it) */
+__attribute__((noinline)) bool eadk_display_wait_for_vblank(void) { /* (kept, a call: tools/ce_emu counts frames by it) */
+ __asm__ volatile("");
+ return true;
+}
/* the NumWorks keys on the CE keypad: (group, bit) for each. 2nd is OK, next to the arrows like the NumWorks's,
* and enter is EXE, at the bottom right like it */