From fdc8d02c3489454082a41d6e4eeeef096ead597b Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Tue, 19 Aug 2025 19:45:00 +0200 Subject: [PATCH] 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 ? --- src/sprites/sprite_caster.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/sprites/sprite_caster.c b/src/sprites/sprite_caster.c index 23e3657..ff5c713 100644 --- a/src/sprites/sprite_caster.c +++ b/src/sprites/sprite_caster.c @@ -6,7 +6,7 @@ /* 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 "sort_sprites.h" #include -#include #include "../../libft/includes/libft.h" 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 + data->sprite_list[i]->sprite_height * 128; tex_y = ((d * SIZE) / data->sprite_list[i]->sprite_height) / 256; + if (tex_y < 0) + tex_y = 0; color = my_mlx_pixel_get( data->sprite_list[data->sprite_order[i]]->image, 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_screen_x)) * 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 && stripe < WIDTH && data->sprite_list[i]->transform_y < data->zbuffer[stripe])