From 897a153d290ed4d3647ad9e0100d1b717f580b5f Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Tue, 12 May 2026 16:41:37 -0400 Subject: typedef all structs and rename to Ada_Case There isn't really a technical reason that I made this change. I just wanted to use Ada_Case. All types were prefixed with ty_, e.g. ty_Image. --- teensy/teensy_renderer.c | 124 +++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 63 deletions(-) (limited to 'teensy/teensy_renderer.c') diff --git a/teensy/teensy_renderer.c b/teensy/teensy_renderer.c index d457ef6..173fbed 100644 --- a/teensy/teensy_renderer.c +++ b/teensy/teensy_renderer.c @@ -9,8 +9,9 @@ #include "teensy_list.h" #define BLEND_COLOR ty_color(255, 0, 255) +#define screenspace() ((ty_Recti){0, 0, r.target.width, r.target.height}) -struct ty_renderer r; +ty_Renderer r; static bool is_renderer_init(void) @@ -19,7 +20,7 @@ bool is_renderer_init(void) } static -void image_bounds_check(struct ty_image img, struct ty_vec2i pos) +void image_bounds_check(ty_Image img, ty_Vec2i pos) { #ifdef TEENSY_DEBUG if ( @@ -58,14 +59,14 @@ void ty_deinit_renderer(void) ty_free_image(r.screen); } -struct ty_image ty_create_image(int w, int h, const struct ty_color* data) +ty_Image ty_create_image(int w, int h, const ty_Color* data) { - struct ty_image img; + ty_Image img; img.width = w; img.height = h; - size_t size = sizeof(struct ty_color) * w * h; + size_t size = sizeof(ty_Color) * w * h; img.data = ty_alloc(size); if (data) memcpy(img.data, data, size); @@ -75,21 +76,21 @@ struct ty_image ty_create_image(int w, int h, const struct ty_color* data) return img; } -void ty_free_image(struct ty_image img) +void ty_free_image(ty_Image img) { ty_free(img.data); } -struct ty_color ty_img_get_pixel(struct ty_image img, struct ty_vec2i pos) +ty_Color ty_img_get_pixel(ty_Image img, ty_Vec2i pos) { image_bounds_check(img, pos); return img.data[pos.y * img.width + pos.x]; } void ty_img_set_pixel( - struct ty_image img, - struct ty_vec2i pos, - struct ty_color color + ty_Image img, + ty_Vec2i pos, + ty_Color color ) { image_bounds_check(img, pos); img.data[pos.y * img.width + pos.x] = color; @@ -97,12 +98,12 @@ void ty_img_set_pixel( // 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) +void ty_font_add_glyph(ty_Font *font, uint8_t c, ty_Image img) { font->glyphs[c] = img; } -int ty_font_width(struct ty_font *font, const char *fmt, ...) +int ty_font_width(ty_Font *font, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -120,7 +121,7 @@ int ty_font_width(struct ty_font *font, const char *fmt, ...) return width; } -void ty_draw_set_target(const struct ty_image *img) +void ty_draw_set_target(const ty_Image *img) { if (!img) { r.target = r.screen; @@ -129,7 +130,7 @@ void ty_draw_set_target(const struct ty_image *img) memcpy(&r.target, img, sizeof(r.target)); } -void ty_draw_clear(struct ty_color col) +void ty_draw_clear(ty_Color col) { assert(is_renderer_init()); for (int i = 0; i < r.target.width * r.target.height; i++) { @@ -137,18 +138,27 @@ void ty_draw_clear(struct ty_color col) } } -void ty_draw_image(struct ty_image img, struct ty_vec2i pos) +static +ty_Vec2i clamp_point(ty_Vec2i p, ty_Recti rect) +{ + p.x = fmin(fmax(p.x, rect.x), rect.w); + p.y = fmin(fmax(p.y, rect.y), rect.h); + return p; +} + +void ty_draw_image(ty_Image img, ty_Vec2i pos) { - int x1 = fmin(fmax(pos.x, 0), r.target.width); - int y1 = fmin(fmax(pos.y, 0), r.target.height); - int x2 = fmin(fmax(pos.x + img.width, 0), r.target.width); - int y2 = fmin(fmax(pos.y + img.height, 0), r.target.height); - - for (int dy = y1; dy < y2; dy++) { - for (int dx = x1; dx < x2; dx++) { - struct ty_color px = ty_img_get_pixel( + ty_Vec2i p1 = clamp_point(ty_vec2i(pos.x, pos.y), screenspace()); + ty_Vec2i p2 = clamp_point( + ty_vec2i(pos.x + img.width, pos.y + img.height), + screenspace() + ); + + for (int dy = p1.y; dy < p2.y; dy++) { + for (int dx = p1.x; dx < p2.x; dx++) { + ty_Color px = ty_img_get_pixel( img, - ty_vec2i(dx - x1, dy - y1) + ty_vec2i(dx - p1.x, dy - p1.y) ); if (memcmp(&px, &BLEND_COLOR, sizeof(px)) == 0) continue; @@ -162,20 +172,18 @@ void ty_draw_image(struct ty_image img, struct ty_vec2i pos) } void ty_draw_image_ex( - struct ty_image img, - struct ty_recti src, - struct ty_recti dst + ty_Image img, + ty_Recti src, + ty_Recti dst ) { - int x1 = fmin(fmax(dst.x, 0), r.target.width); - int y1 = fmin(fmax(dst.y, 0), r.target.height); - int x2 = fmin(fmax(dst.x + dst.w, 0), r.target.width); - int y2 = fmin(fmax(dst.y + dst.h, 0), r.target.height); - - for (int dy = y1; dy < y2; dy++) { - for (int dx = x1; dx < x2; dx++) { - int img_x = ((dx - x1) * src.w) / dst.w + src.x; - int img_y = ((dy - y1) * src.h) / dst.h + src.y; - struct ty_color px = ty_img_get_pixel(img, ty_vec2i(img_x, img_y)); + ty_Vec2i p1 = clamp_point(ty_recti_start(dst), screenspace()); + ty_Vec2i p2 = clamp_point(ty_recti_end(dst), screenspace()); + + for (int dy = p1.y; dy < p2.y; dy++) { + for (int dx = p1.x; dx < p2.x; dx++) { + int img_x = ((dx - p1.x) * src.w) / dst.w + src.x; + int img_y = ((dy - p1.y) * src.h) / dst.h + src.y; + ty_Color px = ty_img_get_pixel(img, ty_vec2i(img_x, img_y)); if (memcmp(&px, &BLEND_COLOR, sizeof(px)) == 0) continue; @@ -188,17 +196,10 @@ void ty_draw_image_ex( } } -struct ty_vec2i clamp_point(struct ty_vec2i p, struct ty_recti rect) -{ - p.x = fmin(fmax(p.x, rect.x), rect.w); - p.y = fmin(fmax(p.y, rect.y), rect.h); - return p; -} - void ty_draw_image_rot( - struct ty_image img, - struct ty_vec2i pos, - struct ty_vec2i offset, + ty_Image img, + ty_Vec2i pos, + ty_Vec2i offset, double rot ) { double s = sin(rot); @@ -219,22 +220,19 @@ void ty_draw_image_rot( if (dx < 0 || dx >= img.width || dy < 0 || dy >= img.height) continue; - struct ty_color px = ty_img_get_pixel(img, ty_vec2i(dx, dy)); + ty_Color px = ty_img_get_pixel(img, ty_vec2i(dx, dy)); ty_img_set_pixel(r.target, ty_vec2i(pos.x + img_x, pos.y + img_y), px); } } } -void ty_draw_rect(struct ty_recti rect, struct ty_color color) +void ty_draw_rect(ty_Recti rect, ty_Color color) { - int x1 = fmin(fmax(rect.x, 0), r.target.width); - int y1 = fmin(fmax(rect.y, 0), r.target.height); - - int x2 = fmin(fmax(rect.x + rect.w, 0), r.target.width); - int y2 = fmin(fmax(rect.y + rect.h, 0), r.target.height); + ty_Vec2i p1 = clamp_point(ty_vec2i(rect.x, rect.y), screenspace()); + ty_Vec2i p2 = clamp_point(ty_recti_end(rect), screenspace()); - for (int dy = y1; dy < y2; dy++) { - for (int dx = x1; dx < x2; dx++) { + for (int dy = p1.y; dy < p2.y; dy++) { + for (int dx = p1.x; dx < p2.x; dx++) { ty_img_set_pixel( r.target, ty_vec2i(dx, dy), @@ -245,9 +243,9 @@ void ty_draw_rect(struct ty_recti rect, struct ty_color color) } void ty_draw_line( - struct ty_vec2i start, - struct ty_vec2i end, - struct ty_color color + ty_Vec2i start, + ty_Vec2i end, + ty_Color color ) { int dx = end.x - start.x; int dy = end.y - start.y; @@ -295,18 +293,18 @@ void ty_draw_line( } } -void ty_draw_text(struct ty_font *font, struct ty_vec2i pos, const char *text) +void ty_draw_text(ty_Font *font, 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_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, + ty_Font *font, + ty_Vec2i pos, const char *fmt, ... ) { -- cgit v1.3-2-g0d8e