summaryrefslogtreecommitdiff
path: root/sprite.c
blob: 09c98158a286a444324ee74c96dbd05d75f065da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   sprite.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: kdx    <kdx   @student.42angouleme.fr      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/10/09 07:15:24 by kdx               #+#    #+#             */
/*   Updated: 2022/10/10 19:18:58 by kdx              ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft/libft.h"
#include "minilibx/mlx.h"
#include "sprite.h"
#include "sily.h"

t_sprite	*sprite_load(t_sily *sily, char *path, int width, int height)
{
	t_sprite	*sprite;

	sprite = ft_calloc(1, sizeof(t_sprite));
	if (sprite == NULL)
		return (NULL);
	sprite->width = width;
	sprite->height = height;
	sprite->img = mlx_xpm_file_to_image(sily->ctx, path, &sprite->width,
			&sprite->height);
	if (sprite->img == NULL)
		return (ft_free(sprite));
	return (sprite);
}

void	sprite_destroy(t_sily *sily, t_sprite *sprite)
{
	if (sprite != NULL)
	{
		if (sprite->img != NULL)
			mlx_destroy_image(sily->ctx, sprite->img);
		ft_free(sprite);
	}
}

void	sprite_draw(t_sily *sily, t_sprite *sprite, int x, int y)
{
	if (sprite == NULL)
	{
		ft_putendl_fd("error: sprite is NULL", 2);
		return ;
	}
	mlx_put_image_to_window(sily->ctx, sily->window, sprite->img, x, y);
}