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.

NumBlocks CE: no picture made again while nothing in it changed

Mason3633ff1843954c9
diff --git a/games/numblocks/ce/src/gui.c b/games/numblocks/ce/src/gui.c
index 19c8b6c4..a5dfbb4e 100644
--- a/games/numblocks/ce/src/gui.c
+++ b/games/numblocks/ce/src/gui.c
@@ -22,6 +22,7 @@ static bool full; /* all of it to draw (just opened) */
/* ---------------------------------------------------------------- drawing off screen, then onto it */
static void offscreen(int x, int y, int w, int h, void (*fn)(void)) {
+ render_stale = 1;
if (x < 0) w += x, x = 0;
if (y < 0) h += y, y = 0;
if (x + w > SCREEN_W) w = SCREEN_W - x;
@@ -56,7 +57,7 @@ static int last_slot = -1, last_id = -1;
int xp_level, xp_points; /* experience: the level, the points into it */
int xp_need(int l) { return l >= 30 ? 9 * l - 158 : l >= 15 ? 5 * l - 38 : 2 * l + 7; }
-void hud_draw(void) {
+bool hud_draw(void) {
static uint32_t sig;
/* redrawn when something on it changed */
uint32_t s = (uint32_t)pl.slot * 7 + (uint32_t)pl.health * 131 + (uint32_t)(pl.air / 30) * 1031 + (uint32_t)xp_points * 7919 +
@@ -66,7 +67,7 @@ void hud_draw(void) {
tip_ticks = held()->id ? 40 : 0;
last_id = held()->id, last_slot = pl.slot;
}
- if (s == sig && !hud_dirty) return;
+ if (s == sig && !hud_dirty) return false;
sig = s, hud_dirty = false;
cv = (Canvas){hud_layer, HUD_W, HUD_X, HUD_Y, HUD_W, HUD_H};
memset(hud_layer, 0, HUD_W * HUD_H);
@@ -107,6 +108,7 @@ void hud_draw(void) {
const char *t = msg_ticks > 0 ? msg : tip_ticks > 0 ? (held()->id >= IT0 ? it_label[held()->id - IT0] : blk_label[held()->id]) : 0;
if (t) text_center(SCREEN_W / 2, SCREEN_H - (pl.mode ? 38 : 59 - 9), t, C_WHITE);
canvas_screen();
+ return true;
}
/* ---------------------------------------------------------------- tiles: chests and furnaces */
@@ -605,6 +607,7 @@ static void draw_menu(void) {
/* ---------------------------------------------------------------- opening, input, drawing */
void gui_open(int screen) {
int prev = gui;
+ render_stale = 1;
gui = screen;
sel = 0;
shown = false, full = true;
@@ -625,6 +628,7 @@ void gui_open(int screen) {
}
void gui_close(void) {
+ render_stale = 1;
if (gui >= GUI_INVENTORY && gui <= GUI_CREATIVE) close_container();
else gui = GUI_NONE;
hud_dirty = true;
diff --git a/games/numblocks/ce/src/main.c b/games/numblocks/ce/src/main.c
index 5ff0e384..7bec6658 100644
--- a/games/numblocks/ce/src/main.c
+++ b/games/numblocks/ce/src/main.c
@@ -28,6 +28,7 @@ static int look_held;
#define TITLE_SEED 20151008u /* the title's world */
static void loading(const char *what) {
+ render_stale = 1;
canvas_screen();
dirt_background(0, SCREEN_H);
text_center(SCREEN_W / 2, SCREEN_H / 2 - 4, what, C_WHITE);
@@ -184,10 +185,9 @@ static void play(uint32_t keys, uint32_t pressed) {
gui_open(GUI_PAUSE);
return;
}
- render_frame();
+ bool drawn = render_frame();
plat_poll();
- hud_draw();
- compose(true, true);
+ if (hud_draw() || drawn) compose(true, true);
}
}
diff --git a/games/numblocks/ce/src/nb.h b/games/numblocks/ce/src/nb.h
index a8876a5d..bafabb3a 100644
--- a/games/numblocks/ce/src/nb.h
+++ b/games/numblocks/ce/src/nb.h
@@ -107,7 +107,8 @@ int item_max(int id);
#define RH 120
extern uint8_t *rbuf; /* the picture, RW x RH */
void render_init(void);
-void render_frame(void); /* from the player's eyes */
+bool render_frame(void); /* from the player's eyes (false: the same picture as the last) */
+extern uint8_t render_stale; /* set when rbuf or what it shows changed otherwise */
void render_title(uint16_t yaw); /* the title's backdrop: the world turning */
void render_picture(const uint8_t *p); /* rbuf and the sky's rows as render_title left them, again */
const uint8_t *render_sky(void); /* the sky's rows (RH) */
@@ -126,7 +127,7 @@ extern int gui;
void gui_open(int screen);
void gui_input(uint32_t keys, uint32_t pressed);
void gui_draw(void); /* the open screen, over the picture */
-void hud_draw(void); /* the hotbar and the rest, over the picture */
+bool hud_draw(void); /* the hotbar and the rest, over the picture (false: as it was) */
void gui_tick(void); /* furnaces */
void text(int x, int y, const char *s, uint8_t c); /* Minecraft's font, with its shadow */
int text_width(const char *s);
diff --git a/games/numblocks/ce/src/plat_ce.c b/games/numblocks/ce/src/plat_ce.c
index c075f454..51e46762 100644
--- a/games/numblocks/ce/src/plat_ce.c
+++ b/games/numblocks/ce/src/plat_ce.c
@@ -94,41 +94,43 @@ uint32_t plat_keys(void) {
}
static uint32_t read_keys(void) {
- /* (keypadc scans on its own in continuous mode) */
- uint32_t k = 0;
+ /* (keypadc scans on its own in continuous mode; the K_* bits set a byte at a time: on the eZ80 a 32-bit or and a
+ * shift are calls) */
+ static union { uint32_t k; uint8_t b[4]; } u;
uint8_t g1 = kb_Data[1], g2 = kb_Data[2], g3 = kb_Data[3], g4 = kb_Data[4], g5 = kb_Data[5], g6 = kb_Data[6],
- g7 = kb_Data[7];
- if (g7 & kb_Left) k |= K_LEFT;
- if (g7 & kb_Right) k |= K_RIGHT;
- if (g7 & kb_Up) k |= K_UP;
- if (g7 & kb_Down) k |= K_DOWN;
- if (g4 & kb_Cos) k |= K_FWD;
- if (g4 & kb_LParen) k |= K_BACK;
- if (g3 & kb_Comma) k |= K_SLEFT;
- if (g5 & kb_RParen) k |= K_SRIGHT;
- if (g1 & kb_Yequ) k |= K_JUMP;
- if (g2 & kb_Alpha) k |= K_SNEAK;
- if (g1 & kb_Del) k |= K_SPRINT;
- if (g1 & kb_2nd) k |= K_ATTACK;
- if (g6 & kb_Clear) k |= K_USE;
- if (g5 & kb_Vars) k |= K_INV;
- if (g5 & kb_Chs) k |= K_PAUSE;
- if (g1 & kb_Mode) k |= K_HOME;
- if (g3 & kb_GraphVar) k |= K_DROP;
- if (g2 & kb_Log) k |= K_PICK;
- if (g6 & kb_Enter) k |= K_ENTER;
- if (g3 & kb_1) k |= K_SLOT(0);
- if (g4 & kb_2) k |= K_SLOT(1);
- if (g5 & kb_3) k |= K_SLOT(2);
- if (g3 & kb_4) k |= K_SLOT(3);
- if (g4 & kb_5) k |= K_SLOT(4);
- if (g5 & kb_6) k |= K_SLOT(5);
- if (g3 & kb_7) k |= K_SLOT(6);
- if (g4 & kb_8) k |= K_SLOT(7);
- if (g5 & kb_9) k |= K_SLOT(8);
- if (kb_On) k |= K_HOME;
- return k;
+ g7 = kb_Data[7], b0 = 0, b1 = 0, b2 = 0, b3 = 0;
+ if (g7 & kb_Left) b0 |= 1; /* K_LEFT */
+ if (g7 & kb_Right) b0 |= 2; /* K_RIGHT */
+ if (g7 & kb_Up) b0 |= 4; /* K_UP */
+ if (g7 & kb_Down) b0 |= 8; /* K_DOWN */
+ if (g4 & kb_Cos) b0 |= 16; /* K_FWD */
+ if (g4 & kb_LParen) b0 |= 32; /* K_BACK */
+ if (g3 & kb_Comma) b0 |= 64; /* K_SLEFT */
+ if (g5 & kb_RParen) b0 |= 128; /* K_SRIGHT */
+ if (g1 & kb_Yequ) b1 |= 1; /* K_JUMP */
+ if (g2 & kb_Alpha) b1 |= 2; /* K_SNEAK */
+ if (g1 & kb_Del) b1 |= 4; /* K_SPRINT */
+ if (g1 & kb_2nd) b1 |= 8; /* K_ATTACK */
+ if (g6 & kb_Clear) b1 |= 16; /* K_USE */
+ if (g5 & kb_Vars) b1 |= 32; /* K_INV */
+ if (g5 & kb_Chs) b1 |= 64; /* K_PAUSE */
+ if ((g1 & kb_Mode) || kb_On) b1 |= 128; /* K_HOME */
+ if (g3 & kb_GraphVar) b2 |= 1; /* K_DROP */
+ if (g2 & kb_Log) b2 |= 2; /* K_PICK */
+ if (g6 & kb_Enter) b2 |= 4; /* K_ENTER */
+ if (g3 & kb_1) b2 |= 8; /* K_SLOT(0) .. */
+ if (g4 & kb_2) b2 |= 16;
+ if (g5 & kb_3) b2 |= 32;
+ if (g3 & kb_4) b2 |= 64;
+ if (g4 & kb_5) b2 |= 128;
+ if (g5 & kb_6) b3 |= 1;
+ if (g3 & kb_7) b3 |= 2;
+ if (g4 & kb_8) b3 |= 4;
+ if (g5 & kb_9) b3 |= 8; /* .. K_SLOT(8) */
+ u.b[0] = b0, u.b[1] = b1, u.b[2] = b2, u.b[3] = b3;
+ return u.k;
}
+_Static_assert(K_SLOT1 == 1 << 19 && K_ENTER == 1 << 18 && K_HOME == 1 << 15 && K_SRIGHT == 1 << 7, "the key bits");
uint32_t plat_ms(void) { return (uint32_t)clock() * 125 >> 12; } /* (32768 a second) */
/* (a frame is done: tools/ce_emu counts frames here) */
diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.c
index 3c5bcd1c..faa97540 100644
--- a/games/numblocks/ce/src/render.c
+++ b/games/numblocks/ce/src/render.c
@@ -25,6 +25,7 @@ uint32_t stat_faces, stat_spans, stat_px, stat_cells;
extern unsigned long st_cols, st_list, st_frus, st_cov, st_culled;
#endif
int view_dist = 10;
+uint8_t render_stale = 1; /* rbuf no longer the last picture (its memory used for something else, the world changed) */
uint32_t world_time;
int crack_stage = -1;
const uint8_t *rs_recip, *rs_ratio;
@@ -119,6 +120,7 @@ static uint16_t celestial(void) {
}
void render_sky_palette(void) {
+ render_stale = 1;
uint16_t ang = celestial();
/* World.getSunBrightness and getCelestialLight-ish: the sky's brightness 0.2 at night to 1 by day */
int c = fcos(ang); /* 1 at noon, -1 at midnight */
@@ -756,14 +758,21 @@ static void hand(void) {
}
}
-void render_frame(void) {
+bool render_frame(void) {
fx ey = pl.y + (pl.sneaking ? FX1 * 154 / 100 : FX1 * 162 / 100);
if (pl.hit_face >= 0) crack_x = pl.hit_x, crack_y = pl.hit_y, crack_z = pl.hit_z;
crack_stage = pl.break_need > 0 && pl.break_ticks > 0 ? pl.break_ticks * 10 / pl.break_need : -1;
if (crack_stage > 9) crack_stage = 9;
+ /* the same view as the last picture, nothing changed since: that picture again */
+ static struct { fx x, y, z; int hx, hy, hz; uint16_t yaw; int16_t pitch; int8_t hf, crack; uint8_t swing, held; } v, last;
+ v.x = pl.x, v.y = ey, v.z = pl.z, v.hx = pl.hit_x, v.hy = pl.hit_y, v.hz = pl.hit_z, v.yaw = pl.yaw, v.pitch = pl.pitch;
+ v.hf = (int8_t)pl.hit_face, v.crack = (int8_t)crack_stage, v.swing = (uint8_t)pl.swing, v.held = pl.inv[pl.slot].id;
+ if (!render_stale && !memcmp(&v, &last, sizeof v)) return false;
+ last = v, render_stale = 0;
frame_from(pl.x, ey, pl.z, pl.yaw, pl.pitch);
outline();
hand();
+ return true;
}
/* shade row level, the sky's colours (1..15) darkened too: a 256-byte aligned row (the screens' veil) */
@@ -781,12 +790,14 @@ const uint8_t *render_veil_row(uint8_t level) {
/* a picture kept (render_title's, rbuf then the sky's rows) shown again; the sky's rows */
void render_picture(const uint8_t *p) {
+ render_stale = 1;
memcpy(rbuf, p, RW * RH);
memcpy(sky_row, p + RW * RH, RH);
}
const uint8_t *render_sky(void) { return sky_row; }
void render_title(uint16_t yaw) {
+ render_stale = 1;
crack_stage = -1;
frame_from(pl.x, pl.y + FX1 * 162 / 100, pl.z, yaw, 1200);
}
diff --git a/games/numblocks/ce/src/save.c b/games/numblocks/ce/src/save.c
index 3b867f42..97135300 100644
--- a/games/numblocks/ce/src/save.c
+++ b/games/numblocks/ce/src/save.c
@@ -84,6 +84,7 @@ bool save_world(void) {
h.n_edits = (uint16_t)n_edits;
/* the header and the tiles, then the edits */
uint8_t *buf = rbuf; /* (the 3D view's picture: not drawn while saving) */
+ render_stale = 1;
memcpy(buf, &h, sizeof h);
memcpy(buf + sizeof h, tiles, n_tiles * sizeof(Tile));
char n[8];
diff --git a/games/numblocks/ce/src/world.c b/games/numblocks/ce/src/world.c
index 5555ea15..c8c2bf8c 100644
--- a/games/numblocks/ce/src/world.c
+++ b/games/numblocks/ce/src/world.c
@@ -363,6 +363,7 @@ __attribute__((noinline)) static void col_seed(uint16_t c, uint8_t ys, uint8_t y
/* makes the columns [x0, x1) x [z0, z1) at levels [y0, y1) (cols: their column data too), then their light and
* faces */
static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {
+ render_stale = 1; /* (the light's queues are in rbuf) */
uint8_t ys0 = MOD32[y0];
for (int z = z0; z < z1; z++)
for (int x = x0; x < x1; x++) {
@@ -441,6 +442,7 @@ static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {
/* ---------------------------------------------------------------- changes */
void world_set(int x, int y, int z, int b) {
if (!in_win(x, y, z)) return;
+ render_stale = 1;
uint16_t i = (uint16_t)WI(x, y, z);
int old = wblk[i];
if (old == b) return;