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