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