textures are working at snail speed

This commit is contained in:
djonker 2023-10-31 16:29:35 +01:00
parent 87eb97b21c
commit 7194cbfc83
5 changed files with 50 additions and 16 deletions

View File

@ -6,13 +6,13 @@
# By: houtworm <codam@houtworm.net> +#+ #
# +#+ #
# 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

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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 <stdio.h>
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;

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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++;
}
}

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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;

View File

@ -6,7 +6,7 @@
/* By: djonker <codam@houtworm.net> +#+ */
/* +#+ */
/* 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);