From 7194cbfc83a4353626e1c7246d332fae97b40f91 Mon Sep 17 00:00:00 2001 From: djonker Date: Tue, 31 Oct 2023 16:29:35 +0100 Subject: [PATCH] textures are working at snail speed --- Makefile | 4 ++-- cub3d.h | 7 ++++++- src/draw.c | 43 ++++++++++++++++++++++++++++++++++++------- src/init.c | 3 ++- src/raycast.c | 9 ++++----- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 50b48e2..07ce8ff 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,13 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/10/29 15:24:58 by houtworm ######## odam.nl # +# Updated: 2023/10/31 15:58:18 by houtworm ######## odam.nl # # # # **************************************************************************** # NAME =cub3d CC =gcc -FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast# -g -fsanitize=address +FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast -g -fsanitize=address HEAD =-I ./include -I $(MLX)/include RM =rm -rf LIB =libft/libft.a getnextline/get_next_line.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm diff --git a/cub3d.h b/cub3d.h index 7935615..1e8cab2 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/10/29 17:04:28 by houtworm ######## odam.nl */ +/* Updated: 2023/10/31 15:22:33 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,12 +19,14 @@ # include "libft/libft.h" # include "getnextline/get_next_line.h" # include "mlx/include/MLX42/MLX42.h" +# include typedef struct s_varlist { mlx_t *mlx; mlx_image_t *img; mlx_image_t *fps; + mlx_texture_t *curtext; int fpsrefresh; int w; int h; @@ -38,6 +40,7 @@ typedef struct s_varlist double planey; double camerax; double cameray; + int hoffset; double raydirx; double raydiry; double movespeed; @@ -49,6 +52,7 @@ typedef struct s_varlist double deltadistx; double deltadisty; double perpwalldist; + int lineheight; int stepx; int stepy; int hit; @@ -58,6 +62,7 @@ typedef struct s_varlist double oldmouseposy; // only needed if we do vertical aiming int32_t fcolor; int32_t ccolor; + uint32_t sbuffer; //?? char *northwt; char *eastwt; char *southwt; diff --git a/src/draw.c b/src/draw.c index a1a5d70..cb8f5ff 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/10/29 12:28:28 by houtworm ######## odam.nl */ +/* Updated: 2023/10/31 16:17:59 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,14 +17,43 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) int y; y = 1; + while (y < drawstart) + { + mlx_put_pixel(vl->img, x, y, vl->ccolor); + y++; + } + double wallx; + if (vl->side == 0) + wallx = vl->posy + vl->perpwalldist * vl->raydiry; + else + wallx = vl->posx + vl->perpwalldist * vl->raydirx; + wallx -= floor(wallx); + int textx; + textx = wallx * 64.0; + if (vl->side == 0 && vl->raydirx > 0) + textx = 64 - textx - 1; + if (vl->side == 1 && vl->raydiry < 0) + textx = 64 - textx - 1; + double step; + step = 1.0 * 64 / vl->lineheight; + double textpos; + textpos = (drawstart - vl->hoffset - vl->h / 2 + vl->lineheight / 2) * step; + vl->curtext = mlx_load_png(vl->southwt); + while (y < drawend) + { + int texty; + texty = (int)textpos & (64 - 1); + textpos += step; + uint8_t *texel; + uint32_t color; + texel = &vl->curtext->pixels[(vl->curtext->width * texty + textx) * vl->curtext->bytes_per_pixel]; + color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; + mlx_put_pixel(vl->img, x, y, color); + y++; + } while (vl->h > y) { - if (y < drawstart) - mlx_put_pixel(vl->img, x, y, vl->ccolor); - else if (y < drawend) - mlx_put_pixel(vl->img, x, y, 0x440000FF); - else - mlx_put_pixel(vl->img, x, y, vl->fcolor); + mlx_put_pixel(vl->img, x, y, vl->fcolor); y++; } } diff --git a/src/init.c b/src/init.c index eb7d3ad..8b4f18b 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/10/29 17:17:54 by houtworm ######## odam.nl */ +/* Updated: 2023/10/31 14:24:17 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ t_varlist initvarlist(void) vl.w = 800; vl.h = 600; + vl.hoffset = 50; vl.ccolor = 0; vl.fcolor = 0; vl.northwt = NULL; diff --git a/src/raycast.c b/src/raycast.c index c54949e..e5779de 100644 --- a/src/raycast.c +++ b/src/raycast.c @@ -6,7 +6,7 @@ /* By: djonker +#+ */ /* +#+ */ /* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ -/* Updated: 2023/10/29 14:23:45 by houtworm ######## odam.nl */ +/* Updated: 2023/10/31 15:30:06 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ void ft_raycast(t_varlist *vl) { int x; - int lineheight; int drawstart; int drawend; @@ -71,11 +70,11 @@ void ft_raycast(t_varlist *vl) vl->perpwalldist = (vl->sidedistx - vl->deltadistx); else vl->perpwalldist = (vl->sidedisty - vl->deltadisty); - lineheight = (int)vl->h / vl->perpwalldist; - drawstart = -lineheight / 2 + vl->h / 2; + vl->lineheight = vl->h / vl->perpwalldist; + drawstart = -vl->lineheight / 2 + vl->h / 2 + vl->hoffset; if (drawstart < 0) drawstart = 0; - drawend = lineheight / 2 + vl->h / 2; + drawend = vl->lineheight / 2 + vl->h / 2 + vl->hoffset; if (drawend >= vl->h) drawend = vl->h - 1; ft_drawline(x, vl, drawstart, drawend);