1 // $Id: surface.cpp 2175 2004-11-24 19:02:49Z sik0fewl $
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
29 #include <SDL_image.h>
31 #include "gameconfig.h"
32 #include "video/surface.h"
33 #include "video/screen.h"
35 Surface::Surfaces Surface::surfaces;
37 extern SDL_Surface* screen;
39 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
40 : type(SURFACE), surface(0), use_alpha(use_alpha_),
41 x(0), y(0), w(0), h(0)
43 // Copy the given surface and make sure that it is not stored in
45 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
47 temp->format->BitsPerPixel,
53 throw std::runtime_error("No memory left for surface");
55 SDL_SetAlpha(temp,0,0);
56 SDL_BlitSurface(temp, NULL, surface, NULL);
59 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
60 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
63 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_,
64 int w_, int h_, bool use_alpha_)
65 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
66 x(x_), y(y_), w(w_), h(h_)
69 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_,
71 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
73 top_gradient = top_gradient_;
74 bottom_gradient = bottom_gradient_;
78 SurfaceData::~SurfaceData()
80 SDL_FreeSurface(surface);
87 return create_SurfaceOpenGL();
89 return create_SurfaceSDL();
93 SurfaceData::create_SurfaceSDL()
98 return new SurfaceSDL(file, use_alpha);
100 return new SurfaceSDL(file, x, y, w, h, use_alpha);
102 return new SurfaceSDL(surface, use_alpha);
104 return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
110 SurfaceData::create_SurfaceOpenGL()
115 return new SurfaceOpenGL(file, use_alpha);
117 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
119 return new SurfaceOpenGL(surface, use_alpha);
121 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 )
139 Surface::Surface(SDL_Surface* surf, bool use_alpha)
140 : data(surf, use_alpha), w(0), h(0)
142 impl = data.create();
148 surfaces.push_back(this);
151 Surface::Surface(const std::string& file, bool use_alpha)
152 : data(file, use_alpha), w(0), h(0)
154 impl = data.create();
160 surfaces.push_back(this);
163 Surface::Surface(const std::string& file, int x, int y, int w_, int h_, bool use_alpha)
164 : data(file, x, y, w_, h_, use_alpha), w(0), h(0)
166 impl = data.create();
172 surfaces.push_back(this);
175 Surface::Surface(Color top_background, Color bottom_background, int w_, int h_)
176 : data(top_background, bottom_background, w_, h_), w(0), h(0)
178 impl = data.create();
184 surfaces.push_back(this);
191 impl = data.create();
196 for(std::vector<SurfaceData::Filter>::iterator i =
197 data.applied_filters.begin(); i != data.applied_filters.end();
199 impl->apply_filter(i->type, i->color);
203 void Surface::apply_filter(int filter, Color color)
205 impl->apply_filter(filter, color);
207 SurfaceData::Filter apply_filter;
208 apply_filter.type = filter;
209 apply_filter.color = color;
210 data.applied_filters.push_back(apply_filter);
217 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
226 printf("Error: Surface freed twice!!!\n");
228 surfaces.remove(this);
233 Surface::reload_all()
235 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
242 Surface::debug_check()
244 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
246 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
247 (*i)->data.file.c_str());
252 apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
254 if(filter == HORIZONTAL_FLIP_FILTER) {
255 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
256 SDL_BlitSurface(surface, NULL, sur_copy, NULL);
257 SDL_SetAlpha(sur_copy,0,0);
262 src.h = dst.h = sur_copy->h;
263 for(int x = 0; x < sur_copy->w; x++)
265 src.x = x; dst.x = sur_copy->w-1 - x;
266 SDL_BlitSurface(sur_copy, &src, surface, &dst);
269 SDL_FreeSurface(sur_copy);
270 } else if(filter == MASK_FILTER) {
271 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
275 SDL_LockSurface(sur_copy);
276 for(int x = 0; x < sur_copy->w; x++)
277 for(int y = 0; y < sur_copy->h; y++) {
278 SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
280 putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
283 SDL_UnlockSurface(sur_copy);
285 SDL_BlitSurface(sur_copy, NULL, surface, NULL);
286 SDL_FreeSurface(sur_copy);
291 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
294 SDL_Surface * sdl_surface;
298 temp = IMG_Load(file.c_str());
301 std::stringstream msg;
302 msg << "Couldn't load '" << file << "': " << SDL_GetError();
303 throw std::runtime_error(msg.str());
306 /* Set source rectangle for conv: */
313 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, temp->format->BitsPerPixel,
317 temp->format->Amask);
319 SDL_SetAlpha(temp,0,0);
321 SDL_BlitSurface(temp, &src, conv, NULL);
322 if(use_alpha == false && !config->use_gl)
323 sdl_surface = SDL_DisplayFormat(conv);
325 sdl_surface = SDL_DisplayFormatAlpha(conv);
327 if (sdl_surface == NULL) {
328 std::stringstream msg;
329 msg << "Can't convert file '" << file << "' to display format.";
330 throw std::runtime_error(msg.str());
333 if (use_alpha == false && !config->use_gl)
334 SDL_SetAlpha(sdl_surface, 0, 0);
336 SDL_FreeSurface(temp);
337 SDL_FreeSurface(conv);
343 sdl_surface_from_file(const std::string& file, bool use_alpha)
345 SDL_Surface* sdl_surface;
348 temp = IMG_Load(file.c_str());
351 std::stringstream msg;
352 msg << "Couldn't load file '" << file << "': " << SDL_GetError();
353 throw std::runtime_error(msg.str());
356 if(use_alpha == false && !config->use_gl)
357 sdl_surface = SDL_DisplayFormat(temp);
359 sdl_surface = SDL_DisplayFormatAlpha(temp);
361 if (sdl_surface == NULL) {
362 std::stringstream msg;
363 msg << "Couldn't convert file '" << file << "' to display format";
364 throw std::runtime_error(msg.str());
367 if (use_alpha == false && !config->use_gl)
368 SDL_SetAlpha(sdl_surface, 0, 0);
370 SDL_FreeSurface(temp);
376 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
378 SDL_Surface* sdl_surface;
383 /* Save the alpha blending attributes */
384 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
385 saved_alpha = sdl_surf->format->alpha;
386 if ( (saved_flags & SDL_SRCALPHA)
389 SDL_SetAlpha(sdl_surf, 0, 0);
393 if(use_alpha == false && !config->use_gl)
394 sdl_surface = SDL_DisplayFormat(sdl_surf);
396 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
399 /* Restore the alpha blending attributes */
400 if ( (saved_flags & SDL_SRCALPHA)
403 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
407 if (sdl_surface == NULL) {
408 std::stringstream msg;
409 msg << "Can't convert surface to display format.";
410 throw std::runtime_error(msg.str());
413 if (use_alpha == false && !config->use_gl)
414 SDL_SetAlpha(sdl_surface, 0, 0);
420 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
422 SDL_Surface* sdl_surface
423 = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
424 screen->format->BitsPerPixel, screen->format->Rmask,
425 screen->format->Gmask, screen->format->Bmask, 0);
428 throw std::runtime_error("Can't create gradient surface");
431 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
432 top.red, top.green, top.blue));
434 float redstep = (float(bottom.red)-float(top.red)) / float(h);
435 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
436 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
442 for(float y = 0; y < h; y++) {
444 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
445 int(float(top.red) + redstep * y),
446 int(float(top.green) + greenstep * y),
447 int(float(top.blue) + bluestep * y)));
454 //---------------------------------------------------------------------------
456 SurfaceImpl::SurfaceImpl()
459 SurfaceImpl::~SurfaceImpl()
461 SDL_FreeSurface(sdl_surface);
464 SDL_Surface* SurfaceImpl::get_sdl_surface() const
469 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
471 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
472 create_gl(sdl_surface,&gl_texture);
478 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
480 sdl_surface = sdl_surface_from_file(file, use_alpha);
481 create_gl(sdl_surface,&gl_texture);
487 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_,
488 int w_, int h_, bool use_alpha_)
490 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
492 create_gl(sdl_surface, &gl_texture);
497 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient,
500 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
501 create_gl(sdl_surface, &gl_texture);
506 SurfaceOpenGL::~SurfaceOpenGL()
508 glDeleteTextures(1, &gl_texture);
512 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
519 w = power_of_two(surf->w);
520 h = power_of_two(surf->h),
522 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
523 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
524 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
526 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
527 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
530 /* Save the alpha blending attributes */
531 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
532 saved_alpha = surf->format->alpha;
533 if ( (saved_flags & SDL_SRCALPHA)
536 SDL_SetAlpha(surf, 0, 0);
539 SDL_BlitSurface(surf, 0, conv, 0);
541 /* Restore the alpha blending attributes */
542 if ( (saved_flags & SDL_SRCALPHA)
545 SDL_SetAlpha(surf, saved_flags, saved_alpha);
548 // We check all the pixels of the surface to figure out which
549 // internal format OpenGL should use for storing it, ie. if no alpha
550 // is present store in RGB instead of RGBA, this saves a few bytes
551 // of memory, but much more importantly it makes the game look
552 // *much* better in 16bit color mode
553 int internal_format = GL_RGB10_A2;
554 bool has_alpha = false;
556 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
557 for (int y = 0; y < surf->h; ++y)
558 for (int x = 0; x < surf->w; ++x)
560 if (buf[(conv->pitch*y + x*4) + 3] != 255)
569 internal_format = GL_RGB;
572 glGenTextures(1, &*tex);
573 glBindTexture(GL_TEXTURE_2D , *tex);
574 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
575 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
576 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
577 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
578 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
579 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
580 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
582 SDL_FreeSurface(conv);
586 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
588 float pw = power_of_two(w);
589 float ph = power_of_two(h);
591 if(effect & SEMI_TRANSPARENT)
594 glEnable(GL_TEXTURE_2D);
596 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
598 glColor4ub(alpha, alpha, alpha, alpha);
600 glBindTexture(GL_TEXTURE_2D, gl_texture);
604 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
607 glVertex2f((float)w+x, (float)h+y);
609 glTexCoord2f((float)w / pw, 0);
610 glVertex2f(x, (float)h+y);
612 glTexCoord2f((float)w / pw, (float)h / ph);
615 glTexCoord2f(0, (float)h / ph);
616 glVertex2f((float)w+x, y);
618 else if(effect & VERTICAL_FLIP)
621 glVertex2f(x, (float)h+y);
623 glTexCoord2f((float)w / pw, 0);
624 glVertex2f((float)w+x, (float)h+y);
626 glTexCoord2f((float)w / pw, (float)h / ph);
627 glVertex2f((float)w+x, y);
629 glTexCoord2f(0, (float)h / ph);
632 else if(effect & HORIZONTAL_FLIP)
635 glVertex2f((float)w+x, y);
637 glTexCoord2f((float)w / pw, 0);
640 glTexCoord2f((float)w / pw, (float)h / ph);
641 glVertex2f(x, (float)h+y);
643 glTexCoord2f(0, (float)h / ph);
644 glVertex2f((float)w+x, (float)h+y);
651 glTexCoord2f((float)w / pw, 0);
652 glVertex2f((float)w+x, y);
654 glTexCoord2f((float)w / pw, (float)h / ph);
655 glVertex2f((float)w+x, (float)h+y);
657 glTexCoord2f(0, (float)h / ph);
658 glVertex2f(x, (float)h+y);
662 glDisable(GL_TEXTURE_2D);
669 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
671 float pw = power_of_two(int(this->w));
672 float ph = power_of_two(int(this->h));
674 if(effect & SEMI_TRANSPARENT)
677 glBindTexture(GL_TEXTURE_2D, gl_texture);
680 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
682 glColor4ub(alpha, alpha, alpha, alpha);
684 glEnable(GL_TEXTURE_2D);
689 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
691 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
692 glVertex2f((float)w+x, (float)h+y);
694 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
695 glVertex2f(x, (float)h+y);
697 glTexCoord2f((float)(sx + w) / pw, sy / ph);
700 glTexCoord2f(sx / pw, sy / ph);
701 glVertex2f((float)w+x, y);
703 else if(effect & VERTICAL_FLIP)
705 glTexCoord2f(sx / pw, sy / ph);
708 glTexCoord2f((float)(sx + w) / pw, sy / ph);
711 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
712 glVertex2f(w +x, h+y);
714 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
717 else if(effect & HORIZONTAL_FLIP)
719 glTexCoord2f(sx / pw, sy / ph);
720 glVertex2f((float)w+x, y);
722 glTexCoord2f((float)(sx + w) / pw, sy / ph);
725 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
726 glVertex2f(x, (float)h+y);
728 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
729 glVertex2f((float)w+x, (float)h+y);
733 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
736 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
737 glVertex2f(w +x, h+y);
739 glTexCoord2f((float)(sx + w) / pw, sy / ph);
742 glTexCoord2f(sx / pw, sy / ph);
748 glDisable(GL_TEXTURE_2D);
755 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
757 float pw = power_of_two(sw);
758 float ph = power_of_two(sh);
760 if(effect & SEMI_TRANSPARENT)
763 glEnable(GL_TEXTURE_2D);
765 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
767 glColor4ub(alpha, alpha, alpha, alpha);
769 glBindTexture(GL_TEXTURE_2D, gl_texture);
773 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
776 glVertex2f((float)sw+x, (float)sh+y);
778 glTexCoord2f((float)w / pw, 0);
779 glVertex2f(x, (float)sh+y);
781 glTexCoord2f((float)w / pw, (float)h / ph);
784 glTexCoord2f(0, (float)h / ph);
785 glVertex2f((float)sw+x, y);
787 else if(effect & VERTICAL_FLIP)
790 glVertex2f(x, (float)sh+y);
792 glTexCoord2f((float)w / pw, 0);
793 glVertex2f((float)sw+x, (float)sh+y);
795 glTexCoord2f((float)w / pw, (float)h / ph);
796 glVertex2f((float)sw+x, y);
798 glTexCoord2f(0, (float)h / ph);
801 else if(effect & HORIZONTAL_FLIP)
804 glVertex2f((float)sw+x, y);
806 glTexCoord2f((float)w / pw, 0);
809 glTexCoord2f((float)w / pw, (float)h / ph);
810 glVertex2f(x, (float)sh+y);
812 glTexCoord2f(0, (float)h / ph);
813 glVertex2f((float)sw+x, (float)sh+y);
820 glTexCoord2f((float)w / pw, 0);
821 glVertex2f((float)sw+x, y);
823 glTexCoord2f((float)w / pw, (float)h / ph);
824 glVertex2f((float)sw+x, (float)sh+y);
826 glTexCoord2f(0, (float)h / ph);
827 glVertex2f(x, (float)sh+y);
831 glDisable(GL_TEXTURE_2D);
838 SurfaceOpenGL::apply_filter(int filter, Color color)
840 ::apply_filter_to_surface(sdl_surface, filter, color);
841 create_gl(sdl_surface,&gl_texture);
847 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
849 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
854 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
856 sdl_surface = sdl_surface_from_file(file, use_alpha);
861 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int _w, int _h,
864 sdl_surface = sdl_surface_part_from_file(file, x, y, _w, _h, use_alpha);
869 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient,
872 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
878 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
887 if(effect & SEMI_TRANSPARENT)
890 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
892 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
893 for(float sx = 0; sx < w; sx++)
894 for(float sy = 0; sy < h; sy++)
895 if(draw_part(sx, sy, x+(w-sx), y+(h-sy), 1, 1, alpha, NONE_EFFECT) == -2)
899 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
901 for(float sy = 0; sy < h; sy++)
902 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
906 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
908 for(float sx = 0; sx < w; sx++)
909 if(draw_part(sx, 0, x+(w-sx), y, 1, h, alpha, NONE_EFFECT) == -2)
916 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
917 to temp sur, blit the temp into the screen */
918 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
919 already have an alpha mask yet... */
921 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
922 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
923 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
924 sdl_surface->format->Bmask,
926 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
927 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
928 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
931 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
932 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
934 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
936 SDL_FreeSurface (sdl_surface_copy);
940 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
946 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
960 if(effect & SEMI_TRANSPARENT)
963 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
965 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
966 for(float sx_ = 0; sx_ < w; sx++)
967 for(float sy_ = 0; sy_ < h; sy++)
968 if(draw_part(sx_, sy_, sx+(w-sx_), sy+(h-sy_), 1, 1, alpha, NONE_EFFECT) == -2)
972 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
974 for(float sy_ = sy; sy_ < h; sy_++)
975 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
979 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
981 for(float sx_ = 0; sx_ < w; sx_++)
982 if(draw_part(sx_, 0, sx+(w-sx_), sy, 1, h, alpha, NONE_EFFECT) == -2)
989 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
990 to temp sur, blit the temp into the screen */
991 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
992 already have an alpha mask, yet... */
994 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
995 (int)w, (int)h, sdl_surface->format->BitsPerPixel,
996 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
997 sdl_surface->format->Bmask,
999 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
1000 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
1001 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
1004 SDL_BlitSurface(sdl_surface, &src, sdl_surface_copy, NULL);
1005 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
1007 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
1009 SDL_FreeSurface (sdl_surface_copy);
1013 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
1019 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
1028 if(effect & SEMI_TRANSPARENT)
1031 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
1032 sw, sh, sdl_surface->format->BitsPerPixel,
1033 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
1034 sdl_surface->format->Bmask,
1037 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
1038 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
1041 SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
1043 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
1044 SDL_FreeSurface(sdl_surface_copy);
1050 SurfaceSDL::apply_filter(int filter, Color color)
1052 ::apply_filter_to_surface(sdl_surface, filter, color);
1058 SurfaceSDL::~SurfaceSDL()