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
Celeste: no more missing walls or black pictures in busy rooms
Mason Chene8c4b37a50a0
diff --git a/games/celeste/src/celeste.h b/games/celeste/src/celeste.hindex e32b4aba..9a05274b 100644--- a/games/celeste/src/celeste.h+++ b/games/celeste/src/celeste.h@@ -199,6 +199,10 @@ void gfx_tex_part(uint16_t tex, float x, float y, int sx, int sy, int w, int h,#define TILE_POOL 1600extern uint8_t g_tile_pool[TILE_POOL]; /* the blocks' tiles (entities.c), which gfx_tiles draws */void gfx_tiles(uint16_t tex, const uint8_t *q, int w, int h, float x, float y, uint16_t tint, uint8_t alpha);+/* Celeste's nine-slice blocks (8x8 cells of a 24x24 texture over w x h, multiples of 8), one command; hollow: no middle */+void gfx_nine(uint16_t tex, float x, float y, int w, int h, bool hollow, uint16_t tint, uint8_t alpha);+/* the part (0, 0, tw, th) of a frame repeated over w x h, the last ones cut: one command */+void gfx_tiled(uint16_t tex, float x, float y, int w, int h, int tw, int th, uint16_t tint, uint8_t alpha);void gfx_rect(float x, float y, float w, float h, uint16_t c, uint8_t alpha);void gfx_hollow_rect(float x, float y, float w, float h, uint16_t c, uint8_t alpha);void gfx_line(float x0, float y0, float x1, float y1, uint16_t c, uint8_t alpha);@@ -209,6 +213,9 @@ typedef void (*StripFn)(uint16_t *strip, int y0, int y1, void *ctx);void gfx_custom(StripFn fn, void *ctx, int y0, int y1);void gfx_keep(bool on); /* the layers that must be drawn (tiles, stylegrounds in front): commands kept for them */void blit_line(uint16_t *strip, int sy0, int sy1, float x0, float y0, float x1, float y1, uint16_t col, uint8_t alpha);+/* gfx_tex_ex drawn into the strip by a StripFn layer (world coordinates; the texture got when the layer was recorded) */+void blit_tex_ex(uint16_t *strip, int sy0, int sy1, uint16_t tex, float x, float y, float ox, float oy, float sx, float sy,+ float rot, uint16_t tint, uint8_t alpha, uint8_t flags);void gfx_camera(float x, float y); /* world position of the view's top left */extern int g_camx, g_camy; /* floored */void gfx_hud(bool on); /* following draws ignore the camera */diff --git a/games/celeste/src/ch5.c b/games/celeste/src/ch5.cindex d16cbf35..557ccdd0 100644--- a/games/celeste/src/ch5.c+++ b/games/celeste/src/ch5.c@@ -851,6 +851,11 @@ static void swap_update(Ent *e) {/* DrawBlockStyle: a nine-slice of 8x8 cells and the middle light */static void swap_style(V2 pos, float w, float h, uint16_t nine, uint16_t mid, uint8_t alpha) {int nw = (int)(w / 8), nh = (int)(h / 8);+ if (nw >= 2 && nh >= 2 && nw * 8 == w && nh * 8 == h) { /* the same cells (none over another), in one command */+ gfx_nine(nine, pos.x, pos.y, (int)w, (int)h, false, 0xFFFF, alpha);+ if (mid != 0xFFFF) draw_centered(mid, pos.x + w / 2, pos.y + h / 2, 0xFFFF, alpha, 1, 1, 0);+ return;+ }gfx_tex_part(nine, pos.x, pos.y, 0, 0, 8, 8, 0, 0xFFFF, alpha);gfx_tex_part(nine, pos.x + w - 8, pos.y, 16, 0, 8, 8, 0, 0xFFFF, alpha);gfx_tex_part(nine, pos.x, pos.y + h - 8, 0, 16, 8, 8, 0, 0xFFFF, alpha);@@ -891,11 +896,8 @@ static void swappath_render(Ent *e) {if (!b->cls || b->cls != &SWAP) return;Swap *s = ST(b, Swap);if (!s->moon) {- uint16_t t = swap_path[s->start.x == s->end.x ? 1 : 0];- for (int i = s->rx; i < s->rx + s->rw; i += 16)- for (int j = s->ry; j < s->ry + s->rh; j += 16)- gfx_tex_part(t, (float)i, (float)j, 0, 0, s->rx + s->rw - i < 16 ? s->rx + s->rw - i : 16,- s->ry + s->rh - j < 16 ? s->ry + s->rh - j : 16, 0, 0xFFFF, 255);+ uint16_t t = swap_path[s->start.x == s->end.x ? 1 : 0]; /* its 16x16 over the move rect, the last ones cut: one command */+ gfx_tiled(t, (float)s->rx, (float)s->ry, s->rw, s->rh, 16, 16, 0xFFFF, 255);}float num = 0.5f * (0.5f + (sinf(p->timer) + 1) * 0.25f);swap_style(v2(s->rx, s->ry), s->rw, s->rh, swap_tex[s->moon][2], 0xFFFF, a8(num));diff --git a/games/celeste/src/ch9.c b/games/celeste/src/ch9.cindex d86615f4..55701c20 100644--- a/games/celeste/src/ch9.c+++ b/games/celeste/src/ch9.c@@ -1247,7 +1247,13 @@ static float fmodp(float x, float m) { return fmodf(fmodf(x, m) + m, m); }static void hd_interior(Ent *e, float bx, float by, int w, int h) {HDoor *d = ST(e, HDoor);gfx_rect(bx, by, (float)w, (float)h, rgb(0x18668F), 255);- for (int pass = 0; pass < 2; pass++) {+ /* the mist takes 26 KB of the texture cache, about all Core leaves: only when there is room for it as it is (made room+ * for, it took the edges', the hearts' and the tiles' every frame, and they and it went missing in turn); tried again+ * a second later */+ static uint16_t mist_tried = 0x8000;+ uint16_t mist = T_objects_heartdoor_mist;+ if (!res_cached(mist) && (uint16_t)(g_res_frame - mist_tried) >= 60) mist_tried = g_res_frame, res_load_frames(&mist, 1, 0xFFFF);+ for (int pass = 0; pass < 2 && res_cached(mist); pass++) {V2 m = pass ? v2(d->mist.y * 1.5f, d->mist.x * 1.5f) : d->mist;for (int i = 0; i < w; i += 160)for (int j = 0; j < h; j += 160) {diff --git a/games/celeste/src/entities.c b/games/celeste/src/entities.cindex c18902d6..80f37fe9 100644--- a/games/celeste/src/entities.c+++ b/games/celeste/src/entities.c@@ -796,11 +796,14 @@ static void zip_render(Ent *e) {if (num3 == num) num = -num;}int cw = (int)(w / 8), ch = (int)(h / 8);- for (int k = 0; k < cw; k++)- for (int l = 0; l < ch; l++) {- int a = k == 0 ? 0 : k != cw - 1 ? 1 : 2, b = l == 0 ? 0 : l != ch - 1 ? 1 : 2;- if (a != 1 || b != 1) gfx_tex_part(z->block, x + k * 8, y + l * 8, a * 8, b * 8, 8, 8, 0, 0xFFFF, 255);- }+ if (((int)w | (int)h) & 7)+ for (int k = 0; k < cw; k++)+ for (int l = 0; l < ch; l++) {+ int a = k == 0 ? 0 : k != cw - 1 ? 1 : 2, b = l == 0 ? 0 : l != ch - 1 ? 1 : 2;+ if (a != 1 || b != 1) gfx_tex_part(z->block, x + k * 8, y + l * 8, a * 8, b * 8, 8, 8, 0, 0xFFFF, 255);+ }+ else+ gfx_nine(z->block, x, y, (int)w, (int)h, true, 0xFFFF, 255); /* the same edges, in one command */gfx_tex(z->light_tex[z->light], x + w / 2 - 4, y, 0, 0xFFFF, 255);}diff --git a/games/celeste/src/gfx.c b/games/celeste/src/gfx.cindex 63a04387..b34c53f9 100644--- a/games/celeste/src/gfx.c+++ b/games/celeste/src/gfx.c@@ -22,7 +22,7 @@ static bool letterbox_dirty = true;static char top_label[24]; /* a line in the top bar (the room's code), redrawn when it changes */static bool top_dirty;-enum { OP_TEX, OP_PART, OP_AFFINE, OP_RECT, OP_LINE, OP_PIXEL, OP_CUSTOM, OP_SCALED, OP_MASK, OP_TILES };+enum { OP_TEX, OP_PART, OP_AFFINE, OP_RECT, OP_LINE, OP_PIXEL, OP_CUSTOM, OP_SCALED, OP_MASK, OP_TILES, OP_GRID };typedef struct {uint8_t op, flags, alpha, extra;@@ -493,6 +493,30 @@ static void blit_tiles(uint16_t *strip, int sy0, int sy1, const Tex *t, int x, i}}+/* gfx_nine's cells (tw 0; th 1: hollow) or gfx_tiled's repeated part (tw x th), w x h at strip-relative (x, y): pixel i of+ * a span comes from column (row) i % tw of the frame, or from the first, middle or last cell's */+static inline int grid_src(int i, int n, int tw) {+ if (tw) return i % tw;+ int cell = i >> 3;+ return (cell == 0 ? 0 : cell == n - 1 ? 16 : 8) + (i & 7);+}+static void blit_grid(uint16_t *strip, int sy0, int sy1, const Tex *t, int x, int y, int w, int h, int tw, int th, uint16_t tint,+ uint8_t alpha) {+ prep(t->pal, tint, alpha, 0);+ int nw = w >> 3, nh = h >> 3, r0 = y > sy0 ? y : sy0, r1 = y + h < sy1 ? y + h : sy1, c0 = x > 0 ? x : 0, c1 = x + w < VIEW_W ? x + w : VIEW_W;+ for (int vy = r0; vy < r1; vy++) {+ int sr = grid_src(vy - y, nh, tw ? th : 0), mid_row = !tw && th && sr >= 8 && sr < 16; /* (hollow: no middle cells) */+ if ((sr -= t->oy) < 0 || sr >= t->h) continue;+ turned_src = NULL, tex_row(t, sr, rowbuf);+ uint16_t *row = strip + (vy - sy0) * VIEW_W;+ for (int vx = c0; vx < c1; vx++) {+ int c = grid_src(vx - x, nw, tw);+ if (mid_row && c >= 8 && c < 16) continue;+ if ((c -= t->ox) >= 0 && c < t->w && rowbuf[c]) plot(row + vx, rowbuf[c]);+ }+ }+}+/* a palette's first 16 colors as drawn (tint folded in) and their alphas (0..256) */void pal_lut(uint16_t pal, uint16_t tint, uint16_t *col, uint16_t *alpha) {int n;@@ -841,6 +865,53 @@ void gfx_tiles(uint16_t tex, const uint8_t *q, int w, int h, float x, float y, uc->flags = (uint8_t)w, c->extra |= (uint8_t)(h << 1), c->a = (uint16_t)(q - g_tile_pool);}+/* gfx_nine and gfx_tiled: one OP_GRID command */+static void grid(uint16_t tex, float x, float y, int w, int h, int tw, int th, uint16_t tint, uint8_t alpha) {+ Tex t;+ if (!tex_get(tex, &t) || !alpha || w <= 0 || h <= 0) return; /* (got first, in view or not, as gfx_tex_part does) */+ int X = sx_(x), Y = sy_(y);+ if (X >= VIEW_W || X + w <= 0) return;+ Cmd *c = add(OP_GRID, Y, Y + h);+ if (!c) return;+ c->tex = tex, c->x = (int16_t)X, c->y = (int16_t)Y, c->tint = tint, c->alpha = alpha;+ c->flags = (uint8_t)tw, c->extra |= (uint8_t)(th << 1), c->a = (uint16_t)w, c->b = (uint16_t)h;+}+/* Celeste's nine-slice blocks in one command (what would be a part for each 8x8 cell): a 24x24 texture's corners, edges+ * and middle over w x h (multiples of 8) at (x, y), a single column (row) taking the first one's cells; hollow: no middle */+void gfx_nine(uint16_t tex, float x, float y, int w, int h, bool hollow, uint16_t tint, uint8_t alpha) {+ if (!((w | h) & 7)) grid(tex, x, y, w, h, 0, hollow, tint, alpha);+}+/* the part (0, 0, tw, th) of a texture's frame repeated over w x h at (x, y), the last ones cut: one command */+void gfx_tiled(uint16_t tex, float x, float y, int w, int h, int tw, int th, uint16_t tint, uint8_t alpha) {+ if (tw > 0 && th > 0 && tw < 256 && th < 128) grid(tex, x, y, w, h, tw, th, tint, alpha);+}++/* gfx_tex_ex (world coordinates) drawn straight into a strip, for StripFn layers: the same pixels as its command, without+ * using up any, or an affine slot (the texture got when the layer is recorded: nothing loads while strips are drawn) */+void blit_tex_ex(uint16_t *strip, int sy0, int sy1, uint16_t tex, float x, float y, float ox, float oy, float sx, float sy,+ float rot, uint16_t tint, uint8_t alpha, uint8_t flags) {+ Tex t;+ if (!alpha || !tex_get(tex, &t)) return;+ int k = t.scale > 1 ? t.scale : 1; /* (textures stored smaller: affine here, as close) */+ if (k == 1 && rot == 0 && (sx == 1 || sx == -1) && (sy == 1 || sy == -1)) { /* gfx_tex */+ uint8_t f = flags;+ float px = x - (sx > 0 ? ox : t.fw - ox), py = y - (sy > 0 ? oy : t.fh - oy);+ if (sx < 0) f ^= GF_FLIPX;+ if (sy < 0) f ^= GF_FLIPY;+ int X = (int)floorf(px + 0.5f) - g_camx + (f & GF_FLIPX ? t.fw - t.ox - t.w : t.ox);+ int Y = (int)floorf(py + 0.5f) - g_camy + (f & GF_FLIPY ? t.fh - t.oy - t.h : t.oy);+ blit_tex(strip, sy0, sy1, &t, X, Y, f, tint, alpha);+ return;+ }+ Affine a;+ a.px = floorf(x + 0.5f) - g_camx, a.py = floorf(y + 0.5f) - g_camy;+ a.ox = ox / k, a.oy = oy / k, a.sx = sx * k, a.sy = sy * k, a.rot = rot;+ a.cx0 = a.cy0 = -32768, a.cx1 = a.cy1 = 32767;+ if (flags & GF_FLIPX) a.sx = -a.sx;+ if (flags & GF_FLIPY) a.sy = -a.sy;+ blit_affine(strip, sy0, sy1, &t, &a, tint, alpha, flags);+}+void gfx_rect(float x, float y, float w, float h, uint16_t col, uint8_t alpha) {if (!alpha) return;int X = sx_(x), Y = sy_(y), W_ = (int)floorf(w + 0.5f), H_ = (int)floorf(h + 0.5f);@@ -947,6 +1018,9 @@ static void replay(int y0, int y1, int pass) {case OP_RECT: fill(g_strip, y0, y1, c->x, c->y, c->a, c->b, c->tint, c->alpha); break;case OP_LINE: line(g_strip, y0, y1, c->x, c->y, (int16_t)c->a, (int16_t)c->b, c->tint, c->alpha); break;case OP_CUSTOM: customs[c->a].fn(g_strip, y0, y1, customs[c->a].ctx); break;+ case OP_GRID:+ if (tex_get(c->tex, &t)) blit_grid(g_strip, y0, y1, &t, c->x, c->y, c->a, c->b, c->flags, c->extra >> 1, c->tint, c->alpha);+ break;case OP_TILES:if (tex_get(c->tex, &t))blit_tiles(g_strip, y0, y1, &t, c->x, c->y, g_tile_pool + c->a, c->flags, c->extra >> 1, c->tint, c->alpha);diff --git a/games/celeste/src/player.c b/games/celeste/src/player.cindex b0de396c..6d5821b2 100644--- a/games/celeste/src/player.c+++ b/games/celeste/src/player.c@@ -632,23 +632,34 @@ static void body_update(void) {if (p->body_step == 5) p->death_effect += DT / 0.834f;}-/* DeathEffect.Draw */-static void death_effect_draw(V2 pos, uint16_t color, float ease) {- uint16_t c2 = ((int)floorf(ease * 10) % 2 == 0) ? color : 0xFFFF;+/* DeathEffect.Draw: a gfx_custom layer (its 40 scaled discs, as affine commands, took 40 of the frame's 48: with anything+ * else turning or scaled, the colors drawn over their black outlines were left out) */+static struct { V2 pos; float ease; uint16_t color; } death_fx;+static void death_effect_strip(uint16_t *strip, int sy0, int sy1, void *ctx) {+ (void)ctx;+ V2 pos = death_fx.pos;+ float ease = death_fx.ease;+ uint16_t c2 = ((int)floorf(ease * 10) % 2 == 0) ? death_fx.color : 0xFFFF;float s = ease < 0.5f ? 0.5f + ease : ease_cube_out(1 - (ease - 0.5f) * 2);for (int pass = 0; pass < 2; pass++)for (int i = 0; i < 8; i++) {V2 v = angle_vec((i / 8.f + ease * 0.25f) * 2 * PI_F, ease_cube_out(ease) * 24);V2 at = v2add(pos, v);if (!pass) {- gfx_tex_ex(T_characters_player_hair00, at.x - 1, at.y, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);- gfx_tex_ex(T_characters_player_hair00, at.x + 1, at.y, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);- gfx_tex_ex(T_characters_player_hair00, at.x, at.y - 1, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);- gfx_tex_ex(T_characters_player_hair00, at.x, at.y + 1, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, T_characters_player_hair00, at.x - 1, at.y, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, T_characters_player_hair00, at.x + 1, at.y, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, T_characters_player_hair00, at.x, at.y - 1, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, T_characters_player_hair00, at.x, at.y + 1, 5, 5, s, s, 0, 0, 255, GF_SILHOUETTE);} else- gfx_tex_ex(T_characters_player_hair00, at.x, at.y, 5, 5, s, s, 0, c2, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, T_characters_player_hair00, at.x, at.y, 5, 5, s, s, 0, c2, 255, GF_SILHOUETTE);}}+static void death_effect_draw(V2 pos, uint16_t color, float ease) {+ Tex t;+ tex_get(T_characters_player_hair00, &t); /* (loaded now) */+ death_fx.pos = pos, death_fx.ease = ease, death_fx.color = color;+ gfx_custom(death_effect_strip, NULL, (int)floorf(pos.y) - 32 - g_camy, (int)ceilf(pos.y) + 33 - g_camy);+}/* ---------------------------------------------------------------- collisions */static bool dream_dash_check(V2 dir);@@ -2700,34 +2711,46 @@ void player_before_down_transition(Player *p) {}/* ---------------------------------------------------------------- rendering */-static void hair_render(void) {+/* PlayerHair.Render: a gfx_custom layer (as affine commands, its scaled nodes and their outlines took up to 25 of the+ * frame's 48: in a busy frame the colors were left out, over black outlines) */+static void hair_strip(uint16_t *strip, int sy0, int sy1, void *ctx) {+ (void)ctx;int hx, hy, fr, carry;bool has;hair_meta(spr_tex(&P->spr), &hx, &hy, &fr, &has, &carry);- if (!has) return;uint16_t col = rgb(hair_rgb);- P->hair[0] = v2(floorf(P->hair[0].x), floorf(P->hair[0].y));- uint16_t bangs = (uint16_t)(T_characters_player_bangs00 + 0);uint16_t bang_tex[3] = {T_characters_player_bangs00, T_characters_player_bangs01, T_characters_player_bangs02};- (void)bangs;int n = P->hair_count;for (int i = 0; i < n; i++) {uint16_t t = i == 0 ? bang_tex[fr < 3 ? fr : 0] : T_characters_player_hair00;float num = 0.25f + (1 - (float)i / n) * 0.75f;float sx = (i == 0 ? (float)P->facing : num) * fabsf(P->spr.sx), sy = num;V2 at = P->hair[i];- gfx_tex_ex(t, at.x - 1, at.y, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);- gfx_tex_ex(t, at.x + 1, at.y, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);- gfx_tex_ex(t, at.x, at.y - 1, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);- gfx_tex_ex(t, at.x, at.y + 1, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, t, at.x - 1, at.y, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, t, at.x + 1, at.y, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, t, at.x, at.y - 1, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, t, at.x, at.y + 1, 5, 5, sx, sy, 0, 0, 255, GF_SILHOUETTE);}for (int i = n - 1; i >= 0; i--) {uint16_t t = i == 0 ? bang_tex[fr < 3 ? fr : 0] : T_characters_player_hair00;float num = 0.25f + (1 - (float)i / n) * 0.75f;float sx = (i == 0 ? (float)P->facing : num) * fabsf(P->spr.sx), sy = num;- gfx_tex_ex(t, P->hair[i].x, P->hair[i].y, 5, 5, sx, sy, 0, col, 255, GF_SILHOUETTE);+ blit_tex_ex(strip, sy0, sy1, t, P->hair[i].x, P->hair[i].y, 5, 5, sx, sy, 0, col, 255, GF_SILHOUETTE);}}+static void hair_render(void) {+ int hx, hy, fr, carry;+ bool has;+ hair_meta(spr_tex(&P->spr), &hx, &hy, &fr, &has, &carry);+ if (!has) return;+ P->hair[0] = v2(floorf(P->hair[0].x), floorf(P->hair[0].y));+ Tex t;+ tex_get(fr == 1 ? T_characters_player_bangs01 : fr == 2 ? T_characters_player_bangs02 : T_characters_player_bangs00, &t);+ tex_get(T_characters_player_hair00, &t); /* (loaded now) */+ float y0 = P->hair[0].y, y1 = y0;+ for (int i = 1; i < P->hair_count; i++) y0 = fminf(y0, P->hair[i].y), y1 = fmaxf(y1, P->hair[i].y);+ gfx_custom(hair_strip, NULL, (int)floorf(y0) - 8 - g_camy, (int)ceilf(y1) + 9 - g_camy);+}static void player_render(Ent *e) {E = e;diff --git a/games/celeste/src/res.c b/games/celeste/src/res.cindex cf387cd9..d9c957a6 100644--- a/games/celeste/src/res.c+++ b/games/celeste/src/res.c@@ -241,21 +241,30 @@ static void compact(void) {hash_rebuild();}-/* frees `need` bytes at the top: what was drawn longest ago goes first (what the rooms want gets 8 frames more), never- * what was drawn this frame; false if that is not enough */-static bool make_room_ex(uint32_t need, bool force) {+/* frees `need` bytes at the top: what was drawn longest ago goes first (what the rooms want gets 8 frames more; of those+ * drawn as long ago, a tile sheet last, the biggest first), never what was drawn this frame; false if that is not enough,+ * and then nothing goes unless `partial` (a texture that could not be loaded used to empty the cache for nothing: what+ * had been drawn came back evicting the rest) */+static bool make_room_ex(uint32_t need, bool force, bool partial) {if (cache_top + need <= cache_limit) return true;- uint32_t freed = 0, want = cache_top + need - cache_limit;+ uint32_t freed = 0, want = cache_top + need - cache_limit, can = 0;+ /* what this frame draws stays while it is drawn, unless a tile sheet needs the room */+#define MAY_GO(e) ((e)->mark && !(g_res_drawing && (e)->used == g_res_frame && (!force || (e)->fmt == TF_RAW4)))+ for (uint32_t at = 0; at < cache_top;) {+ CEnt *e = (CEnt *)(void *)(cache + at);+ at += (sizeof(CEnt) + e->bytes + 3) & ~3u;+ if (MAY_GO(e)) can += (sizeof(CEnt) + e->bytes + 3) & ~3u;+ }+ if (can < want && !partial) return false;while (freed < want) {CEnt *old = NULL;uint32_t oldest = 0;for (uint32_t at = 0; at < cache_top;) {CEnt *e = (CEnt *)(void *)(cache + at);at += (sizeof(CEnt) + e->bytes + 3) & ~3u;- /* what this frame draws stays while it is drawn, unless a tile sheet needs the room */- if (!e->mark || (g_res_drawing && e->used == g_res_frame && (!force || e->fmt == TF_RAW4))) continue;- uint32_t age = (uint32_t)(uint16_t)(g_res_frame - e->used) * 2u + (e->mark == marks_gen ? 0 : 17u);- if (!old || age > oldest) old = e, oldest = age;+ if (!MAY_GO(e)) continue;+ uint32_t age = ((uint32_t)(uint16_t)(g_res_frame - e->used) * 2u + (e->mark == marks_gen ? 0 : 17u)) * 2u + (e->fmt != TF_RAW4);+ if (!old || age > oldest || (age == oldest && e->bytes > old->bytes)) old = e, oldest = age; /* (the biggest: fewer go) */}if (!old) break;old->mark = 0;@@ -263,8 +272,9 @@ static bool make_room_ex(uint32_t need, bool force) {}compact();return cache_top + need <= cache_limit;+#undef MAY_GO}-static bool make_room(uint32_t need) { return make_room_ex(need, false); }+static bool make_room(uint32_t need) { return make_room_ex(need, false, true); } /* (scratch: what it can) *//* RLE of one row of 8-bit pixels (see tools/pack.py rle()); nib: literals two a byte (local colors, TF_NIB) */static uint32_t rle_row(const uint8_t *row, int w, uint8_t *out, bool nib) {@@ -382,7 +392,7 @@ static void start_tex(Reader *r) {uint32_t ms = fmt & TF_NIB ? (1u + h[20] + 1) & ~1u : 0; /* the colors, kept even (the row table) */uint32_t need = rd32(h + 16) + (fmt != TF_RAW4 ? 2 * (hh + 1u) + ms : 0);bool soft = r->soft && r->want[r->cur] != r->hard; /* an animation's other frames: only into free room */- r->skip = soft ? cache_top + sizeof(CEnt) + need + 4 > cache_limit : !make_room_ex(sizeof(CEnt) + need + 4, fmt == TF_RAW4);+ r->skip = soft ? cache_top + sizeof(CEnt) + need + 4 > cache_limit : !make_room_ex(sizeof(CEnt) + need + 4, fmt == TF_RAW4, false);r->e = NULL;if (r->skip) return;CEnt *e = (CEnt *)(void *)(cache + cache_top);@@ -420,7 +430,8 @@ static void reader_sink(const uint8_t *p, uint32_t n, void *ctx) {int done = r->hdr_n < 20 ? 0 : !(r->hdr[14] & TF_NIB) ? 20 : r->hdr_n > 20 ? 21 + r->hdr[20] : 0;if (r->hdr_n == done) {start_tex(r);- if (rd16(r->hdr) * rd16(r->hdr + 2) == 0) finish_tex(r);+ /* (a last one with no room: the rest of the pack is not decoded for nothing) */+ if (rd16(r->hdr) * rd16(r->hdr + 2) == 0 || (r->skip && r->cur == r->n - 1)) finish_tex(r);}continue;}@@ -507,7 +518,9 @@ static bool load_one(uint16_t id) {uint32_t e = tex_entry(id);if (e >> 30 == 1 || e == 0xFFFFFFFF) return false;uint16_t one = id;- load_list(&one, 1, 1);+ /* drawn now: as wanted as the rooms' (marked 1, a tile sheet the room's loading had to let go of went first ever+ * after, every frame, and came back taking the room of what the frame had drawn) */+ load_list(&one, 1, marks_gen);return hash_find(id) >= 0;}/* several (an animation's frames), in one pass: `hard` surely (if it can be), the others if there is