-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.c
More file actions
89 lines (82 loc) · 2.81 KB
/
render.c
File metadata and controls
89 lines (82 loc) · 2.81 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tborges- <tborges-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 13:13:34 by tborges- #+# #+# */
/* Updated: 2024/12/02 19:45:23 by tborges- ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
/**
* Renders the map on the window.
*/
int render_map(void *param)
{
t_game *game;
int x;
int y;
game = (t_game *)param;
y = 0;
while (y < game->map.rows)
{
x = 0;
while (x < game->map.cols)
{
render_tile(game, game->map.data[y][x], x, y);
x++;
}
y++;
}
return (0);
}
/**
* Helper function for the render_map function.
*/
void render_tile(t_game *game, char tile, int x, int y)
{
t_textures *textures;
textures = &game->textures;
if (tile == '1')
mlx_put_image_to_window(game->mlx, game->win, textures->wall, x
* SPRITE_SIZE, y * SPRITE_SIZE);
else if (tile == '0')
mlx_put_image_to_window(game->mlx, game->win, textures->floor, x
* SPRITE_SIZE, y * SPRITE_SIZE);
else if (tile == 'C')
mlx_put_image_to_window(game->mlx, game->win, textures->collectible, x
* SPRITE_SIZE, y * SPRITE_SIZE);
else if (tile == 'E')
mlx_put_image_to_window(game->mlx, game->win, textures->exit, x
* SPRITE_SIZE, y * SPRITE_SIZE);
else if (tile == 'P')
mlx_put_image_to_window(game->mlx, game->win, textures->player, x
* SPRITE_SIZE, y * SPRITE_SIZE);
}
/**
* Loads the textures used in the game.
*/
void load_textures(t_game *game)
{
t_textures *textures;
int height;
int width;
textures = &game->textures;
height = SPRITE_SIZE;
width = SPRITE_SIZE;
textures->wall = mlx_xpm_file_to_image(game->mlx, "assets/wall.xpm",
&height, &width);
textures->floor = mlx_xpm_file_to_image(game->mlx, "assets/background.xpm",
&height, &width);
textures->player = mlx_xpm_file_to_image(game->mlx, "assets/player.xpm",
&height, &width);
textures->collectible = mlx_xpm_file_to_image(game->mlx,
"assets/collectible.xpm", &height, &width);
textures->exit = mlx_xpm_file_to_image(game->mlx, "assets/close_door.xpm",
&height, &width);
if (!textures->wall || !textures->floor || !textures->player
|| !textures->collectible || !textures->exit)
error_exit_game("Failed to load textures", game);
}