fix: segfault when changing level *

init_level() was called while room_loop()'s execution
This commit is contained in:
mcolonna 2024-04-19 19:54:04 +02:00
parent bee91cfd7c
commit f7ebb6b70c
4 changed files with 18 additions and 8 deletions

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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]);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();