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.

Hollow Knight: HUD (masks, soul orb, geo), upright rotated sprites

Mason Chenc1c638b26300
diff --git a/games/hollowknight/Makefile b/games/hollowknight/Makefile
index 51a8eee9..d16ede5f 100644
--- a/games/hollowknight/Makefile
+++ b/games/hollowknight/Makefile
@@ -9,7 +9,7 @@ CC = arm-none-eabi-gcc
NWLINK ?= npx --yes -- [email protected]
STRIP ?= arm-none-eabi-strip --strip-unneeded
BUILD_DIR = output
-GAME = src/res.c src/tex.c src/room.c src/gfx.c src/phys.c src/anim.c src/hero.c src/camera.c src/game.c src/world.c src/obj.c src/enemy.c src/lzma/LzmaDec.c
+GAME = src/res.c src/tex.c src/room.c src/gfx.c src/phys.c src/anim.c src/hero.c src/camera.c src/game.c src/world.c src/obj.c src/enemy.c src/hud.c src/lzma/LzmaDec.c
SRC = $(GAME) src/plat_eadk.c src/main.c
HEADERS = $(wildcard src/*.h) ../common/epsilon_app.h ../common/epsilon_files.h
diff --git a/games/hollowknight/src/data.bin b/games/hollowknight/src/data.bin
index 985675c9..1093dc72 100644
Binary files a/games/hollowknight/src/data.bin and b/games/hollowknight/src/data.bin differ
diff --git a/games/hollowknight/src/data.h b/games/hollowknight/src/data.h
index af866d1f..4c70b5bd 100644
--- a/games/hollowknight/src/data.h
+++ b/games/hollowknight/src/data.h
@@ -157,10 +157,30 @@
#define CLIP_CHEST_OPEN 100
#define CLIP_CHEST_BURST 101
#define CLIP_CHEST_OPENED 102
-#define CLIP_GEO_SMALL_IDLE 103
-#define CLIP_GEO_SMALL_AIR 104
-#define CLIP_GEO_MED_IDLE 105
-#define CLIP_GEO_MED_AIR 106
-#define CLIP_GEO_LARGE_IDLE 107
-#define CLIP_GEO_LARGE_AIR 108
-#define SPRITE_HERO_LIGHT 431
+#define CLIP_HUD_HEALTH_EMPTY 103
+#define CLIP_HUD_HEALTH_IDLE 104
+#define CLIP_HUD_HEALTH_BREAK 105
+#define CLIP_HUD_HEALTH_REFILL 106
+#define CLIP_HUD_HEALTH_APPEAR 107
+#define CLIP_HUD_BLUE_APPEAR 108
+#define CLIP_HUD_BLUE_IDLE 109
+#define CLIP_HUD_BLUE_BREAK 110
+#define CLIP_HUD_HUD_FRAME 111
+#define CLIP_HUD_HUD_FRAME_IDLE 112
+#define CLIP_HUD_COIN_APPEAR 113
+#define CLIP_HUD_COIN_IDLE 114
+#define CLIP_HUD_COIN_GET 115
+#define CLIP_HUD_SOUL_BURST 116
+#define CLIP_LIQUID_FILL 117
+#define CLIP_LIQUID_IDLE 118
+#define CLIP_LIQUID_SHRINK 119
+#define CLIP_LIQUID_DRAIN 120
+#define CLIP_GEO_SMALL_IDLE 121
+#define CLIP_GEO_SMALL_AIR 122
+#define CLIP_GEO_MED_IDLE 123
+#define CLIP_GEO_MED_AIR 124
+#define CLIP_GEO_LARGE_IDLE 125
+#define CLIP_GEO_LARGE_AIR 126
+#define SPRITE_HERO_LIGHT 534
+#define SPRITE_DIGIT0 715
+#define DIGIT_ADV {0.4182f, 0.4182f, 0.4182f, 0.4182f, 0.4182f, 0.4182f, 0.4182f, 0.4182f, 0.4182f, 0.4182f}
diff --git a/games/hollowknight/src/enemy.c b/games/hollowknight/src/enemy.c
index a264647b..e0c3e0f5 100644
--- a/games/hollowknight/src/enemy.c
+++ b/games/hollowknight/src/enemy.c
@@ -524,7 +524,7 @@ void enemies_draw(void) {
Inst in;
const Ent *d = ent_at(e->ent);
float z = e->mode == EM_CORPSE ? 0.0085f : d->x1;
- sprite_inst(e->anim.sprite, e->body.x, e->body.y, z, e->sx, 1, flash_tint(e, 2 + i % 8), &in);
+ sprite_inst(e->anim.sprite, e->body.x, e->body.y, z, e->sx, 1, flash_tint(e, 2 + i % 6), &in);
gfx_actor(&in, SORT_KEY(0, 0));
}
for (int i = 0; i < MAX_GEO; i++) {
diff --git a/games/hollowknight/src/game.c b/games/hollowknight/src/game.c
index e2451675..451b21e1 100644
--- a/games/hollowknight/src/game.c
+++ b/games/hollowknight/src/game.c
@@ -245,6 +245,7 @@ static void step(uint32_t keys) {
fade_tick();
hero_late_update();
cam_tick();
+ hud_tick();
}
void game_tick(uint32_t keys) {
@@ -260,6 +261,7 @@ void game_tick(uint32_t keys) {
}
void game_draw(void) {
+ hud_draw();
obj_draw();
hero_draw();
gfx_frame();
diff --git a/games/hollowknight/src/game.h b/games/hollowknight/src/game.h
index e4e6ea60..9cff3e3c 100644
--- a/games/hollowknight/src/game.h
+++ b/games/hollowknight/src/game.h
@@ -186,6 +186,11 @@ bool game_enter(int room, float x, float y, bool facing_right);
void game_tick(uint32_t keys); /* 1/50 s */
void game_draw(void);
+/* ---------------------------------------------------------------- the HUD (hud.c) */
+void hud_reset(void);
+void hud_tick(void);
+void hud_draw(void);
+
/* ---------------------------------------------------------------- the camera (camera.c) */
void cam_init(void);
void cam_tick(void);
diff --git a/games/hollowknight/src/gfx.c b/games/hollowknight/src/gfx.c
index 7fa09b2e..ab3821a4 100644
--- a/games/hollowknight/src/gfx.c
+++ b/games/hollowknight/src/gfx.c
@@ -40,8 +40,16 @@ typedef struct {
uint8_t y0, y1;
uint8_t pal;
int8_t cty; /* the tile row whose slots the arena holds */
+ uint8_t clip; /* (HUD: 1 + its clip circle) */
uint16_t ar; /* its tiles (a row of them, or all if turned), in the arena */
} Item;
+
+/* the HUD's clip circles */
+typedef struct {
+ float cx, cy, r; /* screen pixels */
+} HudClip;
+#define MAX_HUD_CLIPS 4
+static HudClip hud_clips[MAX_HUD_CLIPS];
#ifndef ARENA
#define ARENA 2048
#endif
@@ -84,15 +92,21 @@ static void make_luts(void) {
for (int i = 0; i < NPAL; i++) pals[i].key = 0xFFFFFFFF;
}
+static bool grade_off; /* (the HUD's tints: the room's grading not for them) */
+#define HUD_TINT 248 /* (dynamic tints from here on are the HUD's) */
+
static uint32_t grade(float r, float g, float b, float a) {
/* r g b a: 0..1 straight */
int ri = (int)(r * 255 + 0.5f), gi = (int)(g * 255 + 0.5f), bi = (int)(b * 255 + 0.5f);
if (ri > 255) ri = 255;
if (gi > 255) gi = 255;
if (bi > 255) bi = 255;
- float R = lut3[0][ri], G = lut3[1][gi], B = lut3[2][bi];
- float L = 0.2126f * R + 0.7152f * G + 0.0722f * B;
- R = L + (R - L) * room_sat, G = L + (G - L) * room_sat, B = L + (B - L) * room_sat;
+ float R = ri, G = gi, B = bi;
+ if (!grade_off) {
+ R = lut3[0][ri], G = lut3[1][gi], B = lut3[2][bi];
+ float L = 0.2126f * R + 0.7152f * G + 0.0722f * B;
+ R = L + (R - L) * room_sat, G = L + (G - L) * room_sat, B = L + (B - L) * room_sat;
+ }
R = R < 0 ? 0 : R > 255 ? 255 : R;
G = G < 0 ? 0 : G > 255 ? 255 : G;
B = B < 0 ? 0 : B > 255 ? 255 : B;
@@ -155,6 +169,7 @@ static int pal_get(uint16_t tex, uint8_t tint, uint8_t flags, uint32_t alpha) {
Pal *p = &pals[s];
p->key = key, p->frame = pal_frame;
const uint8_t *t = tint_rgba(tint);
+ grade_off = tint >= HUD_TINT;
p->solid = alpha == 255;
float tr = t[0] / 255.0f, tg = t[1] / 255.0f, tb = t[2] / 255.0f, ta = alpha / 255.0f;
if (flags & F_LIT) {
@@ -244,6 +259,7 @@ static const uint32_t *soft_get(uint16_t tex, uint8_t tint, uint8_t flags, uint3
const float *am = g_room.h->ambient;
tr *= am[0], tg *= am[1], tb *= am[2];
}
+ grade_off = tint >= HUD_TINT;
for (int i = 0; i < n; i++) {
const uint8_t *c = (const uint8_t *)&px[i]; /* premultiplied r g b a bytes */
int a = c[3];
@@ -765,6 +781,7 @@ __attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, int
float tr = t[0] / 255.0f, tg = t[1] / 255.0f, tb = t[2] / 255.0f;
if (in->flags & F_LIT) tr *= g_room.h->ambient[0], tg *= g_room.h->ambient[1], tb *= g_room.h->ambient[2];
uint32_t base = c.r->map_off;
+ grade_off = in->tint >= HUD_TINT;
uint32_t col = grade((base & 255) / 255.0f * tr, (base >> 8 & 255) / 255.0f * tg, (base >> 16 & 255) / 255.0f * tb, 1);
crb = col & 0xFF00FF, cg = col & 0xFF00, ta = inst_alpha(in) + 1u;
}
@@ -806,8 +823,19 @@ __attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, int
for (int i = 0; i < c.r->tw; i++) tp[i] = UNRES_SLOT;
}
}
- while (x < it->x1) {
- int e = closed_from(cv, x, it->x1);
+ int xe = it->x1;
+ if (it->clip) {
+ /* (a circle: this row's span of it) */
+ const HudClip *k = &hud_clips[it->clip - 1];
+ float dy = y + 0.5f - k->cy, w2 = k->r * k->r - dy * dy;
+ if (w2 <= 0) continue;
+ float w = sqrtf(w2);
+ int ca = (int)(k->cx - w + 0.5f), cb = (int)(k->cx + w + 0.5f);
+ if (cb < xe) xe = cb;
+ if (ca > x) x = open_from(cv, ca, it->x1);
+ }
+ while (x < xe) {
+ int e = closed_from(cv, x, xe);
COUNT_PX(kind, e - x);
#ifdef HOST
if (kind == 3) COUNT_PX(5, (e - x) * (c.r->fmt == FMT_SOFTA ? 1 : 0) * (m.rot ? 1 : 0));
@@ -819,7 +847,7 @@ __attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, int
case 3: run_soft(&c, y, x, e, soft, crb, cg, ta); break;
default: run_bilinear(&c, y, x, e, &bt); break;
}
- x = open_from(cv, e, it->x1);
+ x = open_from(cv, e, xe);
}
}
}
@@ -864,6 +892,7 @@ static void draw_bg_item(Item *it) {
float tr = t[0] / 255.0f, tg = t[1] / 255.0f, tb = t[2] / 255.0f;
if (in->flags & F_LIT) tr *= g_room.h->ambient[0], tg *= g_room.h->ambient[1], tb *= g_room.h->ambient[2];
uint32_t base = r->map_off;
+ grade_off = in->tint >= HUD_TINT;
uint32_t c = grade((base & 255) / 255.0f * tr, (base >> 8 & 255) / 255.0f * tg, (base >> 16 & 255) / 255.0f * tb, 1);
crb = c & 0xFF00FF, cg = c & 0xFF00, ta = inst_alpha(in) + 1u;
}
@@ -959,16 +988,6 @@ __attribute__((noinline)) static void finish_strip(int sy0, int rows) {
}
#endif
uint16_t *out = (uint16_t *)(void *)arb; /* in place: each 16-bit pixel goes where its 32-bit one was read */
- if (g_screen_fade) {
- /* (the camera fading to black: the light, and the background, dimmed) */
- uint32_t k = 255u - g_screen_fade;
- for (int p = 0; p < NPX; p++) {
- uint32_t rb = arb[p];
- arb[p] = ((rb >> 16) * k / 255) << 16 | ((rb & 0xFFFF) * k / 255);
- ag[p] = (uint16_t)(ag[p] * k / 255);
- trans[p] = (uint8_t)(trans[p] * k / 255);
- }
- }
for (int y = 0; y < rows; y++) {
int sy = sy0 + y;
if (bg_on) {
@@ -1053,6 +1072,26 @@ typedef struct {
static Actor actors[MAX_ACTORS];
static int nactors;
+/* the HUD: in front of all, its own camera's (not graded with the room, not faded) */
+#define MAX_HUD 48
+static Inst hud[MAX_HUD];
+static uint8_t hud_clip_of[MAX_HUD];
+static int nhud;
+#define HUD_Z (-1.342f) /* (where the room's camera sees as much as the HUD's, 8.7107 units half height) */
+
+bool gfx_hud(const Inst *in, int clip) {
+ if (nhud >= MAX_HUD) return false;
+ hud_clip_of[nhud] = (uint8_t)clip;
+ hud[nhud++] = *in;
+ return true;
+}
+
+void gfx_hud_clip(int clip, float x, float y, float r) {
+ /* (HUD units, from the screen's center, to pixels) */
+ float k = FOCAL / (HUD_Z - CAM_Z);
+ hud_clips[clip - 1] = (HudClip){VIEW_W / 2 + x * k, VIEW_H / 2 - y * k, r * k};
+}
+
bool gfx_actor(const Inst *in, uint32_t group) {
if (nactors >= MAX_ACTORS) return false;
/* kept in draw order: sorting layer and order, then far first */
@@ -1063,25 +1102,27 @@ bool gfx_actor(const Inst *in, uint32_t group) {
return true;
}
-static void add_item(const Inst *in, float blur_z) {
+static bool add_item(const Inst *in, float blur_z) {
Item cur;
cur.in = *in;
+ cur.clip = 0;
uint32_t alpha = inst_alpha(in);
- if (!alpha) return; /* (a hidden group, or a clear tint) */
- if (!item_box(&cur.in, &cur)) return;
+ if (!alpha) return false; /* (a hidden group, or a clear tint) */
+ if (!item_box(&cur.in, &cur)) return false;
cur.pal = (uint8_t)pal_get(cur.in.flags & F_SOLID ? TEX_NONE : cur.in.tex, cur.in.tint, cur.in.flags, alpha);
if (cur.in.z * (1.0f / 128) > blur_z) {
arena_take(&cur, false);
if (!bg_on) memset(bgbuf, 0, sizeof bgbuf), bg_on = true;
draw_bg_item(&cur);
- return;
+ return false;
}
if (nitems >= MAX_ITEMS) {
g_gfx_dropped++; /* (never in the game's rooms: checked by tests) */
- return;
+ return false;
}
arena_take(&cur, true);
items[nitems++] = cur;
+ return true;
}
void gfx_frame(void) {
@@ -1108,6 +1149,15 @@ void gfx_frame(void) {
}
while (na < nactors) add_item(&actors[na++].in, blur_z);
nactors = 0;
+ /* the HUD's, in front (HUD units from the screen's center: where the camera is) */
+ int nscene = nitems;
+ for (int i = 0; i < nhud; i++) {
+ Inst in = hud[i];
+ in.ax = (int16_t)(in.ax + (int)lrintf(g_cam_x * 64)), in.ay = (int16_t)(in.ay + (int)lrintf(g_cam_y * 64));
+ in.z = (int16_t)lrintf(HUD_Z * 128);
+ if (add_item(&in, 1e9f)) items[nitems - 1].clip = hud_clip_of[i];
+ }
+ nhud = 0;
g_gfx_items = (uint32_t)nitems;
if (!strip_ready) {
memset(arb, 0, sizeof arb), memset(ag, 0, sizeof ag), memset(trans, 255, sizeof trans);
@@ -1129,8 +1179,20 @@ void gfx_frame(void) {
for (int sy0 = 0; sy0 < VIEW_H; sy0 += STRIP_H) {
int sy1 = sy0 + STRIP_H;
memset(cov, 0, sizeof cov);
- for (int i = nitems - 1; i >= 0; i--)
+ for (int i = nitems - 1; i >= nscene; i--)
if (items[i].y0 < sy1 && items[i].y1 > sy0) draw_item(i, &items[i], sy0, sy1);
+ if (g_screen_fade) {
+ /* (the camera fading to black: a black layer between the HUD and the room) */
+ uint32_t k = 255u - g_screen_fade;
+ for (int p = 0; p < NPX; p++) {
+ uint32_t t = trans[p] * k / 255;
+ if (t < 2 && trans[p] >= 2) t = 0, cover(p / VIEW_W, p % VIEW_W);
+ trans[p] = (uint8_t)t;
+ }
+ }
+ if (g_screen_fade < 255)
+ for (int i = nscene - 1; i >= 0; i--)
+ if (items[i].y0 < sy1 && items[i].y1 > sy0) draw_item(i, &items[i], sy0, sy1);
finish_strip(sy0, STRIP_H);
}
#ifdef HOST
diff --git a/games/hollowknight/src/hk.h b/games/hollowknight/src/hk.h
index 42d57d9c..bb7ec7e4 100644
--- a/games/hollowknight/src/hk.h
+++ b/games/hollowknight/src/hk.h
@@ -194,6 +194,10 @@ void gfx_frame(void); /* draws the room at the camera into the screen
/* a sprite of the Knight, an enemy, an effect... for the next frame, among the room's by its sorting layer and order
* (SORT_KEY), then depth */
bool gfx_actor(const Inst *in, uint32_t group);
+/* the HUD: a sprite in front of all, placed in HUD units from the screen's center (its camera's: 8.7107 units half
+ * height), not graded nor faded with the room (tints from HUD_TINT); clip: 0, or 1 + a circle it is drawn in */
+bool gfx_hud(const Inst *in, int clip);
+void gfx_hud_clip(int clip, float x, float y, float r);
uint8_t gfx_dyn_tint(int slot, uint8_t r, uint8_t g, uint8_t b, uint8_t a); /* a tint (for Inst.tint) of that color */
/* the same, flashing: its colors towards (fr, fg, fb) by amount (SpriteFlash) */
uint8_t gfx_dyn_flash(int slot, uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint8_t fr, uint8_t fg, uint8_t fb, uint8_t amount);
diff --git a/games/hollowknight/src/hud.c b/games/hollowknight/src/hud.c
new file mode 100644
index 00000000..af9a345a
--- /dev/null
+++ b/games/hollowknight/src/hud.c
@@ -0,0 +1,97 @@
+/* The HUD (Hud Canvas, drawn by its own camera): the masks, the soul orb, the geo count. Places in HUD units from the
+ * screen's center. */
+#include <math.h>
+#include "game.h"
+
+#define DT 0.02f
+#define MAX_MASKS 11
+
+/* (Hud Canvas, then Health, Soul Orb, Geo Counter: their places) */
+#define CANVAS_X (-8.71f)
+#define CANVAS_Y 6.6f
+#define HEALTH_X (CANVAS_X + 8.19f)
+#define HEALTH_Y (CANVAS_Y - 7.51f)
+#define ORB_X (CANVAS_X - 2.99f)
+#define ORB_Y (CANVAS_Y - 0.9f)
+#define FILL_X (ORB_X + 1.56f)
+#define FILL_Y (ORB_Y - 0.68f)
+#define LIQUID_BOTTOM_Y (-0.59f) /* (Soul Orb Control: Liquid Bottom Y, Liquid Y Per MP) */
+#define LIQUID_Y_PER_MP 0.0171f
+#define FOCUS_MP 33
+
+static struct {
+ Anim mask[MAX_MASKS];
+ int8_t shown_health; /* what the masks show */
+ Anim frame, liquid, coin;
+ float liquid_grey; /* (Liquid Control: grey while it cannot heal) */
+ bool started;
+} hd;
+
+void hud_reset(void) {
+ memset(&hd, 0, sizeof hd);
+ hd.shown_health = -1;
+ anim_play(&hd.frame, CLIP_HUD_HUD_FRAME_IDLE);
+ anim_play(&hd.liquid, CLIP_LIQUID_IDLE);
+ anim_play(&hd.coin, CLIP_HUD_COIN_IDLE);
+}
+
+void hud_tick(void) {
+ if (!hd.started) hud_reset(), hd.started = true;
+ int health = g_pd.health, max = g_pd.max_health > MAX_MASKS ? MAX_MASKS : g_pd.max_health;
+ if (hd.shown_health < 0) {
+ for (int i = 0; i < max; i++) anim_play(&hd.mask[i], i < health ? CLIP_HUD_HEALTH_IDLE : CLIP_HUD_HEALTH_EMPTY);
+ hd.shown_health = (int8_t)health;
+ }
+ /* (health_display: a mask lost breaks, one got back refills) */
+ while (hd.shown_health > health) anim_play_from_frame(&hd.mask[--hd.shown_health], CLIP_HUD_HEALTH_BREAK, 0);
+ while (hd.shown_health < health && hd.shown_health < max) anim_play_from_frame(&hd.mask[hd.shown_health++], CLIP_HUD_HEALTH_REFILL, 0);
+ for (int i = 0; i < max; i++) {
+ Anim *a = &hd.mask[i];
+ a->events = 0;
+ anim_update(a, DT);
+ if (a->events & ANIM_DONE) anim_play(a, a->clip == CLIP_HUD_HEALTH_BREAK ? CLIP_HUD_HEALTH_EMPTY : CLIP_HUD_HEALTH_IDLE);
+ }
+ anim_update(&hd.frame, DT);
+ anim_update(&hd.liquid, DT);
+ anim_update(&hd.coin, DT);
+ /* EaseColor over 0.2 s, to grey or white */
+ float to = g_pd.mp < FOCUS_MP ? 1 : 0;
+ if (hd.liquid_grey < to) hd.liquid_grey = fminf(to, hd.liquid_grey + DT / 0.2f);
+ else if (hd.liquid_grey > to) hd.liquid_grey = fmaxf(to, hd.liquid_grey - DT / 0.2f);
+}
+
+static void hud_sprite(int sprite, float x, float y, float sx, uint8_t tint, int clip) {
+ Inst in;
+ sprite_inst(sprite, x, y, 0, sx, 1, tint, &in);
+ gfx_hud(&in, clip);
+}
+
+void hud_draw(void) {
+ if (!hd.started) return;
+ uint8_t white = gfx_dyn_tint(8, 255, 255, 255, 255);
+ /* the soul orb: its liquid (in the orb's circle), the frame over it */
+ if (g_pd.mp > 1) {
+ float y = FILL_Y + LIQUID_BOTTOM_Y + g_pd.mp * LIQUID_Y_PER_MP;
+ uint8_t g = (uint8_t)(255 - (255 - 110) * hd.liquid_grey);
+ gfx_hud_clip(1, FILL_X - 2.28f, FILL_Y + 1.32f, 0.97f);
+ hud_sprite(hd.liquid.sprite, FILL_X - 2.23f, y, 1, gfx_dyn_tint(9, g, g, g, 255), 1);
+ }
+ hud_sprite(hd.frame.sprite, ORB_X + 0.12f, ORB_Y + 0.92f, 1, white, 0);
+ /* the masks */
+ int max = g_pd.max_health > MAX_MASKS ? MAX_MASKS : g_pd.max_health;
+ for (int i = 0; i < max; i++) hud_sprite(hd.mask[i].sprite, HEALTH_X - 10.32f + 0.94f * i, HEALTH_Y + 7.7f, 1, white, 0);
+ /* geo: the coin, the count (TrajanPro, its left at the text's place) */
+ hud_sprite(hd.coin.sprite, HEALTH_X - 10.33f, HEALTH_Y + 6.61f, 1, white, 0);
+ char buf[12];
+ int n = 0;
+ int32_t v = g_pd.geo < 0 ? 0 : g_pd.geo;
+ do buf[n++] = (char)('0' + v % 10), v /= 10;
+ while (v && n < 11);
+ static const float adv[10] = DIGIT_ADV;
+ float x = HEALTH_X - 9.76f, y = HEALTH_Y + 6.45f + 0.23f; /* (anchored middle left: the baseline a little below) */
+ while (n--) {
+ int d = buf[n] - '0';
+ hud_sprite(SPRITE_DIGIT0 + d, x, y, 1, white, 0);
+ x += adv[d];
+ }
+}
diff --git a/games/hollowknight/tools/actors.py b/games/hollowknight/tools/actors.py
index 43718344..51f7787f 100644
--- a/games/hollowknight/tools/actors.py
+++ b/games/hollowknight/tools/actors.py
@@ -26,6 +26,11 @@ ACTORS = {"knight": KNIGHT,
"buzzer": ("sharedassets6.assets", 1150, None),
"georock": ("sharedassets6.assets", 1149, None),
"chest": ("sharedassets6.assets", 1148, None),
+ # the HUD: drawn by its own camera, 11.48 pixels a unit (scenes: K0), masks at 0.7135
+ "hud": ("resources.assets", 20665, ["Health Empty", "Health Idle", "Health Break", "Health Refill", "Health Appear",
+ "Blue Appear", "Blue Idle", "Blue Break", "HUD Frame", "HUD Frame Idle",
+ "Coin Appear", "Coin Idle", "Coin Get", "Soul Burst"]),
+ "liquid": ("resources.assets", 20843, None),
"geo": ("resources.assets", 23266, ["Small Idle", "Small Air", "Med Idle", "Med Air", "Large Idle", "Large Air"])}
# sprites of prefab objects the game draws itself: name -> (file, prefab, object, resolution)
@@ -48,6 +53,10 @@ def named_sprites(sprites):
DRAWN_SCALE = {"SlashEffect": 1.645, "SlashEffectAlt": 1.422, "UpSlashEffect": 1.4, "DownSlashEffect": 1.28,
"Small Idle": 1.5, "Small Air": 1.5, "Med Idle": 1.5, "Med Air": 1.5, "Large Idle": 1.5, "Large Air": 1.5}
K0 = scene.FOCAL / (0.004 - scene.CAMZ) # screen pixels a unit, where actors are (z near 0)
+HUD_K = (scene.VIEW_H / 2) / 8.7107 # the HUD's (its orthographic camera)
+# (actor, clip): drawn bigger or smaller than their sprites
+ACTOR_SCALE = {"hud": HUD_K / K0, "liquid": 1.4 * HUD_K / K0}
+HUD_MASK = 0.7135
def clip_id(actor, name):
@@ -66,11 +75,13 @@ def build():
frames = []
for f in c["frames"]:
cpath, cpid = tk2d.ref_file(path, f["spriteCollection"])
- key = (cpath, cpid, f["spriteId"])
+ key = (cpath, cpid, f["spriteId"], actor, name.startswith("Health") or name.startswith("Blue"))
if key not in index:
col = tk2d.collection(cpath, cpid)
img, lx, ty, upp, (wu, hu) = tk2d.sprite_image(cpath, col, f["spriteId"])
- k = K0 * DRAWN_SCALE.get(name, 1.0)
+ k = K0 * DRAWN_SCALE.get(name, 1.0) * ACTOR_SCALE.get(actor, 1.0)
+ if actor == "hud" and (name.startswith("Health") or name.startswith("Blue")):
+ k *= HUD_MASK
w = max(1, round(img.width * upp * k))
h = max(1, round(img.height * upp * k))
small = img.resize((w, h), Image.BOX)
diff --git a/games/hollowknight/tools/font.py b/games/hollowknight/tools/font.py
new file mode 100644
index 00000000..ca4fd693
--- /dev/null
+++ b/games/hollowknight/tools/font.py
@@ -0,0 +1,44 @@
+"""Text glyphs from the game's own fonts (Font assets hold their TrueType data): rendered offline at the size the game
+shows them, as sprites."""
+import functools, io
+import numpy as np
+from PIL import Image, ImageFont, ImageDraw
+import unity, art
+
+
[email protected]_cache(maxsize=None)
+def font_data(name):
+ """A Font asset's TrueType data, by its name (resources and shared assets)."""
+ for path in ("sharedassets0.assets", "resources.assets"):
+ f = unity.asset_file(path)
+ for o in f.objects.values():
+ if o.type.name == "Font":
+ t = o.read_typetree()
+ if t["m_Name"] == name and t.get("m_FontData"):
+ return bytes(t["m_FontData"])
+ raise KeyError(name)
+
+
+def glyphs(name, chars, em_px, scale_units):
+ """Sprites (as actors.build's records) of chars in the font at em_px pixels an em; scale_units: units a pixel.
+ -> ([sprite], [advance in units])"""
+ ss = 4
+ ft = ImageFont.truetype(io.BytesIO(font_data(name)), int(round(em_px * ss)))
+ asc, desc = ft.getmetrics()
+ out, adv = [], []
+ for ch in chars:
+ l, t, r, b = ft.getbbox(ch)
+ w, h = max(1, r - l + 2 * ss), max(1, b - t + 2 * ss)
+ im = Image.new("L", (w, h), 0)
+ ImageDraw.Draw(im).text((ss - l, ss - t), ch, font=ft, fill=255)
+ W, H = max(1, round(w / ss)), max(1, round(h / ss))
+ a = np.asarray(im.resize((W, H), Image.BOX))
+ rgba = np.zeros((H, W, 4), np.uint8)
+ rgba[..., :3] = 255
+ rgba[..., 3] = a
+ # (the glyph's top left from the pen, on the baseline: units)
+ lx, ty = (l - ss) / ss * scale_units, (asc - (t - ss)) / ss * scale_units
+ out.append({"key": ("font", name, ch, em_px), "job": art.ImageJob(rgba, "glyph/%s" % ch), "lx": lx, "ty": ty,
+ "tu": w / ss * scale_units / W, "tv": h / ss * scale_units / H})
+ adv.append(ft.getlength(ch) / ss * scale_units)
+ return out, adv
diff --git a/games/hollowknight/tools/pack.py b/games/hollowknight/tools/pack.py
index a15dbc20..c145b3c0 100644
--- a/games/hollowknight/tools/pack.py
+++ b/games/hollowknight/tools/pack.py
@@ -4,7 +4,7 @@ import os, sys, struct, math, lzma, time, multiprocessing as mp
import numpy as np
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
-import unity, scene, visible, art, actors, coll, ents
+import unity, scene, visible, art, actors, coll, ents, font
SRC = os.path.join(HERE, "..", "src")
ROOMS = [l.strip() for l in open(os.path.join(HERE, "rooms.txt")) if l.strip() and not l.startswith("#")]
@@ -294,6 +294,10 @@ def main():
for r in ROOMS:
room_data(r)
sprites += actors.unity_sprites(SPRITES.list)
+ # the HUD's geo count: TrajanPro-Bold digits (TextMesh: size 45, character size 1, scale 0.1527)
+ digit_base = len(sprites)
+ digits, digit_adv = font.glyphs("TrajanPro-Bold", "0123456789", 45 * 0.1 * 0.1527 * actors.HUD_K, 1 / actors.HUD_K)
+ sprites += digits
print("actor frames", len(sprites), "clips", len(clips), "%.0fs" % (time.time() - t0), flush=True)
jobs += [sp["job"] for sp in sprites]
with mp.get_context("spawn").Pool(min(4, os.cpu_count() or 1)) as pool:
@@ -406,6 +410,8 @@ def main():
f.write("#define %s %d\n" % (c["id"], i))
for k, v in named.items():
f.write("#define SPRITE_%s %d\n" % (k, v))
+ f.write("#define SPRITE_DIGIT0 %d\n" % digit_base)
+ f.write("#define DIGIT_ADV {%s}\n" % ", ".join("%.4ff" % a for a in digit_adv))
if __name__ == "__main__":
diff --git a/games/hollowknight/tools/tk2d.py b/games/hollowknight/tools/tk2d.py
index 9503ef51..1b6810ae 100644
--- a/games/hollowknight/tools/tk2d.py
+++ b/games/hollowknight/tools/tk2d.py
@@ -96,8 +96,8 @@ def sprite_image(path, col, sid):
y0, y1 = round((1 - max(vs)) * H), round((1 - min(vs)) * H)
crop = img.crop((x0, y0, x1, y1))
if d["flipped"]:
- # tk2d stores some sprites turned in the atlas
- crop = crop.transpose(Image.Transpose.ROTATE_90).transpose(Image.Transpose.FLIP_TOP_BOTTOM)
+ # tk2d stores some sprites turned in the atlas: their x along its v, their y along its u
+ crop = crop.transpose(Image.Transpose.TRANSVERSE)
pos = d["positions"]
px = [p["x"] for p in pos]
py = [p["y"] for p in pos]