4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
26 #include "SDL_image.h"
28 #include "../video/surface.h"
29 #include "../video/screen.h"
30 #include "../app/globals.h"
31 #include "../app/setup.h"
33 using namespace SuperTux;
35 Surface::Surfaces Surface::surfaces;
37 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
38 : type(SURFACE), surface(0), use_alpha(use_alpha_)
40 // Copy the given surface and make sure that it is not stored in
42 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
44 temp->format->BitsPerPixel,
50 Termination::abort("No memory left.", "");
51 SDL_SetAlpha(temp,0,0);
52 SDL_BlitSurface(temp, NULL, surface, NULL);
55 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
56 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
59 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
60 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
61 x(x_), y(y_), w(w_), h(h_)
64 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_)
65 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
67 top_gradient = top_gradient_;
68 bottom_gradient = bottom_gradient_;
72 SurfaceData::~SurfaceData()
74 SDL_FreeSurface(surface);
82 return create_SurfaceOpenGL();
84 return create_SurfaceSDL();
86 return create_SurfaceSDL();
91 SurfaceData::create_SurfaceSDL()
96 return new SurfaceSDL(file, use_alpha);
98 return new SurfaceSDL(file, x, y, w, h, use_alpha);
100 return new SurfaceSDL(surface, use_alpha);
102 return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
108 SurfaceData::create_SurfaceOpenGL()
114 return new SurfaceOpenGL(file, use_alpha);
116 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
118 return new SurfaceOpenGL(surface, use_alpha);
120 return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
127 /* Quick utility function for texture creation */
128 static int power_of_two(int input)
132 while ( value < input )
140 Surface::Surface(SDL_Surface* surf, bool use_alpha)
141 : data(surf, use_alpha), w(0), h(0)
143 impl = data.create();
149 surfaces.push_back(this);
152 Surface::Surface(const std::string& file, bool use_alpha)
153 : data(file, use_alpha), w(0), h(0)
155 impl = data.create();
161 surfaces.push_back(this);
164 Surface::Surface(const std::string& file, int x, int y, int w, int h, bool use_alpha)
165 : data(file, x, y, w, h, use_alpha), w(0), h(0)
167 impl = data.create();
173 surfaces.push_back(this);
176 Surface::Surface(Color top_background, Color bottom_background, int w_, int h_)
177 : data(top_background, bottom_background, w_, h_), w(0), h(0)
179 impl = data.create();
185 surfaces.push_back(this);
192 impl = data.create();
200 void Surface::apply_mask(Color color)
202 impl->apply_mask(color);
209 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
218 printf("Error: Surface freed twice!!!\n");
220 surfaces.remove(this);
225 Surface::reload_all()
227 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
234 Surface::debug_check()
236 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
238 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
239 (*i)->data.file.c_str());
244 Surface::resize(int w_, int h_)
250 if (impl->resize(w_,h_) == -2)
256 apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
258 if(filter == MASK_FILTER)
263 SDL_LockSurface(surface);
264 for(int x = 0; x < surface->w; x++)
265 for(int y = 0; y < surface->h; y++)
267 // SDL_LockSurface(surface);
268 SDL_GetRGBA(getpixel(surface,x,y), surface->format, &r,&g,&b,&a);
269 // SDL_UnlockSurface(surface);
272 putpixel(surface, x,y, color.map_rgba(surface));
273 // rect.x = x; rect.y = y;
274 // SDL_FillRect(surface, &rect, color.map_rgba(surface));
277 SDL_UnlockSurface(surface);
282 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
285 SDL_Surface * sdl_surface;
289 temp = IMG_Load(file.c_str());
292 Termination::abort("Can't load", file);
294 /* Set source rectangle for conv: */
301 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
305 temp->format->Amask);
307 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
308 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
311 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
314 SDL_SetAlpha(temp,0,0);
316 SDL_BlitSurface(temp, &src, conv, NULL);
317 if(use_alpha == false && !use_gl)
318 sdl_surface = SDL_DisplayFormat(conv);
320 sdl_surface = SDL_DisplayFormatAlpha(conv);
322 if (sdl_surface == NULL)
323 Termination::abort("Can't covert to display format", file);
325 if (use_alpha == false && !use_gl)
326 SDL_SetAlpha(sdl_surface, 0, 0);
328 SDL_FreeSurface(temp);
329 SDL_FreeSurface(conv);
335 sdl_surface_from_file(const std::string& file, bool use_alpha)
337 SDL_Surface* sdl_surface;
340 temp = IMG_Load(file.c_str());
343 Termination::abort("Can't load", file);
345 if(use_alpha == false && !use_gl)
346 sdl_surface = SDL_DisplayFormat(temp);
348 sdl_surface = SDL_DisplayFormatAlpha(temp);
350 if (sdl_surface == NULL)
351 Termination::abort("Can't covert to display format", file);
353 if (use_alpha == false && !use_gl)
354 SDL_SetAlpha(sdl_surface, 0, 0);
356 SDL_FreeSurface(temp);
362 SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
364 SDL_Surface* sdl_surface;
368 /* Save the alpha blending attributes */
369 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
370 saved_alpha = sdl_surf->format->alpha;
371 if ( (saved_flags & SDL_SRCALPHA)
374 SDL_SetAlpha(sdl_surf, 0, 0);
377 if(use_alpha == false && !use_gl)
378 sdl_surface = SDL_DisplayFormat(sdl_surf);
380 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
382 /* Restore the alpha blending attributes */
383 if ( (saved_flags & SDL_SRCALPHA)
386 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
389 if (sdl_surface == NULL)
390 Termination::abort("Can't covert to display format", "SURFACE");
392 if (use_alpha == false && !use_gl)
393 SDL_SetAlpha(sdl_surface, 0, 0);
399 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
401 SDL_Surface* sdl_surface;
403 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
404 screen->format->BitsPerPixel, screen->format->Rmask,
405 screen->format->Gmask, screen->format->Bmask, 0);
407 if(sdl_surface == NULL)
408 Termination::abort("Cannot create surface for the gradient", "SURFACE");
412 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
413 top.red, top.green, top.blue));
417 float redstep = (float(bottom.red)-float(top.red)) / float(h);
418 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
419 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
425 for(float y = 0; y < h; y++)
428 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
429 int(float(top.red) + redstep * y),
430 int(float(top.green) + greenstep * y),
431 int(float(top.blue) + bluestep * y)));
438 //---------------------------------------------------------------------------
440 SurfaceImpl::SurfaceImpl()
443 SurfaceImpl::~SurfaceImpl()
445 SDL_FreeSurface(sdl_surface);
448 SDL_Surface* SurfaceImpl::get_sdl_surface() const
453 int SurfaceImpl::resize(int w_, int h_)
462 int ret = SDL_SoftStretch(sdl_surface, NULL,
468 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
470 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
471 create_gl(sdl_surface,&gl_texture);
477 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
479 sdl_surface = sdl_surface_from_file(file, use_alpha);
480 create_gl(sdl_surface,&gl_texture);
486 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
488 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
490 create_gl(sdl_surface, &gl_texture);
496 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
498 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
499 create_gl(sdl_surface, &gl_texture);
505 SurfaceOpenGL::~SurfaceOpenGL()
507 glDeleteTextures(1, &gl_texture);
511 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
518 w = power_of_two(surf->w);
519 h = power_of_two(surf->h),
521 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
522 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
523 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
525 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
526 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
529 /* Save the alpha blending attributes */
530 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
531 saved_alpha = surf->format->alpha;
532 if ( (saved_flags & SDL_SRCALPHA)
535 SDL_SetAlpha(surf, 0, 0);
538 SDL_BlitSurface(surf, 0, conv, 0);
540 /* Restore the alpha blending attributes */
541 if ( (saved_flags & SDL_SRCALPHA)
544 SDL_SetAlpha(surf, saved_flags, saved_alpha);
547 // We check all the pixels of the surface to figure out which
548 // internal format OpenGL should use for storing it, ie. if no alpha
549 // is present store in RGB instead of RGBA, this saves a few bytes
550 // of memory, but much more importantly it makes the game look
551 // *much* better in 16bit color mode
552 int internal_format = GL_RGB10_A2;
553 bool has_alpha = false;
555 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
556 for (int y = 0; y < surf->h; ++y)
557 for (int x = 0; x < surf->w; ++x)
559 if (buf[(conv->pitch*y + x*4) + 3] != 255)
568 internal_format = GL_RGB;
571 glGenTextures(1, &*tex);
572 glBindTexture(GL_TEXTURE_2D , *tex);
573 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
574 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
575 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
576 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
577 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
578 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
579 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
581 SDL_FreeSurface(conv);
585 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
587 float pw = power_of_two(w);
588 float ph = power_of_two(h);
590 if(effect & SEMI_TRANSPARENT)
593 glEnable(GL_TEXTURE_2D);
595 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
597 glColor4ub(alpha, alpha, alpha, alpha);
599 glBindTexture(GL_TEXTURE_2D, gl_texture);
603 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
606 glVertex2f((float)w+x, (float)h+y);
608 glTexCoord2f((float)w / pw, 0);
609 glVertex2f(x, (float)h+y);
611 glTexCoord2f((float)w / pw, (float)h / ph);
614 glTexCoord2f(0, (float)h / ph);
615 glVertex2f((float)w+x, y);
617 else if(effect & VERTICAL_FLIP)
620 glVertex2f(x, (float)h+y);
622 glTexCoord2f((float)w / pw, 0);
623 glVertex2f((float)w+x, (float)h+y);
625 glTexCoord2f((float)w / pw, (float)h / ph);
626 glVertex2f((float)w+x, y);
628 glTexCoord2f(0, (float)h / ph);
631 else if(effect & HORIZONTAL_FLIP)
634 glVertex2f((float)w+x, y);
636 glTexCoord2f((float)w / pw, 0);
639 glTexCoord2f((float)w / pw, (float)h / ph);
640 glVertex2f(x, (float)h+y);
642 glTexCoord2f(0, (float)h / ph);
643 glVertex2f((float)w+x, (float)h+y);
650 glTexCoord2f((float)w / pw, 0);
651 glVertex2f((float)w+x, y);
653 glTexCoord2f((float)w / pw, (float)h / ph);
654 glVertex2f((float)w+x, (float)h+y);
656 glTexCoord2f(0, (float)h / ph);
657 glVertex2f(x, (float)h+y);
661 glDisable(GL_TEXTURE_2D);
668 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
670 float pw = power_of_two(int(this->w));
671 float ph = power_of_two(int(this->h));
673 if(effect & SEMI_TRANSPARENT)
676 glBindTexture(GL_TEXTURE_2D, gl_texture);
679 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
681 glColor4ub(alpha, alpha, alpha, alpha);
683 glEnable(GL_TEXTURE_2D);
688 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
690 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
691 glVertex2f((float)w+x, (float)h+y);
693 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
694 glVertex2f(x, (float)h+y);
696 glTexCoord2f((float)(sx + w) / pw, sy / ph);
699 glTexCoord2f(sx / pw, sy / ph);
700 glVertex2f((float)w+x, y);
702 else if(effect & VERTICAL_FLIP)
704 glTexCoord2f(sx / pw, sy / ph);
707 glTexCoord2f((float)(sx + w) / pw, sy / ph);
710 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
711 glVertex2f(w +x, h+y);
713 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
716 else if(effect & HORIZONTAL_FLIP)
718 glTexCoord2f(sx / pw, sy / ph);
719 glVertex2f((float)w+x, y);
721 glTexCoord2f((float)(sx + w) / pw, sy / ph);
724 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
725 glVertex2f(x, (float)h+y);
727 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
728 glVertex2f((float)w+x, (float)h+y);
732 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
735 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
736 glVertex2f(w +x, h+y);
738 glTexCoord2f((float)(sx + w) / pw, sy / ph);
741 glTexCoord2f(sx / pw, sy / ph);
747 glDisable(GL_TEXTURE_2D);
754 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
756 if(effect & SEMI_TRANSPARENT)
759 float pw = power_of_two(sw);
760 float ph = power_of_two(sh);
762 if(effect & SEMI_TRANSPARENT)
765 glEnable(GL_TEXTURE_2D);
767 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
769 glColor4ub(alpha, alpha, alpha, alpha);
771 glBindTexture(GL_TEXTURE_2D, gl_texture);
775 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
778 glVertex2f((float)sw+x, (float)sh+y);
780 glTexCoord2f((float)w / pw, 0);
781 glVertex2f(x, (float)sh+y);
783 glTexCoord2f((float)w / pw, (float)h / ph);
786 glTexCoord2f(0, (float)h / ph);
787 glVertex2f((float)sw+x, y);
789 else if(effect & VERTICAL_FLIP)
792 glVertex2f(x, (float)sh+y);
794 glTexCoord2f((float)w / pw, 0);
795 glVertex2f((float)sw+x, (float)sh+y);
797 glTexCoord2f((float)w / pw, (float)h / ph);
798 glVertex2f((float)sw+x, y);
800 glTexCoord2f(0, (float)h / ph);
803 else if(effect & HORIZONTAL_FLIP)
806 glVertex2f((float)sw+x, y);
808 glTexCoord2f((float)w / pw, 0);
811 glTexCoord2f((float)w / pw, (float)h / ph);
812 glVertex2f(x, (float)sh+y);
814 glTexCoord2f(0, (float)h / ph);
815 glVertex2f((float)sw+x, (float)sh+y);
822 glTexCoord2f((float)w / pw, 0);
823 glVertex2f((float)sw+x, y);
825 glTexCoord2f((float)w / pw, (float)h / ph);
826 glVertex2f((float)sw+x, (float)sh+y);
828 glTexCoord2f(0, (float)h / ph);
829 glVertex2f(x, (float)sh+y);
833 glDisable(GL_TEXTURE_2D);
840 SurfaceOpenGL::apply_mask(Color color)
842 ::apply_filter_to_surface(sdl_surface, MASK_FILTER, color);
843 create_gl(sdl_surface,&gl_texture);
851 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
853 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
858 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
860 sdl_surface = sdl_surface_from_file(file, use_alpha);
865 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, bool use_alpha)
867 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
872 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
874 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
880 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
889 if(effect & SEMI_TRANSPARENT)
892 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
894 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
895 for(float sx = 0; sx < w; sx++)
896 for(float sy = 0; sy < h; sy++)
897 if(draw_part(sx, sy, x+(w-sx), y+(h-sy), 1, 1, alpha, NONE_EFFECT) == -2)
901 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
903 for(float sy = 0; sy < h; sy++)
904 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
908 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
910 for(float sx = 0; sx < w; sx++)
911 if(draw_part(sx, 0, x+(w-sx), y, 1, h, alpha, NONE_EFFECT) == -2)
918 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
919 to temp sur, blit the temp into the screen */
920 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
921 already have an alpha mask yet... */
923 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
924 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
925 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
926 sdl_surface->format->Bmask,
928 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
929 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
930 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
933 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
934 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
936 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
938 SDL_FreeSurface (sdl_surface_copy);
942 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
948 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
962 if(effect & SEMI_TRANSPARENT)
965 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
967 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
968 for(float sx_ = 0; sx_ < w; sx++)
969 for(float sy_ = 0; sy_ < h; sy++)
970 if(draw_part(sx_, sy_, sx+(w-sx_), sy+(h-sy_), 1, 1, alpha, NONE_EFFECT) == -2)
974 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
976 for(float sy_ = sy; sy_ < h; sy_++)
977 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
981 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
983 for(float sx_ = 0; sx_ < w; sx_++)
984 if(draw_part(sx_, 0, sx+(w-sx_), sy, 1, h, alpha, NONE_EFFECT) == -2)
991 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
992 to temp sur, blit the temp into the screen */
993 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
994 already have an alpha mask yet... */
996 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
997 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
998 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
999 sdl_surface->format->Bmask,
1001 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
1002 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
1003 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
1006 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
1007 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
1009 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
1011 SDL_FreeSurface (sdl_surface_copy);
1015 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
1021 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
1030 if(effect & SEMI_TRANSPARENT)
1033 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
1034 sw, sh, sdl_surface->format->BitsPerPixel,
1035 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
1036 sdl_surface->format->Bmask,
1039 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
1040 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
1043 SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
1045 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
1046 SDL_FreeSurface(sdl_surface_copy);
1052 SurfaceSDL::apply_mask(Color color)
1054 ::apply_filter_to_surface(sdl_surface, MASK_FILTER, color);
1060 SurfaceSDL::~SurfaceSDL()