misc: optimize render
This commit is contained in:
parent
6007f27510
commit
a09cf69412
3 changed files with 26 additions and 26 deletions
17
src/render.c
17
src/render.c
|
@ -6,7 +6,7 @@
|
|||
/* By: Zy <frzysk@proton.me> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: Zy <frzysk@proton.me> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* render_utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: Zy <frzysk@proton.me> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
Loading…
Add table
Reference in a new issue