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"
34 #include "app/globals.h"
35 #include "app/setup.h"
37 using namespace SuperTux;
39 Surface::Surfaces Surface::surfaces;
41 extern SDL_Surface* screen;
43 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
44 : type(SURFACE), surface(0), use_alpha(use_alpha_),
45 x(0), y(0), w(0), h(0)
47 // Copy the given surface and make sure that it is not stored in
49 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
51 temp->format->BitsPerPixel,
57 throw std::runtime_error("No memory left for surface");
59 SDL_SetAlpha(temp,0,0);
60 SDL_BlitSurface(temp, NULL, surface, NULL);
63 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
64 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
67 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_,
68 int w_, int h_, bool use_alpha_)
69 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
70 x(x_), y(y_), w(w_), h(h_)
73 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_,
75 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
77 top_gradient = top_gradient_;
78 bottom_gradient = bottom_gradient_;
82 SurfaceData::~SurfaceData()
84 SDL_FreeSurface(surface);
91 return create_SurfaceOpenGL();
93 return create_SurfaceSDL();
97 SurfaceData::create_SurfaceSDL()
102 return new SurfaceSDL(file, use_alpha);
104 return new SurfaceSDL(file, x, y, w, h, use_alpha);
106 return new SurfaceSDL(surface, use_alpha);
108 return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
114 SurfaceData::create_SurfaceOpenGL()
119 return new SurfaceOpenGL(file, use_alpha);
121 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
123 return new SurfaceOpenGL(surface, use_alpha);
125 return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
131 /* Quick utility function for texture creation */
132 static int power_of_two(int input)
136 while ( value < input )
143 Surface::Surface(SDL_Surface* surf, bool use_alpha)
144 : data(surf, use_alpha), w(0), h(0)
146 impl = data.create();
152 surfaces.push_back(this);
155 Surface::Surface(const std::string& file, bool use_alpha)
156 : data(file, use_alpha), w(0), h(0)
158 impl = data.create();
164 surfaces.push_back(this);
167 Surface::Surface(const std::string& file, int x, int y, int w_, int h_, bool use_alpha)
168 : data(file, x, y, w_, h_, use_alpha), w(0), h(0)
170 impl = data.create();
176 surfaces.push_back(this);
179 Surface::Surface(Color top_background, Color bottom_background, int w_, int h_)
180 : data(top_background, bottom_background, w_, h_), w(0), h(0)
182 impl = data.create();
188 surfaces.push_back(this);
195 impl = data.create();
200 for(std::vector<SurfaceData::Filter>::iterator i =
201 data.applied_filters.begin(); i != data.applied_filters.end();
203 impl->apply_filter(i->type, i->color);
207 void Surface::apply_filter(int filter, Color color)
209 impl->apply_filter(filter, color);
211 SurfaceData::Filter apply_filter;
212 apply_filter.type = filter;
213 apply_filter.color = color;
214 data.applied_filters.push_back(apply_filter);
221 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
230 printf("Error: Surface freed twice!!!\n");
232 surfaces.remove(this);
237 Surface::reload_all()
239 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
246 Surface::debug_check()
248 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
250 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
251 (*i)->data.file.c_str());
256 apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
258 if(filter == HORIZONTAL_FLIP_FILTER) {
259 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
260 SDL_BlitSurface(surface, NULL, sur_copy, NULL);
261 SDL_SetAlpha(sur_copy,0,0);
266 src.h = dst.h = sur_copy->h;
267 for(int x = 0; x < sur_copy->w; x++)
269 src.x = x; dst.x = sur_copy->w-1 - x;
270 SDL_BlitSurface(sur_copy, &src, surface, &dst);
273 SDL_FreeSurface(sur_copy);
274 } else if(filter == MASK_FILTER) {
275 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
279 SDL_LockSurface(sur_copy);
280 for(int x = 0; x < sur_copy->w; x++)
281 for(int y = 0; y < sur_copy->h; y++) {
282 SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
284 putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
287 SDL_UnlockSurface(sur_copy);
289 SDL_BlitSurface(sur_copy, NULL, surface, NULL);
290 SDL_FreeSurface(sur_copy);
295 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
298 SDL_Surface * sdl_surface;
302 temp = IMG_Load(file.c_str());
305 std::stringstream msg;
306 msg << "Couldn't load '" << file << "': " << SDL_GetError();
307 throw std::runtime_error(msg.str());
310 /* Set source rectangle for conv: */
317 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, temp->format->BitsPerPixel,
321 temp->format->Amask);
323 SDL_SetAlpha(temp,0,0);
325 SDL_BlitSurface(temp, &src, conv, NULL);
326 if(use_alpha == false && !config->use_gl)
327 sdl_surface = SDL_DisplayFormat(conv);
329 sdl_surface = SDL_DisplayFormatAlpha(conv);
331 if (sdl_surface == NULL) {
332 std::stringstream msg;
333 msg << "Can't convert file '" << file << "' to display format.";
334 throw std::runtime_error(msg.str());
337 if (use_alpha == false && !config->use_gl)
338 SDL_SetAlpha(sdl_surface, 0, 0);
340 SDL_FreeSurface(temp);
341 SDL_FreeSurface(conv);
347 sdl_surface_from_file(const std::string& file, bool use_alpha)
349 SDL_Surface* sdl_surface;
352 temp = IMG_Load(file.c_str());
355 std::stringstream msg;
356 msg << "Couldn't load file '" << file << "': " << SDL_GetError();
357 throw std::runtime_error(msg.str());
360 if(use_alpha == false && !config->use_gl)
361 sdl_surface = SDL_DisplayFormat(temp);
363 sdl_surface = SDL_DisplayFormatAlpha(temp);
365 if (sdl_surface == NULL) {
366 std::stringstream msg;
367 msg << "Couldn't convert file '" << file << "' to display format";
368 throw std::runtime_error(msg.str());
371 if (use_alpha == false && !config->use_gl)
372 SDL_SetAlpha(sdl_surface, 0, 0);
374 SDL_FreeSurface(temp);
380 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 && !config->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 std::stringstream msg;
413 msg << "Can't convert surface to display format.";
414 throw std::runtime_error(msg.str());
417 if (use_alpha == false && !config->use_gl)
418 SDL_SetAlpha(sdl_surface, 0, 0);
424 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
426 SDL_Surface* sdl_surface
427 = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
428 screen->format->BitsPerPixel, screen->format->Rmask,
429 screen->format->Gmask, screen->format->Bmask, 0);
432 throw std::runtime_error("Can't create gradient surface");
435 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
436 top.red, top.green, top.blue));
438 float redstep = (float(bottom.red)-float(top.red)) / float(h);
439 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
440 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
446 for(float y = 0; y < h; y++) {
448 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
449 int(float(top.red) + redstep * y),
450 int(float(top.green) + greenstep * y),
451 int(float(top.blue) + bluestep * y)));
458 //---------------------------------------------------------------------------
460 SurfaceImpl::SurfaceImpl()
463 SurfaceImpl::~SurfaceImpl()
465 SDL_FreeSurface(sdl_surface);
468 SDL_Surface* SurfaceImpl::get_sdl_surface() const
473 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
475 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
476 create_gl(sdl_surface,&gl_texture);
482 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
484 sdl_surface = sdl_surface_from_file(file, use_alpha);
485 create_gl(sdl_surface,&gl_texture);
491 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_,
492 int w_, int h_, bool use_alpha_)
494 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
496 create_gl(sdl_surface, &gl_texture);
501 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient,
504 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
505 create_gl(sdl_surface, &gl_texture);
510 SurfaceOpenGL::~SurfaceOpenGL()
512 glDeleteTextures(1, &gl_texture);
516 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
523 w = power_of_two(surf->w);
524 h = power_of_two(surf->h),
526 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
527 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
528 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
530 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
531 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
534 /* Save the alpha blending attributes */
535 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
536 saved_alpha = surf->format->alpha;
537 if ( (saved_flags & SDL_SRCALPHA)
540 SDL_SetAlpha(surf, 0, 0);
543 SDL_BlitSurface(surf, 0, conv, 0);
545 /* Restore the alpha blending attributes */
546 if ( (saved_flags & SDL_SRCALPHA)
549 SDL_SetAlpha(surf, saved_flags, saved_alpha);
552 // We check all the pixels of the surface to figure out which
553 // internal format OpenGL should use for storing it, ie. if no alpha
554 // is present store in RGB instead of RGBA, this saves a few bytes
555 // of memory, but much more importantly it makes the game look
556 // *much* better in 16bit color mode
557 int internal_format = GL_RGB10_A2;
558 bool has_alpha = false;
560 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
561 for (int y = 0; y < surf->h; ++y)
562 for (int x = 0; x < surf->w; ++x)
564 if (buf[(conv->pitch*y + x*4) + 3] != 255)
573 internal_format = GL_RGB;
576 glGenTextures(1, &*tex);
577 glBindTexture(GL_TEXTURE_2D , *tex);
578 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
579 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
580 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
581 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
582 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
583 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
584 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
586 SDL_FreeSurface(conv);
590 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
592 float pw = power_of_two(w);
593 float ph = power_of_two(h);
595 if(effect & SEMI_TRANSPARENT)
598 glEnable(GL_TEXTURE_2D);
600 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
602 glColor4ub(alpha, alpha, alpha, alpha);
604 glBindTexture(GL_TEXTURE_2D, gl_texture);
608 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
611 glVertex2f((float)w+x, (float)h+y);
613 glTexCoord2f((float)w / pw, 0);
614 glVertex2f(x, (float)h+y);
616 glTexCoord2f((float)w / pw, (float)h / ph);
619 glTexCoord2f(0, (float)h / ph);
620 glVertex2f((float)w+x, y);
622 else if(effect & VERTICAL_FLIP)
625 glVertex2f(x, (float)h+y);
627 glTexCoord2f((float)w / pw, 0);
628 glVertex2f((float)w+x, (float)h+y);
630 glTexCoord2f((float)w / pw, (float)h / ph);
631 glVertex2f((float)w+x, y);
633 glTexCoord2f(0, (float)h / ph);
636 else if(effect & HORIZONTAL_FLIP)
639 glVertex2f((float)w+x, y);
641 glTexCoord2f((float)w / pw, 0);
644 glTexCoord2f((float)w / pw, (float)h / ph);
645 glVertex2f(x, (float)h+y);
647 glTexCoord2f(0, (float)h / ph);
648 glVertex2f((float)w+x, (float)h+y);
655 glTexCoord2f((float)w / pw, 0);
656 glVertex2f((float)w+x, y);
658 glTexCoord2f((float)w / pw, (float)h / ph);
659 glVertex2f((float)w+x, (float)h+y);
661 glTexCoord2f(0, (float)h / ph);
662 glVertex2f(x, (float)h+y);
666 glDisable(GL_TEXTURE_2D);
673 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
675 float pw = power_of_two(int(this->w));
676 float ph = power_of_two(int(this->h));
678 if(effect & SEMI_TRANSPARENT)
681 glBindTexture(GL_TEXTURE_2D, gl_texture);
684 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
686 glColor4ub(alpha, alpha, alpha, alpha);
688 glEnable(GL_TEXTURE_2D);
693 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
695 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
696 glVertex2f((float)w+x, (float)h+y);
698 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
699 glVertex2f(x, (float)h+y);
701 glTexCoord2f((float)(sx + w) / pw, sy / ph);
704 glTexCoord2f(sx / pw, sy / ph);
705 glVertex2f((float)w+x, y);
707 else if(effect & VERTICAL_FLIP)
709 glTexCoord2f(sx / pw, sy / ph);
712 glTexCoord2f((float)(sx + w) / pw, sy / ph);
715 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
716 glVertex2f(w +x, h+y);
718 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
721 else if(effect & HORIZONTAL_FLIP)
723 glTexCoord2f(sx / pw, sy / ph);
724 glVertex2f((float)w+x, y);
726 glTexCoord2f((float)(sx + w) / pw, sy / ph);
729 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
730 glVertex2f(x, (float)h+y);
732 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
733 glVertex2f((float)w+x, (float)h+y);
737 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
740 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
741 glVertex2f(w +x, h+y);
743 glTexCoord2f((float)(sx + w) / pw, sy / ph);
746 glTexCoord2f(sx / pw, sy / ph);
752 glDisable(GL_TEXTURE_2D);
759 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
761 float pw = power_of_two(sw);
762 float ph = power_of_two(sh);
764 if(effect & SEMI_TRANSPARENT)
767 glEnable(GL_TEXTURE_2D);
769 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
771 glColor4ub(alpha, alpha, alpha, alpha);
773 glBindTexture(GL_TEXTURE_2D, gl_texture);
777 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
780 glVertex2f((float)sw+x, (float)sh+y);
782 glTexCoord2f((float)w / pw, 0);
783 glVertex2f(x, (float)sh+y);
785 glTexCoord2f((float)w / pw, (float)h / ph);
788 glTexCoord2f(0, (float)h / ph);
789 glVertex2f((float)sw+x, y);
791 else if(effect & VERTICAL_FLIP)
794 glVertex2f(x, (float)sh+y);
796 glTexCoord2f((float)w / pw, 0);
797 glVertex2f((float)sw+x, (float)sh+y);
799 glTexCoord2f((float)w / pw, (float)h / ph);
800 glVertex2f((float)sw+x, y);
802 glTexCoord2f(0, (float)h / ph);
805 else if(effect & HORIZONTAL_FLIP)
808 glVertex2f((float)sw+x, y);
810 glTexCoord2f((float)w / pw, 0);
813 glTexCoord2f((float)w / pw, (float)h / ph);
814 glVertex2f(x, (float)sh+y);
816 glTexCoord2f(0, (float)h / ph);
817 glVertex2f((float)sw+x, (float)sh+y);
824 glTexCoord2f((float)w / pw, 0);
825 glVertex2f((float)sw+x, y);
827 glTexCoord2f((float)w / pw, (float)h / ph);
828 glVertex2f((float)sw+x, (float)sh+y);
830 glTexCoord2f(0, (float)h / ph);
831 glVertex2f(x, (float)sh+y);
835 glDisable(GL_TEXTURE_2D);
842 SurfaceOpenGL::apply_filter(int filter, Color color)
844 ::apply_filter_to_surface(sdl_surface, filter, color);
845 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,
868 sdl_surface = sdl_surface_part_from_file(file, x, y, _w, _h, use_alpha);
873 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient,
876 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
882 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
891 if(effect & SEMI_TRANSPARENT)
894 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
896 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
897 for(float sx = 0; sx < w; sx++)
898 for(float sy = 0; sy < h; sy++)
899 if(draw_part(sx, sy, x+(w-sx), y+(h-sy), 1, 1, alpha, NONE_EFFECT) == -2)
903 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
905 for(float sy = 0; sy < h; sy++)
906 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
910 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
912 for(float sx = 0; sx < w; sx++)
913 if(draw_part(sx, 0, x+(w-sx), y, 1, h, alpha, NONE_EFFECT) == -2)
920 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
921 to temp sur, blit the temp into the screen */
922 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
923 already have an alpha mask yet... */
925 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
926 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
927 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
928 sdl_surface->format->Bmask,
930 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
931 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
932 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
935 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
936 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
938 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
940 SDL_FreeSurface (sdl_surface_copy);
944 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
950 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
964 if(effect & SEMI_TRANSPARENT)
967 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
969 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
970 for(float sx_ = 0; sx_ < w; sx++)
971 for(float sy_ = 0; sy_ < h; sy++)
972 if(draw_part(sx_, sy_, sx+(w-sx_), sy+(h-sy_), 1, 1, alpha, NONE_EFFECT) == -2)
976 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
978 for(float sy_ = sy; sy_ < h; sy_++)
979 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
983 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
985 for(float sx_ = 0; sx_ < w; sx_++)
986 if(draw_part(sx_, 0, sx+(w-sx_), sy, 1, h, alpha, NONE_EFFECT) == -2)
993 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
994 to temp sur, blit the temp into the screen */
995 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
996 already have an alpha mask, yet... */
998 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
999 (int)w, (int)h, sdl_surface->format->BitsPerPixel,
1000 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
1001 sdl_surface->format->Bmask,
1003 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
1004 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
1005 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
1008 SDL_BlitSurface(sdl_surface, &src, sdl_surface_copy, NULL);
1009 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
1011 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
1013 SDL_FreeSurface (sdl_surface_copy);
1017 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
1023 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
1032 if(effect & SEMI_TRANSPARENT)
1035 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
1036 sw, sh, sdl_surface->format->BitsPerPixel,
1037 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
1038 sdl_surface->format->Bmask,
1041 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
1042 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
1045 SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
1047 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
1048 SDL_FreeSurface(sdl_surface_copy);
1054 SurfaceSDL::apply_filter(int filter, Color color)
1056 ::apply_filter_to_surface(sdl_surface, filter, color);
1062 SurfaceSDL::~SurfaceSDL()