From 9b28e20199989430377db9176a326f1166f36984 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Tue, 3 May 2022 18:10:41 +0200 Subject: [PATCH] resolve wall distorsion and lag --- Makefile | 6 +++--- headers/cube3d_struct.h | 1 + maps/map_valid_14.cub | 4 ++-- srcs/draw/draw_column.c | 34 +++++++++++++--------------------- srcs/draw/raycast.c | 11 +++++++++-- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index caf4762..f1efa2a 100644 --- a/Makefile +++ b/Makefile @@ -27,8 +27,8 @@ SRCS += memorybook.c \ memorybook_utils.c # init/ SRCS += init_struct.c \ - init_game.c \ - init_textures.c + init_textures.c \ + init_game.c # gnl/ SRCS += get_next_line.c \ get_next_line_utils.c @@ -79,7 +79,7 @@ RM_OBJS = rm -rf $(D_OBJS) # flags CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -CFLAGS += -g3 +CFLAGS += -g3 LFLAGS = -L$(D_LFT) -lft LFLAGS += -L$(D_LMLX) -lm -lmlx -lXext -lX11 diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index dad5fc9..eca8f0b 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -34,6 +34,7 @@ typedef struct s_wall { t_vec vec; int height; + int limit; int delta; int posx; int imgx; diff --git a/maps/map_valid_14.cub b/maps/map_valid_14.cub index 79b0322..a0cba60 100644 --- a/maps/map_valid_14.cub +++ b/maps/map_valid_14.cub @@ -8,10 +8,10 @@ F 220 , 100, 30 C 225 , 30 , 0 1111111111111111111111111111 -1000000000000000011100000001 +1000000000000000N11100000001 1111000000000000100000110001 1111001000000000000110000111 -100000000N000000010101000001 +1000000000000000010101000001 1111110000000000000000001111 1000000000000000000000000001 1000000000000000000000000001 diff --git a/srcs/draw/draw_column.c b/srcs/draw/draw_column.c index 3e0fb1a..29a38d1 100644 --- a/srcs/draw/draw_column.c +++ b/srcs/draw/draw_column.c @@ -51,32 +51,24 @@ static int get_texture(t_img *img, int imgx, int j, int height) y = (j * img->height) / height; position = y * img->szl + imgx * (img->bpp / 8); color = img->data + position; - return (*(int*)color); + return (*(int *)color); } -static void draw_txt_column(t_img *img, t_rcast *rcast, t_wall *wall, t_img *txt_img) +static void draw_txt_column(t_game *game, t_wall *wall, t_img *txt_img) { - - int color; + int col; int img_x; int i; int j; - int limit; - img_x = (wall->posx * txt_img->width) / rcast->cell; - limit = (wall->height - rcast->screen_height) / 2; - if (limit < 0) - limit = 0; + img_x = (wall->posx * txt_img->width) / game->rcast.cell; + j = wall->limit; i = 0; - j = 0; - while (i < wall->height - limit) + while (j < wall->height - wall->limit) { - if (i > limit) - { - color = get_texture(txt_img, img_x, i, wall->height); - draw_pixel(img, wall->vec.start.x, wall->vec.end.y + j, color); - j++; - } + col = get_texture(txt_img, img_x, j, wall->height); + draw_pixel(&game->img, wall->vec.start.x, wall->vec.end.y + i, col); + j++; i++; } } @@ -85,11 +77,11 @@ void draw_column(t_game *game, t_rcast *rcast, t_wall *wall, t_txt *txt) { draw_floor_ceiling(game, rcast, txt); if (!rcast->is_x && rcast->slope_y > 0) - draw_txt_column(&game->img, rcast, wall, &txt->img_n); + draw_txt_column(game, wall, &txt->img_n); else if (!rcast->is_x && rcast->slope_y < 0) - draw_txt_column(&game->img, rcast, wall, &txt->img_s); + draw_txt_column(game, wall, &txt->img_s); else if (rcast->is_x && rcast->slope_x > 0) - draw_txt_column(&game->img, rcast, wall, &txt->img_e); + draw_txt_column(game, wall, &txt->img_e); else if (rcast->is_x && rcast->slope_x < 0) - draw_txt_column(&game->img, rcast, wall, &txt->img_w); + draw_txt_column(game, wall, &txt->img_w); } diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index caea111..4648808 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -18,12 +18,19 @@ static void calcul_img_column(t_game *game, t_rcast *rcast, t_wall *wall) } } -static void fill_wall_vector(t_rcast *rcast, int height) +static void fill_wall_vector(t_rcast *rcast, t_wall *wall) { + int height; + + height = wall->height; if (height < 0) height = 0; + wall->limit = 0; if (height > rcast->screen_height) + { + wall->limit = (height - rcast->screen_height) / 2; height = rcast->screen_height; + } rcast->wall.vec.start.y = rcast->screen_height / 2 + height / 2; rcast->wall.vec.end.y = rcast->screen_height / 2 - height / 2; } @@ -46,7 +53,7 @@ static void calcul_wall(t_rcast *rcast) if (rcast->wall.delta && rcast->screen_dist) height /= (rcast->wall.delta * rcast->screen_dist); rcast->wall.height = ft_abs(height); - fill_wall_vector(rcast, rcast->wall.height); + fill_wall_vector(rcast, &rcast->wall); } void raycast(t_game *game, t_rcast *rcast)