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 "physfs/physfs_sdl.h"
33 #include "video/surface.h"
34 #include "video/screen.h"
36 Surface::Surfaces Surface::surfaces;
38 extern SDL_Surface* screen;
40 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
41 : type(SURFACE), surface(0), use_alpha(use_alpha_),
42 x(0), y(0), w(0), h(0)
44 // Copy the given surface and make sure that it is not stored in
46 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
48 temp->format->BitsPerPixel,
54 throw std::runtime_error("No memory left for surface");
56 SDL_SetAlpha(temp,0,0);
57 SDL_BlitSurface(temp, NULL, surface, NULL);
60 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
61 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
64 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_,
65 int w_, int h_, bool use_alpha_)
66 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
67 x(x_), y(y_), w(w_), h(h_)
70 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_,
72 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
74 top_gradient = top_gradient_;
75 bottom_gradient = bottom_gradient_;
79 SurfaceData::~SurfaceData()
81 SDL_FreeSurface(surface);
88 return create_SurfaceOpenGL();
90 return create_SurfaceSDL();
94 SurfaceData::create_SurfaceSDL()
99 return new SurfaceSDL(file, use_alpha);
101 return new SurfaceSDL(file, x, y, w, h, use_alpha);
103 return new SurfaceSDL(surface, use_alpha);
105 return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
111 SurfaceData::create_SurfaceOpenGL()
116 return new SurfaceOpenGL(file, use_alpha);
118 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
120 return new SurfaceOpenGL(surface, use_alpha);
122 return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
128 /* Quick utility function for texture creation */
129 static int power_of_two(int input)
133 while ( value < input )
140 Surface::Surface(SDL_Surface* surf, bool use_alpha)
141 : impl(0), 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 : impl(0), 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 : impl(0), 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 : impl(0), data(top_background, bottom_background, w_, h_), w(0), h(0)
179 impl = data.create();
185 surfaces.push_back(this);
192 impl = data.create();
197 for(std::vector<SurfaceData::Filter>::iterator i =
198 data.applied_filters.begin(); i != data.applied_filters.end();
200 impl->apply_filter(i->type, i->color);
204 void Surface::apply_filter(int filter, Color color)
206 impl->apply_filter(filter, color);
208 SurfaceData::Filter apply_filter;
209 apply_filter.type = filter;
210 apply_filter.color = color;
211 data.applied_filters.push_back(apply_filter);
218 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
227 printf("Error: Surface freed twice!!!\n");
229 surfaces.remove(this);
234 Surface::reload_all()
236 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
243 Surface::debug_check()
245 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
247 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
248 (*i)->data.file.c_str());
253 apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
255 if(filter == HORIZONTAL_FLIP_FILTER) {
256 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
257 SDL_BlitSurface(surface, NULL, sur_copy, NULL);
258 SDL_SetAlpha(sur_copy,0,0);
263 src.h = dst.h = sur_copy->h;
264 for(int x = 0; x < sur_copy->w; x++)
266 src.x = x; dst.x = sur_copy->w-1 - x;
267 SDL_BlitSurface(sur_copy, &src, surface, &dst);
270 SDL_FreeSurface(sur_copy);
271 } else if(filter == MASK_FILTER) {
272 SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
276 SDL_LockSurface(sur_copy);
277 for(int x = 0; x < sur_copy->w; x++)
278 for(int y = 0; y < sur_copy->h; y++) {
279 SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
281 putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
284 SDL_UnlockSurface(sur_copy);
286 SDL_BlitSurface(sur_copy, NULL, surface, NULL);
287 SDL_FreeSurface(sur_copy);
292 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
295 SDL_Surface * sdl_surface;
299 temp = IMG_Load_RW(get_physfs_SDLRWops(file), true);
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_RW(get_physfs_SDLRWops(file), true);
350 std::stringstream msg;
351 msg << "Couldn't load file '" << file << "': " << SDL_GetError();
352 throw std::runtime_error(msg.str());
355 if(use_alpha == false && !config->use_gl)
356 sdl_surface = SDL_DisplayFormat(temp);
358 sdl_surface = SDL_DisplayFormatAlpha(temp);
360 if (sdl_surface == NULL) {
361 std::stringstream msg;
362 msg << "Couldn't convert file '" << file << "' to display format";
363 throw std::runtime_error(msg.str());
366 if (use_alpha == false && !config->use_gl)
367 SDL_SetAlpha(sdl_surface, 0, 0);
369 SDL_FreeSurface(temp);
375 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
377 SDL_Surface* sdl_surface;
382 /* Save the alpha blending attributes */
383 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
384 saved_alpha = sdl_surf->format->alpha;
385 if ( (saved_flags & SDL_SRCALPHA)
388 SDL_SetAlpha(sdl_surf, 0, 0);
392 if(use_alpha == false && !config->use_gl)
393 sdl_surface = SDL_DisplayFormat(sdl_surf);
395 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
398 /* Restore the alpha blending attributes */
399 if ( (saved_flags & SDL_SRCALPHA)
402 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
406 if (sdl_surface == NULL) {
407 std::stringstream msg;
408 msg << "Can't convert surface to display format.";
409 throw std::runtime_error(msg.str());
412 if (use_alpha == false && !config->use_gl)
413 SDL_SetAlpha(sdl_surface, 0, 0);
419 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
421 SDL_Surface* sdl_surface
422 = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
423 screen->format->BitsPerPixel, screen->format->Rmask,
424 screen->format->Gmask, screen->format->Bmask, 0);
427 throw std::runtime_error("Can't create gradient surface");
430 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
431 top.red, top.green, top.blue));
433 float redstep = (float(bottom.red)-float(top.red)) / float(h);
434 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
435 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
441 for(float y = 0; y < h; y++) {
443 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
444 int(float(top.red) + redstep * y),
445 int(float(top.green) + greenstep * y),
446 int(float(top.blue) + bluestep * y)));
453 //---------------------------------------------------------------------------
455 SurfaceImpl::SurfaceImpl()
459 SurfaceImpl::~SurfaceImpl()
462 SDL_FreeSurface(sdl_surface);
465 SDL_Surface* SurfaceImpl::get_sdl_surface() const
470 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
472 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
473 create_gl(sdl_surface,&gl_texture);
479 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
481 sdl_surface = sdl_surface_from_file(file, use_alpha);
482 create_gl(sdl_surface,&gl_texture);
488 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_,
489 int w_, int h_, bool use_alpha_)
491 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
493 create_gl(sdl_surface, &gl_texture);
498 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient,
501 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
502 create_gl(sdl_surface, &gl_texture);
507 SurfaceOpenGL::~SurfaceOpenGL()
509 glDeleteTextures(1, &gl_texture);
513 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
520 w = power_of_two(surf->w);
521 h = power_of_two(surf->h),
523 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
524 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
525 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
527 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
528 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
531 /* Save the alpha blending attributes */
532 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
533 saved_alpha = surf->format->alpha;
534 if ( (saved_flags & SDL_SRCALPHA)
537 SDL_SetAlpha(surf, 0, 0);
540 SDL_BlitSurface(surf, 0, conv, 0);
542 /* Restore the alpha blending attributes */
543 if ( (saved_flags & SDL_SRCALPHA)
546 SDL_SetAlpha(surf, saved_flags, saved_alpha);
549 // We check all the pixels of the surface to figure out which
550 // internal format OpenGL should use for storing it, ie. if no alpha
551 // is present store in RGB instead of RGBA, this saves a few bytes
552 // of memory, but much more importantly it makes the game look
553 // *much* better in 16bit color mode
554 int internal_format = GL_RGBA;
555 bool has_alpha = false;
557 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
558 for (int y = 0; y < surf->h; ++y)
559 for (int x = 0; x < surf->w; ++x)
561 if (buf[(conv->pitch*y + x*4) + 3] != 255)
570 internal_format = GL_RGB;
573 glGenTextures(1, &*tex);
574 glBindTexture(GL_TEXTURE_2D , *tex);
575 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
576 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
577 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
578 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
579 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
580 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
581 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
583 SDL_FreeSurface(conv);
587 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
589 float pw = power_of_two(w);
590 float ph = power_of_two(h);
592 if(effect & SEMI_TRANSPARENT)
595 glEnable(GL_TEXTURE_2D);
597 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
599 glColor4ub(alpha, alpha, alpha, alpha);
601 glBindTexture(GL_TEXTURE_2D, gl_texture);
605 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
608 glVertex2f((float)w+x, (float)h+y);
610 glTexCoord2f((float)w / pw, 0);
611 glVertex2f(x, (float)h+y);
613 glTexCoord2f((float)w / pw, (float)h / ph);
616 glTexCoord2f(0, (float)h / ph);
617 glVertex2f((float)w+x, y);
619 else if(effect & VERTICAL_FLIP)
622 glVertex2f(x, (float)h+y);
624 glTexCoord2f((float)w / pw, 0);
625 glVertex2f((float)w+x, (float)h+y);
627 glTexCoord2f((float)w / pw, (float)h / ph);
628 glVertex2f((float)w+x, y);
630 glTexCoord2f(0, (float)h / ph);
633 else if(effect & HORIZONTAL_FLIP)
636 glVertex2f((float)w+x, y);
638 glTexCoord2f((float)w / pw, 0);
641 glTexCoord2f((float)w / pw, (float)h / ph);
642 glVertex2f(x, (float)h+y);
644 glTexCoord2f(0, (float)h / ph);
645 glVertex2f((float)w+x, (float)h+y);
652 glTexCoord2f((float)w / pw, 0);
653 glVertex2f((float)w+x, y);
655 glTexCoord2f((float)w / pw, (float)h / ph);
656 glVertex2f((float)w+x, (float)h+y);
658 glTexCoord2f(0, (float)h / ph);
659 glVertex2f(x, (float)h+y);
663 glDisable(GL_TEXTURE_2D);
670 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
672 float pw = power_of_two(int(this->w));
673 float ph = power_of_two(int(this->h));
675 if(effect & SEMI_TRANSPARENT)
678 glBindTexture(GL_TEXTURE_2D, gl_texture);
681 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
683 glColor4ub(alpha, alpha, alpha, alpha);
685 glEnable(GL_TEXTURE_2D);
690 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
692 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
693 glVertex2f((float)w+x, (float)h+y);
695 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
696 glVertex2f(x, (float)h+y);
698 glTexCoord2f((float)(sx + w) / pw, sy / ph);
701 glTexCoord2f(sx / pw, sy / ph);
702 glVertex2f((float)w+x, y);
704 else if(effect & VERTICAL_FLIP)
706 glTexCoord2f(sx / pw, sy / ph);
709 glTexCoord2f((float)(sx + w) / pw, sy / ph);
712 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
713 glVertex2f(w +x, h+y);
715 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
718 else if(effect & HORIZONTAL_FLIP)
720 glTexCoord2f(sx / pw, sy / ph);
721 glVertex2f((float)w+x, y);
723 glTexCoord2f((float)(sx + w) / pw, sy / ph);
726 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
727 glVertex2f(x, (float)h+y);
729 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
730 glVertex2f((float)w+x, (float)h+y);
734 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
737 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
738 glVertex2f(w +x, h+y);
740 glTexCoord2f((float)(sx + w) / pw, sy / ph);
743 glTexCoord2f(sx / pw, sy / ph);
749 glDisable(GL_TEXTURE_2D);
756 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
758 float pw = power_of_two(sw);
759 float ph = power_of_two(sh);
761 if(effect & SEMI_TRANSPARENT)
764 glEnable(GL_TEXTURE_2D);
766 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
768 glColor4ub(alpha, alpha, alpha, alpha);
770 glBindTexture(GL_TEXTURE_2D, gl_texture);
774 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
777 glVertex2f((float)sw+x, (float)sh+y);
779 glTexCoord2f((float)w / pw, 0);
780 glVertex2f(x, (float)sh+y);
782 glTexCoord2f((float)w / pw, (float)h / ph);
785 glTexCoord2f(0, (float)h / ph);
786 glVertex2f((float)sw+x, y);
788 else if(effect & VERTICAL_FLIP)
791 glVertex2f(x, (float)sh+y);
793 glTexCoord2f((float)w / pw, 0);
794 glVertex2f((float)sw+x, (float)sh+y);
796 glTexCoord2f((float)w / pw, (float)h / ph);
797 glVertex2f((float)sw+x, y);
799 glTexCoord2f(0, (float)h / ph);
802 else if(effect & HORIZONTAL_FLIP)
805 glVertex2f((float)sw+x, y);
807 glTexCoord2f((float)w / pw, 0);
810 glTexCoord2f((float)w / pw, (float)h / ph);
811 glVertex2f(x, (float)sh+y);
813 glTexCoord2f(0, (float)h / ph);
814 glVertex2f((float)sw+x, (float)sh+y);
821 glTexCoord2f((float)w / pw, 0);
822 glVertex2f((float)sw+x, y);
824 glTexCoord2f((float)w / pw, (float)h / ph);
825 glVertex2f((float)sw+x, (float)sh+y);
827 glTexCoord2f(0, (float)h / ph);
828 glVertex2f(x, (float)sh+y);
832 glDisable(GL_TEXTURE_2D);
839 SurfaceOpenGL::apply_filter(int filter, Color color)
841 ::apply_filter_to_surface(sdl_surface, filter, color);
842 create_gl(sdl_surface,&gl_texture);
848 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
850 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
855 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
857 sdl_surface = sdl_surface_from_file(file, use_alpha);
862 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int _w, int _h,
865 sdl_surface = sdl_surface_part_from_file(file, x, y, _w, _h, use_alpha);
870 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient,
873 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
879 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
888 if(effect & SEMI_TRANSPARENT)
891 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
893 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
894 for(float sx = 0; sx < w; sx++)
895 for(float sy = 0; sy < h; sy++)
896 if(draw_part(sx, sy, x+(w-sx), y+(h-sy), 1, 1, alpha, NONE_EFFECT) == -2)
900 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
902 for(float sy = 0; sy < h; sy++)
903 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
907 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
909 for(float sx = 0; sx < w; sx++)
910 if(draw_part(sx, 0, x+(w-sx), y, 1, h, alpha, NONE_EFFECT) == -2)
917 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
918 to temp sur, blit the temp into the screen */
919 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
920 already have an alpha mask yet... */
922 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
923 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
924 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
925 sdl_surface->format->Bmask,
927 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
928 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
929 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
932 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
933 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
935 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
937 SDL_FreeSurface (sdl_surface_copy);
941 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
947 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
961 if(effect & SEMI_TRANSPARENT)
964 if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP)
966 // FIXME: this hack is damn slow. Just keep it cause it isn't that used.
967 for(float sx_ = 0; sx_ < w; sx++)
968 for(float sy_ = 0; sy_ < h; sy++)
969 if(draw_part(sx_, sy_, sx+(w-sx_), sy+(h-sy_), 1, 1, alpha, NONE_EFFECT) == -2)
973 else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
975 for(float sy_ = sy; sy_ < h; sy_++)
976 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
980 else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack
982 for(float sx_ = 0; sx_ < w; sx_++)
983 if(draw_part(sx_, 0, sx+(w-sx_), sy, 1, h, alpha, NONE_EFFECT) == -2)
990 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
991 to temp sur, blit the temp into the screen */
992 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
993 already have an alpha mask, yet... */
995 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
996 (int)w, (int)h, sdl_surface->format->BitsPerPixel,
997 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
998 sdl_surface->format->Bmask,
1000 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
1001 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
1002 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
1005 SDL_BlitSurface(sdl_surface, &src, sdl_surface_copy, NULL);
1006 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
1008 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
1010 SDL_FreeSurface (sdl_surface_copy);
1014 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
1020 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
1029 if(effect & SEMI_TRANSPARENT)
1032 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
1033 sw, sh, sdl_surface->format->BitsPerPixel,
1034 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
1035 sdl_surface->format->Bmask,
1038 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
1039 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
1042 SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
1044 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
1045 SDL_FreeSurface(sdl_surface_copy);
1051 SurfaceSDL::apply_filter(int filter, Color color)
1053 ::apply_filter_to_surface(sdl_surface, filter, color);
1059 SurfaceSDL::~SurfaceSDL()