1adff4f948aa5a08aabf2fa201187b2d5ad7457f
[supertux.git] / lib / video / surface.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 <cassert>
22 #include <iostream>
23 #include <algorithm>
24
25 #include "SDL.h"
26 #include "SDL_image.h"
27
28 #include "../video/surface.h"
29 #include "../app/globals.h"
30 #include "../app/setup.h"
31
32 using namespace SuperTux;
33
34 Surface::Surfaces Surface::surfaces;
35
36 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
37     : type(SURFACE), surface(0), use_alpha(use_alpha_)
38 {
39   // Copy the given surface and make sure that it is not stored in
40   // video memory
41   surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
42                                  temp->w, temp->h,
43                                  temp->format->BitsPerPixel,
44                                  temp->format->Rmask,
45                                  temp->format->Gmask,
46                                  temp->format->Bmask,
47                                  temp->format->Amask);
48   if(!surface)
49     Termination::abort("No memory left.", "");
50   SDL_SetAlpha(temp,0,0);
51   SDL_BlitSurface(temp, NULL, surface, NULL);
52 }
53
54 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
55     : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
56 {}
57
58 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
59     : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
60     x(x_), y(y_), w(w_), h(h_)
61 {}
62
63 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_)
64     : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
65 {
66 top_gradient = top_gradient_;
67 bottom_gradient = bottom_gradient_;
68 }
69
70
71 SurfaceData::~SurfaceData()
72 {
73   SDL_FreeSurface(surface);
74 }
75
76 SurfaceImpl*
77 SurfaceData::create()
78 {
79 #ifndef NOOPENGL
80   if (use_gl)
81     return create_SurfaceOpenGL();
82   else
83     return create_SurfaceSDL();
84 #else
85   return create_SurfaceSDL();
86 #endif
87 }
88
89 SurfaceSDL*
90 SurfaceData::create_SurfaceSDL()
91 {
92   switch(type)
93   {
94   case LOAD:
95     return new SurfaceSDL(file, use_alpha);
96   case LOAD_PART:
97     return new SurfaceSDL(file, x, y, w, h, use_alpha);
98   case SURFACE:
99     return new SurfaceSDL(surface, use_alpha);
100   case GRADIENT:
101     return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
102   }
103   assert(0);
104 }
105
106 SurfaceOpenGL*
107 SurfaceData::create_SurfaceOpenGL()
108 {
109 #ifndef NOOPENGL
110   switch(type)
111   {
112   case LOAD:
113     return new SurfaceOpenGL(file, use_alpha);
114   case LOAD_PART:
115     return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
116   case SURFACE:
117     return new SurfaceOpenGL(surface, use_alpha);
118   case GRADIENT:
119     return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
120   }
121 #endif
122   assert(0);
123 }
124
125 #ifndef NOOPENGL
126 /* Quick utility function for texture creation */
127 static int power_of_two(int input)
128 {
129   int value = 1;
130
131   while ( value < input )
132   {
133     value <<= 1;
134   }
135   return value;
136 }
137 #endif
138
139 Surface::Surface(SDL_Surface* surf, bool use_alpha)
140     : data(surf, use_alpha), w(0), h(0)
141 {
142   impl = data.create();
143   if (impl)
144   {
145     w = impl->w;
146     h = impl->h;
147   }
148   surfaces.push_back(this);
149 }
150
151 Surface::Surface(const std::string& file, bool use_alpha)
152     : data(file, use_alpha), w(0), h(0)
153 {
154   impl = data.create();
155   if (impl)
156   {
157     w = impl->w;
158     h = impl->h;
159   }
160   surfaces.push_back(this);
161 }
162
163 Surface::Surface(const std::string& file, int x, int y, int w, int h, bool use_alpha)
164     : data(file, x, y, w, h, use_alpha), w(0), h(0)
165 {
166   impl = data.create();
167   if (impl)
168   {
169     w = impl->w;
170     h = impl->h;
171   }
172   surfaces.push_back(this);
173 }
174
175 Surface::Surface(Color top_background, Color bottom_background, int w_, int h_)
176     : data(top_background, bottom_background, w_, h_), w(0), h(0)
177 {
178   impl = data.create();
179   if (impl)
180   {
181     w = impl->w;
182     h = impl->h;
183   }
184   surfaces.push_back(this);
185 }
186
187 void
188 Surface::reload()
189 {
190   delete impl;
191   impl = data.create();
192   if (impl)
193   {
194     w = impl->w;
195     h = impl->h;
196   }
197 }
198
199 Surface::~Surface()
200 {
201 #ifdef DEBUG
202   bool found = false;
203   for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
204       ++i)
205   {
206     if(*i == this)
207     {
208       found = true; break;
209     }
210   }
211   if(!found)
212     printf("Error: Surface freed twice!!!\n");
213 #endif
214   surfaces.remove(this);
215   delete impl;
216 }
217
218 void
219 Surface::reload_all()
220 {
221   for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
222   {
223     (*i)->reload();
224   }
225 }
226
227 void
228 Surface::debug_check()
229 {
230   for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
231   {
232     printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
233            (*i)->data.file.c_str());
234   }
235 }
236
237 void
238 Surface::resize(int w_, int h_)
239 {
240   if (impl)
241   {
242     w = w_;
243     h = h_;
244     if (impl->resize(w_,h_) == -2)
245       reload();
246   }
247 }
248
249 SDL_Surface*
250 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h,  bool use_alpha)
251 {
252   SDL_Rect src;
253   SDL_Surface * sdl_surface;
254   SDL_Surface * temp;
255   SDL_Surface * conv;
256
257   temp = IMG_Load(file.c_str());
258
259   if (temp == NULL)
260     Termination::abort("Can't load", file);
261
262   /* Set source rectangle for conv: */
263
264   src.x = x;
265   src.y = y;
266   src.w = w;
267   src.h = h;
268
269   conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
270                               temp->format->Rmask,
271                               temp->format->Gmask,
272                               temp->format->Bmask,
273                               temp->format->Amask);
274
275   /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
276      0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
277      #else
278
279      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
280      #endif*/
281
282   SDL_SetAlpha(temp,0,0);
283
284   SDL_BlitSurface(temp, &src, conv, NULL);
285   if(use_alpha == false && !use_gl)
286     sdl_surface = SDL_DisplayFormat(conv);
287   else
288     sdl_surface = SDL_DisplayFormatAlpha(conv);
289
290   if (sdl_surface == NULL)
291     Termination::abort("Can't covert to display format", file);
292
293   if (use_alpha == false && !use_gl)
294     SDL_SetAlpha(sdl_surface, 0, 0);
295
296   SDL_FreeSurface(temp);
297   SDL_FreeSurface(conv);
298
299   return sdl_surface;
300 }
301
302 SDL_Surface*
303 sdl_surface_from_file(const std::string& file, bool use_alpha)
304 {
305   SDL_Surface* sdl_surface;
306   SDL_Surface* temp;
307
308   temp = IMG_Load(file.c_str());
309
310   if (temp == NULL)
311     Termination::abort("Can't load", file);
312
313   if(use_alpha == false && !use_gl)
314     sdl_surface = SDL_DisplayFormat(temp);
315   else
316     sdl_surface = SDL_DisplayFormatAlpha(temp);
317
318   if (sdl_surface == NULL)
319     Termination::abort("Can't covert to display format", file);
320
321   if (use_alpha == false && !use_gl)
322     SDL_SetAlpha(sdl_surface, 0, 0);
323
324   SDL_FreeSurface(temp);
325
326   return sdl_surface;
327 }
328
329 SDL_Surface*
330 SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
331 {
332   SDL_Surface* sdl_surface;
333   Uint32 saved_flags;
334   Uint8  saved_alpha;
335
336   /* Save the alpha blending attributes */
337   saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
338   saved_alpha = sdl_surf->format->alpha;
339   if ( (saved_flags & SDL_SRCALPHA)
340        == SDL_SRCALPHA )
341   {
342     SDL_SetAlpha(sdl_surf, 0, 0);
343   }
344
345   if(use_alpha == false && !use_gl)
346     sdl_surface = SDL_DisplayFormat(sdl_surf);
347   else
348     sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
349
350   /* Restore the alpha blending attributes */
351   if ( (saved_flags & SDL_SRCALPHA)
352        == SDL_SRCALPHA )
353   {
354     SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
355   }
356
357   if (sdl_surface == NULL)
358     Termination::abort("Can't covert to display format", "SURFACE");
359
360   if (use_alpha == false && !use_gl)
361     SDL_SetAlpha(sdl_surface, 0, 0);
362
363   return sdl_surface;
364 }
365
366 SDL_Surface*
367 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
368 {
369   SDL_Surface* sdl_surface;
370
371   sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
372                     screen->format->BitsPerPixel, screen->format->Rmask,
373                     screen->format->Gmask, screen->format->Bmask, 0);
374
375   if(sdl_surface == NULL)
376       Termination::abort("Cannot create surface for the gradient", "SURFACE");
377
378   if(top == bottom)
379     {
380     SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
381         top.red, top.green, top.blue));
382     }
383   else
384     {
385     float redstep = (float(bottom.red)-float(top.red)) / float(h);
386     float greenstep = (float(bottom.green)-float(top.green)) / float(h);
387     float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
388
389     SDL_Rect rect;
390     rect.x = 0;
391     rect.w = w;
392     rect.h = 1;
393     for(float y = 0; y < h; y++)
394       {
395       rect.y = (int)y;
396       SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
397           int(float(top.red) + redstep * y),
398           int(float(top.green) + greenstep * y),
399           int(float(top.blue) + bluestep * y)));
400       }
401     }
402
403   return sdl_surface;
404 }
405
406 //---------------------------------------------------------------------------
407
408 SurfaceImpl::SurfaceImpl()
409 {}
410
411 SurfaceImpl::~SurfaceImpl()
412 {
413   SDL_FreeSurface(sdl_surface);
414 }
415
416 SDL_Surface* SurfaceImpl::get_sdl_surface() const
417 {
418   return sdl_surface;
419 }
420
421 int SurfaceImpl::resize(int w_, int h_)
422 {
423   w = w_;
424   h = h_;
425   SDL_Rect dest;
426   dest.x = 0;
427   dest.y = 0;
428   dest.w = w;
429   dest.h = h;
430   int ret = SDL_SoftStretch(sdl_surface, NULL,
431                             sdl_surface, &dest);
432   return ret;
433 }
434
435 #ifndef NOOPENGL
436 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
437 {
438   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
439   create_gl(sdl_surface,&gl_texture);
440
441   w = sdl_surface->w;
442   h = sdl_surface->h;
443 }
444
445 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
446 {
447   sdl_surface = sdl_surface_from_file(file, use_alpha);
448   create_gl(sdl_surface,&gl_texture);
449
450   w = sdl_surface->w;
451   h = sdl_surface->h;
452 }
453
454 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
455 {
456   sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
457   
458   create_gl(sdl_surface, &gl_texture);
459
460   w = sdl_surface->w;
461   h = sdl_surface->h;
462 }
463
464 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
465 {
466   sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
467   create_gl(sdl_surface, &gl_texture);
468
469   w = sdl_surface->w;
470   h = sdl_surface->h;
471 }
472
473 SurfaceOpenGL::~SurfaceOpenGL()
474 {
475   glDeleteTextures(1, &gl_texture);
476 }
477
478 void
479 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
480 {
481   Uint32 saved_flags;
482   Uint8  saved_alpha;
483   int w, h;
484   SDL_Surface *conv;
485
486   w = power_of_two(surf->w);
487   h = power_of_two(surf->h),
488
489 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
490       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
491                                   0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
492 #else
493       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
494                                   0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
495 #endif
496
497   /* Save the alpha blending attributes */
498   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
499   saved_alpha = surf->format->alpha;
500   if ( (saved_flags & SDL_SRCALPHA)
501        == SDL_SRCALPHA )
502   {
503     SDL_SetAlpha(surf, 0, 0);
504   }
505
506   SDL_BlitSurface(surf, 0, conv, 0);
507
508   /* Restore the alpha blending attributes */
509   if ( (saved_flags & SDL_SRCALPHA)
510        == SDL_SRCALPHA )
511   {
512     SDL_SetAlpha(surf, saved_flags, saved_alpha);
513   }
514
515   // We check all the pixels of the surface to figure out which
516   // internal format OpenGL should use for storing it, ie. if no alpha
517   // is present store in RGB instead of RGBA, this saves a few bytes
518   // of memory, but much more importantly it makes the game look
519   // *much* better in 16bit color mode
520   int internal_format = GL_RGB10_A2;
521   bool has_alpha = false;
522
523   unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
524   for (int y = 0; y < surf->h; ++y)
525     for (int x = 0; x < surf->w; ++x)
526       {
527         if (buf[(conv->pitch*y + x*4) + 3] != 255)
528           {
529             has_alpha = true;
530             break;
531           }
532       }
533
534   if (!has_alpha)
535     {
536       internal_format = GL_RGB;
537     }
538
539   glGenTextures(1, &*tex);
540   glBindTexture(GL_TEXTURE_2D , *tex);
541   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
542   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
543   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
544   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
545   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
546   glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
547   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
548
549   SDL_FreeSurface(conv);
550 }
551
552 int
553 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
554 {
555   float pw = power_of_two(w);
556   float ph = power_of_two(h);
557
558   if(effect & SEMI_TRANSPARENT)
559     alpha = 128;
560
561   glEnable(GL_TEXTURE_2D);
562   glEnable(GL_BLEND);
563   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
564
565   glColor4ub(alpha, alpha, alpha, alpha);
566
567   glBindTexture(GL_TEXTURE_2D, gl_texture);
568
569   glBegin(GL_QUADS);
570
571   if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
572     {
573     glTexCoord2f(0, 0);
574     glVertex2f((float)w+x, (float)h+y);
575
576     glTexCoord2f((float)w / pw, 0);
577     glVertex2f(x, (float)h+y);
578
579     glTexCoord2f((float)w / pw, (float)h / ph);
580     glVertex2f(x, y);
581
582     glTexCoord2f(0, (float)h / ph);
583     glVertex2f((float)w+x, y);
584     }
585   else if(effect & VERTICAL_FLIP)
586     {
587     glTexCoord2f(0, 0);
588     glVertex2f(x, (float)h+y);
589
590     glTexCoord2f((float)w / pw, 0);
591     glVertex2f((float)w+x, (float)h+y);
592
593     glTexCoord2f((float)w / pw, (float)h / ph);
594     glVertex2f((float)w+x, y);
595     
596     glTexCoord2f(0, (float)h / ph);
597     glVertex2f(x, y);
598     }
599   else if(effect & HORIZONTAL_FLIP)
600     {
601     glTexCoord2f(0, 0);
602     glVertex2f((float)w+x, y);
603
604     glTexCoord2f((float)w / pw, 0);
605     glVertex2f(x, y);
606
607     glTexCoord2f((float)w / pw, (float)h / ph);
608     glVertex2f(x, (float)h+y);
609
610     glTexCoord2f(0, (float)h / ph);
611     glVertex2f((float)w+x, (float)h+y);
612     }
613   else
614     {
615     glTexCoord2f(0, 0);
616     glVertex2f(x, y);
617
618     glTexCoord2f((float)w / pw, 0);
619     glVertex2f((float)w+x, y);
620
621     glTexCoord2f((float)w / pw, (float)h / ph);
622     glVertex2f((float)w+x, (float)h+y);
623
624     glTexCoord2f(0, (float)h / ph);
625     glVertex2f(x, (float)h+y);
626     }
627   glEnd();
628
629   glDisable(GL_TEXTURE_2D);
630   glDisable(GL_BLEND);
631
632   return 0;
633 }
634
635 int
636 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
637 {
638   float pw = power_of_two(int(this->w));
639   float ph = power_of_two(int(this->h));
640
641   if(effect & SEMI_TRANSPARENT)
642     alpha = 128;
643
644   glBindTexture(GL_TEXTURE_2D, gl_texture);
645
646   glEnable(GL_BLEND);
647   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
648
649   glColor4ub(alpha, alpha, alpha, alpha);
650
651   glEnable(GL_TEXTURE_2D);
652
653
654   glBegin(GL_QUADS);
655
656   if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
657     {
658     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
659     glVertex2f((float)w+x, (float)h+y);
660
661     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
662     glVertex2f(x, (float)h+y);
663
664     glTexCoord2f((float)(sx + w) / pw, sy / ph);
665     glVertex2f(x, y);
666
667     glTexCoord2f(sx / pw, sy / ph);
668     glVertex2f((float)w+x, y);
669     }
670   else if(effect & VERTICAL_FLIP)
671     {
672     glTexCoord2f(sx / pw, sy / ph);
673     glVertex2f(x, y);
674
675     glTexCoord2f((float)(sx + w) / pw, sy / ph);
676     glVertex2f(w+x, y);
677
678     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
679     glVertex2f(w +x, h+y);
680
681     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
682     glVertex2f(x, h+y);
683     }
684   else if(effect & HORIZONTAL_FLIP)
685     {
686     glTexCoord2f(sx / pw, sy / ph);
687     glVertex2f((float)w+x, y);
688
689     glTexCoord2f((float)(sx + w) / pw, sy / ph);
690     glVertex2f(x, y);
691
692     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
693     glVertex2f(x, (float)h+y);
694
695     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
696     glVertex2f((float)w+x, (float)h+y);
697     }
698   else
699     {
700     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
701     glVertex2f(x, h+y);
702
703     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
704     glVertex2f(w +x, h+y);
705
706     glTexCoord2f((float)(sx + w) / pw, sy / ph);
707     glVertex2f(w+x, y);
708
709     glTexCoord2f(sx / pw, sy / ph);
710     glVertex2f(x, y);
711     }
712
713   glEnd();
714
715   glDisable(GL_TEXTURE_2D);
716   glDisable(GL_BLEND);
717
718   return 0;
719 }
720
721 int
722 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
723 {
724   if(effect & SEMI_TRANSPARENT)
725     alpha = 128;
726
727   float pw = power_of_two(sw);
728   float ph = power_of_two(sh);
729
730   if(effect & SEMI_TRANSPARENT)
731     alpha = 128;
732
733   glEnable(GL_TEXTURE_2D);
734   glEnable(GL_BLEND);
735   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
736
737   glColor4ub(alpha, alpha, alpha, alpha);
738
739   glBindTexture(GL_TEXTURE_2D, gl_texture);
740
741   glBegin(GL_QUADS);
742
743   if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
744     {
745     glTexCoord2f(0, 0);
746     glVertex2f((float)sw+x, (float)sh+y);
747
748     glTexCoord2f((float)w / pw, 0);
749     glVertex2f(x, (float)sh+y);
750
751     glTexCoord2f((float)w / pw, (float)h / ph);
752     glVertex2f(x, y);
753
754     glTexCoord2f(0, (float)h / ph);
755     glVertex2f((float)sw+x, y);
756     }
757   else if(effect & VERTICAL_FLIP)
758     {
759     glTexCoord2f(0, 0);
760     glVertex2f(x, (float)sh+y);
761
762     glTexCoord2f((float)w / pw, 0);
763     glVertex2f((float)sw+x, (float)sh+y);
764
765     glTexCoord2f((float)w / pw, (float)h / ph);
766     glVertex2f((float)sw+x, y);
767     
768     glTexCoord2f(0, (float)h / ph);
769     glVertex2f(x, y);
770     }
771   else if(effect & HORIZONTAL_FLIP)
772     {
773     glTexCoord2f(0, 0);
774     glVertex2f((float)sw+x, y);
775
776     glTexCoord2f((float)w / pw, 0);
777     glVertex2f(x, y);
778
779     glTexCoord2f((float)w / pw, (float)h / ph);
780     glVertex2f(x, (float)sh+y);
781
782     glTexCoord2f(0, (float)h / ph);
783     glVertex2f((float)sw+x, (float)sh+y);
784     }
785   else
786     {
787     glTexCoord2f(0, 0);
788     glVertex2f(x, y);
789
790     glTexCoord2f((float)w / pw, 0);
791     glVertex2f((float)sw+x, y);
792
793     glTexCoord2f((float)w / pw, (float)h / ph);
794     glVertex2f((float)sw+x, (float)sh+y);
795
796     glTexCoord2f(0, (float)h / ph);
797     glVertex2f(x, (float)sh+y);
798     }
799   glEnd();
800
801   glDisable(GL_TEXTURE_2D);
802   glDisable(GL_BLEND);
803
804   return 0;
805 }
806
807 #endif
808
809 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
810 {
811   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
812   w = sdl_surface->w;
813   h = sdl_surface->h;
814 }
815
816 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
817 {
818   sdl_surface = sdl_surface_from_file(file, use_alpha);
819   w = sdl_surface->w;
820   h = sdl_surface->h;
821 }
822
823 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  bool use_alpha)
824 {
825   sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
826   w = sdl_surface->w;
827   h = sdl_surface->h;
828 }
829
830 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
831 {
832   sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
833   w = sdl_surface->w;
834   h = sdl_surface->h;
835 }
836
837 int
838 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
839 {
840   SDL_Rect dest;
841
842   dest.x = (int)x;
843   dest.y = (int)y;
844   dest.w = w;
845   dest.h = h;
846
847   if(effect & SEMI_TRANSPARENT)
848     alpha = 128;
849
850   if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
851     {
852     // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
853     for(float sx = 0; sx < w; sx++)
854       for(float sy = 0; sy < h; sy++)
855         if(draw_part(sx, sy, x+(w-sx), y+(h-sy), 1, 1, alpha, NONE_EFFECT) == -2)
856           return -2;
857     return 0;
858     }
859   else if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
860     {
861     for(float sy = 0; sy < h; sy++)
862       if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
863         return -2;
864     return 0;
865     }
866   else if(effect & HORIZONTAL_FLIP)    // FIXME: feel free to replace this hack
867     {
868     for(float sx = 0; sx < w; sx++)
869       if(draw_part(sx, 0, x+(w-sx), y, 1, h, alpha, NONE_EFFECT) == -2)
870         return -2;
871     return 0;
872     }
873
874   if(alpha != 255)
875     {
876     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
877       to temp sur, blit the temp into the screen */
878     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
879       already have an alpha mask yet... */
880
881     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
882                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
883                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
884                                     sdl_surface->format->Bmask,
885                                     0);
886     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
887     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
888     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
889
890
891     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
892     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
893
894     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
895
896     SDL_FreeSurface (sdl_surface_copy);
897     return ret;
898     }
899
900   int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
901
902   return ret;
903 }
904
905 int
906 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
907 {
908   SDL_Rect src, dest;
909
910   src.x = (int)sx;
911   src.y = (int)sy;
912   src.w = (int)w;
913   src.h = (int)h;
914
915   dest.x = (int)x;
916   dest.y = (int)y;
917   dest.w = (int)w;
918   dest.h = (int)h;
919
920   if(effect & SEMI_TRANSPARENT)
921     alpha = 128;
922
923   if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
924     {
925     // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
926     for(float sx_ = 0; sx_ < w; sx++)
927       for(float sy_ = 0; sy_ < h; sy++)
928         if(draw_part(sx_, sy_, sx+(w-sx_), sy+(h-sy_), 1, 1, alpha, NONE_EFFECT) == -2)
929           return -2;
930     return 0;
931     }
932   else if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
933     {
934     for(float sy_ = sy; sy_ < h; sy_++)
935       if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
936         return -2;
937     return 0;
938     }
939   else if(effect & HORIZONTAL_FLIP)    // FIXME: feel free to replace this hack
940     {
941     for(float sx_ = 0; sx_ < w; sx_++)
942       if(draw_part(sx_, 0, sx+(w-sx_), sy, 1, h, alpha, NONE_EFFECT) == -2)
943         return -2;
944     return 0;
945     }
946
947   if(alpha != 255)
948     {
949     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
950       to temp sur, blit the temp into the screen */
951     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
952       already have an alpha mask yet... */
953
954     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
955                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
956                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
957                                     sdl_surface->format->Bmask,
958                                     0);
959     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
960     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
961     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
962
963
964     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
965     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
966
967     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
968
969     SDL_FreeSurface (sdl_surface_copy);
970     return ret;
971     }
972
973   int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
974
975   return ret;
976 }
977
978 int
979 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
980 {
981   SDL_Rect dest;
982
983   dest.x = (int)x;
984   dest.y = (int)y;
985   dest.w = (int)sw;
986   dest.h = (int)sh;
987
988   if(effect & SEMI_TRANSPARENT)
989     alpha = 128;
990
991   SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
992                                   sw, sh, sdl_surface->format->BitsPerPixel,
993                                   sdl_surface->format->Rmask, sdl_surface->format->Gmask,
994                                   sdl_surface->format->Bmask,
995                                   0);
996
997   SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
998   SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
999
1000   if(alpha != 255)
1001     SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
1002
1003   int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
1004   SDL_FreeSurface(sdl_surface_copy);
1005
1006   return ret;
1007 }
1008
1009 SurfaceSDL::~SurfaceSDL()
1010 {}
1011
1012 /* EOF */