From 05e24d19f9829c96ca22ec2162694e6f08e851ec Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Thu, 31 Jul 2025 13:31:05 +0200 Subject: [PATCH] cleanup: moved initialization code from main() to a separate file --- Makefile | 1 + src/main.c | 26 +++----------------------- src/utils/inits.c | 38 ++++++++++++++++++++++++++++++++++++++ src/utils/inits.h | 20 ++++++++++++++++++++ 4 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 src/utils/inits.c create mode 100644 src/utils/inits.h diff --git a/Makefile b/Makefile index 52da8cd..f1539c3 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ endif IFLAGS = -I./mlx -I./libft SOURCEFILES = \ + src/utils/inits.c \ src/draw/draw_map.c \ src/draw/drawutils.c \ src/main.c \ diff --git a/src/main.c b/src/main.c index fb16572..1ca5d62 100644 --- a/src/main.c +++ b/src/main.c @@ -6,25 +6,21 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/31 13:25:36 by kcolin ### ########.fr */ +/* Updated: 2025/07/31 13:30:05 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #include "../libft/includes/libft.h" #include "../mlx/mlx.h" -#include "player/player.h" #include "consts.h" #include "cub3d_data.h" #include "player/move.h" -#include "map/map_checker.h" #include "draw/draw_map.h" -#include "map/mapdata.h" -#include "player/player.h" #include "raycast/raycaster.h" #include "renderer/render.h" #include "raycast/ray.h" #include "utils/hooks.h" -#include "utils/frees.h" +#include "utils/inits.h" #include #include #include @@ -54,23 +50,7 @@ int main(int argc, char **argv) if (argc < 2) return (ft_printf("Error: Missing cub3d file\n"), 1); - ft_bzero(&data, sizeof(data)); - data.map = ft_calloc(sizeof(t_mapdata), 1); - if (!check_cubfile(argv[1], data.map)) - return (ft_printf("Error: Wrong map file. Reason: %s\n", - data.map->error), free_map(data.map), 1); - data.mlx = mlx_init(); - if (data.mlx == NULL) - return (ft_printf("Error: Failed to initalize mlx\n"), - free_map(data.map), 1); - data.mlx_win = mlx_new_window(data.mlx, WIDTH, HEIGHT, "Cub3d"); - data.screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT); - data.img_data = ft_calloc(sizeof(t_img_data), 1); - data.img_data->img = mlx_new_image(data.mlx, WIDTH, HEIGHT); - data.img_data->addr = mlx_get_data_addr(data.img_data->img, - &data.img_data->bits_per_pixel, &data.img_data->line_length, - &data.img_data->endian); - init_player(&data.player, data.map); + init_cub3d_data(&data, argv); mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data); mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask, keyrelease_handler, &data); diff --git a/src/utils/inits.c b/src/utils/inits.c new file mode 100644 index 0000000..7603656 --- /dev/null +++ b/src/utils/inits.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* inits.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio map = ft_calloc(sizeof(t_mapdata), 1); + if (!check_cubfile(argv[1], data->map)) + return (ft_printf("Error: Wrong map file. Reason: %s\n", + data->map->error), free_map(data->map), exit(1)); + data->mlx = mlx_init(); + if (data->mlx == NULL) + return (ft_printf("Error: Failed to initalize mlx\n"), + free_map(data->map), exit(1)); + data->mlx_win = mlx_new_window(data->mlx, WIDTH, HEIGHT, "Cub3d"); + data->img_data = ft_calloc(sizeof(t_img_data), 1); + data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); + data->img_data->addr = mlx_get_data_addr(data->img_data->img, + &data->img_data->bits_per_pixel, &data->img_data->line_length, + &data->img_data->endian); + init_player(&data->player, data->map); +} + diff --git a/src/utils/inits.h b/src/utils/inits.h new file mode 100644 index 0000000..37bbef3 --- /dev/null +++ b/src/utils/inits.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* inits.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio