From 8ff76fe2e5fb3f643d54dc277ef32a3f06643f80 Mon Sep 17 00:00:00 2001 From: djonker Date: Sat, 4 Nov 2023 05:41:27 +0100 Subject: [PATCH] fixed random segfault when walking into sprite --- src/draw.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/draw.c b/src/draw.c index ca60e8d..a166da8 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* 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);