-Changed drawing model. Everything is handled through DrawingContext now and
[supertux.git] / src / screen / 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 <algorithm>
24 #include "SDL.h"
25 #include "SDL_image.h"
26 #include "texture.h"
27 #include "globals.h"
28 #include "setup.h"
29
30 Surface::Surfaces Surface::surfaces;
31
32 SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
33     : type(SURFACE), surface(0), use_alpha(use_alpha_)
34 {
35   // Copy the given surface and make sure that it is not stored in
36   // video memory
37   surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
38                                  temp->w, temp->h,
39                                  temp->format->BitsPerPixel,
40                                  temp->format->Rmask,
41                                  temp->format->Gmask,
42                                  temp->format->Bmask,
43                                  temp->format->Amask);
44   if(!surface)
45     st_abort("No memory left.", "");
46   SDL_SetAlpha(temp,0,0);
47   SDL_BlitSurface(temp, NULL, surface, NULL);
48 }
49
50 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
51     : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
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 SurfaceData::~SurfaceData()
60 {
61   SDL_FreeSurface(surface);
62 }
63
64 SurfaceImpl*
65 SurfaceData::create()
66 {
67 #ifndef NOOPENGL
68   if (use_gl)
69     return create_SurfaceOpenGL();
70   else
71     return create_SurfaceSDL();
72 #else
73   return create_SurfaceSDL();
74 #endif
75 }
76
77 SurfaceSDL*
78 SurfaceData::create_SurfaceSDL()
79 {
80   switch(type)
81   {
82   case LOAD:
83     return new SurfaceSDL(file, use_alpha);
84   case LOAD_PART:
85     return new SurfaceSDL(file, x, y, w, h, use_alpha);
86   case SURFACE:
87     return new SurfaceSDL(surface, use_alpha);
88   }
89   assert(0);
90 }
91
92 SurfaceOpenGL*
93 SurfaceData::create_SurfaceOpenGL()
94 {
95 #ifndef NOOPENGL
96   switch(type)
97   {
98   case LOAD:
99     return new SurfaceOpenGL(file, use_alpha);
100   case LOAD_PART:
101     return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
102   case SURFACE:
103     return new SurfaceOpenGL(surface, use_alpha);
104   }
105 #endif
106   assert(0);
107 }
108
109 #ifndef NOOPENGL
110 /* Quick utility function for texture creation */
111 static int power_of_two(int input)
112 {
113   int value = 1;
114
115   while ( value < input )
116   {
117     value <<= 1;
118   }
119   return value;
120 }
121 #endif
122
123 Surface::Surface(SDL_Surface* surf, int use_alpha)
124     : data(surf, use_alpha), w(0), h(0)
125 {
126   impl = data.create();
127   if (impl)
128   {
129     w = impl->w;
130     h = impl->h;
131   }
132   surfaces.push_back(this);
133 }
134
135 Surface::Surface(const std::string& file, int use_alpha)
136     : data(file, use_alpha), w(0), h(0)
137 {
138   impl = data.create();
139   if (impl)
140   {
141     w = impl->w;
142     h = impl->h;
143   }
144   surfaces.push_back(this);
145 }
146
147 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
148     : data(file, x, y, w, h, use_alpha), w(0), h(0)
149 {
150   impl = data.create();
151   if (impl)
152   {
153     w = impl->w;
154     h = impl->h;
155   }
156   surfaces.push_back(this);
157 }
158
159 void
160 Surface::reload()
161 {
162   delete impl;
163   impl = data.create();
164   if (impl)
165   {
166     w = impl->w;
167     h = impl->h;
168   }
169 }
170
171 Surface::~Surface()
172 {
173 #ifdef DEBUG
174   bool found = false;
175   for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
176       ++i)
177   {
178     if(*i == this)
179     {
180       found = true; break;
181     }
182   }
183   if(!found)
184     printf("Error: Surface freed twice!!!\n");
185 #endif
186   surfaces.remove(this);
187   delete impl;
188 }
189
190 void
191 Surface::reload_all()
192 {
193   for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
194   {
195     (*i)->reload();
196   }
197 }
198
199 void
200 Surface::debug_check()
201 {
202   for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
203   {
204     printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
205            (*i)->data.file.c_str());
206   }
207 }
208
209 void
210 Surface::resize(int w_, int h_)
211 {
212   if (impl)
213   {
214     w = w_;
215     h = h_;
216     if (impl->resize(w_,h_) == -2)
217       reload();
218   }
219 }
220
221 SDL_Surface*
222 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h,  int use_alpha)
223 {
224   SDL_Rect src;
225   SDL_Surface * sdl_surface;
226   SDL_Surface * temp;
227   SDL_Surface * conv;
228
229   temp = IMG_Load(file.c_str());
230
231   if (temp == NULL)
232     st_abort("Can't load", file);
233
234   /* Set source rectangle for conv: */
235
236   src.x = x;
237   src.y = y;
238   src.w = w;
239   src.h = h;
240
241   conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
242                               temp->format->Rmask,
243                               temp->format->Gmask,
244                               temp->format->Bmask,
245                               temp->format->Amask);
246
247   /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
248      0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
249      #else
250
251      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
252      #endif*/
253
254   SDL_SetAlpha(temp,0,0);
255
256   SDL_BlitSurface(temp, &src, conv, NULL);
257   if(use_alpha == IGNORE_ALPHA && !use_gl)
258     sdl_surface = SDL_DisplayFormat(conv);
259   else
260     sdl_surface = SDL_DisplayFormatAlpha(conv);
261
262   if (sdl_surface == NULL)
263     st_abort("Can't covert to display format", file);
264
265   if (use_alpha == IGNORE_ALPHA && !use_gl)
266     SDL_SetAlpha(sdl_surface, 0, 0);
267
268   SDL_FreeSurface(temp);
269   SDL_FreeSurface(conv);
270
271   return sdl_surface;
272 }
273
274 SDL_Surface*
275 sdl_surface_from_file(const std::string& file, int use_alpha)
276 {
277   SDL_Surface* sdl_surface;
278   SDL_Surface* temp;
279
280   temp = IMG_Load(file.c_str());
281
282   if (temp == NULL)
283     st_abort("Can't load", file);
284
285   if(use_alpha == IGNORE_ALPHA && !use_gl)
286     sdl_surface = SDL_DisplayFormat(temp);
287   else
288     sdl_surface = SDL_DisplayFormatAlpha(temp);
289
290   if (sdl_surface == NULL)
291     st_abort("Can't covert to display format", file);
292
293   if (use_alpha == IGNORE_ALPHA && !use_gl)
294     SDL_SetAlpha(sdl_surface, 0, 0);
295
296   SDL_FreeSurface(temp);
297
298   return sdl_surface;
299 }
300
301 SDL_Surface*
302 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
303 {
304   SDL_Surface* sdl_surface;
305   Uint32 saved_flags;
306   Uint8  saved_alpha;
307
308   /* Save the alpha blending attributes */
309   saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
310   saved_alpha = sdl_surf->format->alpha;
311   if ( (saved_flags & SDL_SRCALPHA)
312        == SDL_SRCALPHA )
313   {
314     SDL_SetAlpha(sdl_surf, 0, 0);
315   }
316
317   if(use_alpha == IGNORE_ALPHA && !use_gl)
318     sdl_surface = SDL_DisplayFormat(sdl_surf);
319   else
320     sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
321
322   /* Restore the alpha blending attributes */
323   if ( (saved_flags & SDL_SRCALPHA)
324        == SDL_SRCALPHA )
325   {
326     SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
327   }
328
329   if (sdl_surface == NULL)
330     st_abort("Can't covert to display format", "SURFACE");
331
332   if (use_alpha == IGNORE_ALPHA && !use_gl)
333     SDL_SetAlpha(sdl_surface, 0, 0);
334
335   return sdl_surface;
336 }
337
338 //---------------------------------------------------------------------------
339
340 SurfaceImpl::SurfaceImpl()
341 {}
342
343 SurfaceImpl::~SurfaceImpl()
344 {
345   SDL_FreeSurface(sdl_surface);
346 }
347
348 SDL_Surface* SurfaceImpl::get_sdl_surface() const
349 {
350   return sdl_surface;
351 }
352
353 int SurfaceImpl::resize(int w_, int h_)
354 {
355   w = w_;
356   h = h_;
357   SDL_Rect dest;
358   dest.x = 0;
359   dest.y = 0;
360   dest.w = w;
361   dest.h = h;
362   int ret = SDL_SoftStretch(sdl_surface, NULL,
363                             sdl_surface, &dest);
364   return ret;
365 }
366
367 #ifndef NOOPENGL
368 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
369 {
370   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
371   create_gl(sdl_surface,&gl_texture);
372
373   w = sdl_surface->w;
374   h = sdl_surface->h;
375 }
376
377 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
378 {
379   sdl_surface = sdl_surface_from_file(file, use_alpha);
380   create_gl(sdl_surface,&gl_texture);
381
382   w = sdl_surface->w;
383   h = sdl_surface->h;
384 }
385
386 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
387 {
388   sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
389   create_gl(sdl_surface, &gl_texture);
390
391   w = sdl_surface->w;
392   h = sdl_surface->h;
393 }
394
395 SurfaceOpenGL::~SurfaceOpenGL()
396 {
397   glDeleteTextures(1, &gl_texture);
398 }
399
400 void
401 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
402 {
403   Uint32 saved_flags;
404   Uint8  saved_alpha;
405   int w, h;
406   SDL_Surface *conv;
407
408   w = power_of_two(surf->w);
409   h = power_of_two(surf->h),
410
411 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
412       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
413                                   0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
414 #else
415       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
416                                   0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
417 #endif
418
419   /* Save the alpha blending attributes */
420   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
421   saved_alpha = surf->format->alpha;
422   if ( (saved_flags & SDL_SRCALPHA)
423        == SDL_SRCALPHA )
424   {
425     SDL_SetAlpha(surf, 0, 0);
426   }
427
428   SDL_BlitSurface(surf, 0, conv, 0);
429
430   /* Restore the alpha blending attributes */
431   if ( (saved_flags & SDL_SRCALPHA)
432        == SDL_SRCALPHA )
433   {
434     SDL_SetAlpha(surf, saved_flags, saved_alpha);
435   }
436
437   glGenTextures(1, &*tex);
438   glBindTexture(GL_TEXTURE_2D , *tex);
439   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
440   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
441   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
442   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
443   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
444   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
445   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
446
447   SDL_FreeSurface(conv);
448 }
449
450 int
451 SurfaceOpenGL::draw(float x, float y, Uint8 alpha)
452 {
453   float pw = power_of_two(w);
454   float ph = power_of_two(h);
455
456   glEnable(GL_TEXTURE_2D);
457   glEnable(GL_BLEND);
458   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
459
460   glColor4ub(alpha, alpha, alpha, alpha);
461
462   glBindTexture(GL_TEXTURE_2D, gl_texture);
463
464   glBegin(GL_QUADS);
465   glTexCoord2f(0, 0);
466   glVertex2f(x, y);
467   glTexCoord2f((float)w / pw, 0);
468   glVertex2f((float)w+x, y);
469   glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)w+x, (float)h+y);
470   glTexCoord2f(0, (float)h / ph);
471   glVertex2f(x, (float)h+y);
472   glEnd();
473
474   glDisable(GL_TEXTURE_2D);
475   glDisable(GL_BLEND);
476
477   return 0;
478 }
479
480 int
481 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha)
482 {
483   float pw = power_of_two(int(this->w));
484   float ph = power_of_two(int(this->h));
485
486   glBindTexture(GL_TEXTURE_2D, gl_texture);
487
488   glEnable(GL_BLEND);
489   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
490
491   glColor4ub(alpha, alpha, alpha, alpha);
492
493   glEnable(GL_TEXTURE_2D);
494
495
496   glBegin(GL_QUADS);
497   glTexCoord2f(sx / pw, sy / ph);
498   glVertex2f(x, y);
499   glTexCoord2f((float)(sx + w) / pw, sy / ph);
500   glVertex2f(w+x, y);
501   glTexCoord2f((sx+w) / pw, (sy+h) / ph);
502   glVertex2f(w +x, h+y);
503   glTexCoord2f(sx / pw, (float)(sy+h) / ph);
504   glVertex2f(x, h+y);
505   glEnd();
506
507   glDisable(GL_TEXTURE_2D);
508   glDisable(GL_BLEND);
509
510   return 0;
511 }
512
513 #if 0
514 int
515 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
516 {
517   float pw = power_of_two(int(this->w));
518   float ph = power_of_two(int(this->h));
519
520   glBindTexture(GL_TEXTURE_2D, gl_texture);
521
522   glEnable(GL_BLEND);
523   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
524
525   glColor4ub(alpha, alpha, alpha, alpha);
526
527   glEnable(GL_TEXTURE_2D);
528
529
530   glBegin(GL_QUADS);
531   glTexCoord2f(0, 0);
532   glVertex2f(x, y);
533   glTexCoord2f((float)w / pw, 0);
534   glVertex2f(sw+x, y);
535   glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)sw+x, (float)sh+y);
536   glVertex2f(sw +x, sh+y);
537   glTexCoord2f(0, (float)h / ph);
538   glVertex2f(x, sh+y);
539   glEnd();
540
541   glDisable(GL_TEXTURE_2D);
542   glDisable(GL_BLEND);
543
544   return 0;
545 }
546 #endif
547
548 #endif
549
550 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
551 {
552   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
553   w = sdl_surface->w;
554   h = sdl_surface->h;
555 }
556
557 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
558 {
559   sdl_surface = sdl_surface_from_file(file, use_alpha);
560   w = sdl_surface->w;
561   h = sdl_surface->h;
562 }
563
564 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  int use_alpha)
565 {
566   sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
567   w = sdl_surface->w;
568   h = sdl_surface->h;
569 }
570
571 int
572 SurfaceSDL::draw(float x, float y, Uint8 alpha)
573 {
574   SDL_Rect dest;
575
576   dest.x = (int)x;
577   dest.y = (int)y;
578   dest.w = w;
579   dest.h = h;
580
581   if(alpha != 255)
582     {
583     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
584       to temp sur, blit the temp into the screen */
585     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
586       already have an alpha mask yet... */
587
588     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
589                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
590                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
591                                     sdl_surface->format->Bmask,
592                                     0);
593     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
594     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
595     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
596
597
598     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
599     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
600
601     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
602
603     SDL_FreeSurface (sdl_surface_copy);
604     return ret;
605     }
606
607   int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
608
609   return ret;
610 }
611
612 int
613 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha)
614 {
615   SDL_Rect src, dest;
616
617   src.x = (int)sx;
618   src.y = (int)sy;
619   src.w = (int)w;
620   src.h = (int)h;
621
622   dest.x = (int)x;
623   dest.y = (int)y;
624   dest.w = (int)w;
625   dest.h = (int)h;
626
627   if(alpha != 255)
628     {
629     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
630       to temp sur, blit the temp into the screen */
631     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
632       already have an alpha mask yet... */
633
634     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
635                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
636                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
637                                     sdl_surface->format->Bmask,
638                                     0);
639     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
640     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
641     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
642
643
644     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
645     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
646
647     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
648
649     SDL_FreeSurface (sdl_surface_copy);
650     return ret;
651     }
652
653   int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
654
655   return ret;
656 }
657
658 #if 0
659 int
660 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
661 {
662   SDL_Rect dest;
663
664   dest.x = (int)x;
665   dest.y = (int)y;
666   dest.w = (int)sw;
667   dest.h = (int)sh;
668
669   if(alpha != 255)
670     SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
671
672
673   SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
674                                   sw, sh, sdl_surface->format->BitsPerPixel,
675                                   sdl_surface->format->Rmask, sdl_surface->format->Gmask,
676                                   sdl_surface->format->Bmask,
677                                   0);
678
679   SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
680   SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
681
682   int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
683   SDL_FreeSurface(sdl_surface_copy);
684
685   if (update == UPDATE)
686     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
687
688   return ret;
689 }
690 #endif
691
692 SurfaceSDL::~SurfaceSDL()
693 {}
694
695 /* EOF */