fixed random segfault when walking into sprite

This commit is contained in:
djonker 2023-11-04 05:41:27 +01:00
parent e40b668d7e
commit 8ff76fe2e5

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */
/* Updated: 2023/11/04 04:56:26 by houtworm ######## odam.nl */
/* Updated: 2023/11/04 05:33:09 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -58,7 +58,6 @@ void ft_sortsprites(t_varlist *vl)
void ft_drawsprites(t_varlist *vl)
{
mlx_texture_t *sprite;
int i;
int x;
int y;
@ -86,7 +85,7 @@ void ft_drawsprites(t_varlist *vl)
{
spritex = vl->sprite[i].x - vl->posx;
spritey = vl->sprite[i].y - vl->posy;
sprite = ft_selectsprite(vl, vl->sprite[i].type);
vl->curtext = ft_selectsprite(vl, vl->sprite[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);
@ -118,7 +117,15 @@ void ft_drawsprites(t_varlist *vl)
d = (y - vmovescreen) * 256 - vl->h * 128 + spriteheight * 128;
texy = (d * 64) / spriteheight / 256;
uint8_t *texel;
texel = &sprite->pixels[(sprite->width * texy + texx) * 4];
if (texy < 0)
texy = 0;
if (texy > 64)
texy = 64;
if (texx < 0)
texx = 0;
if (texx > 64)
texx = 64;
texel = &vl->curtext->pixels[(vl->curtext->width * texy + texx) * 4];
color = texel[0] << 24 | texel[1] << 16 | texel[2] << 8 | texel[3];
if (color != 0x980088FF)
mlx_put_pixel(vl->img, x, y, color);