From 4e8cb2de1b199d6e439ba64ac3c42366aabc7ca8 Mon Sep 17 00:00:00 2001 From: Ladebeze66 Date: Tue, 23 Jan 2024 21:46:59 +0100 Subject: [PATCH] versiontexturepositioncouleurok --- Makefile | 3 +- cub3d.h | 40 +++++++----- draw/ft_2d_view.c | 8 +-- draw/ft_3d_view.c | 4 +- draw/ft_textures.c | 2 +- main.c | 34 ++++++++-- map.cub | 2 +- parsing/ft_find_player_position.c | 102 ++++++++++++++++++++++++++++++ parsing/ft_parsing.c | 50 ++++++--------- window/ft_player.c | 27 +++++--- window/ft_window.c | 4 +- 11 files changed, 200 insertions(+), 76 deletions(-) create mode 100644 parsing/ft_find_player_position.c diff --git a/Makefile b/Makefile index d4e673f..0463881 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: fgras-ca +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/01/14 16:54:48 by fgras-ca #+# #+# # -# Updated: 2024/01/23 17:36:22 by fgras-ca ### ########.fr # +# Updated: 2024/01/23 19:56:56 by fgras-ca ### ########.fr # # # # **************************************************************************** # @@ -55,6 +55,7 @@ SRC = main.c \ ./window/ft_refresh_window.c \ ./parsing/ft_find_map_start.c \ ./parsing/ft_textures_and_colors.c \ + ./parsing/ft_find_player_position.c \ SRC_DIR_LIBFT = libft/ diff --git a/cub3d.h b/cub3d.h index 5afc29c..eab85b9 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 16:56:52 by fgras-ca #+# #+# */ -/* Updated: 2024/01/16 16:50:38 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 21:07:38 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,10 +35,11 @@ # define FOVIEW 60 # define DISRAY 1000000 # define DOF 120 -# define WIDTH 1780 +# define WIDTH 1280 # define HEIGHT 720 -# define BOV 500 +# define BOV 0 # define MAX_LINE_LENGTH 100000 + typedef enum { NORTH, SOUTH, @@ -59,19 +60,6 @@ typedef struct s_img int endian; } t_img; -typedef struct s_struture_map -{ - char *map; - char *buff; - char **temp_list; - int i; - int j; - int temp; - int mapX; - int mapY; - int mapS; -} t_structure_map; - typedef struct s_struture_player { float px; @@ -125,6 +113,24 @@ typedef struct s_texture unsigned int floor_color; unsigned int ceil_color; } t_texture; + +typedef struct s_struture_map +{ + char *map; + char *buff; + char **temp_list; + int i; + int j; + int temp; + int mapX; + int mapY; + int mapS; + float player_x; + float player_y; + char player_direction; + t_structure_windows s_win; +} t_structure_map; + typedef struct s_structure_main { int fd; @@ -372,6 +378,8 @@ bool handle_map(int fd, char **map_buffer, int *map_length); bool parse_color_line(const char *line, unsigned int *color); bool is_valid_texture(const char *line); bool handle_textures(int fd, t_texture *textures); +bool find_player_position_and_direction(const char *map_content, int length, t_structure_map *map_info); +void calculate_map(t_structure_map *map_info); /*textures*/ void load_wall_textures(t_structure_main *w); void draw_texture(t_texture_params *tex_params); diff --git a/draw/ft_2d_view.c b/draw/ft_2d_view.c index 8a84113..58f825b 100644 --- a/draw/ft_2d_view.c +++ b/draw/ft_2d_view.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/12 17:49:42 by fgras-ca #+# #+# */ -/* Updated: 2024/01/17 21:47:17 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 17:43:56 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,12 +31,6 @@ void init_base_params(t_base_params *params, t_structure_main *w) params->FOV = FOVIEW * (PI / 180); params->DR = params->FOV / params->numrays; params->ra = w->s_player.pa - (params->FOV / 2); - printf("DEBUG: Initialisation des paramètres de base\n"); - printf("DEBUG: tilesize (taille de la tuile) = %d\n", w->s_map.mapS); - printf("DEBUG: numrays (nombre de rayons) = %d\n", NUMRAY); - printf("DEBUG: FOV (champ de vision) = %f\n", FOVIEW * (PI / 180)); - printf("DEBUG: DR (delta rayon) = %f\n", params->FOV / params->numrays); - printf("DEBUG: ra (rayon actuel) = %f\n", w->s_player.pa - (params->FOV / 2)); draw_background(w); } diff --git a/draw/ft_3d_view.c b/draw/ft_3d_view.c index 9e75ca5..91d3e41 100644 --- a/draw/ft_3d_view.c +++ b/draw/ft_3d_view.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/12 17:35:53 by fgras-ca #+# #+# */ -/* Updated: 2024/01/14 17:19:42 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 20:46:34 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,8 +54,8 @@ void drawray(t_ray_params *rayparams) textureparams.startX = rayparams->r * rayparams->raywidth + rayparams->backgroundOffsetX; textureparams.endX = textureparams.startX + rayparams->raywidth; + init_texture_params(&textureparams, rayparams); init_line_params(&lineparams, rayparams); draw_line(&lineparams); - init_texture_params(&textureparams, rayparams); draw_texture(&textureparams); } diff --git a/draw/ft_textures.c b/draw/ft_textures.c index 35d5fce..e5a2070 100644 --- a/draw/ft_textures.c +++ b/draw/ft_textures.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/12 17:39:11 by fgras-ca #+# #+# */ -/* Updated: 2024/01/20 11:05:59 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 21:21:04 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/main.c b/main.c index 84d0ca0..6466ac7 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/15 18:45:52 by fgras-ca #+# #+# */ -/* Updated: 2024/01/20 11:08:57 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 20:54:55 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,20 +32,36 @@ int setup_and_load_map(int argc, char **argv, t_structure_main *w, t_texture *te return (1); } +void init_structure_main(t_structure_main *w) +{ + if (w == NULL) + { + return ; + } + w->t = (t_texture *)malloc(sizeof(t_texture)); + if (w->t != NULL) + { + ft_memset(w->t, 0, sizeof(t_texture)); + } + ft_memset(&(w->s_win), 0, sizeof(t_structure_windows)); + ft_memset(&(w->s_img), 0, sizeof(t_structure_img)); + ft_memset(&(w->s_map), 0, sizeof(t_structure_map)); + ft_memset(&(w->s_player), 0, sizeof(t_structure_player)); + w->s_img.texture_width = 1280; + w->s_img.texture_height = 720; +} + int main(int argc, char **argv) { t_structure_main w; t_global_struct global_struct; - w.t = malloc(sizeof(t_texture)); - if (w.t == NULL) - { - // Gérer l'erreur d'allocation - } - ft_memset(w.t, 0, sizeof(t_texture)); + init_structure_main(&w); global_struct.w = &w; global_struct.state.jkl = -1; global_struct.state.yui = 0; + w.s_win.width = WIDTH; + w.s_win.height = HEIGHT; if (!setup_and_load_map(argc, argv, &w, w.t)) return (1); init_windows(&w); @@ -57,5 +73,9 @@ int main(int argc, char **argv) { free(w.s_map.map); } + if (w.t) + { + free(w.t); + } return (0); } diff --git a/map.cub b/map.cub index 58d7058..9831f81 100644 --- a/map.cub +++ b/map.cub @@ -14,7 +14,7 @@ C 165, 176, 196 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 111111111111000000000001 100000000000000000111100000000000000000000000000000000000000000000000000000000000000000001 1000000000001 1000000000000000001 100000000000000000000000000000000000000000000000000000000000011111111 1000000000001 -100000000000000000111100000000000000000000000000000000000000000000000000000000000011111111111111111111111 1000000000001 +10000000000000000011110000000000000000000000000000000000000N000000000000000000000011111111111111111111111 1000000000001 100000100000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111 1000000000001 110000100000011111100000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1000000000001 10000100000011 1100000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1000000000001 diff --git a/parsing/ft_find_player_position.c b/parsing/ft_find_player_position.c new file mode 100644 index 0000000..d3e99d4 --- /dev/null +++ b/parsing/ft_find_player_position.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_find_player_position.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/23 19:54:51 by fgras-ca #+# #+# */ +/* Updated: 2024/01/23 20:22:20 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d.h" + +static void initialize_variables(int *i, int *line_number, int *column_number, bool *found_player) +{ + *i = 0; + *line_number = 0; + *column_number = 0; + *found_player = false; +} + +static bool check_for_player(char current_char, bool *found_player) +{ + if (current_char == 'N' || current_char == 'S' || current_char == 'E' || current_char == 'W') + { + if (*found_player) + { + printf("Multiple player start positions found. Invalid map.\n"); + return false; + } + *found_player = true; + } + return true; +} + +static void update_player_info(t_structure_map *map_info, int line_number, int column_number, char player_direction) +{ + map_info->player_x = column_number * map_info->mapS + map_info->mapS / 2; + map_info->player_y = line_number * map_info->mapS + map_info->mapS / 2; + map_info->player_direction = player_direction; + printf("Player found: x = %f, y = %f, direction = %c\n", map_info->player_x, map_info->player_y, map_info->player_direction); +} + + +static void update_position(char current_char, int *line_number, int *column_number) +{ + if (current_char == '\n') + { + (*line_number)++; + *column_number = 0; + } + else + { + (*column_number)++; + } +} + +static bool check_for_multiple_players(bool found_player) +{ + if (found_player) + { + printf("Multiple player start positions found. Invalid map.\n"); + return false; + } + return true; +} + +bool find_player_position_and_direction(const char *map_content, int length, t_structure_map *map_info) +{ + int i; + int line_number; + int column_number; + bool found_player; + + initialize_variables(&i, &line_number, &column_number, &found_player); + while (i < length) + { + char current_char = map_content[i]; + if (!found_player && (current_char == 'N' || current_char == 'S' || current_char == 'E' || current_char == 'W')) + { + if (check_for_multiple_players(found_player)) + { + found_player = true; + update_player_info(map_info, line_number, column_number, current_char); + } + else + { + return false; + } + } + calculate_map(map_info); + update_position(current_char, &line_number, &column_number); + i++; + } + if (!found_player) + { + printf("Player start position not found. Invalid map.\n"); + return false; + } + return true; +} \ No newline at end of file diff --git a/parsing/ft_parsing.c b/parsing/ft_parsing.c index 0522360..9f8c4f2 100644 --- a/parsing/ft_parsing.c +++ b/parsing/ft_parsing.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:43:46 by fgras-ca #+# #+# */ -/* Updated: 2024/01/16 15:22:35 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 20:06:58 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ bool init_map_info(t_structure_map *map_info, int maxWidth, int height) { map_info->mapX = maxWidth; map_info->mapY = height; + map_info->s_win.width = WIDTH; + map_info->s_win.height = HEIGHT; map_info->map = malloc(maxWidth * height); if (!map_info->map) { @@ -29,8 +31,7 @@ bool init_map_info(t_structure_map *map_info, int maxWidth, int height) bool copy_map_data_and_check(t_map_params *params) { copy_map_data(params); - if (!is_map_closed(params->map_info->map, - *params->maxWidth, *params->height)) + if (!is_map_closed(params->map_info->map, *params->maxWidth, *params->height)) { printf("Map is not closed!\n"); free(params->map_info->map); @@ -39,33 +40,25 @@ bool copy_map_data_and_check(t_map_params *params) return (true); } -bool process_map(const char *buffer, int length, t_structure_map *map_info) +void calculate_map(t_structure_map *map_info) { - int maxwidth; - int height; - t_map_params map_params; + float maps_x; + float maps_y; - maxwidth = 0; - height = 0; - map_params.map_info = map_info; - map_params.buffer = buffer; - map_params.length = length; - map_params.maxWidth = &maxwidth; - map_params.height = &height; - map_params.currentWidth = NULL; - map_params.isNewLine = NULL; - get_map_dimensions(&map_params); - if (maxwidth <= 0 || height <= 0) + maps_x = (float)map_info->s_win.width / (map_info->mapX * 4); + maps_y = (float)map_info->s_win.height / (map_info->mapY * 2); + if (maps_x < maps_y) { - printf("Invalid map dimensions: maxWidth=%d, height=%d\n", - maxwidth, height); - return (false); + map_info->mapS = (int)maps_x; } - if (!init_map_info(map_info, maxwidth, height)) + else { - return (false); + map_info->mapS = (int)maps_y; + } + if (map_info->mapS == 0) + { + map_info->mapS = 1; } - return (copy_map_data_and_check(&map_params)); } bool parse_map(const char *map_content, int length, t_structure_map *map_info) @@ -73,24 +66,23 @@ bool parse_map(const char *map_content, int length, t_structure_map *map_info) t_map_params map_params; int max_width; int height; - + max_width = 0; height = 0; - printf("Received map content (length = %d): \n%s\n", length, map_content); map_params.map_info = map_info; map_params.buffer = map_content; map_params.length = length; map_params.maxWidth = &max_width; map_params.height = &height; - map_params.currentWidth = NULL; - map_params.isNewLine = NULL; get_map_dimensions(&map_params); if (max_width <= 0 || height <= 0) { - printf("Invalid map dimensions: maxWidth=%d, height=%d\n", max_width, height); + printf("Invalid map dimensions\n"); return (false); } if (!init_map_info(map_info, max_width, height)) return (false); + if (!find_player_position_and_direction(map_content, length, map_info)) + return (false); return (copy_map_data_and_check(&map_params)); } diff --git a/window/ft_player.c b/window/ft_player.c index e0e7aa8..9288b9d 100644 --- a/window/ft_player.c +++ b/window/ft_player.c @@ -6,20 +6,29 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 20:09:23 by fgras-ca #+# #+# */ -/* Updated: 2024/01/17 21:15:02 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 18:38:11 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "../cub3d.h" -void init_player(t_structure_main *w) -{ - w->s_player.px = 10; - w->s_player.py = 10; - w->s_player.pa = 0.1; - w->s_player.pdx = cos(w->s_player.pa) * 5; - w->s_player.pdy = sin(w->s_player.pa) * 5; - printf("DEBUG: pdx = %f, pdy = %f\n", w->s_player.pdx, w->s_player.pdy); +void init_player(t_structure_main *w) { + w->s_player.px = w->s_map.player_x; + w->s_player.py = w->s_map.player_y; + + // Remplacer le switch par des if + if (w->s_map.player_direction == 'N') { + w->s_player.pa = M_PI; + } else if (w->s_map.player_direction == 'S') { + w->s_player.pa = 0; + } else if (w->s_map.player_direction == 'E') { + w->s_player.pa = M_PI / 2; + } else if (w->s_map.player_direction == 'W') { + w->s_player.pa = 3 * M_PI / 2; + } + + w->s_player.pdx = cos(w->s_player.pa) * 5; + w->s_player.pdy = sin(w->s_player.pa) * 5; } void init_mlx_and_window(t_structure_main *w) diff --git a/window/ft_window.c b/window/ft_window.c index afa5379..646b180 100644 --- a/window/ft_window.c +++ b/window/ft_window.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 19:15:53 by fgras-ca #+# #+# */ -/* Updated: 2024/01/17 21:26:12 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/23 19:32:51 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,8 +88,6 @@ void init_windows(t_structure_main *w) { int temp; - w->s_win.width = WIDTH; - w->s_win.height = HEIGHT; init_player(w); init_mlx_and_window(w); load_textures(w, &temp);