From 357e70e12989ac9d92ac0ffa80eb7be3a00cb32c Mon Sep 17 00:00:00 2001 From: Ty3r0X Date: Tue, 19 Dec 2023 12:25:33 +0200 Subject: [PATCH 1/2] Fixed bug: square gets stuck when placed out of bounds The rand() funtion now gets the correct limits, so in theory the square shouldn't go out of bounds anymore... --- src/main.c | 5 ++--- src/window.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index c5b8a75..8354dfa 100644 --- a/src/main.c +++ b/src/main.c @@ -98,8 +98,8 @@ main (int argc, char *argv[]) { printf ("Mouse cursor is inside the square at position (%d,%d)\n", main_event->motion.x, main_event->motion.y); ray.x *= -1; ray.y *= -1; - rectangle->x = rand () % 500; - rectangle->y = rand () % 500; + rectangle->x = rand () % (int) (SCREEN_WIDTH - 1 - RECT_SIZE); + rectangle->y = rand () % (int) (SCREEN_HEIGHT - 1 - RECT_SIZE); bg_red = rand () & HEX_POKE; bg_green = rand () & HEX_POKE; bg_blue = rand () & HEX_POKE; @@ -111,6 +111,5 @@ main (int argc, char *argv[]) { rectangle->y = rectangle->y + (1 * ray.y); SDL_Delay (3); - } } diff --git a/src/window.h b/src/window.h index a7593dc..ad7a948 100644 --- a/src/window.h +++ b/src/window.h @@ -39,7 +39,7 @@ #define COLOR_G 0xA5 #define COLOR_B 0x00 -#define RECT_SIZE 100 +#define RECT_SIZE 300 extern SDL_Window *window; extern SDL_Renderer *main_render; From a41d0ba51c41612a58a08a8beb6d25f4dd70a2de Mon Sep 17 00:00:00 2001 From: Ty3r0X Date: Tue, 19 Dec 2023 12:32:25 +0200 Subject: [PATCH 2/2] Forgot to revert the size back to 100 4 the square --- src/window.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/window.h b/src/window.h index ad7a948..a7593dc 100644 --- a/src/window.h +++ b/src/window.h @@ -39,7 +39,7 @@ #define COLOR_G 0xA5 #define COLOR_B 0x00 -#define RECT_SIZE 300 +#define RECT_SIZE 100 extern SDL_Window *window; extern SDL_Renderer *main_render;