aboutsummaryrefslogtreecommitdiff
path: root/dc/dc.c
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-11 08:42:59 -0400
committeriamcheeseman <[email protected]>2026-05-11 08:42:59 -0400
commitcd15c9789c9eeb9ec2fa367c39872685597e0c17 (patch)
treedf52093b99ba7bb7d1ce7903caed487df382b803 /dc/dc.c
parent18a5d0af4664a1e50d1e69a5ecbcd91a34246ed4 (diff)
text rendering
Diffstat (limited to 'dc/dc.c')
-rw-r--r--dc/dc.c85
1 files changed, 77 insertions, 8 deletions
diff --git a/dc/dc.c b/dc/dc.c
index 0d69710..bccc82f 100644
--- a/dc/dc.c
+++ b/dc/dc.c
@@ -13,6 +13,7 @@
#define TILE_SIZE 16
struct ty_image img;
+struct ty_font font;
void tick(void)
{
@@ -59,10 +60,57 @@ void tick(void)
ty_color(0xFF, 0xF0, 0x80)
);
- ty_draw_end();
+ ty_draw_text(&font, ty_vec2i(5, SCREEN_HEIGHT - 5 - 8), "hello, world!");
double frame_time = ty_get_time() - start;
- ty_log_debug("%g ms, %g FPS", frame_time * 1000, 1.0 / frame_time);
+
+ char ms_fmt[] = "%.2g ms";
+ int ms_width = ty_font_width(&font, ms_fmt, frame_time * 1000);
+
+ ty_draw_rect(
+ (struct ty_recti){
+ 5, 5,
+ ms_width, 8,
+ },
+ TY_COLOR_BLACK
+ );
+ ty_draw_text_fmt(
+ &font,
+ ty_vec2i(5, 5),
+ ms_fmt,
+ frame_time * 1000
+ );
+
+ char fps_fmt[] = "%4d fps";
+ int fps_width = ty_font_width(&font, fps_fmt, (int)(1.0 / frame_time));
+
+ ty_draw_rect(
+ (struct ty_recti){
+ 5, 5 + 8,
+ fps_width, 8,
+ },
+ TY_COLOR_BLACK
+ );
+ ty_draw_text_fmt(
+ &font,
+ ty_vec2i(5, 5 + 8),
+ fps_fmt,
+ (int)(1.0 / frame_time)
+ );
+
+ ty_draw_end();
+}
+
+struct ty_image load_qoi_image(const char *path)
+{
+ qoi_desc desc;
+ void *data = qoi_read(path, &desc, 3);
+ if (!data)
+ ty_log_fatal(TY_ERR_IO, "failed to read image");
+ struct ty_image img = ty_create_image(desc.width, desc.height, data);
+ free(data);
+
+ return img;
}
int main(void)
@@ -75,21 +123,42 @@ int main(void)
};
ty_init(hints);
- qoi_desc desc;
- void *data = qoi_read("test_img.qoi", &desc, 3);
- if (!data)
- ty_log_fatal(TY_ERR_IO, "failed to read image");
- img = ty_create_image(desc.width, desc.height, data);
- free(data);
+ img = load_qoi_image("test_img.qoi");
+
+ char font_chars[] =
+ "abcdefghijklmnopqrstuvwxyz0123456789"
+ "!\"'#$%&()*+,-./@[]\\^_`{}|~:;<>=? "
+ ;
+ struct ty_image font_img = load_qoi_image("font.qoi");
+
+ int glyph_height = 8;
+
+ for (int i = 0; font_chars[i] != '\0'; i++) {
+ struct ty_image glyph = ty_create_image(
+ font_img.width,
+ glyph_height,
+ font_img.data + font_img.width * glyph_height * i
+ );
+
+
+ ty_font_add_glyph(
+ &font,
+ *(uint8_t*)&font_chars[i],
+ glyph
+ );
+ }
while (ty_is_game_running()) {
int ticks = ty_tick();
for (int i = 0; i < ticks; i++)
tick();
+
+ ty_free_temp_allocs();
// ty_sleep(1000 / hints.ticrate);
}
ty_free_image(img);
+ ty_free_image(font_img);
ty_deinit();
return 0;