1 /*!
2  * \file
3  * \ingroup     load
4  * \brief 	Texture loading and handling
5  */
6 #ifndef __TEXTURES_H__
7 #define __TEXTURES_H__
8 
9 #include "cache.h"
10 #include "platform.h"
11 #ifndef MAP_EDITOR
12  #include "actors.h"
13  #ifdef USE_INLINE
14   #include "draw_scene.h"
15  #endif
16 #endif
17 #include "image_loading.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 typedef enum
24 {
25 	tt_gui = 0,
26 	tt_font,
27 	tt_image,
28 	tt_mesh,
29 	tt_atlas
30 } texture_type;
31 
32 typedef enum
33 {
34 	tft_auto = 0,
35 	tft_rgba4,
36 	tft_rgb8,
37 	tft_r5g6b5,
38 	tft_rgba8,
39 	tft_rgb5_a1,
40 	tft_a8,
41 	tft_l8,
42 	tft_la8,
43 	tft_dxt1,
44 	tft_dxt3,
45 	tft_dxt5,
46 	tft_ati1,
47 	tft_ati2
48 } texture_format_type;
49 
50 /*!
51  * we use a separate cache structure to cache textures.
52  */
53 typedef struct
54 {
55 	char file_name[128];		/*!< the filename of the texture */
56 	cache_item_struct *cache_ptr;	/*!< a pointer to the cached item */
57 	GLuint id;			/*!< the id of the texture */
58 	Uint32 hash;			/*!< hash value of the name */
59 	Uint32 size;			/*!< size of the texture */
60 	texture_type type;		/*!< the texture type, needed for loading and unloading */
61 	Uint8 load_err;			/*!< if true, we tried to load this texture before and failed */
62 	Uint8 alpha;			/*!< the texture has an alpha channel */
63 } texture_cache_t;
64 
65 /*!
66  * \ingroup 	textures
67  * \brief 	Load a texture
68  *
69  * 		Loads a texture for gui use. Mipmaps and anisotropic filters are used,
70  *		size reduction can happens and the texture is compressed if supported.
71  *		Also the texture cache is used for it.
72  * \param file_name The file name of the texture to load.
73  * \param type      The intended use of the texture
74  * \return The identifier in the cache for the texture
75  * \callgraph
76  */
77 Uint32 load_texture_cached(const char* file_name, const texture_type type);
78 
79 /*!
80  * \ingroup 	textures
81  * \brief 	Reloads the texture cache
82  *
83  *      	Reloads all textures in the texture cache.
84  *
85  * \callgraph
86  */
87 void init_texture_cache();
88 
89 /*!
90  * \ingroup 	textures
91  * \brief 	Unloads the texture cache
92  *
93  *      	Unloads all textures in the texture cache.
94  *
95  * \callgraph
96  */
97 void unload_texture_cache();
98 
99 /*!
100  * \ingroup 	textures
101  * \brief 	Reloads the texture cache
102  *
103  *      	Reloads all textures in the texture cache.
104  *
105  * \callgraph
106  */
107 void free_texture_cache();
108 
109 /*!
110  * \ingroup 	textures
111  * \brief 	Binds the texture
112  *
113  *      	Binds the texture using the given OpenGL id.
114  *
115  * \param	id The OpenGL id
116  * \callgraph
117  */
118 void bind_texture_id(const GLuint id);
119 
120 /*!
121  * \ingroup 	textures
122  * \brief 	Binds the texture
123  *
124  *      	Binds the texture using the given texture handle.
125  *
126  * \param	handle The texture handle
127  * \callgraph
128  */
129 void bind_texture(const Uint32 handle);
130 
131 /*!
132  * \ingroup 	textures
133  * \brief 	Binds the texture
134  *
135  *      	Binds the texture using the given texture handle. Does not
136  *		check if the texture is already bound. Needed, because at the
137  *		moment we don't track the active texture unit.
138  *
139  * \param	handle The texture handle
140  * \callgraph
141  */
142 void bind_texture_unbuffered(const Uint32 handle);
143 
144 /*!
145  * \ingroup 	textures
146  * \brief 	Gets the texture alpha
147  *
148  *      	Returns the texture alpha. Is one if the texture has a usefull
149  *		alpha value, else zero.
150  *
151  * \retval	Uint32 The texture alpha.
152  * \callgraph
153  */
154 Uint32 get_texture_alpha(const Uint32 handle);
155 
156 #ifdef	ELC
157 
158 /*!
159  * we use a separate cache structure to cache textures.
160  */
161 typedef struct
162 {
163 	char pants_tex[MAX_FILE_PATH];
164 	char pants_mask[MAX_FILE_PATH];
165 
166 	char boots_tex[MAX_FILE_PATH];
167 	char boots_mask[MAX_FILE_PATH];
168 
169 	char torso_tex[MAX_FILE_PATH];
170 	char arms_tex[MAX_FILE_PATH];
171 	char torso_mask[MAX_FILE_PATH];
172 	char arms_mask[MAX_FILE_PATH];
173 
174 	char hands_tex[MAX_FILE_PATH];
175 	char head_tex[MAX_FILE_PATH];
176 	char hands_mask[MAX_FILE_PATH];
177 	char head_mask[MAX_FILE_PATH];
178 
179 	char head_base[MAX_FILE_PATH];
180 	char body_base[MAX_FILE_PATH];
181 	char arms_base[MAX_FILE_PATH];
182 	char legs_base[MAX_FILE_PATH];
183 	char boots_base[MAX_FILE_PATH];
184 
185 	char hair_tex[MAX_FILE_PATH];
186 	char eyes_tex[MAX_FILE_PATH];
187 	char weapon_tex[MAX_FILE_PATH];
188 	char shield_tex[MAX_FILE_PATH];
189 	char helmet_tex[MAX_FILE_PATH];
190 	char neck_tex[MAX_FILE_PATH];
191 	char cape_tex[MAX_FILE_PATH];
192 	char hands_tex_save[MAX_FILE_PATH];
193 } enhanced_actor_images_t;
194 
195 typedef enum
196 {
197 	tst_unloaded = 0,
198 	tst_image_loading,
199 	tst_image_loaded,
200 	tst_texture_loading,
201 	tst_texture_loaded
202 } texture_state_type;
203 
204 #define MAX_ACTOR_NAME 24
205 
206 /*!
207  * we use a separate cache structure to cache textures.
208  */
209 typedef struct
210 {
211 	enhanced_actor_images_t files;	/*!< the files used for the texture */
212 	char name[MAX_ACTOR_NAME];	/*!< used as an uid.... */
213 	SDL_mutex* mutex;		/*!< the mutex used for this structure */
214 	image_t image;			/*!< the image for the texture */
215 	GLuint id;			/*!< the id of the texture */
216 	GLuint new_id;			/*!< the id of the new texture */
217 	Uint32 hash;			/*!< hash value of the files */
218 	Uint32 used;			/*!< if this is used at the moment? */
219 	Uint32 access_time;		/*!< last time used */
220 	texture_state_type state;	/*!< the texture states e.g. loading */
221 } actor_texture_cache_t;
222 
223 /*!
224  * \ingroup 	textures
225  * \brief 	Loads the actors texture
226  *
227  *      	Loads the actors texture from the information given in the
228  *		enhanced actor structure.
229  *
230  * \param actor A pointer to the enhanced_actor structure
231  * \param name  The name of the actor
232  * \retval	Uint32 The actor texture handle.
233  * \callgraph
234  */
235 Uint32 load_enhanced_actor(const enhanced_actor* actor, const char* name);
236 
237 /*!
238  * \ingroup 	textures
239  * \brief 	Binds the actors texture
240  *
241  *      	Binds the actor texture. This is needed, because actors can
242  *		share textures and the loading is threaded.
243  *
244  * \param   	handle The handle of the texture.
245  * \param   	alpha The pointer for the alpha.
246  * \retval	Uint32 Returns one if the texture is loaded, zero else.
247  * \callgraph
248  */
249 Uint32 bind_actor_texture(const Uint32 handle, char* alpha);
250 
251 /*!
252  * \ingroup 	textures
253  * \brief 	Frees the actors texture
254  *
255  *      	Marks the actor texture as unused.
256  *
257  * \param   	handle The handle of the texture.
258  * \callgraph
259  */
260 void free_actor_texture(const Uint32 handle);
261 
262 /*!
263  * \ingroup 	textures
264  * \brief 	Returns if the actor texture is ready
265  *
266  *      	Returns one if the actor texture is ready to use, zero else.
267  *
268  * \param   	handle The handle of the texture.
269  * \retval	Uint32 Returns one if the texture is ready for use, zero else.
270  * \callgraph
271  */
272 Uint32 get_actor_texture_ready(const Uint32 handle);
273 
274 /*!
275  * \ingroup 	textures
276  * \brief 	Use the new actor texture
277  *
278  *      	Use the new actor texture that was loaded in background
279  *
280  * \param   	handle The handle of the texture.
281  * \callgraph
282  */
283 void use_ready_actor_texture(const Uint32 handle);
284 
285 /*!
286  * \ingroup 	textures
287  * \brief 	Changes the actors texture
288  *
289  *      	Changes the actors texture from the information given in the
290  *		enhanced actor structure.
291  *
292  * \param   	handle Handle of the texture to change
293  * \param   	actor A pointer to the enhanced_actor structure
294  * \callgraph
295  */
296 void change_enhanced_actor(const Uint32 handle, enhanced_actor* actor);
297 
298 /*!
299  * \ingroup 	textures
300  * \brief 	Unloads the actor texture cache
301  *
302  *      	Unloads all actor textures in the actor texture cache.
303  *
304  * \callgraph
305  */
306 void unload_actor_texture_cache();
307 
308 #endif	//ELC
309 
310 #ifdef	DEBUG
311 void dump_texture_cache();
312 #endif	/* DEBUG */
313 
314 #ifdef __cplusplus
315 } // extern "C"
316 #endif
317 
318 #endif
319