little cleanup

This commit is contained in:
djonker 2023-11-05 07:27:38 +01:00
parent b4f01a697a
commit 4932da0ad3
4 changed files with 73 additions and 41 deletions

View File

@ -6,7 +6,7 @@
# By: houtworm <codam@houtworm.net> +#+ # # By: houtworm <codam@houtworm.net> +#+ #
# +#+ # # +#+ #
# Created: 2023/10/26 10:46:29 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 LIB =libft/libft.a getnextline/get_next_line.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm
SRC =src/main/main.c\ SRC =src/main/main.c\
src/main/init.c\ src/main/init.c\
src/draw/draw.c\
src/main/error.c\ src/main/error.c\
src/parse/parse.c\
src/parse/map.c\
src/draw/raycast.c\ src/draw/raycast.c\
src/draw/world.c\
src/draw/texture.c\
src/input/game.c\ src/input/game.c\
src/input/move.c\ src/input/move.c\
src/input/turn.c\ src/input/turn.c\
src/input/rest.c\ src/input/rest.c\
src/parse/parse.c\ src/sprite/draw.c
src/sprite/draw.c\
src/parse/map.c
OBJ =$(SRC:src/%.c=obj/%.o) OBJ =$(SRC:src/%.c=obj/%.o)
all: libft getnextline mlx/build/mlx42.a $(NAME) all: libft getnextline mlx/build/mlx42.a $(NAME)

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 10:46:35 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 // draw.c
void ft_drawmap(t_varlist *vl); void ft_drawmap(t_varlist *vl);
void ft_drawsprites(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 // error.c
int ft_errorexit(char *reason, char *function, int code); int ft_errorexit(char *reason, char *function, int code);
#endif #endif

60
src/draw/texture.c Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* texture.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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);
}

View File

@ -1,35 +1,17 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* draw.c :+: :+: */ /* world.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 16:54:20 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" #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 ft_drawceiling(int x, t_varlist *vl, int drawstart)
{ {
int y; 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) int ft_drawwall(int x, t_varlist *vl, int drawstart, int drawend, int y)
{ {
double wallx;
int textx; int textx;
double step; double step;
double textpos; double textpos;
int texty; int texty;
uint8_t *texel;
uint32_t color; uint32_t color;
if (vl->side == 0) textx = ft_gettextx(vl);
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;
step = 64.0 / vl->lineheight; step = 64.0 / vl->lineheight;
textpos = (drawstart - vl->vaim - (vl->jump / vl->walldist) - vl->h / 2 + vl->lineheight / 2) * step; textpos = (drawstart - vl->vaim - (vl->jump / vl->walldist) - vl->h / 2 + vl->lineheight / 2) * step;
while (y < drawend) while (y < drawend)
{ {
texty = (int)textpos & (64 - 1); texty = (int)textpos & (64 - 1);
textpos += step; textpos += step;
texel = &vl->curtext->pixels[(vl->curtext->width * texty + textx) * 4]; color = ft_gettextcolor(vl, texty, textx);
color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3];
mlx_put_pixel(vl->img, x, y, color); mlx_put_pixel(vl->img, x, y, color);
y++; y++;
} }