refactor: refactored initialization

This commit is contained in:
Quinten Mennen 2025-04-22 17:28:26 +02:00
parent 840accea52
commit 55da3d92ed
10 changed files with 105 additions and 119 deletions

View File

@ -3,7 +3,8 @@
"game.h": "c", "game.h": "c",
"cub3d.h": "c", "cub3d.h": "c",
"screen.h": "c", "screen.h": "c",
"mlx42.h": "c" "mlx42.h": "c",
"glfw3.h": "c"
}, },
"cmake.ignoreCMakeListsMissing": true "cmake.ignoreCMakeListsMissing": true
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* cub3d.h :+: :+: */ /* cub3d.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ /* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/04/19 14:46:34 by whaffman ######## odam.nl */ /* Updated: 2025/04/22 17:15:32 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,4 +48,6 @@
# include "collision.h" # include "collision.h"
# include "parser.h" # include "parser.h"
int initialize_cub3d(t_game **game, const char *mapfile);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */ /* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */
/* Updated: 2025/04/15 17:58:22 by qmennen ### ########.fr */ /* Updated: 2025/04/22 17:11:16 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,7 @@
int game_create(t_game **game); int game_create(t_game **game);
void game_loop(void *param); void game_loop(void *param);
void game_free(t_game *game);
void game_terminate(t_game *game); void game_terminate(t_game *game);
#endif #endif

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* map.h :+: :+: */ /* map.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 19:19:19 by qmennen #+# #+# */ /* Created: 2025/04/17 19:19:19 by qmennen #+# #+# */
/* Updated: 2025/04/20 13:51:19 by whaffman ######## odam.nl */ /* Updated: 2025/04/22 17:24:47 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
# include "cub3d.h" # include "cub3d.h"
int map_create(t_game **game, int argc, char **argv); int map_create(t_game **game, const char *mapfile);
void map_free(t_map *map); void map_free(t_map *map);
void print_map(t_map *map); void print_map(t_map *map);
t_tile get_tile(t_map * map, int x, int y); t_tile get_tile(t_map * map, int x, int y);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:30:34 by qmennen #+# #+# */ /* Created: 2025/04/15 15:30:34 by qmennen #+# #+# */
/* Updated: 2025/04/15 16:48:24 by qmennen ### ########.fr */ /* Updated: 2025/04/22 17:20:41 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
#include "cub3d.h" #include "cub3d.h"
t_screen *screen_create(); int screen_create(t_game **game);
void screen_center(t_screen *screen); int screen_display(t_screen *screen);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/04/22 15:24:24 by qmennen ### ########.fr */ /* Updated: 2025/04/22 17:25:34 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,12 +19,8 @@ int game_create(t_game **game)
return (FAILURE); return (FAILURE);
(*game)->player = NULL; (*game)->player = NULL;
(*game)->map = NULL; (*game)->map = NULL;
(*game)->screen = screen_create(); (*game)->screen = NULL;
if ((*game)->screen == NULL) (*game)->keyboard = NULL;
return (FAILURE);
if (((*game)->screen->mlx == NULL) || ((*game)->screen->img == NULL))
return (FAILURE);
mlx_image_to_window((*game)->screen->mlx, (*game)->screen->img, 0, 0);
return (SUCCESS); return (SUCCESS);
} }
@ -42,7 +38,7 @@ void game_loop(void *param)
keyboard_update(game); // Goes last keyboard_update(game); // Goes last
} }
void game_terminate(t_game *game) void game_free(t_game *game)
{ {
if (game->screen) if (game->screen)
{ {
@ -58,5 +54,10 @@ void game_terminate(t_game *game)
if (game->keyboard) if (game->keyboard)
free(game->keyboard); free(game->keyboard);
free(game); free(game);
}
void game_terminate(t_game *game)
{
game_free(game);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@ -1,71 +1,36 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* main.c :+: :+: */ /* main.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */ /* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
/* Updated: 2025/04/20 13:46:26 by whaffman ######## odam.nl */ /* Updated: 2025/04/22 17:21:50 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# include "cub3d.h" # include "cub3d.h"
static int init_game(t_game **game, int argc, char **argv)
{
if (!game_create(game))
return (FAILURE);
if (!player_create(game))
return (FAILURE);
if (!map_create(game, argc, argv))
return (FAILURE);
if (!keyboard_create(game))
return (FAILURE);
screen_center((*game)->screen);
mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
return (SUCCESS);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_game *game; t_game *game;
errno = 0; errno = 0;
game = NULL; game = NULL;
init_game(&game, argc, argv); if (argc != 2)
{
ft_putstr_fd("Usage: ./cub3d <map.cub>\n", 2);
return (EXIT_FAILURE);
}
if (!initialize_cub3d(&game, argv[1]))
{
perror("initialize_cub3d");
game_free(game);
return (EXIT_FAILURE);
}
perror("after init"); perror("after init");
mlx_loop(game->screen->mlx); mlx_loop(game->screen->mlx);
game_terminate(game); game_terminate(game);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
// int main(int argc, char **argv)
// {
// t_map map;
// char *buffer;
// if (argc != 2)
// {
// ft_putstr_fd("Usage: ./cub3d <map.cub>\n", 2);
// return (1);
// }
// buffer = read_map_file(argv[1]);
// if (!buffer)
// {
// perror("Error reading map file");
// return (1);
// }
// if (parse_file(buffer, &map) == FAILURE)
// {
// ft_putstr_fd("Error parsing map file\n", 2);
// free(buffer);
// return (1);
// }
// print_map(&map);
// free(buffer);
// return (0);
// }

View File

@ -149,36 +149,11 @@ int enclosed_map(t_map *map)
return SUCCESS; return SUCCESS;
} }
// int main(void) static int parse_args(const char *mapfile, t_map *map)
// {
// t_map *map;
// map = get_temp_map();
// if (!map)
// {
// fprintf(stderr, "Failed to allocate memory for map\n");
// return 1;
// }
// print_map(map);
// if(!enclosed_map(map))
// fprintf(stderr, "NOT GOOD MAP FRIEND\n");
// else
// fprintf(stderr, "YES, GOOD MAP FRIEND\n");
// free_map(&map);
// return 0;
// }
static int parse_args(int argc, char **argv, t_map *map)
{ {
char *buffer; char *buffer;
if (argc != 2) buffer = read_map_file(mapfile);
{
ft_putstr_fd("Usage: ./cub3d <map.cub>\n", 2);
return (FAILURE);
}
buffer = read_map_file(argv[1]);
if (!buffer) if (!buffer)
{ {
perror("Error reading map file"); perror("Error reading map file");
@ -195,7 +170,7 @@ static int parse_args(int argc, char **argv, t_map *map)
return (SUCCESS); return (SUCCESS);
} }
int map_create(t_game **game, int argc, char **argv) int map_create(t_game **game, const char *mapfile)
{ {
t_map *map; t_map *map;
@ -208,7 +183,7 @@ int map_create(t_game **game, int argc, char **argv)
perror("Error allocating memory for map"); perror("Error allocating memory for map");
return (FAILURE); return (FAILURE);
} }
parse_args(argc, argv, map); parse_args(mapfile, map);
print_map(map); print_map(map);
if(!enclosed_map(map)) if(!enclosed_map(map))
fprintf(stderr, "NOT GOOD MAP FRIEND\n"); fprintf(stderr, "NOT GOOD MAP FRIEND\n");

View File

@ -6,15 +6,13 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */ /* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
/* Updated: 2025/04/15 19:21:23 by qmennen ### ########.fr */ /* Updated: 2025/04/22 17:25:13 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "screen.h" #include "screen.h"
#include "MLX42.h"
#include <GLFW/glfw3.h>
t_screen *screen_create() int screen_create(t_game **game)
{ {
t_screen *screen; t_screen *screen;
mlx_image_t *img; mlx_image_t *img;
@ -22,29 +20,40 @@ t_screen *screen_create()
screen = malloc(sizeof(t_screen)); screen = malloc(sizeof(t_screen));
if (!screen) if (!screen)
return (NULL); return (FAILURE);
screen->width = WIDTH; screen->width = WIDTH;
screen->height = HEIGHT; screen->height = HEIGHT;
mlx = mlx_init(WIDTH, HEIGHT, TITLE, false); mlx = mlx_init(WIDTH, HEIGHT, TITLE, false);
if (!mlx)
return (FAILURE);
//TODO: figure out why errno = 11 after this call //TODO: figure out why errno = 11 after this call
screen->mlx = mlx; screen->mlx = mlx;
img = mlx_new_image(screen->mlx, WIDTH, HEIGHT); img = mlx_new_image(screen->mlx, WIDTH, HEIGHT);
if (!img)
return (FAILURE);
screen->img = img; screen->img = img;
return (screen); (*game)->screen = screen;
return (SUCCESS);
} }
void screen_center(t_screen *screen) int screen_display(t_screen *screen)
{ {
int m_width; int m_width;
int m_height; int m_height;
m_width = 0; m_width = 0;
m_height = 0; m_height = 0;
if (mlx_image_to_window(screen->mlx, screen->img, 0, 0) < 0)
{
printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE);
}
mlx_get_monitor_size(0, &m_width, &m_height); mlx_get_monitor_size(0, &m_width, &m_height);
if (m_width == 0 || m_height == 0) if (m_width == 0 || m_height == 0)
{ {
printf(RED"Failed to retrieve monitor size to center window\n"RESET); printf(RED"Failed to retrieve monitor size to center window\n"RESET);
return ; return (FAILURE);
} }
mlx_set_window_pos(screen->mlx, (m_width - screen->width) / 2, (m_height - screen->height) / 2); mlx_set_window_pos(screen->mlx, (m_width - screen->width) / 2, (m_height - screen->height) / 2);
return (SUCCESS);
} }

32
src/util/initialize.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* initialize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
/* Updated: 2025/04/22 17:21:37 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int initialize_cub3d(t_game **game, const char *mapfile)
{
if (!game_create(game))
return (FAILURE);
if (!map_create(game, mapfile))
return (FAILURE);
if (!screen_create(game))
return (FAILURE);
if (!player_create(game))
return (FAILURE);
if (!keyboard_create(game))
return (FAILURE);
if (!screen_display((*game)->screen))
return (FAILURE);
mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
return (SUCCESS);
}