summaryrefslogtreecommitdiff
path: root/kon
diff options
context:
space:
mode:
authorKonAki <konaki@anokonaki.org>2026-10-08 17:23:50 +0800
committerKonAki <konaki@anokonaki.org>2026-10-08 17:23:50 +0800
commit12c955cf1911edc3fbab9d675c14204058b4a134 (patch)
tree5cba88af6812c89968519e02c81177e1f63dc380 /kon
parentf25fac99b90210cd10a77420ffa179b39be21a67 (diff)
Add resizable debug text with ctrl plus and minus
Diffstat (limited to 'kon')
-rw-r--r--kon/kondebug.h77
-rw-r--r--kon/konfont.h25
2 files changed, 85 insertions, 17 deletions
diff --git a/kon/kondebug.h b/kon/kondebug.h
index ec043f5..6ac51c8 100644
--- a/kon/kondebug.h
+++ b/kon/kondebug.h
@@ -133,6 +133,8 @@ struct kon_debug {
kon_font_t *font;
kon_assets_t *assets;
int lineH, ascent;
+ float baseSize, size;
+ int ctrl;
int overlayKey, consoleKey;
int overlay;
@@ -165,6 +167,26 @@ struct kon_debug {
/*** private helpers ***/
+static void kon_debugMetrics_(kon_debug_t *d)
+{
+ int h = 0;
+
+ kon_measureText(d->font, "A", NULL, &h);
+ d->ascent = h * 4 / 5;
+ d->lineH = h + 2;
+}
+
+static void kon_debugResize_(kon_debug_t *d, float size)
+{
+ if (size < 8.0f) size = 8.0f;
+ if (size > 64.0f) size = 64.0f;
+ if (size == d->size) return;
+ if (kon_fontResize(d->font, size)) {
+ d->size = size;
+ kon_debugMetrics_(d);
+ }
+}
+
static void kon_debugText_(const kon_debug_t *d, kon_framebuffer_t *fb, int x, int y, const char *s, uint32_t color)
{
kon_drawText(fb, d->font, x, y + d->ascent, s, color);
@@ -422,6 +444,13 @@ static void kon_debugCmdQuit_(kon_debug_t *d, void *user, int argc, char **argv)
d->quit = 1;
}
+static void kon_debugCmdFontSize_(kon_debug_t *d, void *user, int argc, char **argv)
+{
+ (void)user;
+ if (argc > 1) kon_debugResize_(d, (float)atof(argv[1]));
+ kon_debugLog(d, "fontsize = %g", (double)d->size);
+}
+
static void kon_debugCmdAssets_(kon_debug_t *d, void *user, int argc, char **argv)
{
int i, count;
@@ -445,16 +474,14 @@ static void kon_debugCmdAssets_(kon_debug_t *d, void *user, int argc, char **arg
kon_debug_t *kon_debugCreate(kon_font_t *font)
{
kon_debug_t *d;
- int h = 0;
if (!font) return NULL;
d = calloc(1, sizeof(kon_debug_t));
if (!d) return NULL;
d->font = font;
- kon_measureText(font, "A", NULL, &h);
- d->ascent = h * 4 / 5;
- d->lineH = h + 2;
+ d->baseSize = d->size = kon_fontPixelHeight(font);
+ kon_debugMetrics_(d);
d->overlayKey = KON_KEY_F1;
d->consoleKey = KON_KEY_GRAVE;
d->last = kon_getTime();
@@ -465,6 +492,7 @@ kon_debug_t *kon_debugCreate(kon_font_t *font)
kon_debugCommand(d, "fps", "overlay mode 0-3, or cycle", kon_debugCmdFps_, NULL);
kon_debugCommand(d, "watch", "show a variable in the overlay", kon_debugCmdWatch_, NULL);
kon_debugCommand(d, "assets", "list loaded assets", kon_debugCmdAssets_, NULL);
+ kon_debugCommand(d, "fontsize", "text size in pixels, ctrl + and ctrl - too", kon_debugCmdFontSize_, NULL);
kon_debugCommand(d, "quit", "close the game", kon_debugCmdQuit_, NULL);
kon_debugCommand(d, "exit", "close the game", kon_debugCmdQuit_, NULL);
kon_debugLog(d, "type help for commands, tab completes");
@@ -550,7 +578,17 @@ int kon_debugFeedEvent(kon_debug_t *d, const kon_event_t *e)
{
if (!d) return 0;
+ if (e->key == KON_KEY_LEFT_CTRL || e->key == KON_KEY_RIGHT_CTRL) {
+ if (e->type == KON_EVENT_KEY_DOWN) d->ctrl = 1;
+ if (e->type == KON_EVENT_KEY_UP) d->ctrl = 0;
+ }
+
if (e->type == KON_EVENT_KEY_DOWN) {
+ if (d->ctrl && (d->open || d->overlay)) {
+ if (e->key == KON_KEY_EQUAL) { kon_debugResize_(d, d->size + 2.0f); return 1; }
+ if (e->key == KON_KEY_MINUS) { kon_debugResize_(d, d->size - 2.0f); return 1; }
+ if (e->key == KON_KEY_0) { kon_debugResize_(d, d->baseSize); return 1; }
+ }
if (e->key == d->consoleKey) {
d->open = !d->open;
d->swallowTick = 1;
@@ -565,6 +603,7 @@ int kon_debugFeedEvent(kon_debug_t *d, const kon_event_t *e)
if (!d->open) return 0;
if (e->type == KON_EVENT_TEXT) {
+ if (d->ctrl) return 1;
if (d->swallowTick && e->codepoint == '`') {
d->swallowTick = 0;
return 1;
@@ -632,6 +671,15 @@ void kon_debugFrame(kon_debug_t *d)
d->graphHead = (d->graphHead + 1) % KON_DEBUG_GRAPH;
}
+static void kon_debugBox_(const kon_debug_t *d, kon_framebuffer_t *fb, int x, int y, const char *s, uint32_t color)
+{
+ int w = 0;
+
+ kon_measureText(d->font, s, &w, NULL);
+ kon_fillRectangle(fb, x - 2, y - 1, w + 4, d->lineH, 0xB0000000);
+ kon_debugText_(d, fb, x, y, s, color);
+}
+
static void kon_debugDrawOverlay_(kon_debug_t *d, kon_framebuffer_t *fb)
{
char line[128];
@@ -640,9 +688,8 @@ static void kon_debugDrawOverlay_(kon_debug_t *d, kon_framebuffer_t *fb)
float worst = 0.0f;
snprintf(line, sizeof(line), "%.0f fps %.2f ms", (double)d->fps, d->frameDt * 1000.0);
- kon_fillRectangle(fb, x - 2, y - 2, 170, d->lineH + 4, 0xB0000000);
- kon_debugText_(d, fb, x, y, line, white);
- y += d->lineH + 4;
+ kon_debugBox_(d, fb, x, y, line, white);
+ y += d->lineH + 3;
if (d->overlay >= 1) {
kon_fillRectangle(fb, x - 2, y - 2, KON_DEBUG_GRAPH + 4, 34, 0xB0000000);
@@ -664,8 +711,7 @@ static void kon_debugDrawOverlay_(kon_debug_t *d, kon_framebuffer_t *fb)
if (!d->vars[i].watched) continue;
snprintf(line, sizeof(line), "%s = %s", d->vars[i].name, kon_debugVarText_(&d->vars[i], value, sizeof(value)));
- kon_fillRectangle(fb, x - 2, y - 1, 170, d->lineH, 0xB0000000);
- kon_debugText_(d, fb, x, y, line, dim);
+ kon_debugBox_(d, fb, x, y, line, dim);
y += d->lineH;
}
@@ -693,13 +739,11 @@ static void kon_debugDrawOverlay_(kon_debug_t *d, kon_framebuffer_t *fb)
y += 2;
snprintf(line, sizeof(line), "assets %d, %.1f KB", total, (double)kon_assetsBytes(d->assets) / 1024.0);
- kon_fillRectangle(fb, x - 2, y - 1, 220, d->lineH, 0xB0000000);
- kon_debugText_(d, fb, x, y, line, white);
+ kon_debugBox_(d, fb, x, y, line, white);
y += d->lineH;
for (i = 0; i < types; i++) {
snprintf(line, sizeof(line), " %-7s %3d %8.1f KB", names[i], counts[i], (double)bytes[i] / 1024.0);
- kon_fillRectangle(fb, x - 2, y - 1, 220, d->lineH, 0xB0000000);
- kon_debugText_(d, fb, x, y, line, dim);
+ kon_debugBox_(d, fb, x, y, line, dim);
y += d->lineH;
}
@@ -708,13 +752,12 @@ static void kon_debugDrawOverlay_(kon_debug_t *d, kon_framebuffer_t *fb)
for (i = 0; i < total && i < rows; i++) {
if (!kon_assetsInfo(d->assets, i, &info)) continue;
snprintf(line, sizeof(line), "%-6s x%d %7.1fK %s%s", info.typeName, info.refs, (double)info.bytes / 1024.0, info.name, info.loose ? " *" : "");
- kon_fillRectangle(fb, x - 2, y - 1, 330, d->lineH, 0xB0000000);
- kon_debugText_(d, fb, x, y, line, dim);
+ kon_debugBox_(d, fb, x, y, line, dim);
y += d->lineH;
}
}
} else if (d->overlay >= 2) {
- kon_debugText_(d, fb, x, y + 2, "no asset manager attached", dim);
+ kon_debugBox_(d, fb, x, y + 2, "no asset manager attached", dim);
}
}
@@ -738,7 +781,7 @@ static void kon_debugDrawConsole_(kon_debug_t *d, kon_framebuffer_t *fb)
if (index < 0) break;
line = (d->logHead + index) % KON_DEBUG_LOGLINES;
- kon_debugText_(d, fb, 6, inputY - 4 - (i + 1) * d->lineH + d->lineH - 2, d->log[line], d->log[line][0] == '>' ? 0xFF9098C0 : 0xFFE0E0E8);
+ kon_debugText_(d, fb, 6, inputY - 4 - (i + 1) * d->lineH, d->log[line], d->log[line][0] == '>' ? 0xFF9098C0 : 0xFFE0E0E8);
}
snprintf(shown, sizeof(shown), "> %s", d->input);
diff --git a/kon/konfont.h b/kon/konfont.h
index 822e1f3..a75c502 100644
--- a/kon/konfont.h
+++ b/kon/konfont.h
@@ -45,6 +45,10 @@ void kon_freeFont(kon_font_t *font);
kon_image *kon_fontGetAtlas(kon_font_t *font);
+/* the ttf data has to stay alive for as long as the font, resizing bakes from it again */
+float kon_fontPixelHeight(const kon_font_t *font);
+int kon_fontResize(kon_font_t *font, float pixelHeight);
+
/*** text draw declarations ***/
void kon_drawText(kon_framebuffer_t *fb, kon_font_t *font, int x, int y, const char *text, uint32_t color);
@@ -57,6 +61,7 @@ void kon_measureText(kon_font_t *font, const char *text, int *outWidth, int *out
#include "stb_truetype.h"
struct kon_font {
+ const uint8_t *ttf;
stbtt_bakedchar chardata[KON_FONT_NUM_CHARS];
uint8_t *atlasPixels;
int atlasSize;
@@ -98,6 +103,7 @@ kon_font_t *kon_loadFont(const uint8_t *ttf_data, size_t size, float pixelHeight
kon_font_t *font = malloc(sizeof(kon_font_t));
if (!font) return NULL;
+ font->ttf = ttf_data;
font->atlasPixels = NULL;
font->pixelHeight = pixelHeight;
@@ -116,6 +122,25 @@ void kon_freeFont(kon_font_t *font) {
free(font);
}
+float kon_fontPixelHeight(const kon_font_t *font) {
+ return font ? font->pixelHeight : 0.0f;
+}
+
+int kon_fontResize(kon_font_t *font, float pixelHeight) {
+ kon_font_t next;
+
+ if (!font || pixelHeight <= 0.0f) return 0;
+
+ next = *font;
+ next.atlasPixels = NULL;
+ if (!kon_fontBakeAtlas(&next, font->ttf, pixelHeight)) return 0;
+
+ free(font->atlasPixels);
+ next.pixelHeight = pixelHeight;
+ *font = next;
+ return 1;
+}
+
kon_image *kon_fontGetAtlas(kon_font_t *font) {
if (!font) return NULL;