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_filter(int filter, Color color)
202 impl->apply_filter(filter, 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 == HORIZONTAL_FLIP_FILTER)
260 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
261 SDL_BlitSurface(surface, NULL, sur_copy, NULL);
262 SDL_SetAlpha(sur_copy,0,0);
267 src.h = dst.h = sur_copy->h;
268 for(int x = 0; x < sur_copy->w; x++)
270 src.x = x; dst.x = sur_copy->w - x;
271 SDL_BlitSurface(sur_copy, &src, surface, &dst);
274 SDL_FreeSurface(sur_copy);
276 else if(filter == MASK_FILTER)
278 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
282 SDL_LockSurface(sur_copy);
283 for(int x = 0; x < sur_copy->w; x++)
284 for(int y = 0; y < sur_copy->h; y++)
286 SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
289 putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
292 SDL_UnlockSurface(sur_copy);
294 SDL_BlitSurface(sur_copy, NULL, surface, NULL);
295 SDL_FreeSurface(sur_copy);
300 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
303 SDL_Surface * sdl_surface;
307 temp = IMG_Load(file.c_str());
310 Termination::abort("Can't load", file);
312 /* Set source rectangle for conv: */
319 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
323 temp->format->Amask);
325 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
326 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
329 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
332 SDL_SetAlpha(temp,0,0);
334 SDL_BlitSurface(temp, &src, conv, NULL);
335 if(use_alpha == false && !use_gl)
336 sdl_surface = SDL_DisplayFormat(conv);
338 sdl_surface = SDL_DisplayFormatAlpha(conv);
340 if (sdl_surface == NULL)
341 Termination::abort("Can't covert to display format", file);
343 if (use_alpha == false && !use_gl)
344 SDL_SetAlpha(sdl_surface, 0, 0);
346 SDL_FreeSurface(temp);
347 SDL_FreeSurface(conv);
353 sdl_surface_from_file(const std::string& file, bool use_alpha)
355 SDL_Surface* sdl_surface;
358 temp = IMG_Load(file.c_str());
361 Termination::abort("Can't load", file);
363 if(use_alpha == false && !use_gl)
364 sdl_surface = SDL_DisplayFormat(temp);
366 sdl_surface = SDL_DisplayFormatAlpha(temp);
368 if (sdl_surface == NULL)
369 Termination::abort("Can't covert to display format", file);
371 if (use_alpha == false && !use_gl)
372 SDL_SetAlpha(sdl_surface, 0, 0);
374 SDL_FreeSurface(temp);
380 SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
382 SDL_Surface* sdl_surface;
387 /* Save the alpha blending attributes */
388 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
389 saved_alpha = sdl_surf->format->alpha;
390 if ( (saved_flags & SDL_SRCALPHA)
393 SDL_SetAlpha(sdl_surf, 0, 0);
397 if(use_alpha == false && !use_gl)
398 sdl_surface = SDL_DisplayFormat(sdl_surf);
400 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
403 /* Restore the alpha blending attributes */
404 if ( (saved_flags & SDL_SRCALPHA)
407 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
411 if (sdl_surface == NULL)
412 Termination::abort("Can't covert to display format", "SURFACE");
414 if (use_alpha == false && !use_gl)
415 SDL_SetAlpha(sdl_surface, 0, 0);
421 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
423 SDL_Surface* sdl_surface;
425 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
426 screen->format->BitsPerPixel, screen->format->Rmask,
427 screen->format->Gmask, screen->format->Bmask, 0);
429 if(sdl_surface == NULL)
430 Termination::abort("Cannot create surface for the gradient", "SURFACE");
434 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
435 top.red, top.green, top.blue));
439 float redstep = (float(bottom.red)-float(top.red)) / float(h);
440 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
441 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
447 for(float y = 0; y < h; y++)
450 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
451 int(float(top.red) + redstep * y),
452 int(float(top.green) + greenstep * y),
453 int(float(top.blue) + bluestep * y)));
460 //---------------------------------------------------------------------------
462 SurfaceImpl::SurfaceImpl()
465 SurfaceImpl::~SurfaceImpl()
467 SDL_FreeSurface(sdl_surface);
470 SDL_Surface* SurfaceImpl::get_sdl_surface() const
475 int SurfaceImpl::resize(int w_, int h_)
484 int ret = SDL_SoftStretch(sdl_surface, NULL,
490 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
492 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
493 create_gl(sdl_surface,&gl_texture);
499 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
501 sdl_surface = sdl_surface_from_file(file, use_alpha);
502 create_gl(sdl_surface,&gl_texture);
508 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
510 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
512 create_gl(sdl_surface, &gl_texture);
518 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
520 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
521 create_gl(sdl_surface, &gl_texture);
527 SurfaceOpenGL::~SurfaceOpenGL()
529 glDeleteTextures(1, &gl_texture);
533 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
540 w = power_of_two(surf->w);
541 h = power_of_two(surf->h),
543 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
544 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
545 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
547 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
548 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
551 /* Save the alpha blending attributes */
552 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
553 saved_alpha = surf->format->alpha;
554 if ( (saved_flags & SDL_SRCALPHA)
557 SDL_SetAlpha(surf, 0, 0);
560 SDL_BlitSurface(surf, 0, conv, 0);
562 /* Restore the alpha blending attributes */
563 if ( (saved_flags & SDL_SRCALPHA)
566 SDL_SetAlpha(surf, saved_flags, saved_alpha);
569 // We check all the pixels of the surface to figure out which
570 // internal format OpenGL should use for storing it, ie. if no alpha
571 // is present store in RGB instead of RGBA, this saves a few bytes
572 // of memory, but much more importantly it makes the game look
573 // *much* better in 16bit color mode
574 int internal_format = GL_RGB10_A2;
575 bool has_alpha = false;
577 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
578 for (int y = 0; y < surf->h; ++y)
579 for (int x = 0; x < surf->w; ++x)
581 if (buf[(conv->pitch*y + x*4) + 3] != 255)
590 internal_format = GL_RGB;
593 glGenTextures(1, &*tex);
594 glBindTexture(GL_TEXTURE_2D , *tex);
595 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
596 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
597 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
598 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
599 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
600 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
601 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
603 SDL_FreeSurface(conv);
607 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
609 float pw = power_of_two(w);
610 float ph = power_of_two(h);
612 if(effect & SEMI_TRANSPARENT)
615 glEnable(GL_TEXTURE_2D);
617 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
619 glColor4ub(alpha, alpha, alpha, alpha);
621 glBindTexture(GL_TEXTURE_2D, gl_texture);
625 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
628 glVertex2f((float)w+x, (float)h+y);
630 glTexCoord2f((float)w / pw, 0);
631 glVertex2f(x, (float)h+y);
633 glTexCoord2f((float)w / pw, (float)h / ph);
636 glTexCoord2f(0, (float)h / ph);
637 glVertex2f((float)w+x, y);
639 else if(effect & VERTICAL_FLIP)
642 glVertex2f(x, (float)h+y);
644 glTexCoord2f((float)w / pw, 0);
645 glVertex2f((float)w+x, (float)h+y);
647 glTexCoord2f((float)w / pw, (float)h / ph);
648 glVertex2f((float)w+x, y);
650 glTexCoord2f(0, (float)h / ph);
653 else if(effect & HORIZONTAL_FLIP)
656 glVertex2f((float)w+x, y);
658 glTexCoord2f((float)w / pw, 0);
661 glTexCoord2f((float)w / pw, (float)h / ph);
662 glVertex2f(x, (float)h+y);
664 glTexCoord2f(0, (float)h / ph);
665 glVertex2f((float)w+x, (float)h+y);
672 glTexCoord2f((float)w / pw, 0);
673 glVertex2f((float)w+x, y);
675 glTexCoord2f((float)w / pw, (float)h / ph);
676 glVertex2f((float)w+x, (float)h+y);
678 glTexCoord2f(0, (float)h / ph);
679 glVertex2f(x, (float)h+y);
683 glDisable(GL_TEXTURE_2D);
690 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
692 float pw = power_of_two(int(this->w));
693 float ph = power_of_two(int(this->h));
695 if(effect & SEMI_TRANSPARENT)
698 glBindTexture(GL_TEXTURE_2D, gl_texture);
701 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
703 glColor4ub(alpha, alpha, alpha, alpha);
705 glEnable(GL_TEXTURE_2D);
710 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
712 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
713 glVertex2f((float)w+x, (float)h+y);
715 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
716 glVertex2f(x, (float)h+y);
718 glTexCoord2f((float)(sx + w) / pw, sy / ph);
721 glTexCoord2f(sx / pw, sy / ph);
722 glVertex2f((float)w+x, y);
724 else if(effect & VERTICAL_FLIP)
726 glTexCoord2f(sx / pw, sy / ph);
729 glTexCoord2f((float)(sx + w) / pw, sy / ph);
732 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
733 glVertex2f(w +x, h+y);
735 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
738 else if(effect & HORIZONTAL_FLIP)
740 glTexCoord2f(sx / pw, sy / ph);
741 glVertex2f((float)w+x, y);
743 glTexCoord2f((float)(sx + w) / pw, sy / ph);
746 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
747 glVertex2f(x, (float)h+y);
749 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
750 glVertex2f((float)w+x, (float)h+y);
754 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
757 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
758 glVertex2f(w +x, h+y);
760 glTexCoord2f((float)(sx + w) / pw, sy / ph);
763 glTexCoord2f(sx / pw, sy / ph);
769 glDisable(GL_TEXTURE_2D);
776 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
778 if(effect & SEMI_TRANSPARENT)
781 float pw = power_of_two(sw);
782 float ph = power_of_two(sh);
784 if(effect & SEMI_TRANSPARENT)
787 glEnable(GL_TEXTURE_2D);
789 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
791 glColor4ub(alpha, alpha, alpha, alpha);
793 glBindTexture(GL_TEXTURE_2D, gl_texture);
797 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
800 glVertex2f((float)sw+x, (float)sh+y);
802 glTexCoord2f((float)w / pw, 0);
803 glVertex2f(x, (float)sh+y);
805 glTexCoord2f((float)w / pw, (float)h / ph);
808 glTexCoord2f(0, (float)h / ph);
809 glVertex2f((float)sw+x, y);
811 else if(effect & VERTICAL_FLIP)
814 glVertex2f(x, (float)sh+y);
816 glTexCoord2f((float)w / pw, 0);
817 glVertex2f((float)sw+x, (float)sh+y);
819 glTexCoord2f((float)w / pw, (float)h / ph);
820 glVertex2f((float)sw+x, y);
822 glTexCoord2f(0, (float)h / ph);
825 else if(effect & HORIZONTAL_FLIP)
828 glVertex2f((float)sw+x, y);
830 glTexCoord2f((float)w / pw, 0);
833 glTexCoord2f((float)w / pw, (float)h / ph);
834 glVertex2f(x, (float)sh+y);
836 glTexCoord2f(0, (float)h / ph);
837 glVertex2f((float)sw+x, (float)sh+y);
844 glTexCoord2f((float)w / pw, 0);
845 glVertex2f((float)sw+x, y);
847 glTexCoord2f((float)w / pw, (float)h / ph);
848 glVertex2f((float)sw+x, (float)sh+y);
850 glTexCoord2f(0, (float)h / ph);
851 glVertex2f(x, (float)sh+y);
855 glDisable(GL_TEXTURE_2D);
862 SurfaceOpenGL::apply_filter(int filter, Color color)
864 ::apply_filter_to_surface(sdl_surface, filter, color);
865 create_gl(sdl_surface,&gl_texture);
873 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
875 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
880 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
882 sdl_surface = sdl_surface_from_file(file, use_alpha);
887 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, bool use_alpha)
889 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
894 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
896 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
902 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
911 if(effect & SEMI_TRANSPARENT)
914 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
916 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
917 for(float sx = 0; sx < w; sx++)
918 for(float sy = 0; sy < h; sy++)
919 if(draw_part(sx, sy, x+(w-sx), y+(h-sy), 1, 1, alpha, NONE_EFFECT) == -2)
923 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
925 for(float sy = 0; sy < h; sy++)
926 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
930 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
932 for(float sx = 0; sx < w; sx++)
933 if(draw_part(sx, 0, x+(w-sx), y, 1, h, alpha, NONE_EFFECT) == -2)
940 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
941 to temp sur, blit the temp into the screen */
942 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
943 already have an alpha mask yet... */
945 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
946 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
947 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
948 sdl_surface->format->Bmask,
950 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
951 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
952 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
955 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
956 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
958 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
960 SDL_FreeSurface (sdl_surface_copy);
964 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
970 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
984 if(effect & SEMI_TRANSPARENT)
987 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
989 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
990 for(float sx_ = 0; sx_ < w; sx++)
991 for(float sy_ = 0; sy_ < h; sy++)
992 if(draw_part(sx_, sy_, sx+(w-sx_), sy+(h-sy_), 1, 1, alpha, NONE_EFFECT) == -2)
996 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
998 for(float sy_ = sy; sy_ < h; sy_++)
999 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
1003 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
1005 for(float sx_ = 0; sx_ < w; sx_++)
1006 if(draw_part(sx_, 0, sx+(w-sx_), sy, 1, h, alpha, NONE_EFFECT) == -2)
1013 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
1014 to temp sur, blit the temp into the screen */
1015 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
1016 already have an alpha mask, yet... */
1018 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
1019 (int)w, (int)h, sdl_surface->format->BitsPerPixel,
1020 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
1021 sdl_surface->format->Bmask,
1023 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
1024 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
1025 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
1028 SDL_BlitSurface(sdl_surface, &src, sdl_surface_copy, NULL);
1029 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
1031 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
1033 SDL_FreeSurface (sdl_surface_copy);
1037 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
1043 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
1052 if(effect & SEMI_TRANSPARENT)
1055 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
1056 sw, sh, sdl_surface->format->BitsPerPixel,
1057 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
1058 sdl_surface->format->Bmask,
1061 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
1062 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
1065 SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
1067 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
1068 SDL_FreeSurface(sdl_surface_copy);
1074 SurfaceSDL::apply_filter(int filter, Color color)
1076 ::apply_filter_to_surface(sdl_surface, filter, color);
1082 SurfaceSDL::~SurfaceSDL()