Skip to content

Commit 14a79c5

Browse files
committed
fix seg fault
1 parent 171e030 commit 14a79c5

3 files changed

Lines changed: 47 additions & 27 deletions

File tree

srcs/engine/draw/draw_menu.c

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,48 +50,58 @@ void draw_select_gun(t_game *game, int position)
5050
}
5151
}
5252

53-
static void set_animation_position(t_texture *frame, t_vector *pos)
53+
static void process_animation_pixels(t_game *game, t_texture *frame,
54+
t_vector pos)
5455
{
55-
pos->x = (WINDOW_WIDTH - frame->width) >> 1;
56-
pos->y = (WINDOW_HEIGHT - frame->height) >> 1;
57-
}
58-
59-
void draw_animation(t_game *game, t_animation *anim)
60-
{
61-
t_texture *current_frame;
62-
t_vector pos;
63-
t_vector_i pixel;
64-
unsigned int color;
56+
t_vector_i pixel;
57+
int color;
6558

66-
if (!anim->active || anim->frame_count == 0)
67-
return ;
68-
current_frame = &anim->frames[anim->current_frame];
69-
if (!current_frame->img)
70-
return ;
71-
set_animation_position(current_frame, &pos);
7259
pixel.y = -1;
73-
while (++pixel.y < current_frame->height)
60+
while (++pixel.y < frame->height)
7461
{
7562
pixel.x = -1;
76-
while (++pixel.x < current_frame->width)
63+
while (++pixel.x < frame->width)
7764
{
7865
if (pos.x + pixel.x < 0 || pos.x + pixel.x >= WINDOW_WIDTH || pos.y
7966
+ pixel.y < 0 || pos.y + pixel.y >= WINDOW_HEIGHT)
8067
continue ;
81-
color = get_texture_pixel(current_frame, pixel.x, pixel.y);
68+
color = get_texture_pixel(frame, pixel.x, pixel.y);
8269
if ((color & 0xFFC0CB) != 0)
8370
draw_pixel(game, pos.x + pixel.x, pos.y + pixel.y, color);
8471
}
8572
}
8673
}
8774

75+
void draw_animation(t_game *game, t_animation *anim)
76+
{
77+
t_texture *current_frame;
78+
t_vector pos;
79+
80+
if (!anim)
81+
return ;
82+
if (!anim->active || anim->frame_count == 0)
83+
return ;
84+
current_frame = &anim->frames[anim->current_frame];
85+
if (!current_frame->img)
86+
return ;
87+
pos.x = (WINDOW_WIDTH - current_frame->width) >> 1;
88+
pos.y = (WINDOW_HEIGHT - current_frame->height) >> 1;
89+
process_animation_pixels(game, current_frame, pos);
90+
}
91+
8892
void draw_menu(t_game *game)
8993
{
94+
if (!game || !game->menu)
95+
return ;
9096
update_menu_animations(game);
91-
draw_animation(game, &game->menu->bg);
92-
draw_animation(game, &game->menu->cube);
93-
draw_animation(game, &game->menu->start_btn.anim);
94-
draw_animation(game, &game->menu->exit_btn.anim);
97+
if (game->menu->bg.active)
98+
draw_animation(game, &game->menu->bg);
99+
if (game->menu->cube.active)
100+
draw_animation(game, &game->menu->cube);
101+
if (game->menu->start_btn.anim.active)
102+
draw_animation(game, &game->menu->start_btn.anim);
103+
if (game->menu->exit_btn.anim.active)
104+
draw_animation(game, &game->menu->exit_btn.anim);
95105
if (game->menu->selected_option == 0)
96106
draw_select_gun(game, 1);
97107
else

srcs/events/key_handle2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ static void handle_menu_selection(t_game *game)
3333
}
3434
else
3535
{
36-
cleanup_menu(game);
3736
close_window(game);
37+
cleanup_menu(game);
3838
}
3939
}
4040

srcs/initializer/menu_animation.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ void update_menu_animations(t_game *g)
1616
{
1717
double current_time;
1818

19+
if (!g || !g->menu)
20+
return ;
1921
current_time = get_time_ms();
2022
if (g->menu->bg.active && g->menu->bg.frame_count > 1)
2123
{
@@ -32,6 +34,8 @@ void update_menu_animations(t_game *g)
3234

3335
void update_cube_exit_animations(t_game *g, double current_time)
3436
{
37+
if (!g || !g->menu)
38+
return ;
3539
if (g->menu->cube.active && g->menu->cube.frame_count > 1)
3640
{
3741
if (current_time
@@ -47,20 +51,26 @@ void update_cube_exit_animations(t_game *g, double current_time)
4751

4852
void update_start_exit_animations(t_game *game, double current_time)
4953
{
54+
if (!game || !game->menu)
55+
return ;
5056
if (game->menu->selected_option == 0)
5157
{
5258
update_button_animation(&game->menu->start_btn.anim, current_time);
53-
game->menu->exit_btn.anim.current_frame = 0;
59+
if (game->menu->exit_btn.anim.frame_count > 0)
60+
game->menu->exit_btn.anim.current_frame = 0;
5461
}
5562
else
5663
{
5764
update_button_animation(&game->menu->exit_btn.anim, current_time);
58-
game->menu->start_btn.anim.current_frame = 0;
65+
if (game->menu->start_btn.anim.frame_count > 0)
66+
game->menu->start_btn.anim.current_frame = 0;
5967
}
6068
}
6169

6270
void update_button_animation(t_animation *anim, double current_time)
6371
{
72+
if (!anim)
73+
return ;
6474
if (anim->active && anim->frame_count > 1)
6575
{
6676
if (current_time - anim->last_update >= anim->frame_duration)

0 commit comments

Comments
 (0)