From b34220f5df6ab917f191ee4bc0189ba85065199c Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Mon, 11 May 2026 21:24:49 -0400 Subject: allow the user to change the render target --- teensy/teensy.h | 2 ++ teensy/teensy_renderer.c | 50 +++++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 20 deletions(-) (limited to 'teensy') diff --git a/teensy/teensy.h b/teensy/teensy.h index 568430c..13216a8 100644 --- a/teensy/teensy.h +++ b/teensy/teensy.h @@ -123,6 +123,8 @@ void ty_img_set_pixel( 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, ...); +// Pass NULL to restore the screen. +void ty_draw_set_target(const struct ty_image *img); 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( diff --git a/teensy/teensy_renderer.c b/teensy/teensy_renderer.c index da35738..d457ef6 100644 --- a/teensy/teensy_renderer.c +++ b/teensy/teensy_renderer.c @@ -48,6 +48,7 @@ void ty_init_renderer(void) ctx.hints.scr_height, NULL ); + r.target = r.screen; } void ty_deinit_renderer(void) @@ -119,20 +120,29 @@ int ty_font_width(struct ty_font *font, const char *fmt, ...) return width; } +void ty_draw_set_target(const struct ty_image *img) +{ + if (!img) { + r.target = r.screen; + return; + } + memcpy(&r.target, img, sizeof(r.target)); +} + void ty_draw_clear(struct ty_color col) { assert(is_renderer_init()); - for (int i = 0; i < r.screen.width * r.screen.height; i++) { - r.screen.data[i] = col; + for (int i = 0; i < r.target.width * r.target.height; i++) { + r.target.data[i] = col; } } void ty_draw_image(struct ty_image img, struct ty_vec2i pos) { - int x1 = fmin(fmax(pos.x, 0), r.screen.width); - int y1 = fmin(fmax(pos.y, 0), r.screen.height); - int x2 = fmin(fmax(pos.x + img.width, 0), r.screen.width); - int y2 = fmin(fmax(pos.y + img.height, 0), r.screen.height); + 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++) { @@ -143,7 +153,7 @@ void ty_draw_image(struct ty_image img, struct ty_vec2i pos) if (memcmp(&px, &BLEND_COLOR, sizeof(px)) == 0) continue; ty_img_set_pixel( - r.screen, + r.target, ty_vec2i(dx, dy), px ); @@ -156,10 +166,10 @@ void ty_draw_image_ex( struct ty_recti src, struct ty_recti dst ) { - int x1 = fmin(fmax(dst.x, 0), r.screen.width); - int y1 = fmin(fmax(dst.y, 0), r.screen.height); - int x2 = fmin(fmax(dst.x + dst.w, 0), r.screen.width); - int y2 = fmin(fmax(dst.y + dst.h, 0), r.screen.height); + 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++) { @@ -170,7 +180,7 @@ void ty_draw_image_ex( continue; ty_img_set_pixel( - r.screen, + r.target, ty_vec2i(dx, dy), px ); @@ -210,23 +220,23 @@ void ty_draw_image_rot( continue; struct ty_color px = ty_img_get_pixel(img, ty_vec2i(dx, dy)); - ty_img_set_pixel(r.screen, ty_vec2i(pos.x + img_x, pos.y + img_y), px); + 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) { - int x1 = fmin(fmax(rect.x, 0), r.screen.width); - int y1 = fmin(fmax(rect.y, 0), r.screen.height); + 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.screen.width); - int y2 = fmin(fmax(rect.y + rect.h, 0), r.screen.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); for (int dy = y1; dy < y2; dy++) { for (int dx = x1; dx < x2; dx++) { ty_img_set_pixel( - r.screen, + r.target, ty_vec2i(dx, dy), color ); @@ -257,7 +267,7 @@ void ty_draw_line( int y = start.y + dy * (x - start.x) / dx; ty_img_set_pixel( - r.screen, + r.target, ty_vec2i(x, y), color ); @@ -277,7 +287,7 @@ void ty_draw_line( int x = start.x + dx * (y - start.y) / dy; ty_img_set_pixel( - r.screen, + r.target, ty_vec2i(x, y), color ); -- cgit v1.3-2-g0d8e