refactored the error handling a bit
This commit is contained in:
parent
81f38daf23
commit
0bfb9a46cd
@ -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
|
||||
|
||||
20
src/errors.c
20
src/errors.c
@ -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);
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user