dev: improved sprites by creating a create_sprite function

This commit is contained in:
Theo Champion 2025-08-12 15:57:27 +02:00
parent 09ff569a31
commit 00e8a73d14
4 changed files with 60 additions and 31 deletions

View file

@ -32,6 +32,7 @@ SOURCEFILES = \
src/renderer/render.c \
src/sprites/sort_sprites.c \
src/sprites/sprite_caster.c \
src/sprites/create_sprite.c \
OBJECTS = $(SOURCEFILES:.c=.o)
NAME = cub3d

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_sprite.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/12 15:43:19 by tchampio #+# #+# */
/* Updated: 2025/08/12 15:53:05 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "sprite.h"
#include "../../libft/includes/libft.h"
#include "../cub3d_data.h"
#include "../utils/inits.h"
t_sprite *create_sprite(t_cub3d_data *data, char *texture,
double x, double y)
{
t_sprite *sprite;
sprite = ft_calloc(sizeof(t_sprite), 1);
if (!sprite)
return (NULL);
sprite->x = x;
sprite->y = y;
sprite->image = load_single_texture(data, texture);
return (sprite);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_sprite.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/12 15:49:25 by tchampio #+# #+# */
/* Updated: 2025/08/12 15:53:12 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CREATE_SPRITE_H
# define CREATE_SPRITE_H
# include "../cub3d_data.h"
t_sprite *create_sprite(t_cub3d_data *data,
char *filename, double x, double y);
#endif // CREATE_SPRITE_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/08/12 15:21:00 by tchampio ### ########.fr */
/* Updated: 2025/08/12 15:55:51 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include "../../libft/includes/libft.h"
#include "../../mlx/mlx.h"
#include "../map/map_checker.h"
#include "../sprites/create_sprite.h"
#include "frees.h"
#include <stdio.h>
@ -57,47 +58,23 @@ void place_base_sprites(t_cub3d_data *data, char **map)
{
int y;
int x;
int currentsprite;
int c;
y = 0;
currentsprite = 0;
c = 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++;
}
data->sprite_list[c++] = create_sprite(data, "ressources/box.xpm", x + 0.5, y + 0.5);
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++;
}
data->sprite_list[c++] = create_sprite(data, "ressources/revive.xpm", x + 0.5, y + 0.5);
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++;
}
data->sprite_list[c++] = create_sprite(data, "ressources/juggernog.xpm", x + 0.5, y + 0.5);
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++;
}
data->sprite_list[c++] = create_sprite(data, "ressources/doubletap.xpm", x + 0.5, y + 0.5);
x++;
}
y++;