summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authororangerot <orangerot@orangerot.dev>2025-10-15 16:02:36 +0200
committerorangerot <orangerot@orangerot.dev>2025-10-15 16:02:36 +0200
commit6f3e302e0998fd3b5c98a6ad531093fc2678fc96 (patch)
treecea787ac32e743da6fdb6629de5f01719c0c6ac2
parenta65050fbb3475e94f842fe35cca2652c02cb339d (diff)
fix: crash when dragging out of the window
-rw-r--r--game.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/game.c b/game.c
index e05f39b..04e7ba2 100644
--- a/game.c
+++ b/game.c
@@ -220,9 +220,10 @@ void init(struct image canvas) {
void draw( struct image canvas, struct image texture, size_t xpos, size_t ypos, bool vertical) {
for (size_t y = 0; y < texture.height; y++) {
for (size_t x = 0; x < texture.width; x++) {
- size_t canvas_y = vertical ? x : y;
- size_t canvas_x = vertical ? y : x;
- canvas.buf[(ypos + canvas_y) * canvas.width + xpos + canvas_x] = texture.buf[y * texture.width + x];
+ int canvas_y = (vertical ? x : y) + ypos;
+ int canvas_x = (vertical ? y : x) + xpos;
+ if (canvas_x < 0 || canvas_x >= (int) canvas.width - 1 || canvas_y < 0 || canvas_y >= (int) canvas.height - 1) continue;
+ canvas.buf[canvas_y * canvas.width + canvas_x] = texture.buf[y * texture.width + x];
}
}
}
@@ -280,7 +281,7 @@ void render(struct image canvas) {
// active
if (has_active) {
- draw(canvas, red_and_peach_dominoes[active.back.val][active.front.val], CLAMP(mouse_x + active.front.x, 0, canvas.width), CLAMP(mouse_y + active.front.y, 0, canvas.height), active.front.vertical);
+ draw(canvas, red_and_peach_dominoes[active.back.val][active.front.val], mouse_x + active.front.x, mouse_y + active.front.y, active.front.vertical);
}
// character