Passed color mask from screen SDL_Surface to the gradients cache one. This fixes...
[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   sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
370                     screen->format->BitsPerPixel, screen->format->Rmask,
371                     screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
372
373   if(sdl_surface == NULL)
374       st_abort("Cannot create surface for the gradient", "SURFACE");
375
376   float redstep = (float(bottom.red)-float(top.red)) / float(h);
377   float greenstep = (float(bottom.green)-float(top.green)) / float(h);
378   float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
379
380   SDL_Rect rect;
381   rect.x = 0;
382   rect.w = w;
383   rect.h = 1;
384   for(float y = 0; y < h; y++)
385     {
386     rect.y = (int)y;
387     SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
388         int(float(top.red) + redstep * y),
389         int(float(top.green) + greenstep * y),
390         int(float(top.blue) + bluestep * y)));
391     }
392
393   return sdl_surface;
394 }
395
396 //---------------------------------------------------------------------------
397
398 SurfaceImpl::SurfaceImpl()
399 {}
400
401 SurfaceImpl::~SurfaceImpl()
402 {
403   SDL_FreeSurface(sdl_surface);
404 }
405
406 SDL_Surface* SurfaceImpl::get_sdl_surface() const
407 {
408   return sdl_surface;
409 }
410
411 int SurfaceImpl::resize(int w_, int h_)
412 {
413   w = w_;
414   h = h_;
415   SDL_Rect dest;
416   dest.x = 0;
417   dest.y = 0;
418   dest.w = w;
419   dest.h = h;
420   int ret = SDL_SoftStretch(sdl_surface, NULL,
421                             sdl_surface, &dest);
422   return ret;
423 }
424
425 #ifndef NOOPENGL
426 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
427 {
428   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
429   create_gl(sdl_surface,&gl_texture);
430
431   w = sdl_surface->w;
432   h = sdl_surface->h;
433 }
434
435 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
436 {
437   sdl_surface = sdl_surface_from_file(file, use_alpha);
438   create_gl(sdl_surface,&gl_texture);
439
440   w = sdl_surface->w;
441   h = sdl_surface->h;
442 }
443
444 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
445 {
446   sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
447   create_gl(sdl_surface, &gl_texture);
448
449   w = sdl_surface->w;
450   h = sdl_surface->h;
451 }
452
453 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
454 {
455   sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
456   create_gl(sdl_surface, &gl_texture);
457
458   w = sdl_surface->w;
459   h = sdl_surface->h;
460 }
461
462 SurfaceOpenGL::~SurfaceOpenGL()
463 {
464   glDeleteTextures(1, &gl_texture);
465 }
466
467 void
468 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
469 {
470   Uint32 saved_flags;
471   Uint8  saved_alpha;
472   int w, h;
473   SDL_Surface *conv;
474
475   w = power_of_two(surf->w);
476   h = power_of_two(surf->h),
477
478 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
479       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
480                                   0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
481 #else
482       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
483                                   0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
484 #endif
485
486   /* Save the alpha blending attributes */
487   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
488   saved_alpha = surf->format->alpha;
489   if ( (saved_flags & SDL_SRCALPHA)
490        == SDL_SRCALPHA )
491   {
492     SDL_SetAlpha(surf, 0, 0);
493   }
494
495   SDL_BlitSurface(surf, 0, conv, 0);
496
497   /* Restore the alpha blending attributes */
498   if ( (saved_flags & SDL_SRCALPHA)
499        == SDL_SRCALPHA )
500   {
501     SDL_SetAlpha(surf, saved_flags, saved_alpha);
502   }
503
504   glGenTextures(1, &*tex);
505   glBindTexture(GL_TEXTURE_2D , *tex);
506   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
507   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
508   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
509   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
510   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
511   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
512   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
513
514   SDL_FreeSurface(conv);
515 }
516
517 int
518 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
519 {
520   float pw = power_of_two(w);
521   float ph = power_of_two(h);
522
523   if(effect & SEMI_TRANSPARENT)
524     alpha = 128;
525
526   glEnable(GL_TEXTURE_2D);
527   glEnable(GL_BLEND);
528   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
529
530   glColor4ub(alpha, alpha, alpha, alpha);
531
532   glBindTexture(GL_TEXTURE_2D, gl_texture);
533
534   glBegin(GL_QUADS);
535
536   if(effect & VERTICAL_FLIP)
537     {
538     glTexCoord2f(0, 0);
539     glVertex2f(x, (float)h+y);
540
541     glTexCoord2f((float)w / pw, 0);
542     glVertex2f((float)w+x, (float)h+y);
543
544     glTexCoord2f((float)w / pw, (float)h / ph);
545     glVertex2f((float)w+x, y);
546     
547     glTexCoord2f(0, (float)h / ph);
548     glVertex2f(x, y);
549     }
550   else
551     {
552     glTexCoord2f(0, 0);
553     glVertex2f(x, y);
554
555     glTexCoord2f((float)w / pw, 0);
556     glVertex2f((float)w+x, y);
557
558     glTexCoord2f((float)w / pw, (float)h / ph);
559     glVertex2f((float)w+x, (float)h+y);
560
561     glTexCoord2f(0, (float)h / ph);
562     glVertex2f(x, (float)h+y);
563     }
564   glEnd();
565
566   glDisable(GL_TEXTURE_2D);
567   glDisable(GL_BLEND);
568
569   return 0;
570 }
571
572 int
573 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
574 {
575   float pw = power_of_two(int(this->w));
576   float ph = power_of_two(int(this->h));
577
578   if(effect & SEMI_TRANSPARENT)
579     alpha = 128;
580
581   glBindTexture(GL_TEXTURE_2D, gl_texture);
582
583   glEnable(GL_BLEND);
584   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
585
586   glColor4ub(alpha, alpha, alpha, alpha);
587
588   glEnable(GL_TEXTURE_2D);
589
590
591   glBegin(GL_QUADS);
592
593   if(effect & VERTICAL_FLIP)
594     {
595     glTexCoord2f(sx / pw, sy / ph);
596     glVertex2f(x, y);
597
598     glTexCoord2f((float)(sx + w) / pw, sy / ph);
599     glVertex2f(w+x, y);
600
601     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
602     glVertex2f(w +x, h+y);
603
604     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
605     glVertex2f(x, h+y);
606     }
607   else
608     {
609     glTexCoord2f(sx / pw, (float)(sy+h) / ph);
610     glVertex2f(x, h+y);
611
612     glTexCoord2f((sx+w) / pw, (sy+h) / ph);
613     glVertex2f(w +x, h+y);
614
615     glTexCoord2f((float)(sx + w) / pw, sy / ph);
616     glVertex2f(w+x, y);
617
618     glTexCoord2f(sx / pw, sy / ph);
619     glVertex2f(x, y);
620     }
621
622   glEnd();
623
624   glDisable(GL_TEXTURE_2D);
625   glDisable(GL_BLEND);
626
627   return 0;
628 }
629
630 #if 0
631 int
632 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
633 {
634   float pw = power_of_two(int(this->w));
635   float ph = power_of_two(int(this->h));
636
637   glBindTexture(GL_TEXTURE_2D, gl_texture);
638
639   glEnable(GL_BLEND);
640   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
641
642   glColor4ub(alpha, alpha, alpha, alpha);
643
644   glEnable(GL_TEXTURE_2D);
645
646
647   glBegin(GL_QUADS);
648   glTexCoord2f(0, 0);
649   glVertex2f(x, y);
650   glTexCoord2f((float)w / pw, 0);
651   glVertex2f(sw+x, y);
652   glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)sw+x, (float)sh+y);
653   glVertex2f(sw +x, sh+y);
654   glTexCoord2f(0, (float)h / ph);
655   glVertex2f(x, sh+y);
656   glEnd();
657
658   glDisable(GL_TEXTURE_2D);
659   glDisable(GL_BLEND);
660
661   return 0;
662 }
663 #endif
664
665 #endif
666
667 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
668 {
669   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
670   w = sdl_surface->w;
671   h = sdl_surface->h;
672 }
673
674 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
675 {
676   sdl_surface = sdl_surface_from_file(file, use_alpha);
677   w = sdl_surface->w;
678   h = sdl_surface->h;
679 }
680
681 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  int use_alpha)
682 {
683   sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
684   w = sdl_surface->w;
685   h = sdl_surface->h;
686 }
687
688 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
689 {
690   sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
691   w = sdl_surface->w;
692   h = sdl_surface->h;
693 }
694
695 int
696 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
697 {
698   SDL_Rect dest;
699
700   dest.x = (int)x;
701   dest.y = (int)y;
702   dest.w = w;
703   dest.h = h;
704
705   if(effect & SEMI_TRANSPARENT)
706     alpha = 128;
707
708   if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
709     {
710     for(float sy = 0; sy < h; sy++)
711       if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
712         return -2;
713     return 0;
714     }
715
716   if(alpha != 255)
717     {
718     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
719       to temp sur, blit the temp into the screen */
720     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
721       already have an alpha mask yet... */
722
723     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
724                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
725                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
726                                     sdl_surface->format->Bmask,
727                                     0);
728     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
729     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
730     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
731
732
733     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
734     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
735
736     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
737
738     SDL_FreeSurface (sdl_surface_copy);
739     return ret;
740     }
741
742   int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
743
744   return ret;
745 }
746
747 int
748 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
749 {
750   SDL_Rect src, dest;
751
752   src.x = (int)sx;
753   src.y = (int)sy;
754   src.w = (int)w;
755   src.h = (int)h;
756
757   dest.x = (int)x;
758   dest.y = (int)y;
759   dest.w = (int)w;
760   dest.h = (int)h;
761
762   if(effect & SEMI_TRANSPARENT)
763     alpha = 128;
764
765   if(effect & VERTICAL_FLIP)    // FIXME: feel free to replace this hack
766     {
767     for(float sy_ = sy; sy_ < h; sy_++)
768       if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
769         return -2;
770     return 0;
771     }
772
773   if(alpha != 255)
774     {
775     /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
776       to temp sur, blit the temp into the screen */
777     /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
778       already have an alpha mask yet... */
779
780     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
781                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
782                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
783                                     sdl_surface->format->Bmask,
784                                     0);
785     int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
786     SDL_FillRect(sdl_surface_copy, NULL, colorkey);
787     SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
788
789
790     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
791     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
792
793     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
794
795     SDL_FreeSurface (sdl_surface_copy);
796     return ret;
797     }
798
799   int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
800
801   return ret;
802 }
803
804 #if 0
805 int
806 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
807 {
808   SDL_Rect dest;
809
810   dest.x = (int)x;
811   dest.y = (int)y;
812   dest.w = (int)sw;
813   dest.h = (int)sh;
814
815   if(alpha != 255)
816     SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
817
818
819   SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
820                                   sw, sh, sdl_surface->format->BitsPerPixel,
821                                   sdl_surface->format->Rmask, sdl_surface->format->Gmask,
822                                   sdl_surface->format->Bmask,
823                                   0);
824
825   SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
826   SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
827
828   int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
829   SDL_FreeSurface(sdl_surface_copy);
830
831   if (update == UPDATE)
832     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
833
834   return ret;
835 }
836 #endif
837
838 SurfaceSDL::~SurfaceSDL()
839 {}
840
841 /* EOF */