From a09cf69412ef284e6bc33ce3e3e5faed0e8683dc Mon Sep 17 00:00:00 2001 From: Zy Date: Wed, 13 Nov 2024 18:33:41 +0100 Subject: [PATCH] misc: optimize render --- src/render.c | 17 ++--------------- src/render_utils.c | 28 ++++++++++++++++++++-------- src/render_utils.h | 7 ++++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/render.c b/src/render.c index 0ae21df..945c40d 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: Zy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 14:55:05 by greg #+# #+# */ -/* Updated: 2024/11/13 17:53:57 by Zy ### ########.fr */ +/* Updated: 2024/11/13 18:33:00 by Zy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -104,21 +104,8 @@ static void draw_vertical_line(u_int32_t *img_data, t_ray *ray) calculate_texdir_and_wallx(ray); line_height = (int)(SCREEN_WIDTH / FOV / 2 / ray->perpwalldist); draw_start = -line_height / 2 + SCREEN_HEIGHT / 2; - if (draw_start < 0) - draw_start = 0; draw_end = line_height / 2 + SCREEN_HEIGHT / 2; - if (draw_end >= SCREEN_HEIGHT) - draw_end = SCREEN_HEIGHT - 1; - ray->y = 0; - while (ray->y++ < SCREEN_HEIGHT) - { - if (ray->y < draw_start) - img_data[ray->y * SCREEN_WIDTH + ray->x] = g_map.color_ceiling; - else if (ray->y < draw_end) - draw_wall(ray, line_height, img_data); - else - img_data[ray->y * SCREEN_WIDTH + ray->x] = g_map.color_floor; - } + draw_vertical_line_end(ray, draw_start, draw_end, img_data); } int render(u_int32_t *img_data) diff --git a/src/render_utils.c b/src/render_utils.c index 6cfd96c..f93e2f9 100644 --- a/src/render_utils.c +++ b/src/render_utils.c @@ -6,7 +6,7 @@ /* By: Zy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/24 14:26:22 by grobledo #+# #+# */ -/* Updated: 2024/11/13 18:11:14 by Zy ### ########.fr */ +/* Updated: 2024/11/13 18:31:15 by Zy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,8 @@ void calculate_texdir_and_wallx(t_ray *ray) ray->wallx -= floor((ray->wallx)); } -void draw_wall(t_ray *ray, const int line_height, u_int32_t *img_data) +void draw_vertical_line_end(t_ray *ray, int draw_start, int draw_end, + u_int32_t *img_data) { u_int32_t color; u_int32_t *texture_data; @@ -44,10 +45,21 @@ void draw_wall(t_ray *ray, const int line_height, u_int32_t *img_data) int texy; const t_texture texture = g_map.textures[ray->texdir]; - texy = ((ray->y - (-line_height / 2 + SCREEN_HEIGHT / 2)) - * texture.height) / line_height; - texx = (int)(ray->wallx * (double)texture.width); - texture_data = get_data_addr(texture.image); - color = texture_data[texy * texture.width + texx]; - img_data[ray->y * SCREEN_WIDTH + ray->x] = color; + ray->y = 0; + while (ray->y++ < SCREEN_HEIGHT) + { + if (ray->y < draw_start) + img_data[ray->y * SCREEN_WIDTH + ray->x] = g_map.color_ceiling; + else if (ray->y < draw_end) + { + texy = ((ray->y - ((draw_start - draw_end) / 2 + SCREEN_HEIGHT / 2)) + * texture.height) / (draw_end - draw_start); + texx = (int)(ray->wallx * (double)texture.width); + texture_data = get_data_addr(texture.image); + color = texture_data[texy * texture.width + texx]; + img_data[ray->y * SCREEN_WIDTH + ray->x] = color; + } + else + img_data[ray->y * SCREEN_WIDTH + ray->x] = g_map.color_floor; + } } diff --git a/src/render_utils.h b/src/render_utils.h index a0a055e..a10f359 100644 --- a/src/render_utils.h +++ b/src/render_utils.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* render_utils.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: Zy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/24 14:26:22 by grobledo #+# #+# */ -/* Updated: 2024/11/12 17:38:08 by mcolonna ### ########.fr */ +/* Updated: 2024/11/13 18:31:28 by Zy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,7 @@ typedef struct s_ray // needs ray->x, ->side and ->dir to be defined. void calculate_texdir_and_wallx(t_ray *ray); -void draw_wall(t_ray *ray, const int line_height, u_int32_t *img_data); +void draw_vertical_line_end(t_ray *ray, int draw_start, int draw_end, + u_int32_t *img_data); #endif