Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: protected some functions that could fail #28

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void init_hooks(t_data *data);
int init_mlx(t_data *data);
void init2null(t_data *data);
// init/keys.c
int init_keys(t_key *keys, t_player *players);
void init_keys(t_key *keys, t_player *players);
// init/player.c
int init_players(t_player **players, t_map *map);

Expand Down
8 changes: 6 additions & 2 deletions src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ void delta_time(t_data *data)
struct timeval tv_now;

if (gettimeofday(&tv_now, NULL))
printf("gettimeofday failed");
{
printf("Error: gettimeofday failed\n");
free_and_exit(data, 1);
}
data->delta_time = (tv_now.tv_sec * 1000 + tv_now.tv_usec / 1000)
- (data->lastframe.tv_sec * 1000 + data->lastframe.tv_usec / 1000);
data->lastframe = tv_now;
Expand Down Expand Up @@ -48,7 +51,8 @@ int loop_hook(t_data *data)
}
render_walls(data);
render_minimap(data);
mlx_put_image_to_window(data->mlx, data->win, data->img, 0, 0);
if (mlx_put_image_to_window(data->mlx, data->win, data->img, 0, 0))
free_and_exit(data, 1);
delta_time(data);
return (0);
}
Expand Down
5 changes: 4 additions & 1 deletion src/init/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ void init2null(t_data *data)
data->players = NULL;
data->use_mouse = false;
if (gettimeofday(&tv_now, NULL))
printf("gettimeofday failed");
{
printf("Error: gettimeofday failed\n");
free_and_exit(data, 1);
}
data->lastframe = tv_now;
}
3 changes: 1 addition & 2 deletions src/init/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static t_key init_key_look(int keycode, t_player *player, double rotation)
}

// Needs to match NUM_OF_KEYS
int init_keys(t_key *keys, t_player *players)
void init_keys(t_key *keys, t_player *players)
{
keys[0] = init_key_move(XK_Escape, &players[0], (t_dvec2){0.0, 0.0});
keys[1] = init_key_move(XK_w, &players[0], (t_dvec2){0.0, 1.0});
Expand All @@ -46,5 +46,4 @@ int init_keys(t_key *keys, t_player *players)
keys[4] = init_key_move(XK_d, &players[0], (t_dvec2){-1.0, 0.0});
keys[5] = init_key_look(XK_Right, &players[0], (double)1.0);
keys[6] = init_key_look(XK_Left, &players[0], (double)-1.0);
return (0);
}
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ int main(int argc, char **argv)
init_keys(data.keys, data.players);
init_hooks(&data);
mlx_loop(data.mlx);
free_and_exit(&data, 0);
free_and_exit(&data, 1);
}