diff options
| author | iamcheeseman <[email protected]> | 2026-05-11 08:42:59 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-05-11 08:42:59 -0400 |
| commit | cd15c9789c9eeb9ec2fa367c39872685597e0c17 (patch) | |
| tree | df52093b99ba7bb7d1ce7903caed487df382b803 /teensy/teensy_renderer.c | |
| parent | 18a5d0af4664a1e50d1e69a5ecbcd91a34246ed4 (diff) | |
text rendering
Diffstat (limited to 'teensy/teensy_renderer.c')
| -rw-r--r-- | teensy/teensy_renderer.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/teensy/teensy_renderer.c b/teensy/teensy_renderer.c index b29e485..b51d2a9 100644 --- a/teensy/teensy_renderer.c +++ b/teensy/teensy_renderer.c @@ -3,6 +3,7 @@ #include <stdio.h> #include <string.h> #include <math.h> +#include <stdarg.h> #include "teensy_context.h" #include "teensy_list.h" @@ -91,6 +92,31 @@ void ty_img_set_pixel( img.data[pos.y * img.width + pos.x] = color; } +// This function technically is not needed, but I may want unicode support in +// the future, when an abstraction like this is needed. +void ty_font_add_glyph(struct ty_font *font, uint8_t c, struct ty_image img) +{ + font->glyphs[c] = img; +} + +int ty_font_width(struct ty_font *font, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + int len = vsnprintf(NULL, 0, fmt, args); + char *text = ty_talloc(sizeof(char) * (len + 1)); + va_end(args); + + va_start(args, fmt); + vsnprintf(text, len + 1, fmt, args); + va_end(args); + + int width = 0; + for (uint8_t *c = (uint8_t*)text; *c != '\0'; c++) + width += font->glyphs[*c].width; + return width; +} + void ty_draw_clear(struct ty_color col) { assert(is_renderer_init()); @@ -211,6 +237,34 @@ void ty_draw_line( } } +void ty_draw_text(struct ty_font *font, struct ty_vec2i pos, const char *text) +{ + for (const uint8_t *c = (const uint8_t*)text; *c != '\0'; c++) { + struct ty_image glyph = font->glyphs[*c]; + ty_draw_image(glyph, pos); + pos.x += glyph.width; + } +} + +void ty_draw_text_fmt( + struct ty_font *font, + struct ty_vec2i pos, + const char *fmt, + ... +) { + va_list args; + va_start(args, fmt); + int len = vsnprintf(NULL, 0, fmt, args); + char *text = ty_talloc(sizeof(char) * (len + 1)); + va_end(args); + + va_start(args, fmt); + vsnprintf(text, len + 1, fmt, args); + va_end(args); + + ty_draw_text(font, pos, text); +} + void ty_draw_end(void) { assert(is_renderer_init()); |
