feat: made sprites be placed in map file

This commit is contained in:
Theo Champion 2025-08-12 15:41:42 +02:00
parent 04705fe9e5
commit 09ff569a31
3 changed files with 64 additions and 5 deletions

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/11 12:04:26 by tchampio #+# #+# */
/* Updated: 2025/08/12 13:44:52 by tchampio ### ########.fr */
/* Updated: 2025/08/12 15:39:30 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,6 +35,11 @@ void destroy_sprites(t_cub3d_data *data)
int sprite;
sprite = 0;
while (sprite < MAX_SPRITES)
free(data->sprite_list[sprite++]);
while (data->sprite_list[sprite] && sprite < MAX_SPRITES)
{
mlx_destroy_image(data->mlx, data->sprite_list[sprite]->image->img);
free(data->sprite_list[sprite]->image);
free(data->sprite_list[sprite]);
sprite++;
}
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */
/* Updated: 2025/08/11 12:45:32 by tchampio ### ########.fr */
/* Updated: 2025/08/12 15:38:45 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -67,6 +67,7 @@ int destroy(t_cub3d_data *data, int exit_code)
if (data->img_data)
mlx_destroy_image(data->mlx, data->img_data->img);
destroy_textures(data);
destroy_sprites(data);
free(data->img_data);
if (data->mlx)
mlx_destroy_display(data->mlx);

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/08/11 12:44:57 by tchampio ### ########.fr */
/* Updated: 2025/08/12 15:21:00 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
#include "../../mlx/mlx.h"
#include "../map/map_checker.h"
#include "frees.h"
#include <stdio.h>
t_img_data *load_single_texture(t_cub3d_data *data, char *path)
{
@ -52,6 +53,57 @@ void load_textures(t_cub3d_data *data)
data->ea_texture = load_single_texture(data, data->map->ea_texture);
}
void place_base_sprites(t_cub3d_data *data, char **map)
{
int y;
int x;
int currentsprite;
y = 0;
currentsprite = 0;
while (y < data->map->mapheight)
{
x = 0;
while (x < (int)ft_strlen(map[y]))
{
if (map[y][x] == 'M')
{
data->sprite_list[currentsprite] = ft_calloc(sizeof(t_sprite), 1);
data->sprite_list[currentsprite]->image = load_single_texture(data, "ressources/box.xpm");
data->sprite_list[currentsprite]->x = x + 0.5;
data->sprite_list[currentsprite]->y = y + 0.5;
currentsprite++;
}
if (map[y][x] == 'Q')
{
data->sprite_list[currentsprite] = ft_calloc(sizeof(t_sprite), 1);
data->sprite_list[currentsprite]->image = load_single_texture(data, "ressources/revive.xpm");
data->sprite_list[currentsprite]->x = x + 0.5;
data->sprite_list[currentsprite]->y = y + 0.5;
currentsprite++;
}
if (map[y][x] == 'J')
{
data->sprite_list[currentsprite] = ft_calloc(sizeof(t_sprite), 1);
data->sprite_list[currentsprite]->image = load_single_texture(data, "ressources/juggernog.xpm");
data->sprite_list[currentsprite]->x = x + 0.5;
data->sprite_list[currentsprite]->y = y + 0.5;
currentsprite++;
}
if (map[y][x] == 'D')
{
data->sprite_list[currentsprite] = ft_calloc(sizeof(t_sprite), 1);
data->sprite_list[currentsprite]->image = load_single_texture(data, "ressources/doubletap.xpm");
data->sprite_list[currentsprite]->x = x + 0.5;
data->sprite_list[currentsprite]->y = y + 0.5;
currentsprite++;
}
x++;
}
y++;
}
}
void init_cub3d_data(t_cub3d_data *data, char **argv)
{
ft_bzero(data, sizeof(*data));
@ -74,4 +126,5 @@ void init_cub3d_data(t_cub3d_data *data, char **argv)
load_textures(data);
data->sprite_list = ft_calloc(sizeof(t_sprite *), MAX_SPRITES);
ft_memset(data->sprite_distances, -1, MAX_SPRITES);
place_base_sprites(data, data->map->map);
}