From dea9844db4bf893525d692209d004968e4bf89ad Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Wed, 4 Mar 2020 16:03:30 +0100
Subject: Tab

---
 inc/cub3d_defines.h |   4 +-
 inc/cub3d_structs.h | 300 ++++++++++++++++++++++++++++------------------------
 src/ft_error.c      |   4 +-
 src/ft_exit.c       |   1 +
 src/ft_init_bmp.c   |  18 ++--
 src/ft_init_map.c   |   3 +-
 src/ft_parse_map.c  |  15 +--
 7 files changed, 189 insertions(+), 156 deletions(-)

diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h
index ed57f3e..c0d3355 100644
--- a/inc/cub3d_defines.h
+++ b/inc/cub3d_defines.h
@@ -20,6 +20,7 @@ enum
 {
 	FT_RET_FINE,
 	FT_RET_BAD_ARGV,
+	FT_RET_ALLOC_ERR,
 	FT_RET_FAILED_STRUCTS,
 	FT_RET_FAILED_MLX,
 	FT_RET_MAP_ERR,
@@ -112,7 +113,7 @@ enum
 ** ====== MAP ERROR MSG ======
 */
 
-#	define FT_ERR_ALLOCATE		"allocation error"
+#	define FT_ERR_ALLOCATE		"Allocation error"
 #	define FT_ERR_READ			"read error"
 #	define FT_ERR_ARGS			"too many or to few arguments"
 #	define FT_ERR_RES_SMALL		"resolution is too small, 50x50 minimum pls"
@@ -130,6 +131,7 @@ enum
 #	define FT_ERR_ILL_MAP		"map contains illegal char"
 #	define FT_ERR_MULT_SPAWN	"multiple spawn points"
 #	define FT_ERR_MULT_NLVL		"multiple spawn new level access points"
+#	define FT_ERR_NO_MAP		"No map"
 #	define FT_ERR_MAP_WALLS		"bad space or map is not surrounded by walls"
 #	define FT_ERR_MAP_EMPL		"empty line in map"
 #	define FT_ERR_MAP_L_L		"last line is invalid"
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index df26c26..19e211e 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -17,152 +17,178 @@
 #include <stdint.h>
 #include <pthread.h>
 
-typedef struct		s_win
+typedef struct			s_win
 {
-	void			*wlx;
-	void			*winptr;
-	uint8_t			inited;
-	uint16_t		x_max_size;
-	uint16_t		y_max_size;
-	uint32_t		x_size;
-	uint32_t		y_size;
-}					t_win;
-
-typedef struct		s_bmp
+	void				*wlx;
+	void				*winptr;
+	uint8_t				inited;
+	uint16_t			x_max_size;
+	uint16_t			y_max_size;
+	uint32_t			x_size;
+	uint32_t			y_size;
+}						t_win;
+
+#	pragma pack(push, 1)
+
+typedef struct			s_bmp_info
 {
-	uint16_t		file_type;
-	uint32_t		file_size;
-	uint16_t		reserv_one;
-	uint16_t		reserv_two;
-	uint32_t		offset_data;
-}					t_bmp;
-
-typedef struct		s_img
+	uint32_t			size;
+	int32_t				width;
+	int32_t				height;
+	uint16_t			planes;
+	uint16_t			bit_count;
+	uint32_t			compression;
+	uint32_t			size_image;
+	int32_t				x_pixels_per_meter;
+	int32_t				y_pixels_per_meter;
+	uint32_t			colors_used;
+	uint32_t			colors_important;
+}						t_bmp_info;
+
+typedef struct			s_bmp_colors
 {
-	void			*img;
-	char			*ptr;
-	int				bpp;
-	int				sizeline;
-	int				endian;
-	int				img_w;
-	int				img_h;
-	int				tex_x;
-	int				tex_y;
-}					t_img;
-
-typedef struct		s_rgb
+	
+}						t_bmp_color;
+
+typedef struct			s_bmp_file
 {
-	int16_t			r;
-	int16_t			g;
-	int16_t			b;
-}					t_rgb;
+	uint16_t			file_type;
+	uint32_t			file_size;
+	uint16_t			reserv_one;
+	uint16_t			reserv_two;
+	uint32_t			offset_data;
+	struct s_bmp_info
+}						t_bmp_file_head;
 
-typedef struct		s_sprite
+#	pragma pack(pop)
+
+typedef struct			s_img
 {
-	int32_t			s_screen_x;
-	int32_t			s_pos_x;
-	int32_t			s_pos_y;
-	double			s_x;
-	double			s_y;
-	int32_t			s_h;
-	int32_t			s_w;
-	int32_t			s_start_x;
-	int32_t			s_start_y;
-	int32_t			s_end_x;
-	int32_t			s_end_y;
-	int32_t			s_tex_y;
-	double			sprite_transform_x;
-	double			sprite_transform_y;
-	double			sprite_dist;
-	double			inv_c_m;
-}					t_sprite;
-
-typedef struct		s_player
+	void				*img;
+	char				*ptr;
+	int					bpp;
+	int					sizeline;
+	int					endian;
+	int					img_w;
+	int					img_h;
+	int					tex_x;
+	int					tex_y;
+}						t_img;
+
+typedef struct			s_rgb
 {
-	float			pos_x;
-	float			pos_y;
-	float			start_x;
-	float			start_y;
-	float			dir_x;
-	float			dir_y;
-	float			cam_x;
-	float			plane_x;
-	float			plane_y;
-}					t_player;
-
-typedef struct		s_ray
+	int16_t				r;
+	int16_t				g;
+	int16_t				b;
+}						t_rgb;
+
+typedef struct			s_sprite
 {
-	uint16_t		line_h;
-	float			wall_dist;
-	float			mid_dist;
-	float			x_ray_pos;
-	float			y_ray_pos;
-	float			x_ray_dir;
-	float			y_ray_dir;
-	float			x_side_dist;
-	float			y_side_dist;
-	float			x_delta_dist;
-	float			y_delta_dist;
-	int16_t			wall_t;
-	int16_t			wall_b;
-	uint8_t			side;
-	size_t			sqx;
-	size_t			sqy;
-	uint8_t			hit;
-	double			wall_hit_x;
-	double			step_tex_v;
-}					t_ray;
-
-typedef struct		s_map
+	int32_t				s_screen_x;
+	int32_t				s_pos_x;
+	int32_t				s_pos_y;
+	double				s_x;
+	double				s_y;
+	int32_t				s_h;
+	int32_t				s_w;
+	int32_t				s_start_x;
+	int32_t				s_start_y;
+	int32_t				s_end_x;
+	int32_t				s_end_y;
+	int32_t				s_tex_y;
+	double				sprite_transform_x;
+	double				sprite_transform_y;
+	double				sprite_dist;
+	double				inv_c_m;
+}						t_sprite;
+
+typedef struct			s_player
+{
+	float				pos_x;
+	float				pos_y;
+	float				start_x;
+	float				start_y;
+	float				dir_x;
+	float				dir_y;
+	float				cam_x;
+	float				plane_x;
+	float				plane_y;
+}						t_player;
+
+typedef struct			s_ray
 {
-	char			*no_tex_path;
-	char			*so_tex_path;
-	char			*ea_tex_path;
-	char			*we_tex_path;
-	char			*sprite_path;
-	char			*nl_tex_path;
-	char			*nlevel_path;
-	char			*music_path;
-	char			*music_cmd;
-	char			*mapl;
-	char			**map;
-	int8_t			x_step;
-	int8_t			y_step;
-	size_t			map_w;
-	size_t			map_h;
-	size_t			mapl_len;
-	size_t			line_chk;
-	size_t			map_start;
-	uint8_t			isspawn;
-	uint8_t			isnlvl;
-	uint8_t			ismusic;
-	uint8_t			scale;
-	uint32_t		nlx;
-	uint32_t		nly;
-}					t_map;
-
-typedef struct		s_cub
+	uint16_t			line_h;
+	float				wall_dist;
+	float				mid_dist;
+	float				x_ray_pos;
+	float				y_ray_pos;
+	float				x_ray_dir;
+	float				y_ray_dir;
+	float				x_side_dist;
+	float				y_side_dist;
+	float				x_delta_dist;
+	float				y_delta_dist;
+	int16_t				wall_t;
+	int16_t				wall_b;
+	uint8_t				side;
+	size_t				sqx;
+	size_t				sqy;
+	uint8_t				hit;
+	double				wall_hit_x;
+	double				step_tex_v;
+}						t_ray;
+
+typedef struct			s_map
+{
+	char				*filename;
+	char				*no_tex_path;
+	char				*so_tex_path;
+	char				*ea_tex_path;
+	char				*we_tex_path;
+	char				*sprite_path;
+	char				*nl_tex_path;
+	char				*nlevel_path;
+	char				*music_path;
+	char				*music_cmd;
+	char				*mapl;
+	char				**map;
+	int8_t				x_step;
+	int8_t				y_step;
+	size_t				map_w;
+	size_t				map_h;
+	size_t				mapl_len;
+	size_t				line_chk;
+	size_t				map_start;
+	uint8_t				isspawn;
+	uint8_t				isnlvl;
+	uint8_t				ismusic;
+	uint8_t				scale;
+	uint32_t			nlx;
+	uint32_t			nly;
+}						t_map;
+
+typedef struct			s_cub
 {
-	uint8_t			w_side;
-	uint8_t			ishud;
-	uint8_t			walltexgood;
-	uint8_t			isoldmus;
-	uint16_t		currlvl;
-	uint16_t		i;
-	char			errmsg[64];
-	int32_t			key_input[5];
-	pthread_t		tid;
-	int				(*key_ptr[6])(struct s_cub*);
-	int8_t			(*get_ptr[11])(char**, struct s_cub*);
-	struct s_win	wlist;
-	struct s_player	plist;
-	struct s_map	mlist;
-	struct s_ray	rlist;
-	struct s_img	img;
-	struct s_rgb	f_rgb;
-	struct s_rgb	c_rgb;
-	struct s_img	tlist[6];
-	struct s_sprite	sp_list;
-}					t_cub;
+	uint8_t				w_side;
+	uint8_t				ishud;
+	uint8_t				walltexgood;
+	uint8_t				isoldmus;
+	uint16_t			currlvl;
+	uint16_t			i;
+	char				errmsg[64];
+	int32_t				key_input[5];
+	pthread_t			tid;
+	int					(*key_ptr[6])(struct s_cub*);
+	int8_t				(*get_ptr[11])(char**, struct s_cub*);
+	struct s_win		wlist;
+	struct s_player		plist;
+	struct s_map		mlist;
+	struct s_ray		rlist;
+	struct s_img		img;
+	struct s_rgb		f_rgb;
+	struct s_rgb		c_rgb;
+	struct s_img		tlist[6];
+	struct s_sprite		sp_list;
+}						t_cub;
 
 #	endif
diff --git a/src/ft_error.c b/src/ft_error.c
index b793b96..87be01e 100644
--- a/src/ft_error.c
+++ b/src/ft_error.c
@@ -15,13 +15,13 @@
 #include <unistd.h>
 
 int
-	ft_error(const char *errmsg, t_cub *clist)
+	ft_error(uint8_t retval, const char *errmsg, t_cub *clist)
 {
 	ft_dprintf(STDERR_FILENO, "Error\n");
 	ft_dprintf(STDERR_FILENO,
 				"\033[1;31m%s\033[0m\n",
 				errmsg);
-	return (ft_exit(FT_RET_BMP_ERR, clist));
+	return (ft_exit(retval, clist));
 }
 
 int
diff --git a/src/ft_exit.c b/src/ft_exit.c
index 1be7a32..23cd466 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -21,6 +21,7 @@
 static void
 	ft_free_lists(t_cub *clist)
 {
+	ft_memdel((void**)&clist->mlist.filename);
 	ft_memdel((void**)&clist->mlist.no_tex_path);
 	ft_memdel((void**)&clist->mlist.so_tex_path);
 	ft_memdel((void**)&clist->mlist.ea_tex_path);
diff --git a/src/ft_init_bmp.c b/src/ft_init_bmp.c
index cc8d59b..3236e2f 100644
--- a/src/ft_init_bmp.c
+++ b/src/ft_init_bmp.c
@@ -13,15 +13,15 @@
 #include <cub3d.h>
 #include <stdint.h>
 
-t_bmp
-	ft_init_bmp(void)
+int8_t
+	ft_init_bmp(t_bmp_file *bmp_file)
 {
-	t_bmp	bmp;
+	t_bmp_file	bmp_file;
 
-	bmp.file_type = 0x4d42;
-	bmp.file_size = 0;
-	bmp.reserv_one = 0;
-	bmp.reserv_two = 0;
-	bmp.offset_data = 0;
-	return (bmp);
+	bmp_file.file_type = 0x4d42;
+	bmp_file.file_size = 0;
+	bmp_file.reserv_one = 0;
+	bmp_file.reserv_two = 0;
+	bmp_file.offset_data = 0;
+	return (bmp_file);
 }
diff --git a/src/ft_init_map.c b/src/ft_init_map.c
index e2cfa21..4fa127c 100644
--- a/src/ft_init_map.c
+++ b/src/ft_init_map.c
@@ -18,7 +18,8 @@
 static int8_t
 	ft_init_map_callocs(t_map *mlist)
 {
-	if (!(mlist->no_tex_path = (char*)ft_calloc(1, sizeof(char))) ||
+	if (!(mlist->filename = (char*)ft_calloc(1, sizeof(char))) ||
+		!(mlist->no_tex_path = (char*)ft_calloc(1, sizeof(char))) ||
 		!(mlist->so_tex_path = (char*)ft_calloc(1, sizeof(char))) ||
 		!(mlist->ea_tex_path = (char*)ft_calloc(1, sizeof(char))) ||
 		!(mlist->we_tex_path = (char*)ft_calloc(1, sizeof(char))) ||
diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c
index 3d19fdc..d3a90af 100644
--- a/src/ft_parse_map.c
+++ b/src/ft_parse_map.c
@@ -74,12 +74,14 @@ static int8_t
 	return (ret);
 }
 
-static void
-	ft_no_map_error(t_cub *clist)
+void
+	ft_save_name(t_map *mlist, t_cub *clist)
 {
-	ft_dprintf(STDERR_FILENO, "Error\n");
-	ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n");
-	ft_exit(FT_RET_NO_MAP, clist);
+	ft_memdel((void**)mlist->filename);
+	if (!(mlist->filename =
+		(char*)malloc((ft_strlen(map_path) + 1) * sizeof(char))))
+		ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, clist);
+	ft_sprintf(mlist->filename, map_path);
 }
 
 void
@@ -90,9 +92,10 @@ void
 
 	if (ft_check_ext(map_path, ".cub") < 0)
 		ft_map_error(FT_ERR_NOT_A_CUB, clist);
+	ft_save_name(&clist->mlist, clist);
 	fd = open(map_path, O_RDONLY);
 	if (fd < 0)
-		ft_no_map_error(clist);
+		ft_error(FT_RET_NO_MAP, FT_ERR_NO_MAP, clist);
 	ret = 1;
 	while (ret != 12 && ret >= 0)
 		ret = ft_parse_it(fd, clist);
-- 
cgit v1.2.3