fix(sprite caster): Fixed crash when sprites overlap

Actually I'm not that sure it's the sprite overlap but it happened when
the sprites overlapped too close to the player, but basically the
program tried to render pixels out of bounds from the images, tex_x or
tex_y were able to go < 0. I forcefully set them to 0 if it was the
case, and I didn't see really much of visual glitches or something.
Anyways I'll stop rambling in this commit, just to say I stopped a real
weird crash because when I tried using valgrind or sanitizers, the
program would behave well and not crash. Might be tied to how fast the
program renders frames ?
This commit is contained in:
Theo Champion 2025-08-19 19:45:00 +02:00
parent 0d89bdf034
commit fdc8d02c34

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 15:51:01 by tchampio #+# #+# */ /* Created: 2025/08/05 15:51:01 by tchampio #+# #+# */
/* Updated: 2025/08/18 13:39:26 by tchampio ### ########.fr */ /* Updated: 2025/08/19 19:42:43 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
#include "../renderer/render.h" #include "../renderer/render.h"
#include "sort_sprites.h" #include "sort_sprites.h"
#include <math.h> #include <math.h>
#include <stdlib.h>
#include "../../libft/includes/libft.h" #include "../../libft/includes/libft.h"
static void calculate_pos_and_transform(t_cub3d_data *data, t_sprite *sprite, static void calculate_pos_and_transform(t_cub3d_data *data, t_sprite *sprite,
@ -73,6 +72,8 @@ static void render_sprite_col(t_cub3d_data *data, int i, int tex_x, int stripe)
d = (j) * 256 - HEIGHT * 128 d = (j) * 256 - HEIGHT * 128
+ data->sprite_list[i]->sprite_height * 128; + data->sprite_list[i]->sprite_height * 128;
tex_y = ((d * SIZE) / data->sprite_list[i]->sprite_height) / 256; tex_y = ((d * SIZE) / data->sprite_list[i]->sprite_height) / 256;
if (tex_y < 0)
tex_y = 0;
color = my_mlx_pixel_get( color = my_mlx_pixel_get(
data->sprite_list[data->sprite_order[i]]->image, data->sprite_list[data->sprite_order[i]]->image,
tex_x, tex_y); tex_x, tex_y);
@ -94,6 +95,8 @@ static void render_sprites(t_cub3d_data *data, int i)
- (-data->sprite_list[i]->sprite_width / 2 - (-data->sprite_list[i]->sprite_width / 2
+ data->sprite_list[i]->sprite_screen_x)) + data->sprite_list[i]->sprite_screen_x))
* SIZE / data->sprite_list[i]->sprite_width) / 256; * SIZE / data->sprite_list[i]->sprite_width) / 256;
if (tex_x < 0)
tex_x = 0;
if (data->sprite_list[i]->transform_y > 0 && stripe > 0 if (data->sprite_list[i]->transform_y > 0 && stripe > 0
&& stripe < WIDTH && stripe < WIDTH
&& data->sprite_list[i]->transform_y < data->zbuffer[stripe]) && data->sprite_list[i]->transform_y < data->zbuffer[stripe])