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> +#+ */ /* 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/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 typedef struct s_sprite
{ {
int x; double x;
int y; double y;
int type; int type;
} t_sprite; } t_sprite;
@ -79,6 +79,7 @@ typedef struct s_varlist
int32_t fcolor; int32_t fcolor;
int32_t ccolor; int32_t ccolor;
int resize; int resize;
int *zbuffer;
} t_varlist; } t_varlist;
void ft_frametime(t_varlist *vl); void ft_frametime(t_varlist *vl);

View File

@ -6,7 +6,7 @@
/* 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/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) void ft_drawsprites(t_varlist *vl)
{ {
int i;
double spritex;
double spritey;
mlx_texture_t *sprite; mlx_texture_t *sprite;
double invdet; int i;
double transformx; int x;
double transformy; int y;
int udiv; double spritex;
int vdiv; double spritey;
double vmove; double invdet;
int spritescreenx; double transformx;
int vmovescreen; double transformy;
int spriteheight; int spritescreenx;
int spritewidth; int vmovescreen;
int drawstarty; int spriteheight;
int drawstartx; int spritewidth;
int drawendy; int drawstarty;
int drawendx; int drawstartx;
int texx; int drawendy;
int texy; int drawendx;
int x; int texx;
int y; int texy;
int d; int d;
uint32_t color; 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; i = 0;
while (vl->spritecount > i) 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); invdet = 1.0 / (vl->planex * vl->diry - vl->dirx * vl->planey);
transformx = invdet * (vl->diry * spritex - vl->dirx * spritey); transformx = invdet * (vl->diry * spritex - vl->dirx * spritey);
transformy = invdet * (-vl->planey * spritex + vl->planex * spritey); transformy = invdet * (-vl->planey * spritex + vl->planex * spritey);
spritescreenx = (vl->w / 2) * (int)(1 + transformx / transformy); spritescreenx = (vl->w / 2) * (1 + transformx / transformy);
udiv = 1; vmovescreen = (int)transformy + vl->hoffset + 0 / transformy;
vdiv = 1; spriteheight = abs((int)(vl->h / (transformy)));
vmove = 0.0;
vmovescreen = (int)(vmove / transformy) + vl->hoffset + 0 / transformy;
spriteheight = abs((int)(vl->h / (transformy))) / vdiv;
drawstarty = -spriteheight / 2 + vl->h / 2 + vmovescreen; drawstarty = -spriteheight / 2 + vl->h / 2 + vmovescreen;
if (drawstarty < 0) if (drawstarty < 0)
drawstarty = 0; drawstarty = 0;
drawendy = spriteheight / 2 + vl->h / 2 + vmovescreen; drawendy = spriteheight / 2 + vl->h / 2 + vmovescreen;
if (drawendy >= vl->h) if (drawendy >= vl->h)
drawendy = vl->h - 1; drawendy = vl->h - 1;
spritewidth = abs((int)(vl->h / transformy)) / udiv; spritewidth = abs((int)(vl->h / transformy));
drawstartx = -spritewidth / 2 + spritescreenx; drawstartx = -spritewidth / 2 + spritescreenx;
if (drawstartx < 0) if (drawstartx < 0)
drawstartx = 0; drawstartx = 0;
@ -103,11 +108,8 @@ void ft_drawsprites(t_varlist *vl)
} }
x++; x++;
} }
i++; i++;
} }
} }
void ft_selecttexture(t_varlist *vl) void ft_selecttexture(t_varlist *vl)

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ /* 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.w = 800;
vl.h = 600; vl.h = 600;
vl.sprite = ft_calloc(4096, 8); vl.sprite = ft_calloc(4096, 8);
vl.zbuffer = ft_calloc(4096, 8);
vl.spritecount = 0; vl.spritecount = 0;
vl.hoffset = 0; vl.hoffset = 0;
vl.ccolor = 0; vl.ccolor = 0;

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ /* 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) 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->posx = x + 0.5;
vl->posy = y + 0.5; vl->posy = y + 0.5;
if (dir == 'N') 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) char ft_addsprite(t_varlist *vl, int x, int y, int type)
{ {
vl->sprite[vl->spritecount].x = x; vl->sprite[vl->spritecount].x = x + 0.5;
vl->sprite[vl->spritecount].y = y; vl->sprite[vl->spritecount].y = y + 0.5;
vl->sprite[vl->spritecount].type = type; vl->sprite[vl->spritecount].type = type;
vl->spritecount++; vl->spritecount++;
return ('0'); return ('0');
@ -101,23 +100,23 @@ char **ft_getmap(t_varlist *vl, int fd)
else if (line[x] == 'D') else if (line[x] == 'D')
map[y][x] = 'D'; map[y][x] = 'D';
else if (line[x] == 'B') 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') 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') 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') 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') 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') else if (line[x] == 'K')
map[y][x] = ft_addsprite(vl, x, y, 1); map[y][x] = ft_addsprite(vl, y, x, 1);
else else
{ {
ft_putstr("Invalid character on the map\n Valid options are\n"); 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("NESW Player\n' ' or 0 empty space\n1 wall\n");
ft_putstr("D Door\nC Collectible\nB Barrel\nX Level end\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++; x++;
} }

View File

@ -6,7 +6,7 @@
/* By: djonker <codam@houtworm.net> +#+ */ /* By: djonker <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ /* 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) if (drawend >= vl->h)
drawend = vl->h - 1; drawend = vl->h - 1;
ft_drawline(x, vl, drawstart, drawend); ft_drawline(x, vl, drawstart, drawend);
vl->zbuffer[x] = vl->perpwalldist;
x++; x++;
} }
} }