sprites render in correct position

This commit is contained in:
djonker 2023-11-04 00:27:12 +01:00
parent 9a77da76d0
commit 67670b63ea
5 changed files with 54 additions and 50 deletions

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */
/* Updated: 2023/11/03 22:43:10 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 00:10:18 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -23,8 +23,8 @@
typedef struct s_sprite
{
int x;
int y;
double x;
double y;
int type;
} t_sprite;
@ -79,6 +79,7 @@ typedef struct s_varlist
int32_t fcolor;
int32_t ccolor;
int resize;
int *zbuffer;
} t_varlist;
void ft_frametime(t_varlist *vl);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */
/* Updated: 2023/11/03 23:17:11 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 00:23:51 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -29,31 +29,39 @@ mlx_texture_t *ft_selectsprite(t_varlist *vl, int type)
void ft_drawsprites(t_varlist *vl)
{
int i;
double spritex;
double spritey;
mlx_texture_t *sprite;
double invdet;
double transformx;
double transformy;
int udiv;
int vdiv;
double vmove;
int spritescreenx;
int vmovescreen;
int spriteheight;
int spritewidth;
int drawstarty;
int drawstartx;
int drawendy;
int drawendx;
int texx;
int texy;
int x;
int y;
int d;
uint32_t color;
int i;
int x;
int y;
double spritex;
double spritey;
double invdet;
double transformx;
double transformy;
int spritescreenx;
int vmovescreen;
int spriteheight;
int spritewidth;
int drawstarty;
int drawstartx;
int drawendy;
int drawendx;
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);
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)
{
@ -63,19 +71,16 @@ void ft_drawsprites(t_varlist *vl)
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) * (int)(1 + transformx / transformy);
udiv = 1;
vdiv = 1;
vmove = 0.0;
vmovescreen = (int)(vmove / transformy) + vl->hoffset + 0 / transformy;
spriteheight = abs((int)(vl->h / (transformy))) / vdiv;
spritescreenx = (vl->w / 2) * (1 + transformx / transformy);
vmovescreen = (int)transformy + vl->hoffset + 0 / transformy;
spriteheight = abs((int)(vl->h / (transformy)));
drawstarty = -spriteheight / 2 + vl->h / 2 + vmovescreen;
if (drawstarty < 0)
drawstarty = 0;
drawendy = spriteheight / 2 + vl->h / 2 + vmovescreen;
if (drawendy >= vl->h)
drawendy = vl->h - 1;
spritewidth = abs((int)(vl->h / transformy)) / udiv;
spritewidth = abs((int)(vl->h / transformy));
drawstartx = -spritewidth / 2 + spritescreenx;
if (drawstartx < 0)
drawstartx = 0;
@ -103,11 +108,8 @@ void ft_drawsprites(t_varlist *vl)
}
x++;
}
i++;
}
}
void ft_selecttexture(t_varlist *vl)

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */
/* Updated: 2023/11/03 22:42:48 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 00:10:50 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@ t_varlist initvarlist(void)
vl.w = 800;
vl.h = 600;
vl.sprite = ft_calloc(4096, 8);
vl.zbuffer = ft_calloc(4096, 8);
vl.spritecount = 0;
vl.hoffset = 0;
vl.ccolor = 0;

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:25:15 by houtworm ######## odam.nl */
/* Updated: 2023/11/03 23:59:37 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -15,7 +15,6 @@
char ft_setplayerpos(t_varlist *vl, char dir, int x, int y)
{
printf("x: %d, y: %d\n", x, y);
vl->posx = x + 0.5;
vl->posy = y + 0.5;
if (dir == 'N')
@ -51,8 +50,8 @@ char ft_setplayerpos(t_varlist *vl, char dir, int x, int y)
char ft_addsprite(t_varlist *vl, int x, int y, int type)
{
vl->sprite[vl->spritecount].x = x;
vl->sprite[vl->spritecount].y = y;
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 ('0');
@ -101,23 +100,23 @@ 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, x, y, 1);
map[y][x] = ft_addsprite(vl, y, x, 1);
else if (line[x] == 'G')
map[y][x] = ft_addsprite(vl, x, y, 2);
map[y][x] = ft_addsprite(vl, y, x, 2);
else if (line[x] == 'H')
map[y][x] = ft_addsprite(vl, x, y, 3);
map[y][x] = ft_addsprite(vl, y, x, 3);
else if (line[x] == 'C')
map[y][x] = ft_addsprite(vl, x, y, 1);
map[y][x] = ft_addsprite(vl, y, x, 1);
else if (line[x] == 'X')
map[y][x] = ft_addsprite(vl, x, y, 1);
map[y][x] = ft_addsprite(vl, y, x, 1);
else if (line[x] == 'K')
map[y][x] = ft_addsprite(vl, x, y, 1);
map[y][x] = ft_addsprite(vl, y, x, 1);
else
{
ft_putstr("Invalid character on the map\n Valid options are\n");
ft_putstr("NESW Player\n' ' or 0 empty space\n1 wall\n");
ft_putstr("D Door\nC Collectible\nB Barrel\nX Level end\n");
ft_errorexit("K Enemy\n", "", 1);
ft_errorexit("K Enemy\nG groundlamp\nH Hanglamp", "", 1);
}
x++;
}

View File

@ -6,7 +6,7 @@
/* By: djonker <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/27 14:36:42 by djonker #+# #+# */
/* Updated: 2023/11/03 22:08:37 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 00:09:58 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -78,6 +78,7 @@ void ft_raycast(t_varlist *vl)
if (drawend >= vl->h)
drawend = vl->h - 1;
ft_drawline(x, vl, drawstart, drawend);
vl->zbuffer[x] = vl->perpwalldist;
x++;
}
}