From 8e650871e75fda1e5cbdb20425221ca9a92332b6 Mon Sep 17 00:00:00 2001 From: Gregory Robledo Date: Tue, 29 Oct 2024 13:52:11 +0100 Subject: [PATCH] norme --- algo.c | 10 +++++----- algo.h | 11 ++++++++++- render.c | 46 +++++++-------------------------------------- render2.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ utils2.c | 56 ------------------------------------------------------- 5 files changed, 73 insertions(+), 101 deletions(-) create mode 100644 render2.c delete mode 100644 utils2.c diff --git a/algo.c b/algo.c index 95bcf92..51f24f0 100644 --- a/algo.c +++ b/algo.c @@ -6,12 +6,13 @@ /* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ -/* Updated: 2024/10/22 15:48:47 by grobledo ### ########.fr */ +/* Updated: 2024/10/24 14:37:04 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ #include "algo.h" #include "input.h" +#include void *g_mlx = NULL; void *g_win = NULL; @@ -25,18 +26,17 @@ static void load_textures(t_tex *tex) { tex->tex_width = 64; tex->tex_height = 64; - tex->textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", &tex->tex_width, &tex->tex_height); tex->textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", &tex->tex_width, &tex->tex_height); - tex->textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", + tex->textures[2] = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", &tex->tex_width, &tex->tex_height); - tex->textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", + tex->textures[3] = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", &tex->tex_width, &tex->tex_height); } -void draw_screen() +void draw_screen(void) { int bpp; int size_line; diff --git a/algo.h b/algo.h index 270937f..32839ac 100644 --- a/algo.h +++ b/algo.h @@ -6,7 +6,7 @@ /* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ -/* Updated: 2024/10/22 15:48:21 by grobledo ### ########.fr */ +/* Updated: 2024/10/24 14:27:27 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,11 @@ typedef struct s_tex int tex_height; int tex_width; int tex_dir; + int tex_x; + int tex_y; + int bpp; + int size_line; + int endian; } t_tex; typedef struct s_ray @@ -77,4 +82,8 @@ void draw_screen(void); void move(void); +void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex); + +void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x, + int y, u_int32_t *img_data); #endif diff --git a/render.c b/render.c index 27bb911..15985c7 100644 --- a/render.c +++ b/render.c @@ -6,7 +6,7 @@ /* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 14:55:05 by greg #+# #+# */ -/* Updated: 2024/10/22 15:50:27 by grobledo ### ########.fr */ +/* Updated: 2024/10/24 14:27:52 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -98,41 +98,17 @@ static double calculate_perpwalldist(int x, t_ray *ray) return (sidedist.y - deltadist.y); } -static void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex) -{ - if (ray->side == 0 && ray->dir.x < 0) - tex->tex_dir = 0; - if (ray->side == 0 && ray->dir.x >= 0) - tex->tex_dir = 1; - if (ray->side == 1 && ray->dir.y < 0) - tex->tex_dir = 2; - if (ray->side == 1 && ray->dir.y >= 0) - tex->tex_dir = 3; - if (ray->side == 0) - ray->wallx = g_map.player.pos.y + perpwalldist \ - * ray->dir.y; - else - ray->wallx = g_map.player.pos.x + perpwalldist \ - * ray->dir.x; - ray->wallx -= floor((ray->wallx)); -} - /// @brief Draw a vertical line according to the ray direction x. /// /// @param x Ray direction x between 0 and 1. -static void draw_vertical_line(int x, u_int32_t *img_data, t_ray *ray, t_tex *tex) +static void draw_vertical_line(int x, u_int32_t *img_data, + t_ray *ray, t_tex *tex) { const double perpwalldist = calculate_perpwalldist(x, ray); const int line_height = (int)(SCREEN_HEIGHT / perpwalldist); 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, ray, tex); draw_start = -line_height / 2 + SCREEN_HEIGHT / 2; @@ -142,25 +118,16 @@ static void draw_vertical_line(int x, u_int32_t *img_data, t_ray *ray, t_tex *te if (draw_end >= SCREEN_HEIGHT) draw_end = SCREEN_HEIGHT - 1; y = 0; - while (y < SCREEN_HEIGHT) + while (y++ < SCREEN_HEIGHT) { if (y < draw_start) img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING; else if (y < draw_end) { - tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) * tex->tex_height) / line_height; - tex_x = (int)(ray->wallx * (double)tex->tex_width); - if (ray->side == 0 && ray->dir.x > 0) - tex_x = tex->tex_width - tex_x - 1; - if (ray->side == 1 && ray->dir.y < 0) - tex_x = tex->tex_height - tex_x - 1; - u_int32_t *texture_data = (u_int32_t *)mlx_get_data_addr(tex->textures[tex->tex_dir], &bpp, &size_line, &endian); - color = texture_data[tex_x * tex->tex_width + tex_y]; - img_data[y * SCREEN_WIDTH + x] = color; + draw_wall(tex, ray, line_height, x, y, img_data); } else img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR; - y++; } } @@ -168,6 +135,7 @@ int render(u_int32_t *img_data, t_tex *tex) { int x; t_ray ray; + x = 0; while (x < SCREEN_WIDTH) { @@ -175,4 +143,4 @@ int render(u_int32_t *img_data, t_tex *tex) x++; } return (0); -} \ No newline at end of file +} diff --git a/render2.c b/render2.c new file mode 100644 index 0000000..f6df898 --- /dev/null +++ b/render2.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grobledo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/24 14:26:22 by grobledo #+# #+# */ +/* Updated: 2024/10/24 14:27:37 by grobledo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "algo.h" + +void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex) +{ + if (ray->side == 0 && ray->dir.x < 0) + tex->tex_dir = 0; + if (ray->side == 0 && ray->dir.x >= 0) + tex->tex_dir = 1; + if (ray->side == 1 && ray->dir.y < 0) + tex->tex_dir = 2; + if (ray->side == 1 && ray->dir.y >= 0) + tex->tex_dir = 3; + if (ray->side == 0) + ray->wallx = g_map.player.pos.y + perpwalldist \ + * ray->dir.y; + else + ray->wallx = g_map.player.pos.x + perpwalldist \ + * ray->dir.x; + ray->wallx -= floor((ray->wallx)); +} + +void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x, + int y, u_int32_t *img_data) +{ + u_int32_t color; + u_int32_t *texture_data; + + tex->tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) \ + * tex->tex_height) / line_height; + tex->tex_x = (int)(ray->wallx * (double)tex->tex_width); + if (ray->side == 0 && ray->dir.x > 0) + tex->tex_x = tex->tex_width - tex->tex_x - 1; + if (ray->side == 1 && ray->dir.y < 0) + tex->tex_x = tex->tex_height - tex->tex_x - 1; + texture_data = (u_int32_t *)mlx_get_data_addr(tex->textures[tex->tex_dir], + &tex->bpp, &tex->size_line, &tex->endian); + color = texture_data[tex->tex_x * tex->tex_width + tex->tex_y]; + img_data[y * SCREEN_WIDTH + x] = color; +} \ No newline at end of file diff --git a/utils2.c b/utils2.c deleted file mode 100644 index 9a323a8..0000000 --- a/utils2.c +++ /dev/null @@ -1,56 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* utils2.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: grobledo +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/10/17 01:57:03 by greg #+# #+# */ -/* Updated: 2024/10/17 17:39:18 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