b609f7a9b19c1c710924d5a4234d13c840158a01
[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)
572     {
573     glTexCoord2f(0, 0);
574     glVertex2f(x, (float)h+y);
575
576     glTexCoord2f((float)w / pw, 0);
577     glVertex2f((float)w+x, (float)h+y);
578
579     glTexCoord2f((float)w / pw, (float)h / ph);
580     glVertex2f((float)w+x, y);
581     
582     glTexCoord2f(0, (float)h / ph);
583     glVertex2f(x, y);
584     }
585   else
586     {
587     glTexCoord2f(0, 0);
588     glVertex2f(x, y);
589
590     glTexCoord2f((float)w / pw, 0);
591     glVertex2f((float)w+x, y);
592
593     glTexCoord2f((float)w / pw, (float)h / ph);
594     glVertex2f((float)w+x, (float)h+y);
595
596     glTexCoord2f(0, (float)h / ph);
597     glVertex2f(x, (float)h+y);
598     }
599   glEnd();
600
601   glDisable(GL_TEXTURE_2D);
602   glDisable(GL_BLEND);
603
604   return 0;
605 }
606
607 int
608 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
609 {
610   float pw = power_of_two(int(this->w));
611   float ph = power_of_two(int(this->h));
612
613   if(effect & SEMI_TRANSPARENT)
614     alpha = 128;
615
616   glBindTexture(GL_TEXTURE_2D, gl_texture);
617
618   glEnable(GL_BLEND);
619   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
620
621   glColor4ub(alpha, alpha, alpha, alpha);
622
623   glEnable(GL_TEXTURE_2D);
624
625
626   glBegin(GL_QUADS);
627
628   if(effect & VERTICAL_FLIP)
629     {
630     glTexCoord2f(sx / pw, sy / ph);
631     glVertex2f(x, y);
632
633     glTexCoord2f((float)(sx + w) / pw, sy / ph);
634     glVertex2f(w+x, y);
635
636     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
637     glVertex2f(w +x, h+y);
638
639     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
640     glVertex2f(x, h+y);
641     }
642   else
643     {
644     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
645     glVertex2f(x, h+y);
646
647     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
648     glVertex2f(w +x, h+y);
649
650     glTexCoord2f((float)(sx + w) / pw, sy / ph);
651     glVertex2f(w+x, y);
652
653     glTexCoord2f(sx / pw, sy / ph);
654     glVertex2f(x, y);
655     }
656
657   glEnd();
658
659   glDisable(GL_TEXTURE_2D);
660   glDisable(GL_BLEND);
661
662   return 0;
663 }
664
665 int
666 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
667 {
668   if(effect & SEMI_TRANSPARENT)
669     alpha = 128;
670
671   float pw = power_of_two(sw);
672   float ph = power_of_two(sh);
673
674   if(effect & SEMI_TRANSPARENT)
675     alpha = 128;
676
677   glEnable(GL_TEXTURE_2D);
678   glEnable(GL_BLEND);
679   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
680
681   glColor4ub(alpha, alpha, alpha, alpha);
682
683   glBindTexture(GL_TEXTURE_2D, gl_texture);
684
685   glBegin(GL_QUADS);
686
687   if(effect & VERTICAL_FLIP)
688     {
689     glTexCoord2f(0, 0);
690     glVertex2f(x, (float)sh+y);
691
692     glTexCoord2f((float)w / pw, 0);
693     glVertex2f((float)sw+x, (float)sh+y);
694
695     glTexCoord2f((float)w / pw, (float)h / ph);
696     glVertex2f((float)sw+x, y);
697     
698     glTexCoord2f(0, (float)h / ph);
699     glVertex2f(x, y);
700     }
701   else
702     {
703     glTexCoord2f(0, 0);
704     glVertex2f(x, y);
705
706     glTexCoord2f((float)w / pw, 0);
707     glVertex2f((float)sw+x, y);
708
709     glTexCoord2f((float)w / pw, (float)h / ph);
710     glVertex2f((float)sw+x, (float)sh+y);
711
712     glTexCoord2f(0, (float)h / ph);
713     glVertex2f(x, (float)sh+y);
714     }
715   glEnd();
716
717   glDisable(GL_TEXTURE_2D);
718   glDisable(GL_BLEND);
719
720   return 0;
721 }
722
723 #endif
724
725 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
726 {
727   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
728   w = sdl_surface->w;
729   h = sdl_surface->h;
730 }
731
732 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
733 {
734   sdl_surface = sdl_surface_from_file(file, use_alpha);
735   w = sdl_surface->w;
736   h = sdl_surface->h;
737 }
738
739 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  bool use_alpha)
740 {
741   sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
742   w = sdl_surface->w;
743   h = sdl_surface->h;
744 }
745
746 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
747 {
748   sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
749   w = sdl_surface->w;
750   h = sdl_surface->h;
751 }
752
753 int
754 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
755 {
756   SDL_Rect dest;
757
758   dest.x = (int)x;
759   dest.y = (int)y;
760   dest.w = w;
761   dest.h = h;
762
763   if(effect & SEMI_TRANSPARENT)
764     alpha = 128;
765
766   if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
767     {
768     for(float sy = 0; sy < h; sy++)
769       if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
770         return -2;
771     return 0;
772     }
773
774   if(alpha != 255)
775     {
776     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
777       to temp sur, blit the temp into the screen */
778     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
779       already have an alpha mask yet... */
780
781     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
782                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
783                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
784                                     sdl_surface->format->Bmask,
785                                     0);
786     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
787     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
788     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
789
790
791     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
792     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
793
794     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
795
796     SDL_FreeSurface (sdl_surface_copy);
797     return ret;
798     }
799
800   int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
801
802   return ret;
803 }
804
805 int
806 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
807 {
808   SDL_Rect src, dest;
809
810   src.x = (int)sx;
811   src.y = (int)sy;
812   src.w = (int)w;
813   src.h = (int)h;
814
815   dest.x = (int)x;
816   dest.y = (int)y;
817   dest.w = (int)w;
818   dest.h = (int)h;
819
820   if(effect & SEMI_TRANSPARENT)
821     alpha = 128;
822
823   if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
824     {
825     for(float sy_ = sy; sy_ < h; sy_++)
826       if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
827         return -2;
828     return 0;
829     }
830
831   if(alpha != 255)
832     {
833     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
834       to temp sur, blit the temp into the screen */
835     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
836       already have an alpha mask yet... */
837
838     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
839                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
840                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
841                                     sdl_surface->format->Bmask,
842                                     0);
843     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
844     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
845     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
846
847
848     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
849     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
850
851     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
852
853     SDL_FreeSurface (sdl_surface_copy);
854     return ret;
855     }
856
857   int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
858
859   return ret;
860 }
861
862 int
863 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
864 {
865   SDL_Rect dest;
866
867   dest.x = (int)x;
868   dest.y = (int)y;
869   dest.w = (int)sw;
870   dest.h = (int)sh;
871
872   if(effect & SEMI_TRANSPARENT)
873     alpha = 128;
874
875   SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
876                                   sw, sh, sdl_surface->format->BitsPerPixel,
877                                   sdl_surface->format->Rmask, sdl_surface->format->Gmask,
878                                   sdl_surface->format->Bmask,
879                                   0);
880
881   SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
882   SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
883
884   if(alpha != 255)
885     SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
886
887   int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
888   SDL_FreeSurface(sdl_surface_copy);
889
890   return ret;
891 }
892
893 SurfaceSDL::~SurfaceSDL()
894 {}
895
896 /* EOF */