diff --git a/Makefile b/Makefile index 4ef9264..ad84bcb 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ NAME = cub3d CPP = gcc $(FLAGS) LIBRARIES_DIR = Libft Minilibx LIBRARIES_A = Libft/libft.a Minilibx/libmlx.a -CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1) +CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1) -g CPP_2 = $(CPP) $(FLAGS_2) SHARED = -lX11 -lXext -lm C_FILES = $(wildcard **.c) diff --git a/algo.c b/algo.c index d3a3668..24e3964 100644 --- a/algo.c +++ b/algo.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ -/* Updated: 2024/10/17 15:00:28 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 16:35:28 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ void *g_mlx = NULL; void *g_win = NULL; t_map g_map; t_player g_player; +t_tex g_tex; +t_ray g_ray; // TODO manage image format error better @@ -40,19 +42,19 @@ void draw_screen(void) mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0); } -void load_textures(t_tex *tex) +void load_textures() { - int width; - int height; + g_tex.tex_width = 64; + g_tex.tex_height = 64; - tex->tex_north = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", - &width, &height); - tex->tex_south = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", - &width, &height); - tex->tex_east = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", - &width, &height); - tex->tex_west = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", - &width, &height); + g_tex.textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", + &g_tex.tex_width, &g_tex.tex_height); } static void loop(void) @@ -77,6 +79,7 @@ int main(int argc, char *argv[]) { g_mlx = mlx_init(); input_init(); + load_textures(); if (argc != 2) { printf("Syntax: %s \n", argv[0]); diff --git a/algo.h b/algo.h index fcff0de..0700d7a 100644 --- a/algo.h +++ b/algo.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ -/* Updated: 2024/10/17 15:07:14 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 16:34:32 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,20 +41,29 @@ # define FOV 0.66 // ? TODO unit # define HITBOX 0.25 // cases. should be more than MOVE_SPEED +typedef struct s_tex +{ + void *textures[4]; + void *current_tex; + int tex_height; + int tex_width; + int tex_dir; +} t_tex; + +typedef struct s_ray +{ + // pos player on map (cases) + t_point_double dir; + bool side; + double wallx; +} t_ray; + extern void *g_mlx; extern void *g_win; extern t_player g_player; extern t_map g_map; - -typedef struct s_tex -{ - void *tex_north; - void *tex_south; - void *tex_east; - void *tex_west; - void *current_tex; - -} t_tex; +extern t_tex g_tex; +extern t_ray g_ray; /// @brief Write an error message on stderr. /// diff --git a/render.c b/render.c index 6279fd5..26633f1 100644 --- a/render.c +++ b/render.c @@ -3,17 +3,18 @@ /* ::: :::::::: */ /* render.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 14:55:05 by greg #+# #+# */ -/* Updated: 2024/10/15 16:55:02 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 16:28:20 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ #include "algo.h" +#include static void calculate_perpwalldist3(int x, - t_point_double *raydir, t_point_double *deltadist) + t_point_double *deltadist) { const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1; t_point_double plane; @@ -21,14 +22,14 @@ static void calculate_perpwalldist3(int x, vector_from_rotation(&dir, g_map.player.rot, 1); vector_from_rotation(&plane, g_map.player.rot + PI / 2, 1); - raydir->x = dir.x + plane.x * ray_direction; - raydir->y = dir.y + plane.y * ray_direction; + g_ray.dir.x = dir.x + plane.x * ray_direction; + g_ray.dir.y = dir.y + plane.y * ray_direction; deltadist->x = 1e30; - if (raydir->x != 0) - deltadist->x = fabs(1 / raydir->x); + if (g_ray.dir.x != 0) + deltadist->x = fabs(1 / g_ray.dir.x); deltadist->y = 1e30; - if (raydir->y != 0) - deltadist->y = fabs(1 / raydir->y); + if (g_ray.dir.y != 0) + deltadist->y = fabs(1 / g_ray.dir.y); } static void calculate_perpwalldist2(int x, @@ -38,10 +39,10 @@ static void calculate_perpwalldist2(int x, { const int map_x = (int)g_map.player.pos.x; const int map_y = (int)g_map.player.pos.y; - t_point_double raydir; + // t_point_double raydir; - calculate_perpwalldist3(x, &raydir, deltadist); - if (raydir.x < 0) + calculate_perpwalldist3(x, deltadist); + if (g_ray.dir.x < 0) { step->x = -1; sidedist->x = (g_map.player.pos.x - map_x) * deltadist->x; @@ -51,7 +52,7 @@ static void calculate_perpwalldist2(int x, step->x = 1; sidedist->x = (map_x + 1.0 - g_map.player.pos.x) * deltadist->x; } - if (raydir.y < 0) + if (g_ray.dir.y < 0) { step->y = -1; sidedist->y = (g_map.player.pos.y - map_y) * deltadist->y; @@ -71,7 +72,7 @@ static void calculate_perpwalldist2(int x, static double calculate_perpwalldist(int x) { t_point_int map_pos; - bool side; + // bool side; t_point_double sidedist; t_point_double deltadist; t_point_int step; @@ -85,18 +86,37 @@ static double calculate_perpwalldist(int x) { sidedist.x += deltadist.x; map_pos.x += step.x; - side = false; + g_ray.side = false; continue ; } sidedist.y += deltadist.y; map_pos.y += step.y; - side = true; + g_ray.side = true; } - if (!side) + if (!g_ray.side) return (sidedist.x - deltadist.x); return (sidedist.y - deltadist.y); } +static void ft_init_texture(const double perpwalldist) +{ + if (g_ray.side == 0 && g_ray.dir.x < 0) + g_tex.tex_dir = 0; + if (g_ray.side == 0 && g_ray.dir.x >= 0) + g_tex.tex_dir = 1; + if (g_ray.side == 1 && g_ray.dir.y < 0) + g_tex.tex_dir = 2; + if (g_ray.side == 1 && g_ray.dir.y >= 0) + g_tex.tex_dir = 3; + if (g_ray.side == 0) + g_ray.wallx = g_player.pos.y + perpwalldist \ + * g_ray.dir.y; + else + g_ray.wallx = g_player.pos.x + perpwalldist \ + * g_ray.dir.x; + g_ray.wallx -= floor((g_ray.wallx)); +} + /// @brief Draw a vertical line according to the ray direction x. /// /// @param x Ray direction x between 0 and 1. @@ -107,7 +127,14 @@ static void draw_vertical_line(int x, u_int32_t *img_data) int draw_start; int draw_end; int y; + int tex_y; + int tex_x; + int bpp; + int size_line; + int endian; + u_int32_t color; + ft_init_texture(perpwalldist); draw_start = -line_height / 2 + SCREEN_HEIGHT / 2; if (draw_start < 0) draw_start = 0; @@ -120,7 +147,18 @@ static void draw_vertical_line(int x, u_int32_t *img_data) if (y < draw_start) img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING; else if (y < draw_end) - img_data[y * SCREEN_WIDTH + x] = COLOR_WALL; + { + tex_y = ((y - draw_start) * g_tex.tex_height) / line_height; + tex_x = (int)(g_ray.wallx * g_tex.tex_width); + if (g_ray.side == 0 && g_ray.dir.x > 0) + tex_x = g_tex.tex_width - tex_x - 1; + if (g_ray.side == 1 && g_ray.dir.y < 0) + tex_x = g_tex.tex_height - tex_x - 1; + u_int32_t *texture_data = (u_int32_t *)mlx_get_data_addr(g_tex.textures[g_tex.tex_dir], &bpp, &size_line, &endian); + color = texture_data[tex_y * g_tex.tex_width + tex_x]; + img_data[y * SCREEN_WIDTH + x] = color; + // img_data[y * SCREEN_WIDTH + x] = COLOR_WALL; + } else img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR; y++; @@ -138,4 +176,4 @@ int render(u_int32_t *img_data) x++; } return (0); -} +} \ No newline at end of file diff --git a/utils.c b/utils.c index 6e8a570..e265cdf 100644 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */ -/* Updated: 2024/10/17 15:05:15 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 16:36:14 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/utils2.c b/utils2.c new file mode 100644 index 0000000..dc81ffa --- /dev/null +++ b/utils2.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 01:57:03 by greg #+# #+# */ +/* Updated: 2024/10/17 14:58:33 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// #include "algo.h" + +// void ft_init_texture(const double perpwalldist) +// { +// if (g_ray.side == 0 && g_ray.dir.x < 0) +// g_tex.tex_dir = 0; +// if (g_ray.side == 0 && g_ray.dir.x >= 0) +// g_tex.tex_dir = 1; +// if (g_ray.side == 1 && g_ray.dir.y < 0) +// g_tex.tex_dir = 2; +// if (g_ray.side == 1 && g_ray.dir.y >= 0) +// g_tex.tex_dir = 3; +// if (g_ray.side == 0) +// g_ray.wallx = g_player.pos.y + perpwalldist +// * g_ray.dir.y; +// else +// g_ray.wallx = g_player.pos.x + perpwalldist +// * g_ray.dir.x; +// g_ray.wallx -= floor((g_ray.wallx)); +// } + +// void *tex_ptr = (int *)mlx_get_data_addr(texture[0].img, &bpp, +// g_tex, &endian); + + +// tex_y = ((y - draw_start) * texture_height) / line_height; + +// tex_x = (int)(wall_x * (double)texture_width); +// if ((!side && raydir.x > 0) || (side && raydir.y < 0)) +// { +// tex_x = texture_width - tex_x - 1; +// } + +// u_int32_t color = textures[direction][tex_y * texture_width + tex_x]; + + + // else if (y < draw_end) + // { + // tex_y = ((y - draw_start) * g_tex.tex_height) / line_height; + // tex_x = (int)(g_ray.wallx * g_tex.tex_width); + // u_int32_t *texture_data = (u_int32_t *)g_tex.textures[g_tex.tex_dir]; + // u_int32_t color = texture_data[tex_y * g_tex.tex_width + tex_x]; + // img_data[y * SCREEN_WIDTH + x] = color; + // } \ No newline at end of file