- memleak fix and menu fix from MatzeB
[supertux.git] / src / texture.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include <assert.h>
22 #include <iostream>
23 #include "SDL.h"
24 #include "SDL_image.h"
25 #include "texture.h"
26 #include "globals.h"
27 #include "setup.h"
28
29 Surface::Surfaces Surface::surfaces;
30
31 SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
32   : type(SURFACE), surface(0), use_alpha(use_alpha_)
33 {
34   // Copy the given surface and make sure that it is not stored in
35   // video memory
36   surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
37                                  temp->w, temp->h,
38                                  temp->format->BitsPerPixel,
39                                  temp->format->Rmask,
40                                  temp->format->Gmask,
41                                  temp->format->Bmask,
42                                  temp->format->Amask);
43   if(!surface)
44     st_abort("No memory left.", "");
45   SDL_SetAlpha(temp,0,0);
46   SDL_BlitSurface(temp, NULL, surface, NULL);
47 }
48
49 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
50   : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
51 {
52 }
53   
54 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
55   : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
56     x(x_), y(y_), w(w_), h(h_)
57 {
58 }
59
60 SurfaceData::~SurfaceData()
61 {
62   SDL_FreeSurface(surface);  
63 }
64
65 SurfaceImpl*
66 SurfaceData::create()
67 {
68   if (use_gl)
69     return create_SurfaceOpenGL();
70   else
71     return create_SurfaceSDL();
72 }
73
74 SurfaceSDL*
75 SurfaceData::create_SurfaceSDL()
76 {
77   switch(type)
78     {
79     case LOAD:
80       return new SurfaceSDL(file, use_alpha);
81     case LOAD_PART:
82       return new SurfaceSDL(file, x, y, w, h, use_alpha);
83     case SURFACE:
84       return new SurfaceSDL(surface, use_alpha);
85     }
86   assert(0);
87 }
88
89 SurfaceOpenGL*
90 SurfaceData::create_SurfaceOpenGL()
91 {
92   switch(type)
93     {
94     case LOAD:
95       return new SurfaceOpenGL(file, use_alpha);
96     case LOAD_PART:
97       return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
98     case SURFACE:
99       return new SurfaceOpenGL(surface, use_alpha);
100     }
101   assert(0);
102 }
103
104
105 /* Quick utility function for texture creation */
106 static int power_of_two(int input)
107 {
108   int value = 1;
109
110   while ( value < input ) {
111     value <<= 1;
112   }
113   return value;
114 }
115
116 Surface::Surface(SDL_Surface* surf, int use_alpha)
117   : data(surf, use_alpha), w(0), h(0)
118 {
119   impl = data.create();
120   if (impl) 
121     {
122       w = impl->w;
123       h = impl->h;
124     }
125   surfaces.push_back(this);
126 }
127
128 Surface::Surface(const std::string& file, int use_alpha)
129   : data(file, use_alpha), w(0), h(0)
130 {
131   impl = data.create();
132   if (impl) 
133     {
134       w = impl->w;
135       h = impl->h;
136     }
137   surfaces.push_back(this);
138 }
139
140 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
141   : data(file, x, y, w, h, use_alpha), w(0), h(0)
142 {
143   impl = data.create();
144   if (impl) 
145     {
146       w = impl->w;
147       h = impl->h;
148     }
149   surfaces.push_back(this);
150 }
151
152 void
153 Surface::reload()
154 {
155   delete impl;
156   impl = data.create();
157   if (impl) 
158     {
159       w = impl->w;
160       h = impl->h;  
161     }
162 }
163
164 Surface::~Surface()
165 {
166   surfaces.remove(this);
167   delete impl;
168 }
169
170 void
171 Surface::reload_all()
172 {
173   for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
174     {
175       (*i)->reload();
176     }
177 }
178
179 void
180 Surface::draw(float x, float y, Uint8 alpha, bool update)
181 {
182   if (impl) 
183     {
184       if (impl->draw(x, y, alpha, update) == -2)
185         reload();
186     }
187 }
188
189 void
190 Surface::draw_bg(Uint8 alpha, bool update)
191 {
192   if (impl)
193     {
194       if (impl->draw_bg(alpha, update) == -2)
195         reload();
196     }
197 }
198
199 void
200 Surface::draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update)
201 {
202   if (impl)
203     {
204       if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
205         reload();
206     }
207 }
208
209 SDL_Surface*
210 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h,  int use_alpha)
211 {
212   SDL_Rect src;
213   SDL_Surface * sdl_surface;
214   SDL_Surface * temp;
215   SDL_Surface * conv;
216
217   temp = IMG_Load(file.c_str());
218
219   if (temp == NULL)
220     st_abort("Can't load", file);
221
222   /* Set source rectangle for conv: */
223
224   src.x = x;
225   src.y = y;
226   src.w = w;
227   src.h = h;
228
229   conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
230                               temp->format->Rmask,
231                               temp->format->Gmask,
232                               temp->format->Bmask,
233                               temp->format->Amask);
234
235   /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
236      0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
237      #else
238
239      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
240      #endif*/
241
242   SDL_SetAlpha(temp,0,0);
243
244   SDL_BlitSurface(temp, &src, conv, NULL);
245   if(use_alpha == IGNORE_ALPHA && !use_gl)
246     sdl_surface = SDL_DisplayFormat(conv);
247   else
248     sdl_surface = SDL_DisplayFormatAlpha(conv);
249
250   if (sdl_surface == NULL)
251     st_abort("Can't covert to display format", file);
252
253   if (use_alpha == IGNORE_ALPHA && !use_gl)
254     SDL_SetAlpha(sdl_surface, 0, 0);
255
256   SDL_FreeSurface(temp);
257   SDL_FreeSurface(conv);
258   
259   return sdl_surface;
260 }
261
262 SDL_Surface*
263 sdl_surface_from_file(const std::string& file, int use_alpha)
264 {
265   SDL_Surface* sdl_surface;
266   SDL_Surface* temp;
267   
268   temp = IMG_Load(file.c_str());
269
270   if (temp == NULL)
271     st_abort("Can't load", file);
272
273   if(use_alpha == IGNORE_ALPHA && !use_gl)
274     sdl_surface = SDL_DisplayFormat(temp);
275   else
276     sdl_surface = SDL_DisplayFormatAlpha(temp);
277   
278   if (sdl_surface == NULL)
279     st_abort("Can't covert to display format", file);
280
281   if (use_alpha == IGNORE_ALPHA && !use_gl)
282     SDL_SetAlpha(sdl_surface, 0, 0);
283
284   SDL_FreeSurface(temp);
285
286   return sdl_surface;
287 }
288
289 SDL_Surface* 
290 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
291 {
292   SDL_Surface* sdl_surface;
293   Uint32 saved_flags;
294   Uint8  saved_alpha;
295   
296   /* Save the alpha blending attributes */
297   saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
298   saved_alpha = sdl_surf->format->alpha;
299   if ( (saved_flags & SDL_SRCALPHA)
300        == SDL_SRCALPHA )
301     {
302       SDL_SetAlpha(sdl_surf, 0, 0);
303     }
304    
305   if(use_alpha == IGNORE_ALPHA && !use_gl)
306     sdl_surface = SDL_DisplayFormat(sdl_surf);
307   else
308     sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
309
310   /* Restore the alpha blending attributes */
311   if ( (saved_flags & SDL_SRCALPHA)
312        == SDL_SRCALPHA )
313     {
314       SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
315     }
316   
317   if (sdl_surface == NULL)
318     st_abort("Can't covert to display format", "SURFACE");
319
320   if (use_alpha == IGNORE_ALPHA && !use_gl)
321     SDL_SetAlpha(sdl_surface, 0, 0);
322
323   return sdl_surface;
324 }
325
326 //---------------------------------------------------------------------------
327
328 SurfaceImpl::SurfaceImpl()
329 {
330 }
331
332 SurfaceImpl::~SurfaceImpl()
333 {
334   SDL_FreeSurface(sdl_surface);
335 }
336
337 SDL_Surface* SurfaceImpl::get_sdl_surface() const
338 {
339   return sdl_surface;
340 }
341
342 #ifndef NOOPENGL
343 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
344 {
345   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
346   create_gl(sdl_surface,&gl_texture);
347
348   w = sdl_surface->w;
349   h = sdl_surface->h;
350 }
351
352 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha) 
353 {
354   sdl_surface = sdl_surface_from_file(file, use_alpha);
355   create_gl(sdl_surface,&gl_texture);
356
357   w = sdl_surface->w;
358   h = sdl_surface->h;
359 }
360
361 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
362 {
363   sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
364   create_gl(sdl_surface, &gl_texture);
365
366   w = sdl_surface->w;
367   h = sdl_surface->h;
368 }
369
370 SurfaceOpenGL::~SurfaceOpenGL()
371 {
372   glDeleteTextures(1, &gl_texture);
373 }
374
375 void
376 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
377 {
378   Uint32 saved_flags;
379   Uint8  saved_alpha;
380   int w, h;
381   SDL_Surface *conv;
382
383   w = power_of_two(surf->w);
384   h = power_of_two(surf->h),
385
386 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
387   conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
388                                 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
389 #else
390   conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
391                               0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
392 #endif
393
394   /* Save the alpha blending attributes */
395   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
396   saved_alpha = surf->format->alpha;
397   if ( (saved_flags & SDL_SRCALPHA)
398        == SDL_SRCALPHA )
399     {
400       SDL_SetAlpha(surf, 0, 0);
401     }
402
403   SDL_BlitSurface(surf, 0, conv, 0);
404
405   /* Restore the alpha blending attributes */
406   if ( (saved_flags & SDL_SRCALPHA)
407        == SDL_SRCALPHA )
408     {
409       SDL_SetAlpha(surf, saved_flags, saved_alpha);
410     }
411
412   glGenTextures(1, &*tex);
413   glBindTexture(GL_TEXTURE_2D , *tex);
414   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
415   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
416   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
417   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
418   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
419   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
420   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
421       
422   SDL_FreeSurface(conv);
423 }
424
425 int
426 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
427 {
428   float pw = power_of_two(w);
429   float ph = power_of_two(h);
430
431   glEnable(GL_TEXTURE_2D);
432   glEnable(GL_BLEND);
433   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
434
435   glColor4ub(alpha, alpha, alpha, alpha);
436
437   glBindTexture(GL_TEXTURE_2D, gl_texture);
438
439   glBegin(GL_QUADS);
440   glTexCoord2f(0, 0);
441   glVertex2f(x, y);
442   glTexCoord2f((float)w / pw, 0);
443   glVertex2f((float)w+x, y);
444   glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)w+x, (float)h+y);
445   glTexCoord2f(0, (float)h / ph);
446   glVertex2f(x, (float)h+y);
447   glEnd();
448   
449   glDisable(GL_TEXTURE_2D);
450   glDisable(GL_BLEND);
451   
452   (void) update; // avoid compiler warning
453   
454   return 0;
455 }
456
457 int
458 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
459 {
460   float pw = power_of_two(w);
461   float ph = power_of_two(h);
462
463   glColor3ub(alpha, alpha, alpha);
464
465   glEnable(GL_TEXTURE_2D);
466   glBindTexture(GL_TEXTURE_2D, gl_texture);
467
468   glBegin(GL_QUADS);
469   glTexCoord2f(0, 0);
470   glVertex2f(0, 0);
471   glTexCoord2f((float)w / pw, 0);
472   glVertex2f(screen->w, 0);
473   glTexCoord2f((float)w / pw, (float)h / ph);
474   glVertex2f(screen->w, screen->h);
475   glTexCoord2f(0, (float)h / ph);
476   glVertex2f(0, screen->h);
477   glEnd();
478   
479   glDisable(GL_TEXTURE_2D);
480
481   (void) update; // avoid compiler warning
482
483   return 0;
484 }
485
486 int
487 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
488 {
489   float pw = power_of_two(int(this->w));
490   float ph = power_of_two(int(this->h));
491
492   glBindTexture(GL_TEXTURE_2D, gl_texture);
493
494   glEnable(GL_BLEND);
495   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
496
497   glColor4ub(alpha, alpha, alpha, alpha);
498
499   glEnable(GL_TEXTURE_2D);
500
501
502   glBegin(GL_QUADS);
503   glTexCoord2f(sx / pw, sy / ph);
504   glVertex2f(x, y);
505   glTexCoord2f((float)(sx + w) / pw, sy / ph);
506   glVertex2f(w+x, y);
507   glTexCoord2f((sx+w) / pw, (sy+h) / ph);
508   glVertex2f(w +x, h+y);
509   glTexCoord2f(sx / pw, (float)(sy+h) / ph);
510   glVertex2f(x, h+y);
511   glEnd();
512
513   glDisable(GL_TEXTURE_2D);
514   glDisable(GL_BLEND);
515
516   /* Avoid compiler warnings */
517   if(update)
518     {}
519   return 0;
520 }
521 #endif
522
523 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
524 {
525   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
526   w = sdl_surface->w;
527   h = sdl_surface->h;
528 }
529
530 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
531 {
532   sdl_surface = sdl_surface_from_file(file, use_alpha);
533   w = sdl_surface->w;
534   h = sdl_surface->h;
535 }
536
537 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  int use_alpha)
538 {
539   sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
540   w = sdl_surface->w;
541   h = sdl_surface->h;
542 }
543
544 int
545 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
546 {
547   SDL_Rect dest;
548
549   dest.x = (int)x;
550   dest.y = (int)y;
551   dest.w = w;
552   dest.h = h;
553   
554   if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
555     return -1;
556   
557   SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
558   int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
559   
560   if (update == UPDATE)
561     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
562
563   return ret;
564 }
565
566 int
567 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
568 {
569   SDL_Rect dest;
570   
571   dest.x = 0;
572   dest.y = 0;
573   dest.w = screen->w;
574   dest.h = screen->h;
575
576   if(alpha != 255)
577     SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
578   
579   int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
580   
581   if (update == UPDATE)
582     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
583
584   return ret;
585 }
586
587 int
588 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
589 {
590   SDL_Rect src, dest;
591
592   src.x = (int)sx;
593   src.y = (int)sy;
594   src.w = (int)w;
595   src.h = (int)h;
596
597   dest.x = (int)x;
598   dest.y = (int)y;
599   dest.w = (int)w;
600   dest.h = (int)h;
601
602   if(alpha != 255)
603     SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
604   
605   int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
606
607   if (update == UPDATE)
608     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
609   
610   return ret;
611 }
612
613 SurfaceSDL::~SurfaceSDL()
614 {
615 }
616
617 /* EOF */