-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextures.c
More file actions
71 lines (64 loc) · 2.34 KB
/
textures.c
File metadata and controls
71 lines (64 loc) · 2.34 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dhuss <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/12 15:50:11 by dhuss #+# #+# */
/* Updated: 2024/09/18 12:53:39 by dhuss ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void ft_error(t_game_state *game)
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("failed to load texture\n", 1);
if (game->map)
free_double_exit(game->map);
exit(EXIT_FAILURE);
}
void fill_map(t_game_state *game)
{
int y;
int x;
y = 0;
while (game->map[y] != NULL)
{
x = 0;
while (game->map[y][x] != '\0')
{
mlx_image_to_window(game->mlx, game->floor_img, x * 32, y * 32);
if (game->map[y][x] == '1')
mlx_image_to_window(game->mlx, game->wall_img, x * 32, y * 32);
if (game->map[y][x] == 'C')
mlx_image_to_window(game->mlx, game->coin_img, x * 32, y * 32);
if (game->map[y][x] == 'E')
mlx_image_to_window(game->mlx, game->exit_img, x * 32, y * 32);
x++;
}
y++;
}
mlx_image_to_window(game->mlx, game->player_img,
game->player.x * 32, game->player.y * 32);
}
static void handle_textures(t_game_state *game, mlx_image_t **img, char *path)
{
mlx_texture_t *texture;
texture = NULL;
texture = mlx_load_png(path);
if (!texture)
ft_error(game);
*img = mlx_texture_to_image(game->mlx, texture);
mlx_delete_texture(texture);
if (!(*img))
ft_error(game);
}
void load_textures(t_game_state *game)
{
handle_textures(game, &game->floor_img, "./sprites/floor.png");
handle_textures(game, &game->wall_img, "./sprites/wall.png");
handle_textures(game, &game->coin_img, "./sprites/key.png");
handle_textures(game, &game->exit_img, "./sprites/exit.png");
handle_textures(game, &game->player_img, "./sprites/player.png");
}