diff options
Diffstat (limited to 'teensy')
| -rw-r--r-- | teensy/teensy.h | 6 | ||||
| -rw-r--r-- | teensy/teensy_common.h | 4 | ||||
| -rw-r--r-- | teensy/teensy_renderer.c | 45 |
3 files changed, 50 insertions, 5 deletions
diff --git a/teensy/teensy.h b/teensy/teensy.h index 0b7829c..cf0479f 100644 --- a/teensy/teensy.h +++ b/teensy/teensy.h @@ -108,6 +108,12 @@ void ty_draw_image_ex( struct ty_recti src, struct ty_recti dst ); +void ty_draw_image_rot( + struct ty_image img, + struct ty_vec2i pos, + struct ty_vec2i offset, + double rot +); void ty_draw_rect(struct ty_recti rect, struct ty_color color); void ty_draw_line( struct ty_vec2i start, diff --git a/teensy/teensy_common.h b/teensy/teensy_common.h index dc631cb..ea52982 100644 --- a/teensy/teensy_common.h +++ b/teensy/teensy_common.h @@ -9,7 +9,9 @@ #include "teensy_log.h" #include "teensy_mem.h" -#define TY_PI 3.14 +#define TY_PI (3.1415926535897932384626433) +#define TY_TAU (TY_PI * 2) #define TY_DEG2RAD (180 / PI) +#define TY_RAD2DEG (PI / 180) #endif // TEENSY_COMMON_H_ 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++) { |
