dev: Improved memory management of sprites

This commit is contained in:
Theo Champion 2025-08-11 13:00:38 +02:00
parent 3230a60c9f
commit f1697a6c73
6 changed files with 73 additions and 16 deletions

View file

@ -4,13 +4,14 @@ CC = cc
# https://github.com/google/sanitizers/wiki/AddressSanitizer
SANITIZERS = -fsanitize=address,undefined -fno-omit-frame-pointer
ifeq ($(CFLAGS),)
CFLAGS = -Wall -Wextra -Werror -g -O3 -ffast-math
CFLAGS = -Wall -Wextra -Werror -g -O2
endif
IFLAGS = -I./mlx -I./libft
SOURCEFILES = \
src/utils/inits.c \
src/utils/time.c \
src/utils/destroy_utils.c \
src/draw/draw_map.c \
src/draw/drawutils.c \
src/main.c \

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
/* Updated: 2025/08/07 11:28:49 by tchampio ### ########.fr */
/* Updated: 2025/08/11 11:59:21 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,9 @@
# include "utils/keypresses.h"
# include "consts.h"
// FIXME: Delete this line to let it compile for bonus part
# define BONUS 1
// the 4 static sprites are some of the perks and the mystery box
typedef struct s_cub3d_data
{

41
src/utils/destroy_utils.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* destroy_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/11 12:04:26 by tchampio #+# #+# */
/* Updated: 2025/08/11 12:30:06 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../../mlx/mlx.h"
#include <stdlib.h>
void destroy_textures(t_cub3d_data *data)
{
if (data->no_texture)
mlx_destroy_image(data->mlx, data->no_texture->img);
free(data->no_texture);
if (data->so_texture)
mlx_destroy_image(data->mlx, data->so_texture->img);
free(data->so_texture);
if (data->ea_texture)
mlx_destroy_image(data->mlx, data->ea_texture->img);
free(data->ea_texture);
if (data->we_texture)
mlx_destroy_image(data->mlx, data->we_texture->img);
free(data->we_texture);
}
void destroy_sprites(t_cub3d_data *data)
{
int sprite;
sprite = 0;
while (sprite < MAX_SPRITES)
free(data->sprite_list[sprite++]);
}

21
src/utils/destroy_utils.h Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* destroy_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/11 12:07:48 by tchampio #+# #+# */
/* Updated: 2025/08/11 12:10:21 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DESTROY_UTILS_H
# define DESTROY_UTILS_H
# include "../cub3d_data.h"
void destroy_textures(t_cub3d_data *data);
void destroy_sprites(t_cub3d_data *data);
#endif

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */
/* Updated: 2025/07/31 14:25:10 by kcolin ### ########.fr */
/* Updated: 2025/08/11 12:45:32 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#include "../../libft/includes/libft.h"
#include "../map/mapdata.h"
#include "../cub3d_data.h"
#include "./destroy_utils.h"
#include <stdlib.h>
void free_tab(char **tab)
@ -65,21 +66,11 @@ int destroy(t_cub3d_data *data, int exit_code)
mlx_destroy_window(data->mlx, data->mlx_win);
if (data->img_data)
mlx_destroy_image(data->mlx, data->img_data->img);
destroy_textures(data);
free(data->img_data);
if (data->no_texture)
mlx_destroy_image(data->mlx, data->no_texture->img);
free(data->no_texture);
if (data->so_texture)
mlx_destroy_image(data->mlx, data->so_texture->img);
free(data->so_texture);
if (data->ea_texture)
mlx_destroy_image(data->mlx, data->ea_texture->img);
free(data->ea_texture);
if (data->we_texture)
mlx_destroy_image(data->mlx, data->we_texture->img);
free(data->we_texture);
if (data->mlx)
mlx_destroy_display(data->mlx);
free(data->sprite_list);
free(data->mlx);
free(data->screen_matrix);
exit(exit_code);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/08/11 11:52:31 by tchampio ### ########.fr */
/* Updated: 2025/08/11 12:44:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */