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 "../app/globals.h"
30 #include "../app/setup.h"
32 using namespace SuperTux;
34 Surface::Surfaces Surface::surfaces;
36 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
37 : type(SURFACE), surface(0), use_alpha(use_alpha_)
39 // Copy the given surface and make sure that it is not stored in
41 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
43 temp->format->BitsPerPixel,
49 Termination::abort("No memory left.", "");
50 SDL_SetAlpha(temp,0,0);
51 SDL_BlitSurface(temp, NULL, surface, NULL);
54 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
55 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
58 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
59 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
60 x(x_), y(y_), w(w_), h(h_)
63 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_)
64 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
66 top_gradient = top_gradient_;
67 bottom_gradient = bottom_gradient_;
71 SurfaceData::~SurfaceData()
73 SDL_FreeSurface(surface);
81 return create_SurfaceOpenGL();
83 return create_SurfaceSDL();
85 return create_SurfaceSDL();
90 SurfaceData::create_SurfaceSDL()
95 return new SurfaceSDL(file, use_alpha);
97 return new SurfaceSDL(file, x, y, w, h, use_alpha);
99 return new SurfaceSDL(surface, use_alpha);
101 return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
107 SurfaceData::create_SurfaceOpenGL()
113 return new SurfaceOpenGL(file, use_alpha);
115 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
117 return new SurfaceOpenGL(surface, use_alpha);
119 return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
126 /* Quick utility function for texture creation */
127 static int power_of_two(int input)
131 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();
203 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
212 printf("Error: Surface freed twice!!!\n");
214 surfaces.remove(this);
219 Surface::reload_all()
221 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
228 Surface::debug_check()
230 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
232 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
233 (*i)->data.file.c_str());
238 Surface::resize(int w_, int h_)
244 if (impl->resize(w_,h_) == -2)
250 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
253 SDL_Surface * sdl_surface;
257 temp = IMG_Load(file.c_str());
260 Termination::abort("Can't load", file);
262 /* Set source rectangle for conv: */
269 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
273 temp->format->Amask);
275 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
276 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
279 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
282 SDL_SetAlpha(temp,0,0);
284 SDL_BlitSurface(temp, &src, conv, NULL);
285 if(use_alpha == false && !use_gl)
286 sdl_surface = SDL_DisplayFormat(conv);
288 sdl_surface = SDL_DisplayFormatAlpha(conv);
290 if (sdl_surface == NULL)
291 Termination::abort("Can't covert to display format", file);
293 if (use_alpha == false && !use_gl)
294 SDL_SetAlpha(sdl_surface, 0, 0);
296 SDL_FreeSurface(temp);
297 SDL_FreeSurface(conv);
303 sdl_surface_from_file(const std::string& file, bool use_alpha)
305 SDL_Surface* sdl_surface;
308 temp = IMG_Load(file.c_str());
311 Termination::abort("Can't load", file);
313 if(use_alpha == false && !use_gl)
314 sdl_surface = SDL_DisplayFormat(temp);
316 sdl_surface = SDL_DisplayFormatAlpha(temp);
318 if (sdl_surface == NULL)
319 Termination::abort("Can't covert to display format", file);
321 if (use_alpha == false && !use_gl)
322 SDL_SetAlpha(sdl_surface, 0, 0);
324 SDL_FreeSurface(temp);
330 SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
332 SDL_Surface* sdl_surface;
336 /* Save the alpha blending attributes */
337 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
338 saved_alpha = sdl_surf->format->alpha;
339 if ( (saved_flags & SDL_SRCALPHA)
342 SDL_SetAlpha(sdl_surf, 0, 0);
345 if(use_alpha == false && !use_gl)
346 sdl_surface = SDL_DisplayFormat(sdl_surf);
348 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
350 /* Restore the alpha blending attributes */
351 if ( (saved_flags & SDL_SRCALPHA)
354 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
357 if (sdl_surface == NULL)
358 Termination::abort("Can't covert to display format", "SURFACE");
360 if (use_alpha == false && !use_gl)
361 SDL_SetAlpha(sdl_surface, 0, 0);
367 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
369 SDL_Surface* sdl_surface;
371 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
372 screen->format->BitsPerPixel, screen->format->Rmask,
373 screen->format->Gmask, screen->format->Bmask, 0);
375 if(sdl_surface == NULL)
376 Termination::abort("Cannot create surface for the gradient", "SURFACE");
380 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
381 top.red, top.green, top.blue));
385 float redstep = (float(bottom.red)-float(top.red)) / float(h);
386 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
387 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
393 for(float y = 0; y < h; y++)
396 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
397 int(float(top.red) + redstep * y),
398 int(float(top.green) + greenstep * y),
399 int(float(top.blue) + bluestep * y)));
406 //---------------------------------------------------------------------------
408 SurfaceImpl::SurfaceImpl()
411 SurfaceImpl::~SurfaceImpl()
413 SDL_FreeSurface(sdl_surface);
416 SDL_Surface* SurfaceImpl::get_sdl_surface() const
421 int SurfaceImpl::resize(int w_, int h_)
430 int ret = SDL_SoftStretch(sdl_surface, NULL,
436 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
438 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
439 create_gl(sdl_surface,&gl_texture);
445 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
447 sdl_surface = sdl_surface_from_file(file, use_alpha);
448 create_gl(sdl_surface,&gl_texture);
454 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
456 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
458 create_gl(sdl_surface, &gl_texture);
464 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
466 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
467 create_gl(sdl_surface, &gl_texture);
473 SurfaceOpenGL::~SurfaceOpenGL()
475 glDeleteTextures(1, &gl_texture);
479 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
486 w = power_of_two(surf->w);
487 h = power_of_two(surf->h),
489 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
490 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
491 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
493 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
494 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
497 /* Save the alpha blending attributes */
498 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
499 saved_alpha = surf->format->alpha;
500 if ( (saved_flags & SDL_SRCALPHA)
503 SDL_SetAlpha(surf, 0, 0);
506 SDL_BlitSurface(surf, 0, conv, 0);
508 /* Restore the alpha blending attributes */
509 if ( (saved_flags & SDL_SRCALPHA)
512 SDL_SetAlpha(surf, saved_flags, saved_alpha);
515 // We check all the pixels of the surface to figure out which
516 // internal format OpenGL should use for storing it, ie. if no alpha
517 // is present store in RGB instead of RGBA, this saves a few bytes
518 // of memory, but much more importantly it makes the game look
519 // *much* better in 16bit color mode
520 int internal_format = GL_RGB10_A2;
521 bool has_alpha = false;
523 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
524 for (int y = 0; y < surf->h; ++y)
525 for (int x = 0; x < surf->w; ++x)
527 if (buf[(conv->pitch*y + x*4) + 3] != 255)
536 internal_format = GL_RGB;
539 glGenTextures(1, &*tex);
540 glBindTexture(GL_TEXTURE_2D , *tex);
541 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
542 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
543 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
544 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
545 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
546 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
547 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
549 SDL_FreeSurface(conv);
553 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
555 float pw = power_of_two(w);
556 float ph = power_of_two(h);
558 if(effect & SEMI_TRANSPARENT)
561 glEnable(GL_TEXTURE_2D);
563 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
565 glColor4ub(alpha, alpha, alpha, alpha);
567 glBindTexture(GL_TEXTURE_2D, gl_texture);
571 if(effect & VERTICAL_FLIP)
574 glVertex2f(x, (float)h+y);
576 glTexCoord2f((float)w / pw, 0);
577 glVertex2f((float)w+x, (float)h+y);
579 glTexCoord2f((float)w / pw, (float)h / ph);
580 glVertex2f((float)w+x, y);
582 glTexCoord2f(0, (float)h / ph);
590 glTexCoord2f((float)w / pw, 0);
591 glVertex2f((float)w+x, y);
593 glTexCoord2f((float)w / pw, (float)h / ph);
594 glVertex2f((float)w+x, (float)h+y);
596 glTexCoord2f(0, (float)h / ph);
597 glVertex2f(x, (float)h+y);
601 glDisable(GL_TEXTURE_2D);
608 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
610 float pw = power_of_two(int(this->w));
611 float ph = power_of_two(int(this->h));
613 if(effect & SEMI_TRANSPARENT)
616 glBindTexture(GL_TEXTURE_2D, gl_texture);
619 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
621 glColor4ub(alpha, alpha, alpha, alpha);
623 glEnable(GL_TEXTURE_2D);
628 if(effect & VERTICAL_FLIP)
630 glTexCoord2f(sx / pw, sy / ph);
633 glTexCoord2f((float)(sx + w) / pw, sy / ph);
636 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
637 glVertex2f(w +x, h+y);
639 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
644 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
647 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
648 glVertex2f(w +x, h+y);
650 glTexCoord2f((float)(sx + w) / pw, sy / ph);
653 glTexCoord2f(sx / pw, sy / ph);
659 glDisable(GL_TEXTURE_2D);
666 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
668 if(effect & SEMI_TRANSPARENT)
671 float pw = power_of_two(sw);
672 float ph = power_of_two(sh);
674 if(effect & SEMI_TRANSPARENT)
677 glEnable(GL_TEXTURE_2D);
679 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
681 glColor4ub(alpha, alpha, alpha, alpha);
683 glBindTexture(GL_TEXTURE_2D, gl_texture);
687 if(effect & VERTICAL_FLIP)
690 glVertex2f(x, (float)sh+y);
692 glTexCoord2f((float)w / pw, 0);
693 glVertex2f((float)sw+x, (float)sh+y);
695 glTexCoord2f((float)w / pw, (float)h / ph);
696 glVertex2f((float)sw+x, y);
698 glTexCoord2f(0, (float)h / ph);
706 glTexCoord2f((float)w / pw, 0);
707 glVertex2f((float)sw+x, y);
709 glTexCoord2f((float)w / pw, (float)h / ph);
710 glVertex2f((float)sw+x, (float)sh+y);
712 glTexCoord2f(0, (float)h / ph);
713 glVertex2f(x, (float)sh+y);
717 glDisable(GL_TEXTURE_2D);
725 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
727 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
732 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
734 sdl_surface = sdl_surface_from_file(file, use_alpha);
739 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, bool use_alpha)
741 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
746 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
748 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
754 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
763 if(effect & SEMI_TRANSPARENT)
766 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
768 for(float sy = 0; sy < h; sy++)
769 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
776 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
777 to temp sur, blit the temp into the screen */
778 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
779 already have an alpha mask yet... */
781 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
782 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
783 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
784 sdl_surface->format->Bmask,
786 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
787 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
788 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
791 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
792 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
794 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
796 SDL_FreeSurface (sdl_surface_copy);
800 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
806 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
820 if(effect & SEMI_TRANSPARENT)
823 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
825 for(float sy_ = sy; sy_ < h; sy_++)
826 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
833 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
834 to temp sur, blit the temp into the screen */
835 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
836 already have an alpha mask yet... */
838 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
839 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
840 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
841 sdl_surface->format->Bmask,
843 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
844 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
845 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
848 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
849 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
851 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
853 SDL_FreeSurface (sdl_surface_copy);
857 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
863 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
872 if(effect & SEMI_TRANSPARENT)
875 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
876 sw, sh, sdl_surface->format->BitsPerPixel,
877 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
878 sdl_surface->format->Bmask,
881 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
882 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
885 SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
887 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
888 SDL_FreeSurface(sdl_surface_copy);
893 SurfaceSDL::~SurfaceSDL()