refactored the error handling a bit

This commit is contained in:
Quinten Mennen 2025-04-15 18:52:47 +02:00
parent 81f38daf23
commit 0bfb9a46cd
4 changed files with 21 additions and 15 deletions

View File

@ -15,6 +15,8 @@
# include "cub3d.h"
void game_error(t_game *game, const char *msg, int mlx_error);
const char *last_error();
void game_error(t_game *game, const char *msg);
#endif

View File

@ -13,14 +13,22 @@
#include "MLX42.h"
#include "cub3d.h"
void game_error(t_game *game, const char *msg, int mlx_error)
const char *last_error()
{
if (mlx_error)
printf(RED"%s\n"RESET, mlx_strerror(mlx_errno));
else if (msg == NULL)
printf(RED"%s\n"RESET, strerror(errno));
if (mlx_errno > 0)
return (mlx_strerror(mlx_errno));
else if (errno > 0)
return (strerror(errno));
else
return (NULL);
}
void game_error(t_game *game, const char *msg)
{
const char *last_err = last_error();
if (msg == NULL && last_err)
printf(RED"%s\n"RESET, last_err);
else
printf(RED"%s\n"RESET, msg);
game_terminate(game);
exit(FAILURE);
}

View File

@ -10,16 +10,13 @@
/* */
/* ************************************************************************** */
#include "game.h"
#include "cub3d.h"
int game_create(t_game **game)
{
*game = malloc(sizeof(t_game));
if (!game)
{
printf("malloc: game failed\n");
exit(FAILURE);
}
return (FAILURE);
(*game)->player = NULL;
(*game)->map = NULL;
(*game)->screen = screen_create();
@ -53,5 +50,5 @@ void game_terminate(t_game *game)
if (game->map)
free(game->map);
free(game);
exit(EXIT_SUCCESS);
exit(EXIT_SUCCESS); //TODO: Make this variable on the last error
}

View File

@ -10,7 +10,6 @@
/* */
/* ************************************************************************** */
#include "MLX42.h"
# include "cub3d.h"
int main(void)
@ -19,7 +18,7 @@ int main(void)
game = NULL;
if (! game_create(&game)){
game_error(game, "Failed to create game", 0);
game_error(game, NULL);
return (EXIT_FAILURE);
}
screen_center(game->screen);