942881546f8e45a8445a17859f2755c90f6213d7
[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 update)
211 {
212   if (impl)
213   {
214     if (impl->draw(x, y, alpha, 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 SDL_Surface*
262 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h,  int use_alpha)
263 {
264   SDL_Rect src;
265   SDL_Surface * sdl_surface;
266   SDL_Surface * temp;
267   SDL_Surface * conv;
268
269   temp = IMG_Load(file.c_str());
270
271   if (temp == NULL)
272     st_abort("Can't load", file);
273
274   /* Set source rectangle for conv: */
275
276   src.x = x;
277   src.y = y;
278   src.w = w;
279   src.h = h;
280
281   conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
282                               temp->format->Rmask,
283                               temp->format->Gmask,
284                               temp->format->Bmask,
285                               temp->format->Amask);
286
287   /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
288      0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
289      #else
290
291      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
292      #endif*/
293
294   SDL_SetAlpha(temp,0,0);
295
296   SDL_BlitSurface(temp, &src, conv, NULL);
297   if(use_alpha == IGNORE_ALPHA && !use_gl)
298     sdl_surface = SDL_DisplayFormat(conv);
299   else
300     sdl_surface = SDL_DisplayFormatAlpha(conv);
301
302   if (sdl_surface == NULL)
303     st_abort("Can't covert to display format", file);
304
305   if (use_alpha == IGNORE_ALPHA && !use_gl)
306     SDL_SetAlpha(sdl_surface, 0, 0);
307
308   SDL_FreeSurface(temp);
309   SDL_FreeSurface(conv);
310
311   return sdl_surface;
312 }
313
314 SDL_Surface*
315 sdl_surface_from_file(const std::string& file, int use_alpha)
316 {
317   SDL_Surface* sdl_surface;
318   SDL_Surface* temp;
319
320   temp = IMG_Load(file.c_str());
321
322   if (temp == NULL)
323     st_abort("Can't load", file);
324
325   if(use_alpha == IGNORE_ALPHA && !use_gl)
326     sdl_surface = SDL_DisplayFormat(temp);
327   else
328     sdl_surface = SDL_DisplayFormatAlpha(temp);
329
330   if (sdl_surface == NULL)
331     st_abort("Can't covert to display format", file);
332
333   if (use_alpha == IGNORE_ALPHA && !use_gl)
334     SDL_SetAlpha(sdl_surface, 0, 0);
335
336   SDL_FreeSurface(temp);
337
338   return sdl_surface;
339 }
340
341 SDL_Surface*
342 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
343 {
344   SDL_Surface* sdl_surface;
345   Uint32 saved_flags;
346   Uint8  saved_alpha;
347
348   /* Save the alpha blending attributes */
349   saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
350   saved_alpha = sdl_surf->format->alpha;
351   if ( (saved_flags & SDL_SRCALPHA)
352        == SDL_SRCALPHA )
353   {
354     SDL_SetAlpha(sdl_surf, 0, 0);
355   }
356
357   if(use_alpha == IGNORE_ALPHA && !use_gl)
358     sdl_surface = SDL_DisplayFormat(sdl_surf);
359   else
360     sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
361
362   /* Restore the alpha blending attributes */
363   if ( (saved_flags & SDL_SRCALPHA)
364        == SDL_SRCALPHA )
365   {
366     SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
367   }
368
369   if (sdl_surface == NULL)
370     st_abort("Can't covert to display format", "SURFACE");
371
372   if (use_alpha == IGNORE_ALPHA && !use_gl)
373     SDL_SetAlpha(sdl_surface, 0, 0);
374
375   return sdl_surface;
376 }
377
378 //---------------------------------------------------------------------------
379
380 SurfaceImpl::SurfaceImpl()
381 {}
382
383 SurfaceImpl::~SurfaceImpl()
384 {
385   SDL_FreeSurface(sdl_surface);
386 }
387
388 SDL_Surface* SurfaceImpl::get_sdl_surface() const
389 {
390   return sdl_surface;
391 }
392
393 int SurfaceImpl::resize(int w_, int h_)
394 {
395   w = w_;
396   h = h_;
397   SDL_Rect dest;
398   dest.x = 0;
399   dest.y = 0;
400   dest.w = w;
401   dest.h = h;
402   int ret = SDL_SoftStretch(sdl_surface, NULL,
403                   sdl_surface, &dest);
404   return ret;
405 }
406
407 #ifndef NOOPENGL
408 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
409 {
410   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
411   create_gl(sdl_surface,&gl_texture);
412
413   w = sdl_surface->w;
414   h = sdl_surface->h;
415 }
416
417 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
418 {
419   sdl_surface = sdl_surface_from_file(file, use_alpha);
420   create_gl(sdl_surface,&gl_texture);
421
422   w = sdl_surface->w;
423   h = sdl_surface->h;
424 }
425
426 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
427 {
428   sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,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()
436 {
437   glDeleteTextures(1, &gl_texture);
438 }
439
440 void
441 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
442 {
443   Uint32 saved_flags;
444   Uint8  saved_alpha;
445   int w, h;
446   SDL_Surface *conv;
447
448   w = power_of_two(surf->w);
449   h = power_of_two(surf->h),
450
451 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
452       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
453                                   0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
454 #else
455       conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
456                                   0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
457 #endif
458
459   /* Save the alpha blending attributes */
460   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
461   saved_alpha = surf->format->alpha;
462   if ( (saved_flags & SDL_SRCALPHA)
463        == SDL_SRCALPHA )
464   {
465     SDL_SetAlpha(surf, 0, 0);
466   }
467
468   SDL_BlitSurface(surf, 0, conv, 0);
469
470   /* Restore the alpha blending attributes */
471   if ( (saved_flags & SDL_SRCALPHA)
472        == SDL_SRCALPHA )
473   {
474     SDL_SetAlpha(surf, saved_flags, saved_alpha);
475   }
476
477   glGenTextures(1, &*tex);
478   glBindTexture(GL_TEXTURE_2D , *tex);
479   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
480   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
481   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
482   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
483   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
484   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
485   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
486
487   SDL_FreeSurface(conv);
488 }
489
490 int
491 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
492 {
493   float pw = power_of_two(w);
494   float ph = power_of_two(h);
495
496   glEnable(GL_TEXTURE_2D);
497   glEnable(GL_BLEND);
498   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
499
500   glColor4ub(alpha, alpha, alpha, alpha);
501
502   glBindTexture(GL_TEXTURE_2D, gl_texture);
503
504   glBegin(GL_QUADS);
505   glTexCoord2f(0, 0);
506   glVertex2f(x, y);
507   glTexCoord2f((float)w / pw, 0);
508   glVertex2f((float)w+x, y);
509   glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)w+x, (float)h+y);
510   glTexCoord2f(0, (float)h / ph);
511   glVertex2f(x, (float)h+y);
512   glEnd();
513
514   glDisable(GL_TEXTURE_2D);
515   glDisable(GL_BLEND);
516
517   (void) update; // avoid compiler warning
518
519   return 0;
520 }
521
522 int
523 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
524 {
525   float pw = power_of_two(w);
526   float ph = power_of_two(h);
527
528   glColor3ub(alpha, alpha, alpha);
529
530   glEnable(GL_TEXTURE_2D);
531   glBindTexture(GL_TEXTURE_2D, gl_texture);
532
533   glBegin(GL_QUADS);
534   glTexCoord2f(0, 0);
535   glVertex2f(0, 0);
536   glTexCoord2f((float)w / pw, 0);
537   glVertex2f(screen->w, 0);
538   glTexCoord2f((float)w / pw, (float)h / ph);
539   glVertex2f(screen->w, screen->h);
540   glTexCoord2f(0, (float)h / ph);
541   glVertex2f(0, screen->h);
542   glEnd();
543
544   glDisable(GL_TEXTURE_2D);
545
546   (void) update; // avoid compiler warning
547
548   return 0;
549 }
550
551 int
552 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
553 {
554   float pw = power_of_two(int(this->w));
555   float ph = power_of_two(int(this->h));
556
557   glBindTexture(GL_TEXTURE_2D, gl_texture);
558
559   glEnable(GL_BLEND);
560   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
561
562   glColor4ub(alpha, alpha, alpha, alpha);
563
564   glEnable(GL_TEXTURE_2D);
565
566
567   glBegin(GL_QUADS);
568   glTexCoord2f(sx / pw, sy / ph);
569   glVertex2f(x, y);
570   glTexCoord2f((float)(sx + w) / pw, sy / ph);
571   glVertex2f(w+x, y);
572   glTexCoord2f((sx+w) / pw, (sy+h) / ph);
573   glVertex2f(w +x, h+y);
574   glTexCoord2f(sx / pw, (float)(sy+h) / ph);
575   glVertex2f(x, h+y);
576   glEnd();
577
578   glDisable(GL_TEXTURE_2D);
579   glDisable(GL_BLEND);
580
581   (void) update; // avoid warnings
582   return 0;
583 }
584
585 int
586 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
587 {
588   float pw = power_of_two(int(this->w));
589   float ph = power_of_two(int(this->h));
590
591   glBindTexture(GL_TEXTURE_2D, gl_texture);
592
593   glEnable(GL_BLEND);
594   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
595
596   glColor4ub(alpha, alpha, alpha, alpha);
597
598   glEnable(GL_TEXTURE_2D);
599
600
601   glBegin(GL_QUADS);
602   glTexCoord2f(0, 0);
603   glVertex2f(x, y);
604   glTexCoord2f((float)w / pw, 0);
605   glVertex2f(sw+x, y);
606   glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)sw+x, (float)sh+y);
607   glVertex2f(sw +x, sh+y);
608   glTexCoord2f(0, (float)h / ph);
609   glVertex2f(x, sh+y);
610   glEnd();
611
612   glDisable(GL_TEXTURE_2D);
613   glDisable(GL_BLEND);
614
615   (void) update; // avoid warnings
616   return 0;
617 }
618
619 #endif
620
621 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
622 {
623   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
624   w = sdl_surface->w;
625   h = sdl_surface->h;
626 }
627
628 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
629 {
630   sdl_surface = sdl_surface_from_file(file, use_alpha);
631   w = sdl_surface->w;
632   h = sdl_surface->h;
633 }
634
635 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  int use_alpha)
636 {
637   sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
638   w = sdl_surface->w;
639   h = sdl_surface->h;
640 }
641
642 int
643 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
644 {
645   SDL_Rect dest;
646
647   dest.x = (int)x;
648   dest.y = (int)y;
649   dest.w = w;
650   dest.h = h;
651
652   if(alpha != 255)
653   {
654     /* Copy the SDL surface, then make it using alpha and use it to blit into the screen */
655 /*
656     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
657                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
658                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
659                                     sdl_surface->format->Bmask,
660                                     sdl_surface->format->Amask);
661
662     SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
663
664     SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
665
666     int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest); */
667
668 /* *TEST* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
669 to temp sur, blit the temp into the screen */
670
671     SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
672                                     sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
673                                     sdl_surface->format->Rmask, sdl_surface->format->Gmask,
674                                     sdl_surface->format->Bmask,
675                                     0);
676 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
677 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
678 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
679
680
681 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
682 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
683
684 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
685
686
687
688     if (update == UPDATE)
689       SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
690
691     SDL_FreeSurface (sdl_surface_copy) ;
692     return ret;
693   }
694
695   int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
696
697   if (update == UPDATE)
698     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
699
700   return ret;
701 }
702
703 int
704 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
705 {
706   SDL_Rect dest;
707
708   dest.x = 0;
709   dest.y = 0;
710   dest.w = screen->w;
711   dest.h = screen->h;
712
713   if(alpha != 255)
714     SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
715
716   int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
717
718   if (update == UPDATE)
719     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
720
721   return ret;
722 }
723
724 int
725 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
726 {
727   SDL_Rect src, dest;
728
729   src.x = (int)sx;
730   src.y = (int)sy;
731   src.w = (int)w;
732   src.h = (int)h;
733
734   dest.x = (int)x;
735   dest.y = (int)y;
736   dest.w = (int)w;
737   dest.h = (int)h;
738
739   if(alpha != 255)
740     SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
741
742   int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
743
744   if (update == UPDATE)
745     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
746
747   return ret;
748 }
749
750 int
751 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
752 {
753   SDL_Rect dest;
754
755   dest.x = (int)x;
756   dest.y = (int)y;
757   dest.w = (int)sw;
758   dest.h = (int)sh;
759
760   if(alpha != 255)
761     SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
762
763   int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
764
765   if (update == UPDATE)
766     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
767
768   return ret;
769 }
770
771 SurfaceSDL::~SurfaceSDL()
772 {}
773
774 /* EOF */