diff options
| author | iamcheeseman <[email protected]> | 2026-05-11 13:05:17 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-05-11 13:05:17 -0400 |
| commit | 8a91b5adccbb24a17ff69e81d08f3e6dad4f9fda (patch) | |
| tree | c1ca776d7c45e861e033ea948cf938de772d06d2 /teensy/teensy_renderer.c | |
| parent | 2c27120a61631dffd4527db8b1349379d53ccc3b (diff) | |
rotated iamges
Diffstat (limited to 'teensy/teensy_renderer.c')
| -rw-r--r-- | teensy/teensy_renderer.c | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/teensy/teensy_renderer.c b/teensy/teensy_renderer.c index d3f73fd..2104695 100644 --- a/teensy/teensy_renderer.c +++ b/teensy/teensy_renderer.c @@ -178,13 +178,50 @@ 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, + double rot +) { + double s = sin(rot); + double c = cos(rot); + + // x1 = c*x - s*y + // y1 = s*x + c*y + + // FIXME: the corners of the rotated image get cut off. + for (int img_y = 0; img_y < img.height; img_y++) { + for (int img_x = 0; img_x < img.width; img_x++) { + double pixel_x = img_x + offset.x; + double pixel_y = img_y + offset.y; + + int dx = floor(pixel_x * c - pixel_y * s - offset.x); + int dy = floor(pixel_x * s + pixel_y * c - offset.y); + + 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_img_set_pixel(r.screen, 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(floor(rect.x), 0), r.screen.width); - int y1 = fmin(fmax(floor(rect.y), 0), r.screen.height); + int x1 = fmin(fmax(rect.x, 0), r.screen.width); + int y1 = fmin(fmax(rect.y, 0), r.screen.height); - int x2 = fmin(fmax(floor(rect.x + rect.w), 0), r.screen.width); - int y2 = fmin(fmax(floor(rect.y + rect.h), 0), r.screen.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); for (int dy = y1; dy < y2; dy++) { for (int dx = x1; dx < x2; dx++) { |
