From 01432a529c665a57e83bfbd670c3f277e1029ac9 Mon Sep 17 00:00:00 2001 From: Ty3r0X Date: Fri, 15 Dec 2023 19:38:51 +0200 Subject: [PATCH 1/5] Added delay, finally CPU can breathe... --- src/init.c | 4 ++-- src/main.c | 6 ++++-- src/window.h | 2 ++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/init.c b/src/init.c index b5da705..5ec1551 100644 --- a/src/init.c +++ b/src/init.c @@ -71,8 +71,8 @@ init_program (void) { /* Initialize rectangle*/ rectangle = calloc (1, sizeof (SDL_Rect)); - rectangle->w = 100; - rectangle->h = 100; + rectangle->w = RECT_SIZE; + rectangle->h = RECT_SIZE; /* A new variable is created called main_event, which is a SDL_Event type, which itself is a structure * of multiple structures... of events... */ diff --git a/src/main.c b/src/main.c index 3b10a85..c7bb20d 100644 --- a/src/main.c +++ b/src/main.c @@ -67,8 +67,8 @@ main (int argc, char *argv[]) { /* Constantly initialize boolean values for corners, each for loop * iteration they change values */ - bool x_limit = (rectangle->x == 0 || rectangle->x == SCREEN_WIDTH); - bool y_limit = (rectangle->y == 0 || rectangle->y == SCREEN_HEIGHT); + bool x_limit = (rectangle->x == 0 || rectangle->x == SCREEN_WIDTH - RECT_SIZE); + bool y_limit = (rectangle->y == 0 || rectangle->y == SCREEN_HEIGHT - RECT_SIZE); /* Normally the rect constantly goes up diagonally, if it reaches a * corner the next position gets multiplied with -1 on its respective @@ -110,6 +110,8 @@ main (int argc, char *argv[]) { rectangle->x = rectangle->x + (1 * ray.x); rectangle->y = rectangle->y + (1 * ray.y); + SDL_Delay (3); + /*while (SDL_PollEvent (main_event)) { switch (main_event->type) { case SDL_MOUSEMOTION: diff --git a/src/window.h b/src/window.h index 9371f98..a7593dc 100644 --- a/src/window.h +++ b/src/window.h @@ -39,6 +39,8 @@ #define COLOR_G 0xA5 #define COLOR_B 0x00 +#define RECT_SIZE 100 + extern SDL_Window *window; extern SDL_Renderer *main_render; extern SDL_Texture *background; From 144342ab8110a20618c84380e015f35c4561f266 Mon Sep 17 00:00:00 2001 From: Ty3r0X Date: Sat, 16 Dec 2023 09:00:08 +0200 Subject: [PATCH 2/5] Fix bug: square does not honour the limits If the square's x pos is placed a tiny bit outside the canvas, it will infinitely go out of bounds, so I placed a comparison to mitigate that. Thank you @thefirethirteen for letting me know ^_^ --- src/main.c | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/main.c b/src/main.c index c7bb20d..c5b8a75 100644 --- a/src/main.c +++ b/src/main.c @@ -67,8 +67,8 @@ main (int argc, char *argv[]) { /* Constantly initialize boolean values for corners, each for loop * iteration they change values */ - bool x_limit = (rectangle->x == 0 || rectangle->x == SCREEN_WIDTH - RECT_SIZE); - bool y_limit = (rectangle->y == 0 || rectangle->y == SCREEN_HEIGHT - RECT_SIZE); + bool x_limit = (rectangle->x <= 0 || rectangle->x >= SCREEN_WIDTH - RECT_SIZE); + bool y_limit = (rectangle->y <= 0 || rectangle->y >= SCREEN_HEIGHT - RECT_SIZE); /* Normally the rect constantly goes up diagonally, if it reaches a * corner the next position gets multiplied with -1 on its respective @@ -112,26 +112,5 @@ main (int argc, char *argv[]) { SDL_Delay (3); - /*while (SDL_PollEvent (main_event)) { - switch (main_event->type) { - case SDL_MOUSEMOTION: - if (check_interaction_in_rect (main_event->motion.x, main_event->motion.y, rectangle)) { - rectangle->x = rand () % 500; - rectangle->y = rand () % 500; - printf ("You touched the square! X position: %d, Y position %d\n", main_event->motion.x, main_event->motion.y); - } - break; - - case SDL_WINDOWEVENT: - printf ("Window position is being altered!\n"); - printf ("Current window position: (%d,%d)\t", main_event->window.data1, main_event->window.data2); - break; - - default: - printf ("Unhandled event\n\n"); - break; - } - } - */ } } From 9dbcbc62a2b0cbcc3400656d16b11b6ebcddd893 Mon Sep 17 00:00:00 2001 From: Ty3r0X Date: Sun, 17 Dec 2023 21:11:39 +0200 Subject: [PATCH 3/5] Implemented caskd's fix for the Makefile Argument order matters, and in some systems it ain't compiling, so this is set in place just for that. Kudos to @casKd-dev --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 5076965..8eaf384 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,13 @@ OBJ := $(SRC:.c=.o) all: $(OBJ) $(OUT) %.o: %.c - $(CC) -o $@ -c $(CFLAGS) $< + $(CC) $< -c $(CFLAGS) -o "$@" clean: rm -f $(OBJ) $(OUT) ifneq ($(OUT),) $(OUT): $(OBJ) - $(CC) -o "$@" $(LDFLAGS) $^ + $(CC) $^ $(LDFLAGS) -o "$@" endif .PHONY: all clean From 357e70e12989ac9d92ac0ffa80eb7be3a00cb32c Mon Sep 17 00:00:00 2001 From: Ty3r0X Date: Tue, 19 Dec 2023 12:25:33 +0200 Subject: [PATCH 4/5] 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 5/5] 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;