cleanup: moved initialization code from main() to a separate file

This commit is contained in:
Theo Champion 2025-07-31 13:31:05 +02:00
parent 7b0d05532c
commit 05e24d19f9
4 changed files with 62 additions and 23 deletions

View file

@ -9,6 +9,7 @@ endif
IFLAGS = -I./mlx -I./libft IFLAGS = -I./mlx -I./libft
SOURCEFILES = \ SOURCEFILES = \
src/utils/inits.c \
src/draw/draw_map.c \ src/draw/draw_map.c \
src/draw/drawutils.c \ src/draw/drawutils.c \
src/main.c \ src/main.c \

View file

@ -6,25 +6,21 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 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 "../libft/includes/libft.h"
#include "../mlx/mlx.h" #include "../mlx/mlx.h"
#include "player/player.h"
#include "consts.h" #include "consts.h"
#include "cub3d_data.h" #include "cub3d_data.h"
#include "player/move.h" #include "player/move.h"
#include "map/map_checker.h"
#include "draw/draw_map.h" #include "draw/draw_map.h"
#include "map/mapdata.h"
#include "player/player.h"
#include "raycast/raycaster.h" #include "raycast/raycaster.h"
#include "renderer/render.h" #include "renderer/render.h"
#include "raycast/ray.h" #include "raycast/ray.h"
#include "utils/hooks.h" #include "utils/hooks.h"
#include "utils/frees.h" #include "utils/inits.h"
#include <stdbool.h> #include <stdbool.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/X.h> #include <X11/X.h>
@ -54,23 +50,7 @@ int main(int argc, char **argv)
if (argc < 2) if (argc < 2)
return (ft_printf("Error: Missing cub3d file\n"), 1); return (ft_printf("Error: Missing cub3d file\n"), 1);
ft_bzero(&data, sizeof(data)); init_cub3d_data(&data, argv);
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);
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data); mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask, mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
keyrelease_handler, &data); keyrelease_handler, &data);

38
src/utils/inits.c Normal file
View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:26:53 by tchampio #+# #+# */
/* Updated: 2025/07/31 13:28:03 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../../libft/includes/libft.h"
#include "../../mlx/mlx.h"
#include "../map/map_checker.h"
#include "frees.h"
void init_cub3d_data(t_cub3d_data *data, char **argv)
{
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), 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);
}

20
src/utils/inits.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inits.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:28:47 by tchampio #+# #+# */
/* Updated: 2025/07/31 13:29:35 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INITS_H
# define INITS_H
# include "../cub3d_data.h"
void init_cub3d_data(t_cub3d_data *data, char **argv);
#endif // INITS_H