sprites always render at correct height

This commit is contained in:
djonker 2023-11-04 02:37:01 +01:00
parent 67670b63ea
commit 8eb0f6c5ea
7 changed files with 104 additions and 46 deletions

View File

@ -6,13 +6,13 @@
# By: houtworm <codam@houtworm.net> +#+ #
# +#+ #
# Created: 2023/10/26 10:46:29 by houtworm #+# #+# #
# Updated: 2023/11/02 18:31:43 by houtworm ######## odam.nl #
# Updated: 2023/11/04 02:26:44 by houtworm ######## odam.nl #
# #
# **************************************************************************** #
NAME =cub3d
CC =gcc
FC =-Wall -Werror -Wextra -Wunreachable-code -flto -Ofast -march=native #-g #-fsanitize=address
FC =-Wall -Werror -Wextra -Wunreachable-code -flto -Ofast -march=native -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/11/04 00:10:18 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 01:53:08 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -41,7 +41,11 @@ typedef struct s_varlist
mlx_texture_t *barreltext;
mlx_texture_t *hlamptext;
mlx_texture_t *slamptext;
mlx_texture_t *treasuretext;
mlx_texture_t *endtext;
mlx_texture_t *nazitext;
t_sprite *sprite;
t_sprite *spriteorder;
int spritecount;
int fpsrefresh;
int w;

View File

@ -7,26 +7,26 @@ SO ./assets/redbrick.png
WE ./assets/wood.png
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1 B1 1 1 1 1 1
1 K D K K 1 C D D K K X 1
1G B1 1 1 1 1 1
1 KH H H H H H D KH K 1 C H D D KHK X 1
1 1 1 1 1 1 1
1 11111111111 11111111111 111111111111111111111 1 1111111111 1111111111
1 1B C B1 1 1 1 1 1 D K K 1 1
1 1 K K 1 1 K K D 1 C 1 K K D 1 1 K K K K K K K K 1
1 1 1 1 1 1 1 1 1111111111 K K K K K K 1
1 1B C B1 1 1 1 1 1 D KH K 1 1
1 1 KH HK 1 1 K H K D 1 C 1 K H K D H 1 1 K K K K K K K K 1
1 1 1 1 1 1 H 1 1 1111111111 K K K K K K 1
1 11111 11111 11111111111 D K K K 11111111111 1 1 K K K K 1
1 1 1 1 K K 1 1 D K K 1 K K 1
1 1 K K D 1 1 K K D 1 1 1
1 1 1 1 K K 1 1 D KH K 1 K K 1
1 1 K H K D 1 1 K H K D H 1 1 1
1 1 1 11111111111 1 1111111111 1
1 11111111111 1 11111111111 1 1 1
1 1 1 D K K 1 1 D K K 1 1
1B 1 K K D 1 1 K K D 1 1 1
1 H H 11111111111 1 11111111111 1 1 1
1 1 1 D K H K 1 1 D KH K 1 1
1B 1 K H K D 1 1 K H K D H 1 1 1
1BB 1 1 11111111111 1 1111111111 1
111111111 11111111111 1 11111111111 1 1 1
1B1 1 1 1 D K K 1 1 D C 1 1
1 K K K 1 1 K K 1 1 1 K K D 1 1 1
1 1 C 1 1 K K K D 11111111111 K K K 1 1111111111 1
1 1 1 N 1 K K 1 1 1 K K 1 1 1
1 1111111 1 C 1 D K K 1 C 1B 1
1B1 1 1 1 D K H K 1 1 D HC 1 1
1 K K K 1 1 K K 1 1 1 KHK D H 1 1 1
1 1 H 1 H 1 K K K D 11111111111 K K K 1 1111111111 1
1 1 C 1 N 1 KHK 1 1 1 KHK 1 1 1
1 1111111 1 C 1 D K H K 1 C 1BH H H 1
1 1 1BBB1 1 1BB B1 1
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */
/* Updated: 2023/11/04 00:23:51 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 02:29:31 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -22,11 +22,63 @@ mlx_texture_t *ft_selectsprite(t_varlist *vl, int type)
sprite = vl->slamptext;
else if (type == 3)
sprite = vl->hlamptext;
else if (type == 4)
sprite = vl->treasuretext;
else if (type == 5)
sprite = vl->endtext;
else if (type == 6)
sprite = vl->nazitext;
else
ft_errorexit("what?", "what?", 1);
return (sprite);
}
double ft_puthighestfirst(t_varlist *vl, double *spritedistance, double oldrecord, int j)
{
int i;
double record;
int furthest;
i = 0;
record = 0;
furthest = 0;
while (vl->spritecount > i)
{
if (spritedistance[i] > record && spritedistance[i] < oldrecord)
{
record = spritedistance[i];
furthest = i;
}
i++;
}
oldrecord = record;
vl->spriteorder[j] = vl->sprite[furthest];
return (oldrecord);
}
void ft_sortsprites(t_varlist *vl)
{
int i;
double *spritedistance;
double oldrecord;
i = 0;
spritedistance = ft_calloc(4096, 8);
vl->spriteorder = ft_calloc(4096, 8);
while (vl->spritecount > i)
{
spritedistance[i] = ((vl->posx - vl->sprite[i].x) * (vl->posx - vl->sprite[i].x) + (vl->posy - vl->sprite[i].y) * (vl->posy - vl->sprite[i].y));
i++;
}
i = 0;
oldrecord = 99999999999999.0;
while (vl->spritecount > i)
{
oldrecord = ft_puthighestfirst(vl, spritedistance, oldrecord, i);
i++;
}
}
void ft_drawsprites(t_varlist *vl)
{
mlx_texture_t *sprite;
@ -49,31 +101,21 @@ void ft_drawsprites(t_varlist *vl)
int texx;
int texy;
int d;
int *spriteorder;
double *spritedistance;
uint32_t color;
i = 0;
spritedistance = ft_calloc(4096, 8);
spriteorder = ft_calloc(4096, 8);
ft_sortsprites(vl);
while (vl->spritecount > i)
{
spriteorder[i] = i;
spritedistance[i] = ((vl->posx - vl->sprite[i].x) * (vl->posx - vl->sprite[i].x) + (vl->posy - vl->sprite[i].y) * (vl->posy - vl->sprite[i].y));
i++;
}
i = 0;
while (vl->spritecount > i)
{
spritex = vl->sprite[i].x - vl->posx;
spritey = vl->sprite[i].y - vl->posy;
sprite = ft_selectsprite(vl, vl->sprite[i].type);
spritex = vl->spriteorder[i].x - vl->posx;
spritey = vl->spriteorder[i].y - vl->posy;
sprite = ft_selectsprite(vl, vl->spriteorder[i].type);
invdet = 1.0 / (vl->planex * vl->diry - vl->dirx * vl->planey);
transformx = invdet * (vl->diry * spritex - vl->dirx * spritey);
transformy = invdet * (-vl->planey * spritex + vl->planex * spritey);
spritescreenx = (vl->w / 2) * (1 + transformx / transformy);
vmovescreen = (int)transformy + vl->hoffset + 0 / transformy;
spriteheight = abs((int)(vl->h / (transformy)));
vmovescreen = vl->hoffset + 0 / transformy;
spriteheight = fabs((vl->h / transformy));
drawstarty = -spriteheight / 2 + vl->h / 2 + vmovescreen;
if (drawstarty < 0)
drawstarty = 0;
@ -91,7 +133,7 @@ void ft_drawsprites(t_varlist *vl)
while (x < drawendx)
{
texx = (int)(256 * (x - (-spritewidth / 2 + spritescreenx)) * 64 / spritewidth) / 256;
if (transformy > 0 && x > 0 && x < vl->w)
if (transformy > 0 && x > 0 && x < vl->w && transformy < vl->zbuffer[x] + 0.5)
{
y = drawstarty;
while (y < drawendy)

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */
/* Updated: 2023/11/04 00:10:50 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 01:52:30 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -31,6 +31,9 @@ t_varlist initvarlist(void)
vl.barreltext = mlx_load_png("./assets/barrel.png");
vl.hlamptext = mlx_load_png("./assets/hlamp.png");
vl.slamptext = mlx_load_png("./assets/slamp.png");
vl.treasuretext = mlx_load_png("./assets/treasure3.png");
vl.endtext = mlx_load_png("./assets/bkey.png");
vl.nazitext = mlx_load_png("./assets/guard1.png");
vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true);
if (!vl.mlx)
ft_errorexit("MLX failed to init", "initvarlist", 1);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/11/03 22:41:58 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 02:31:20 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */
/* Updated: 2023/11/03 23:59:37 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 02:34:10 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -48,7 +48,7 @@ char ft_setplayerpos(t_varlist *vl, char dir, int x, int y)
return ('0');
}
char ft_addsprite(t_varlist *vl, int x, int y, int type)
char ft_addwalktroughsprite(t_varlist *vl, int x, int y, int type)
{
vl->sprite[vl->spritecount].x = x + 0.5;
vl->sprite[vl->spritecount].y = y + 0.5;
@ -57,6 +57,15 @@ char ft_addsprite(t_varlist *vl, int x, int y, int type)
return ('0');
}
char ft_addsolidsprite(t_varlist *vl, int x, int y, int type)
{
vl->sprite[vl->spritecount].x = x + 0.5;
vl->sprite[vl->spritecount].y = y + 0.5;
vl->sprite[vl->spritecount].type = type;
vl->spritecount++;
return ('2');
}
char **ft_getmap(t_varlist *vl, int fd)
{
int y;
@ -100,17 +109,17 @@ char **ft_getmap(t_varlist *vl, int fd)
else if (line[x] == 'D')
map[y][x] = 'D';
else if (line[x] == 'B')
map[y][x] = ft_addsprite(vl, y, x, 1);
map[y][x] = ft_addsolidsprite(vl, y, x, 1);
else if (line[x] == 'G')
map[y][x] = ft_addsprite(vl, y, x, 2);
map[y][x] = ft_addsolidsprite(vl, y, x, 2);
else if (line[x] == 'H')
map[y][x] = ft_addsprite(vl, y, x, 3);
map[y][x] = ft_addwalktroughsprite(vl, y, x, 3);
else if (line[x] == 'C')
map[y][x] = ft_addsprite(vl, y, x, 1);
map[y][x] = ft_addsolidsprite(vl, y, x, 4);
else if (line[x] == 'X')
map[y][x] = ft_addsprite(vl, y, x, 1);
map[y][x] = ft_addsolidsprite(vl, y, x, 5);
else if (line[x] == 'K')
map[y][x] = ft_addsprite(vl, y, x, 1);
map[y][x] = ft_addsolidsprite(vl, y, x, 6);
else
{
ft_putstr("Invalid character on the map\n Valid options are\n");