aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--teensy/teensy.h5
-rw-r--r--teensy/teensy_renderer.c44
-rw-r--r--teensy/teensy_renderer.h1
3 files changed, 36 insertions, 14 deletions
diff --git a/teensy/teensy.h b/teensy/teensy.h
index 1a738d7..300939c 100644
--- a/teensy/teensy.h
+++ b/teensy/teensy.h
@@ -23,6 +23,8 @@
#define ty_max(a, b) ((a) > (b) ? (a) : (b))
#define ty_min(a, b) ((a) < (b) ? (a) : (b))
+#define TY_CLIP_NONE ((ty_Recti){0})
+
#define TY_COLOR_BLACK ty_color(0, 0, 0 )
#define TY_COLOR_RED ty_color(255, 0, 0 )
#define TY_COLOR_GREEN ty_color(0, 255, 0 )
@@ -178,6 +180,9 @@ int ty_font_height(const ty_Font *font);
// Sets the image that the renderer draws to. Pass NULL to restore the screen.
void ty_draw_set_target(const ty_Image *img);
+// Prevents anything from being drawn outside of the passed rect. Pass
+// TY_CLIP_NONE to reset the clipspace.
+void ty_draw_set_clip(ty_Recti clip);
// Sets the entire render target to one color.
void ty_draw_clear(ty_Color col);
// Draws an image at the specified position. The color #FF00FF will be ignored
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++) {
diff --git a/teensy/teensy_renderer.h b/teensy/teensy_renderer.h
index ebd5d6e..28843ac 100644
--- a/teensy/teensy_renderer.h
+++ b/teensy/teensy_renderer.h
@@ -7,6 +7,7 @@
typedef struct {
ty_Image screen;
ty_Image target;
+ ty_Recti clip;
} ty_Renderer;
extern ty_Renderer r;