From 6f3e302e0998fd3b5c98a6ad531093fc2678fc96 Mon Sep 17 00:00:00 2001 From: orangerot Date: Wed, 15 Oct 2025 16:02:36 +0200 Subject: [PATCH] fix: crash when dragging out of the window --- game.c | 9 +++++---- 1 file 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