refactored the error handling a bit
This commit is contained in:
parent
81f38daf23
commit
0bfb9a46cd
@ -15,6 +15,8 @@
|
|||||||
|
|
||||||
# include "cub3d.h"
|
# 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
|
#endif
|
||||||
|
|||||||
20
src/errors.c
20
src/errors.c
@ -13,14 +13,22 @@
|
|||||||
#include "MLX42.h"
|
#include "MLX42.h"
|
||||||
#include "cub3d.h"
|
#include "cub3d.h"
|
||||||
|
|
||||||
void game_error(t_game *game, const char *msg, int mlx_error)
|
const char *last_error()
|
||||||
{
|
{
|
||||||
if (mlx_error)
|
if (mlx_errno > 0)
|
||||||
printf(RED"%s\n"RESET, mlx_strerror(mlx_errno));
|
return (mlx_strerror(mlx_errno));
|
||||||
else if (msg == NULL)
|
else if (errno > 0)
|
||||||
printf(RED"%s\n"RESET, strerror(errno));
|
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
|
else
|
||||||
printf(RED"%s\n"RESET, msg);
|
printf(RED"%s\n"RESET, msg);
|
||||||
game_terminate(game);
|
game_terminate(game);
|
||||||
exit(FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,16 +10,13 @@
|
|||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "game.h"
|
#include "cub3d.h"
|
||||||
|
|
||||||
int game_create(t_game **game)
|
int game_create(t_game **game)
|
||||||
{
|
{
|
||||||
*game = malloc(sizeof(t_game));
|
*game = malloc(sizeof(t_game));
|
||||||
if (!game)
|
if (!game)
|
||||||
{
|
return (FAILURE);
|
||||||
printf("malloc: game failed\n");
|
|
||||||
exit(FAILURE);
|
|
||||||
}
|
|
||||||
(*game)->player = NULL;
|
(*game)->player = NULL;
|
||||||
(*game)->map = NULL;
|
(*game)->map = NULL;
|
||||||
(*game)->screen = screen_create();
|
(*game)->screen = screen_create();
|
||||||
@ -53,5 +50,5 @@ void game_terminate(t_game *game)
|
|||||||
if (game->map)
|
if (game->map)
|
||||||
free(game->map);
|
free(game->map);
|
||||||
free(game);
|
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"
|
# include "cub3d.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
@ -19,7 +18,7 @@ int main(void)
|
|||||||
|
|
||||||
game = NULL;
|
game = NULL;
|
||||||
if (! game_create(&game)){
|
if (! game_create(&game)){
|
||||||
game_error(game, "Failed to create game", 0);
|
game_error(game, NULL);
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
screen_center(game->screen);
|
screen_center(game->screen);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user