From 4932da0ad387a5166c4d270a0726892a3dae87d6 Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 5 Nov 2023 07:27:38 +0100 Subject: [PATCH] little cleanup --- Makefile | 11 ++++--- cub3d.h | 4 ++- src/draw/texture.c | 60 ++++++++++++++++++++++++++++++++++++ src/draw/{draw.c => world.c} | 39 +++-------------------- 4 files changed, 73 insertions(+), 41 deletions(-) create mode 100644 src/draw/texture.c rename src/draw/{draw.c => world.c} (73%) diff --git a/Makefile b/Makefile index 6504b8a..0e988b9 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/11/05 06:56:35 by houtworm ######## odam.nl # +# Updated: 2023/11/05 07:27:22 by houtworm ######## odam.nl # # # # **************************************************************************** # @@ -18,16 +18,17 @@ RM =rm -rf LIB =libft/libft.a getnextline/get_next_line.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm SRC =src/main/main.c\ src/main/init.c\ - src/draw/draw.c\ src/main/error.c\ + src/parse/parse.c\ + src/parse/map.c\ src/draw/raycast.c\ + src/draw/world.c\ + src/draw/texture.c\ src/input/game.c\ src/input/move.c\ src/input/turn.c\ src/input/rest.c\ - src/parse/parse.c\ - src/sprite/draw.c\ - src/parse/map.c + src/sprite/draw.c OBJ =$(SRC:src/%.c=obj/%.o) all: libft getnextline mlx/build/mlx42.a $(NAME) diff --git a/cub3d.h b/cub3d.h index e35569f..8d32c62 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/11/05 06:43:10 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 07:24:54 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -106,6 +106,8 @@ void ft_raycast(t_varlist *vl, int x, int mapx, int mapy); // draw.c void ft_drawmap(t_varlist *vl); void ft_drawsprites(t_varlist *vl); +int ft_gettextx(t_varlist *vl); +uint32_t ft_gettextcolor(t_varlist *vl, int texty, int textx); // error.c int ft_errorexit(char *reason, char *function, int code); #endif diff --git a/src/draw/texture.c b/src/draw/texture.c new file mode 100644 index 0000000..361b2ec --- /dev/null +++ b/src/draw/texture.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* texture.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ +/* Updated: 2023/11/05 07:23:45 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_selecttexture(t_varlist *vl) +{ + if (vl->side == 0) + { + if (vl->raydirx > 0) + vl->curtext = vl->northtext; + else + vl->curtext = vl->southtext; + } + else + { + if (vl->raydiry > 0) + vl->curtext = vl->westtext; + else + vl->curtext = vl->easttext; + } +} + +int ft_gettextx(t_varlist *vl) +{ + double wallx; + int textx; + + if (vl->side == 0) + wallx = vl->posy + vl->walldist * vl->raydiry; + else + wallx = vl->posx + vl->walldist * vl->raydirx; + ft_selecttexture(vl); + wallx -= floor(wallx); + 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; + return (textx); +} + +uint32_t ft_gettextcolor(t_varlist *vl, int texty, int textx) +{ + uint8_t *texel; + uint32_t color; + + texel = &vl->curtext->pixels[(vl->curtext->width * texty + textx) * 4]; + color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; + return (color); +} diff --git a/src/draw/draw.c b/src/draw/world.c similarity index 73% rename from src/draw/draw.c rename to src/draw/world.c index 5faff1a..65163c5 100644 --- a/src/draw/draw.c +++ b/src/draw/world.c @@ -1,35 +1,17 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* draw.c :+: :+: */ +/* world.c :+: :+: */ /* +:+ */ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 06:59:29 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 07:23:20 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_selecttexture(t_varlist *vl) -{ - if (vl->side == 0) - { - if (vl->raydirx > 0) - vl->curtext = vl->northtext; - else - vl->curtext = vl->southtext; - } - else - { - if (vl->raydiry > 0) - vl->curtext = vl->westtext; - else - vl->curtext = vl->easttext; - } -} - int ft_drawceiling(int x, t_varlist *vl, int drawstart) { int y; @@ -45,33 +27,20 @@ int ft_drawceiling(int x, t_varlist *vl, int drawstart) int ft_drawwall(int x, t_varlist *vl, int drawstart, int drawend, int y) { - double wallx; int textx; double step; double textpos; int texty; - uint8_t *texel; uint32_t color; - if (vl->side == 0) - wallx = vl->posy + vl->walldist * vl->raydiry; - else - wallx = vl->posx + vl->walldist * vl->raydirx; - ft_selecttexture(vl); - wallx -= floor(wallx); - 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; + textx = ft_gettextx(vl); step = 64.0 / vl->lineheight; textpos = (drawstart - vl->vaim - (vl->jump / vl->walldist) - vl->h / 2 + vl->lineheight / 2) * step; while (y < drawend) { texty = (int)textpos & (64 - 1); textpos += step; - texel = &vl->curtext->pixels[(vl->curtext->width * texty + textx) * 4]; - color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3]; + color = ft_gettextcolor(vl, texty, textx); mlx_put_pixel(vl->img, x, y, color); y++; }