aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_renderer.c
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-21 20:03:50 -0400
committeriamcheeseman <[email protected]>2026-05-21 20:04:19 -0400
commita573068a4faad9aa1a0aa9fad92162ce0f2ad483 (patch)
tree0a1d78af447476f61c780f03244fb9ece7f01b99 /teensy/teensy_renderer.c
parentb98d216add98579c3a91b74fa5233f8534200297 (diff)
Allow the clipping of rendering
Diffstat (limited to 'teensy/teensy_renderer.c')
-rw-r--r--teensy/teensy_renderer.c44
1 files changed, 30 insertions, 14 deletions
diff --git a/teensy/teensy_renderer.c b/teensy/teensy_renderer.c
index 0dd4fc3..eee67a3 100644
--- a/teensy/teensy_renderer.c
+++ b/teensy/teensy_renderer.c
@@ -42,6 +42,14 @@ void image_bounds_check(ty_Image img, ty_Vec2i pos)
#endif
}
+static
+ty_Vec2i clamp_point(ty_Vec2i p, ty_Recti rect)
+{
+ p.x = fmin(fmax(p.x, rect.x), rect.x + rect.w);
+ p.y = fmin(fmax(p.y, rect.y), rect.y + rect.h);
+ return p;
+}
+
void ty_init_renderer(void)
{
r.screen = ty_create_image(
@@ -50,6 +58,8 @@ void ty_init_renderer(void)
NULL
);
r.target = r.screen;
+
+ ty_draw_set_clip(TY_CLIP_NONE);
}
void ty_deinit_renderer(void)
@@ -136,6 +146,20 @@ void ty_draw_set_target(const ty_Image *img)
memcpy(&r.target, img, sizeof(r.target));
}
+void ty_draw_set_clip(ty_Recti clip)
+{
+ if (clip.w <= 0 || clip.h <= 0)
+ clip = screenspace();
+
+ ty_Vec2i start = clamp_point(ty_recti_start(clip), screenspace());
+ ty_Vec2i end = clamp_point(ty_recti_end(clip), screenspace());
+
+ r.clip = ty_recti(
+ start.x, start.y,
+ end.x - start.x, end.y - start.y
+ );
+}
+
void ty_draw_clear(ty_Color col)
{
assert(is_renderer_init());
@@ -144,20 +168,12 @@ void ty_draw_clear(ty_Color col)
}
}
-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)
{
- ty_Vec2i p1 = clamp_point(ty_vec2i(pos.x, pos.y), screenspace());
+ ty_Vec2i p1 = clamp_point(ty_vec2i(pos.x, pos.y), r.clip);
ty_Vec2i p2 = clamp_point(
ty_vec2i(pos.x + img.width, pos.y + img.height),
- screenspace()
+ r.clip
);
for (int dy = p1.y; dy < p2.y; dy++) {
@@ -182,8 +198,8 @@ void ty_draw_image_ex(
ty_Recti src,
ty_Recti dst
) {
- ty_Vec2i p1 = clamp_point(ty_recti_start(dst), screenspace());
- ty_Vec2i p2 = clamp_point(ty_recti_end(dst), screenspace());
+ ty_Vec2i p1 = clamp_point(ty_recti_start(dst), r.clip);
+ ty_Vec2i p2 = clamp_point(ty_recti_end(dst), r.clip);
for (int dy = p1.y; dy < p2.y; dy++) {
for (int dx = p1.x; dx < p2.x; dx++) {
@@ -237,8 +253,8 @@ void ty_draw_image_rot(
void ty_draw_rect(ty_Recti rect, ty_Color color)
{
- ty_Vec2i p1 = clamp_point(ty_vec2i(rect.x, rect.y), screenspace());
- ty_Vec2i p2 = clamp_point(ty_recti_end(rect), screenspace());
+ ty_Vec2i p1 = clamp_point(ty_vec2i(rect.x, rect.y), r.clip);
+ ty_Vec2i p2 = clamp_point(ty_recti_end(rect), r.clip);
for (int dy = p1.y; dy < p2.y; dy++) {
for (int dx = p1.x; dx < p2.x; dx++) {