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