From 6322d05b222421d1502dce9dfe974174134ca910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 17 Jul 2025 14:57:29 +0200 Subject: [PATCH] style: split structs.h header into multiple, remove from central dir --- src/cub3d_data.h | 29 +++++++++++++++ src/draw/draw_map.c | 9 ++--- src/draw/draw_map.h | 8 +++-- src/draw/drawutils.c | 6 ++-- src/draw/drawutils.h | 6 ++-- src/draw/img_data.h | 25 +++++++++++++ src/main.c | 21 ++++++----- src/map/checkers.c | 4 +-- src/map/checkers.h | 5 +-- src/map/map_checker.c | 3 +- src/map/map_checker.h | 4 +-- includes/structs.h => src/map/mapdata.h | 47 ++++--------------------- src/map/populate_map.c | 3 +- src/map/setters.h | 5 +-- src/player/player.h | 31 ++++++++++++++++ src/utils/frees.c | 11 +++--- src/utils/frees.h | 4 +-- src/utils/hooks.c | 3 +- src/utils/hooks.h | 4 +-- 19 files changed, 141 insertions(+), 87 deletions(-) create mode 100644 src/cub3d_data.h create mode 100644 src/draw/img_data.h rename includes/structs.h => src/map/mapdata.h (59%) create mode 100644 src/player/player.h diff --git a/src/cub3d_data.h b/src/cub3d_data.h new file mode 100644 index 0000000..dbadee9 --- /dev/null +++ b/src/cub3d_data.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cub3d_data.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ +/* Updated: 2025/07/17 15:54:29 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CUB3D_DATA_H +# define CUB3D_DATA_H + +# include "map/mapdata.h" +# include "draw/img_data.h" +# include "player/player.h" + +typedef struct s_cub3d_data +{ + void *mlx; + void *mlx_win; + t_img_data *img_data; + t_mapdata *map; + t_player player; +} t_cub3d_data; + +#endif // CUB3D_DATA_H diff --git a/src/draw/draw_map.c b/src/draw/draw_map.c index 645c819..15e94f7 100644 --- a/src/draw/draw_map.c +++ b/src/draw/draw_map.c @@ -6,14 +6,15 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:20:00 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:30:59 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:55:44 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../../includes/structs.h" +#include "../map/mapdata.h" +#include "../player/player.h" #include "drawutils.h" -void draw_2d_wall(unsigned int color, t_mlx_data *data, +void draw_2d_wall(unsigned int color, t_img_data *data, int x, int y) { int i; @@ -33,7 +34,7 @@ void draw_2d_wall(unsigned int color, t_mlx_data *data, } } -void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data) +void draw_map(t_mapdata *map, t_player *player, t_img_data *data) { int i; int j; diff --git a/src/draw/draw_map.h b/src/draw/draw_map.h index 7343db3..aa16e44 100644 --- a/src/draw/draw_map.h +++ b/src/draw/draw_map.h @@ -6,15 +6,17 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:20:13 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:35:31 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:52:07 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef DRAW_MAP_H # define DRAW_MAP_H -# include "../../includes/structs.h" +# include "../map/mapdata.h" +# include "../draw/img_data.h" +# include "../player/player.h" -void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data); +void draw_map(t_mapdata *map, t_player *player, t_img_data *data); #endif diff --git a/src/draw/drawutils.c b/src/draw/drawutils.c index 8592e7e..85d3537 100644 --- a/src/draw/drawutils.c +++ b/src/draw/drawutils.c @@ -6,13 +6,13 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:28:56 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:29:08 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:48:56 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../../includes/structs.h" +#include "img_data.h" -void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color) +void my_mlx_pixel_put(t_img_data *data, int x, int y, int color) { char *dst; diff --git a/src/draw/drawutils.h b/src/draw/drawutils.h index 0ed70cd..31c5927 100644 --- a/src/draw/drawutils.h +++ b/src/draw/drawutils.h @@ -6,15 +6,15 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:30:04 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:30:38 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:57:47 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef DRAWUTILS_H # define DRAWUTILS_H -# include "../../includes/structs.h" +# include "img_data.h" -void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color); +void my_mlx_pixel_put(t_img_data *data, int x, int y, int color); #endif // DRAWUTILS_H diff --git a/src/draw/img_data.h b/src/draw/img_data.h new file mode 100644 index 0000000..118dee1 --- /dev/null +++ b/src/draw/img_data.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* img_data.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/17 15:47:13 by kcolin #+# #+# */ +/* Updated: 2025/07/17 15:47:39 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef IMG_DATA_H +# define IMG_DATA_H + +typedef struct s_img_data +{ + void *img; + char *addr; + int bits_per_pixel; + int line_length; + int endian; +} t_img_data; + +#endif // IMG_DATA_H diff --git a/src/main.c b/src/main.c index 34ca65e..11f157b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,11 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:55:29 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:52:57 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../libft/includes/libft.h" -#include "../includes/structs.h" #include "../mlx/mlx.h" #include "consts.h" #include "map/map_checker.h" @@ -34,11 +33,11 @@ int game_loop(t_cub3d_data *data) { data->player.x += data->player.movement.x; data->player.y += data->player.movement.y; - mlx_destroy_image(data->mlx, data->mlx_data->img); - data->mlx_data->img = mlx_new_image(data->mlx, 800, 600); - draw_map(data->map, &data->player, data->mlx_data); + mlx_destroy_image(data->mlx, data->img_data->img); + data->img_data->img = mlx_new_image(data->mlx, 800, 600); + draw_map(data->map, &data->player, data->img_data); mlx_put_image_to_window(data->mlx, data->mlx_win, - data->mlx_data->img, 0, 0); + data->img_data->img, 0, 0); mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT); return (0); } @@ -55,11 +54,11 @@ int main(int argc, char **argv) data.map->error), 1); data.mlx = mlx_init(); data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d"); - data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1); - data.mlx_data->img = mlx_new_image(data.mlx, 800, 600); - data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img, - &data.mlx_data->bits_per_pixel, &data.mlx_data->line_length, - &data.mlx_data->endian); + data.img_data = ft_calloc(sizeof(t_img_data), 1); + data.img_data->img = mlx_new_image(data.mlx, 800, 600); + 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.map, &(data.player)); mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data); mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask, diff --git a/src/map/checkers.c b/src/map/checkers.c index 78cbb4f..59b3ac7 100644 --- a/src/map/checkers.c +++ b/src/map/checkers.c @@ -6,13 +6,13 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:15:26 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:45:47 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:53:29 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../libft/includes/libft.h" -#include "../../includes/structs.h" #include "forbidden_characters.h" +#include "mapdata.h" bool check_filename(t_mapdata *map, char *file) { diff --git a/src/map/checkers.h b/src/map/checkers.h index 7e46e57..3db8c10 100644 --- a/src/map/checkers.h +++ b/src/map/checkers.h @@ -6,14 +6,15 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:39:41 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:41:45 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:55:09 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CHECKERS_H # define CHECKERS_H -# include "../../includes/structs.h" +# include +# include "mapdata.h" bool check_filename(t_mapdata *map, char *file); void populate_maps(t_mapdata *map, int fd); diff --git a/src/map/map_checker.c b/src/map/map_checker.c index ddaf490..0f08e2e 100644 --- a/src/map/map_checker.c +++ b/src/map/map_checker.c @@ -6,12 +6,11 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */ -/* Updated: 2025/07/17 14:47:16 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:53:51 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../libft/includes/libft.h" -#include "../../includes/structs.h" #include "../utils/colors.h" #include "checkers.h" #include "setters.h" diff --git a/src/map/map_checker.h b/src/map/map_checker.h index 4ff9bca..cbb7a60 100644 --- a/src/map/map_checker.h +++ b/src/map/map_checker.h @@ -6,14 +6,14 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:34:38 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:35:09 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:49:16 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MAP_CHECKER_H # define MAP_CHECKER_H -# include "../../includes/structs.h" +# include "mapdata.h" bool check_cubfile(char *file, t_mapdata *map); diff --git a/includes/structs.h b/src/map/mapdata.h similarity index 59% rename from includes/structs.h rename to src/map/mapdata.h index d475695..c6e7f07 100644 --- a/includes/structs.h +++ b/src/map/mapdata.h @@ -1,29 +1,20 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* structs.h :+: :+: :+: */ +/* mapdata.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/07/15 10:32:57 by tchampio #+# #+# */ -/* Updated: 2025/07/15 10:32:58 by tchampio ### ########.fr */ +/* Created: 2025/07/17 14:58:19 by kcolin #+# #+# */ +/* Updated: 2025/07/17 15:57:59 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef STRUCTS_H -# define STRUCTS_H +#ifndef MAPDATA_H +# define MAPDATA_H # include -typedef struct s_mlx_data -{ - void *img; - char *addr; - int bits_per_pixel; - int line_length; - int endian; -} t_mlx_data; - typedef enum e_direction { NORTH, @@ -32,12 +23,6 @@ typedef enum e_direction WEST } t_direction; -typedef struct s_vec2 -{ - double x; - double y; -} t_vec2; - typedef struct s_mapdata { char *filename; @@ -58,22 +43,4 @@ typedef struct s_mapdata char error[1024]; } t_mapdata; -typedef struct s_player -{ - double x; - double y; - double yaw; - int health; - t_vec2 movement; -} t_player; - -typedef struct s_cub3d_data -{ - void *mlx; - void *mlx_win; - t_mlx_data *mlx_data; - t_mapdata *map; - t_player player; -} t_cub3d_data; - -#endif +#endif // MAPDATA_H diff --git a/src/map/populate_map.c b/src/map/populate_map.c index c25af5c..7cae3c2 100644 --- a/src/map/populate_map.c +++ b/src/map/populate_map.c @@ -6,11 +6,10 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:44:02 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:52:01 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:53:38 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../../includes/structs.h" #include "../../libft/includes/libft.h" #include "setters.h" diff --git a/src/map/setters.h b/src/map/setters.h index 1d47d5a..e4d5e81 100644 --- a/src/map/setters.h +++ b/src/map/setters.h @@ -6,14 +6,15 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:40:32 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:42:15 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:54:11 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef SETTERS_H # define SETTERS_H -# include "../../includes/structs.h" +# include +# include "mapdata.h" bool add_textures(int fd, t_mapdata *map); void add_map_line(const char *line, t_mapdata *map); diff --git a/src/player/player.h b/src/player/player.h new file mode 100644 index 0000000..806cffa --- /dev/null +++ b/src/player/player.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/17 15:51:29 by kcolin #+# #+# */ +/* Updated: 2025/07/17 15:51:42 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PLAYER_H +# define PLAYER_H + +typedef struct s_vec2 +{ + double x; + double y; +} t_vec2; + +typedef struct s_player +{ + double x; + double y; + double yaw; + int health; + t_vec2 movement; +} t_player; + +#endif // PLAYER_H diff --git a/src/utils/frees.c b/src/utils/frees.c index 958aed1..da917c6 100644 --- a/src/utils/frees.c +++ b/src/utils/frees.c @@ -6,13 +6,14 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:47:25 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:55:22 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../../includes/structs.h" #include "../../mlx/mlx.h" #include "../../libft/includes/libft.h" +#include "../map/mapdata.h" +#include "../cub3d_data.h" #include void free_tab(char **tab) @@ -62,9 +63,9 @@ int destroy(t_cub3d_data *data) free_map(data->map); if (data->mlx_win) mlx_destroy_window(data->mlx, data->mlx_win); - if (data->mlx_data) - mlx_destroy_image(data->mlx, data->mlx_data->img); - free(data->mlx_data); + if (data->img_data) + mlx_destroy_image(data->mlx, data->img_data->img); + free(data->img_data); if (data->mlx) mlx_destroy_display(data->mlx); free(data->mlx); diff --git a/src/utils/frees.h b/src/utils/frees.h index 167723a..0b596bb 100644 --- a/src/utils/frees.h +++ b/src/utils/frees.h @@ -6,14 +6,14 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:27:11 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:47:27 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:54:20 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FREES_H # define FREES_H -# include "../../includes/structs.h" +# include "../cub3d_data.h" void gnl_exhaust(int fd); int destroy(t_cub3d_data *data); diff --git a/src/utils/hooks.c b/src/utils/hooks.c index fc15335..afbb212 100644 --- a/src/utils/hooks.c +++ b/src/utils/hooks.c @@ -6,12 +6,11 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:55:42 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:53:49 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../consts.h" -#include "../../includes/structs.h" #include "frees.h" #include #include diff --git a/src/utils/hooks.h b/src/utils/hooks.h index ba3944c..95fda45 100644 --- a/src/utils/hooks.h +++ b/src/utils/hooks.h @@ -6,14 +6,14 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:23:06 by kcolin #+# #+# */ -/* Updated: 2025/07/17 14:24:58 by kcolin ### ########.fr */ +/* Updated: 2025/07/17 15:50:08 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef HOOKS_H # define HOOKS_H -# include "../../includes/structs.h" +# include "../cub3d_data.h" int keypress_handler(int keycode, t_cub3d_data *data); int keyrelease_handler(int keycode, t_cub3d_data *data);