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