aboutsummaryrefslogtreecommitdiff
path: root/teensy
diff options
context:
space:
mode:
Diffstat (limited to 'teensy')
-rw-r--r--teensy/teensy.h16
-rw-r--r--teensy/teensy_renderer.c54
2 files changed, 70 insertions, 0 deletions
diff --git a/teensy/teensy.h b/teensy/teensy.h
index 1f082f9..0b7829c 100644
--- a/teensy/teensy.h
+++ b/teensy/teensy.h
@@ -1,6 +1,8 @@
#ifndef TEENSY_H_
#define TEENSY_H_
+#include <limits.h>
+
#include "teensy_common.h"
#include "teensy_context.h"
#include "teensy_list.h"
@@ -68,6 +70,10 @@ struct ty_image {
int height;
};
+struct ty_font {
+ struct ty_image glyphs[CHAR_MAX];
+};
+
struct ty_renderer {
struct ty_image screen;
};
@@ -92,6 +98,9 @@ void ty_img_set_pixel(
struct ty_color color
);
+void ty_font_add_glyph(struct ty_font *font, uint8_t c, struct ty_image img);
+int ty_font_width(struct ty_font *font, const char *fmt, ...);
+
void ty_draw_clear(struct ty_color col);
void ty_draw_image(struct ty_image img, struct ty_vec2i pos);
void ty_draw_image_ex(
@@ -105,6 +114,13 @@ void ty_draw_line(
struct ty_vec2i end,
struct ty_color color
);
+void ty_draw_text(struct ty_font *font, struct ty_vec2i pos, const char *text);
+void ty_draw_text_fmt(
+ struct ty_font *font,
+ struct ty_vec2i pos,
+ const char *fmt,
+ ...
+);
void ty_draw_end(void);
void ty_sleep(uint64_t ms);
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());