-
Notifications
You must be signed in to change notification settings - Fork 0
/
textures.c
112 lines (107 loc) · 3.43 KB
/
textures.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: polmarti <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/16 18:39:35 by polmarti #+# #+# */
/* Updated: 2024/06/16 18:39:38 by polmarti ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
/**
* Selects and returns the appropriate texture for
* rendering walls based on the direction
* indicated by the flag. If flag is 0, selects
* between east and west textures; if flag is 1,
* selects between south and north textures.
*
* @param d Pointer to the main data structure containing
* scene and graphics information.
* @param flag Flag indicating the direction of the wall
* texture to select (0 for east/west, 1 for south/north).
* @return Pointer to the selected texture.
*/
mlx_texture_t *get_texture_walls(t_data *d, int collission)
{
if (collission == VERTICAL)
{
if (d->data_player.west)
return (d->map.tex.ea);
else
return (d->map.tex.we);
}
else
{
if (d->data_player.south)
return (d->map.tex.no);
else
return (d->map.tex.so);
}
}
/**
* @brief Loads a texture file and performs basic validation.
*
* Skips leading whitespace, removes trailing newline,
* loads the texture using * `mlx_load_png`, and checks for
* successful loading and dimensions (64x64).
* Returns the texture pointer on success,
* or NULL with error message on failure.
*
* @param line String containing the path to the texture file.
*
* @return Pointer to the loaded texture (mlx_texture_t) or NULL on error.
*/
mlx_texture_t *ft_load_texture(char *line)
{
mlx_texture_t *p_tex;
line += 2;
line = ft_skip_spaces(line);
p_tex = mlx_load_png(line);
if (!p_tex)
return (ft_putstr_fd("Error\nUnable to open texture asset\n", 2), NULL);
if (p_tex->height != 64 || p_tex->width != 64)
return (ft_putstr_fd("Error\nTextures must be 64x64 pixel\n", 2), NULL);
return (p_tex);
}
/**
* @brief Assigns loaded texture based on identifier (NO, SO, WE, EA)
*
* Reads texture identifier (first 2 characters) and assigns loaded texture
* from `ft_load_texture` to corresponding member in `map->tex`.
* Returns 1 on error, * 0 on success.
*
* @param map Pointer to the map structure.
* @param line String containing texture identifier and path.
*
* @return 0 on success, 1 on error (loading or invalid identifier).
*/
int ft_read_texture(t_map *map, char *line)
{
if (!ft_strncmp(line, "NO", 2))
{
map->tex.no = ft_load_texture(line);
if (!map->tex.no)
return (1);
}
else if (!ft_strncmp(line, "SO", 2))
{
map->tex.so = ft_load_texture(line);
if (!map->tex.so)
return (1);
}
else if (!ft_strncmp(line, "WE", 2))
{
map->tex.we = ft_load_texture(line);
if (!map->tex.we)
return (1);
}
else if (!ft_strncmp(line, "EA", 2))
{
map->tex.ea = ft_load_texture(line);
if (!map->tex.ea)
return (1);
}
return (0);
}