- turned menu into a class, still a lot of public variables around and menu_item...
[supertux.git] / src / texture.cpp
1 //
2 // C Implementation: texture
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "SDL.h"
14 #include "SDL_image.h"
15 #include "globals.h"
16 #include "screen.h"
17 #include "setup.h"
18 #include "texture.h"
19
20 void (*texture_load)     (texture_type* ptexture, const std::string& file, int use_alpha);
21 void (*texture_load_part)(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha);
22 void (*texture_free)     (texture_type* ptexture);  
23 void (*texture_draw)     (texture_type* ptexture, float x, float y, bool update);  
24 void (*texture_draw_bg)  (texture_type* ptexture, bool update);  
25 void (*texture_draw_part)(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update);
26
27
28 void texture_setup(void)
29 {
30 #ifdef NOOPENGL
31   texture_load = texture_load_sdl;
32   texture_load_part = texture_load_part_sdl;
33   texture_free = texture_free_sdl;
34   texture_draw = texture_draw_sdl;
35   texture_draw_bg = texture_draw_bg_sdl;
36   texture_draw_part = texture_draw_part_sdl;
37 #else
38
39   if(use_gl)
40     {
41       texture_load = texture_load_gl;
42       texture_load_part = texture_load_part_gl;
43       texture_free = texture_free_gl;
44       texture_draw = texture_draw_gl;
45       texture_draw_bg = texture_draw_bg_gl;
46       texture_draw_part = texture_draw_part_gl;
47     }
48   else
49     {
50       texture_load = texture_load_sdl;
51       texture_load_part = texture_load_part_sdl;
52       texture_free = texture_free_sdl;
53       texture_draw = texture_draw_sdl;
54       texture_draw_bg = texture_draw_bg_sdl;
55       texture_draw_part = texture_draw_part_sdl;
56     }
57 #endif
58 }
59
60 #ifndef NOOPENGL
61 void texture_load_gl(texture_type* ptexture, const std::string& file, int use_alpha)
62 {
63   texture_load_sdl(ptexture,file,use_alpha);
64   texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
65 }
66
67 void texture_load_part_gl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
68 {
69   texture_load_part_sdl(ptexture,file,x,y,w,h,use_alpha);
70   texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
71 }
72
73 /* Quick utility function for texture creation */
74 static int power_of_two(int input)
75 {
76         int value = 1;
77         int a;
78
79         while ( value < input ) {
80                 value <<= 1;
81         }
82         return value;
83 }
84
85 void texture_create_gl(SDL_Surface * surf, GLuint * tex)
86 {
87   Uint32 saved_flags;
88   Uint8  saved_alpha;
89   int w, h;
90   SDL_Surface *conv;
91   
92   w = power_of_two(surf->w);
93   h = power_of_two(surf->h),
94   conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
95 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
96                               0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
97 #else
98
99                               0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
100 #endif
101
102   /* Save the alpha blending attributes */
103   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
104   saved_alpha = surf->format->alpha;
105   if ( (saved_flags & SDL_SRCALPHA)
106        == SDL_SRCALPHA )
107     {
108       SDL_SetAlpha(surf, 0, 0);
109     }
110
111   SDL_BlitSurface(surf, 0, conv, 0);
112
113   /* Restore the alpha blending attributes */
114   if ( (saved_flags & SDL_SRCALPHA)
115        == SDL_SRCALPHA )
116     {
117       SDL_SetAlpha(surf, saved_flags, saved_alpha);
118     }
119
120   glGenTextures(1, &*tex);
121   glBindTexture(GL_TEXTURE_2D , *tex);
122   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
123   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
124   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
125   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
126   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
127   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
128   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
129       
130   SDL_FreeSurface(conv);
131 }
132
133 void texture_free_gl(texture_type* ptexture)
134 {
135   SDL_FreeSurface(ptexture->sdl_surface);
136   glDeleteTextures(1, &ptexture->gl_texture);
137 }
138
139 void texture_draw_gl(texture_type* ptexture, float x, float y, bool update)
140 {
141 float pw = power_of_two(ptexture->w);
142 float ph = power_of_two(ptexture->h);
143
144   glEnable(GL_TEXTURE_2D);
145   glEnable(GL_BLEND);
146   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
147
148   glColor4ub(255, 255, 255,255);
149
150   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
151
152   glBegin(GL_QUADS);
153   glTexCoord2f(0, 0);
154   glVertex2f(x, y);
155   glTexCoord2f((float)ptexture->w / pw, 0);
156   glVertex2f((float)ptexture->w+x, y);
157   glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);  glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
158   glTexCoord2f(0, (float)ptexture->h / ph);
159   glVertex2f(x, (float)ptexture->h+y);
160   glEnd();
161   
162   glDisable(GL_TEXTURE_2D);
163   glDisable(GL_BLEND);
164 }
165
166 void texture_draw_bg_gl(texture_type* ptexture, bool update)
167 {
168 float pw = power_of_two(ptexture->w);
169 float ph = power_of_two(ptexture->h);
170
171   glColor3ub(255, 255, 255);
172
173   glEnable(GL_TEXTURE_2D);
174   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
175
176   glBegin(GL_QUADS);
177   glTexCoord2f(0, 0);
178   glVertex2f(0, 0);
179   glTexCoord2f((float)ptexture->w / pw, 0);
180   glVertex2f(screen->w, 0);
181   glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);
182   glVertex2f(screen->w, screen->h);
183   glTexCoord2f(0, (float)ptexture->h / ph);
184   glVertex2f(0, screen->h);
185   glEnd();
186   
187   glDisable(GL_TEXTURE_2D);
188 }
189
190 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, bool update)
191 {
192 float pw = power_of_two(ptexture->w);
193 float ph = power_of_two(ptexture->h);
194
195   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
196
197   glEnable(GL_BLEND);
198   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
199
200   glColor4ub(255, 255, 255,255);
201
202   glEnable(GL_TEXTURE_2D);
203
204
205   glBegin(GL_QUADS);
206   glTexCoord2f(sx / pw, sy / ph);
207   glVertex2f(x, y);
208   glTexCoord2f((float)(sx + w) / pw, sy / ph);
209   glVertex2f(w+x, y);
210   glTexCoord2f((sx+w) / pw, (sy+h) / ph);
211   glVertex2f(w +x, h+y);
212   glTexCoord2f(sx / pw, (float)(sy+h) / ph);
213   glVertex2f(x, h+y);
214   glEnd();
215
216   glDisable(GL_TEXTURE_2D);
217   glDisable(GL_BLEND);
218
219 }
220 #endif
221
222 void texture_load_sdl(texture_type* ptexture, const std::string& file, int use_alpha)
223 {
224   SDL_Surface * temp;
225   
226   temp = IMG_Load(file.c_str());
227
228   if (temp == NULL)
229     st_abort("Can't load", file);
230
231   if(use_alpha == IGNORE_ALPHA && !use_gl)
232   ptexture->sdl_surface = SDL_DisplayFormat(temp);
233   else
234   ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
235   
236   if (ptexture->sdl_surface == NULL)
237     st_abort("Can't covert to display format", file);
238
239   if (use_alpha == IGNORE_ALPHA && !use_gl)
240     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
241
242   SDL_FreeSurface(temp);
243
244   ptexture->w = ptexture->sdl_surface->w;
245   ptexture->h = ptexture->sdl_surface->h;
246   
247 }
248
249 void texture_load_part_sdl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h,  int use_alpha)
250 {
251
252   SDL_Rect src;
253   SDL_Surface * temp;
254   SDL_Surface * conv;
255
256   temp = IMG_Load(file.c_str());
257
258   if (temp == NULL)
259     st_abort("Can't load", file);
260
261   /* Set source rectangle for conv: */
262
263   src.x = x;
264   src.y = y;
265   src.w = w;
266   src.h = h;
267
268   conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
269                               temp->format->Rmask,
270                               temp->format->Gmask,
271                               temp->format->Bmask,
272                               temp->format->Amask);
273
274   /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
275                                0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
276   #else
277
278                                0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
279   #endif*/
280
281   SDL_SetAlpha(temp,0,0);
282
283   SDL_BlitSurface(temp, &src, conv, NULL);
284   if(use_alpha == IGNORE_ALPHA && !use_gl)
285   ptexture->sdl_surface = SDL_DisplayFormat(conv);
286   else
287   ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
288
289   if (ptexture->sdl_surface == NULL)
290     st_abort("Can't covert to display format", file);
291
292   if (use_alpha == IGNORE_ALPHA && !use_gl)
293     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
294
295   SDL_FreeSurface(temp);
296   SDL_FreeSurface(conv);
297
298   ptexture->w = ptexture->sdl_surface->w;
299   ptexture->h = ptexture->sdl_surface->h;
300 }
301
302 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
303 {
304   Uint32 saved_flags;
305   Uint8  saved_alpha;
306   
307   /* Save the alpha blending attributes */
308   saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
309   saved_alpha = sdl_surf->format->alpha;
310   if ( (saved_flags & SDL_SRCALPHA)
311        == SDL_SRCALPHA )
312     {
313       SDL_SetAlpha(sdl_surf, 0, 0);
314     }
315    
316   if(use_alpha == IGNORE_ALPHA && !use_gl)
317   ptexture->sdl_surface = SDL_DisplayFormat(sdl_surf);
318   else
319   ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
320
321   /* Restore the alpha blending attributes */
322   if ( (saved_flags & SDL_SRCALPHA)
323        == SDL_SRCALPHA )
324     {
325       SDL_SetAlpha(ptexture->sdl_surface, saved_flags, saved_alpha);
326     }
327   
328   if (ptexture->sdl_surface == NULL)
329     st_abort("Can't covert to display format", "SURFACE");
330
331   if (use_alpha == IGNORE_ALPHA && !use_gl)
332     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
333
334   ptexture->w = ptexture->sdl_surface->w;
335   ptexture->h = ptexture->sdl_surface->h;
336
337 #ifndef NOOPENGL
338
339   if(use_gl)
340     {
341       texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
342     }
343 #endif
344 }
345
346 void texture_draw_sdl(texture_type* ptexture, float x, float y, bool update)
347 {
348
349   SDL_Rect dest;
350
351   dest.x = (int)x;
352   dest.y = (int)y;
353   dest.w = ptexture->w;
354   dest.h = ptexture->h;
355   SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
356   
357   if (update == UPDATE)
358     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
359 }
360
361
362 void texture_draw_bg_sdl(texture_type* ptexture, bool update)
363 {
364   SDL_Rect dest;
365   
366   dest.x = 0;
367   dest.y = 0;
368   dest.w = screen->w;
369   dest.h = screen->h;
370   
371   SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
372   
373   if (update == UPDATE)
374     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
375 }
376
377 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update)
378 {
379   SDL_Rect src, dest;
380
381   src.x = (int)sx;
382   src.y = (int)sy;
383   src.w = (int)w;
384   src.h = (int)h;
385
386   dest.x = (int)x;
387   dest.y = (int)y;
388   dest.w = (int)w;
389   dest.h = (int)h;
390
391
392   SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
393
394   if (update == UPDATE)
395     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
396 }
397
398 void texture_free_sdl(texture_type* ptexture)
399 {
400   SDL_FreeSurface(ptexture->sdl_surface);
401 }
402