From f7ebb6b70ca6fe44401b4bdb8ea7025ba702f8ed Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 19 Apr 2024 19:54:04 +0200 Subject: [PATCH] fix: segfault when changing level * init_level() was called while room_loop()'s execution --- includes/main/env.h | 3 ++- src/main/levels1.c | 11 ++++++----- src/main/levels2.c | 4 +++- src/main/main.c | 8 +++++++- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/includes/main/env.h b/includes/main/env.h index 740e575..7302942 100644 --- a/includes/main/env.h +++ b/includes/main/env.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */ -/* Updated: 2024/04/19 14:14:09 by mcolonna ### ########.fr */ +/* Updated: 2024/04/19 19:39:18 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ typedef struct s_env const t_const_string *levels; int level_count; int level_current; + int level_to_load; // room t_room room; t_camera camera; diff --git a/src/main/levels1.c b/src/main/levels1.c index 6c4b077..7e99138 100644 --- a/src/main/levels1.c +++ b/src/main/levels1.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/18 17:49:39 by mcolonna #+# #+# */ -/* Updated: 2024/04/19 14:13:01 by mcolonna ### ########.fr */ +/* Updated: 2024/04/19 20:35:03 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ void ask_retry(bool current) { g_env.moves = 0; g_env.level_current = 0; + g_env.level_to_load = 0; } init_level(); } @@ -34,6 +35,8 @@ void init_levels(int count, const t_const_string *srcs) g_env.moves = 0; g_env.levels = srcs; g_env.level_count = count; + g_env.level_current = 0; + g_env.level_to_load = 0; if (count <= 0) error_err("if you don't want to play any level don't start the game"); init_level(); @@ -61,12 +64,10 @@ static void end_draw(void) void win(void) { - g_env.level_current++; - if (g_env.level_current >= g_env.level_count) + g_env.level_to_load = g_env.level_current + 1; + if (g_env.level_to_load >= g_env.level_count) { g_loopfunctions.loop = end_loop; g_loopfunctions.draw = end_draw; } - else - init_level(); } diff --git a/src/main/levels2.c b/src/main/levels2.c index 4280910..0f483b6 100644 --- a/src/main/levels2.c +++ b/src/main/levels2.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/19 13:18:06 by mcolonna #+# #+# */ -/* Updated: 2024/04/19 13:18:18 by mcolonna ### ########.fr */ +/* Updated: 2024/04/19 19:46:14 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ void init_level(void) { + if (g_env.level_current >= g_env.level_count) + win(); g_loopfunctions.loop = room_loop; g_loopfunctions.draw = room_draw; room_init(g_env.levels[g_env.level_current]); diff --git a/src/main/main.c b/src/main/main.c index 60ccfe0..1d7e6d9 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */ -/* Updated: 2024/04/19 14:11:01 by mcolonna ### ########.fr */ +/* Updated: 2024/04/19 20:30:19 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,12 @@ int close_hook(void) static void loop(void) { + if (g_loopfunctions.loop == room_loop + && g_env.level_to_load != g_env.level_current) + { + g_env.level_current = g_env.level_to_load; + init_level(); + } input_loop(); g_loopfunctions.loop(); display_erase();