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 // FIXME: Gradient surfaces currently don't accept width/height
179 // If nonzero values are passed to data.create(), supertux
181 impl = data.create();
187 surfaces.push_back(this);
194 impl = data.create();
206 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
215 printf("Error: Surface freed twice!!!\n");
217 surfaces.remove(this);
222 Surface::reload_all()
224 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
231 Surface::debug_check()
233 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
235 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
236 (*i)->data.file.c_str());
241 Surface::resize(int w_, int h_)
247 if (impl->resize(w_,h_) == -2)
253 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
256 SDL_Surface * sdl_surface;
260 temp = IMG_Load(file.c_str());
263 Termination::abort("Can't load", file);
265 /* Set source rectangle for conv: */
272 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
276 temp->format->Amask);
278 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
279 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
282 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
285 SDL_SetAlpha(temp,0,0);
287 SDL_BlitSurface(temp, &src, conv, NULL);
288 if(use_alpha == false && !use_gl)
289 sdl_surface = SDL_DisplayFormat(conv);
291 sdl_surface = SDL_DisplayFormatAlpha(conv);
293 if (sdl_surface == NULL)
294 Termination::abort("Can't covert to display format", file);
296 if (use_alpha == false && !use_gl)
297 SDL_SetAlpha(sdl_surface, 0, 0);
299 SDL_FreeSurface(temp);
300 SDL_FreeSurface(conv);
306 sdl_surface_from_file(const std::string& file, bool use_alpha)
308 SDL_Surface* sdl_surface;
311 temp = IMG_Load(file.c_str());
314 Termination::abort("Can't load", file);
316 if(use_alpha == false && !use_gl)
317 sdl_surface = SDL_DisplayFormat(temp);
319 sdl_surface = SDL_DisplayFormatAlpha(temp);
321 if (sdl_surface == NULL)
322 Termination::abort("Can't covert to display format", file);
324 if (use_alpha == false && !use_gl)
325 SDL_SetAlpha(sdl_surface, 0, 0);
327 SDL_FreeSurface(temp);
333 SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
335 SDL_Surface* sdl_surface;
339 /* Save the alpha blending attributes */
340 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
341 saved_alpha = sdl_surf->format->alpha;
342 if ( (saved_flags & SDL_SRCALPHA)
345 SDL_SetAlpha(sdl_surf, 0, 0);
348 if(use_alpha == false && !use_gl)
349 sdl_surface = SDL_DisplayFormat(sdl_surf);
351 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
353 /* Restore the alpha blending attributes */
354 if ( (saved_flags & SDL_SRCALPHA)
357 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
360 if (sdl_surface == NULL)
361 Termination::abort("Can't covert to display format", "SURFACE");
363 if (use_alpha == false && !use_gl)
364 SDL_SetAlpha(sdl_surface, 0, 0);
370 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
372 SDL_Surface* sdl_surface;
374 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
375 screen->format->BitsPerPixel, screen->format->Rmask,
376 screen->format->Gmask, screen->format->Bmask, 0);
378 if(sdl_surface == NULL)
379 Termination::abort("Cannot create surface for the gradient", "SURFACE");
383 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
384 top.red, top.green, top.blue));
388 float redstep = (float(bottom.red)-float(top.red)) / float(h);
389 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
390 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
396 for(float y = 0; y < h; y++)
399 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
400 int(float(top.red) + redstep * y),
401 int(float(top.green) + greenstep * y),
402 int(float(top.blue) + bluestep * y)));
409 //---------------------------------------------------------------------------
411 SurfaceImpl::SurfaceImpl()
414 SurfaceImpl::~SurfaceImpl()
416 SDL_FreeSurface(sdl_surface);
419 SDL_Surface* SurfaceImpl::get_sdl_surface() const
424 int SurfaceImpl::resize(int w_, int h_)
433 int ret = SDL_SoftStretch(sdl_surface, NULL,
439 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
441 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
442 create_gl(sdl_surface,&gl_texture);
448 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
450 sdl_surface = sdl_surface_from_file(file, use_alpha);
451 create_gl(sdl_surface,&gl_texture);
457 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
459 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
461 create_gl(sdl_surface, &gl_texture);
467 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
469 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
470 create_gl(sdl_surface, &gl_texture);
476 SurfaceOpenGL::~SurfaceOpenGL()
478 glDeleteTextures(1, &gl_texture);
482 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
489 w = power_of_two(surf->w);
490 h = power_of_two(surf->h),
492 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
493 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
494 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
496 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
497 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
500 /* Save the alpha blending attributes */
501 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
502 saved_alpha = surf->format->alpha;
503 if ( (saved_flags & SDL_SRCALPHA)
506 SDL_SetAlpha(surf, 0, 0);
509 SDL_BlitSurface(surf, 0, conv, 0);
511 /* Restore the alpha blending attributes */
512 if ( (saved_flags & SDL_SRCALPHA)
515 SDL_SetAlpha(surf, saved_flags, saved_alpha);
518 // We check all the pixels of the surface to figure out which
519 // internal format OpenGL should use for storing it, ie. if no alpha
520 // is present store in RGB instead of RGBA, this saves a few bytes
521 // of memory, but much more importantly it makes the game look
522 // *much* better in 16bit color mode
523 int internal_format = GL_RGB10_A2;
524 bool has_alpha = false;
526 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
527 for (int y = 0; y < surf->h; ++y)
528 for (int x = 0; x < surf->w; ++x)
530 if (buf[(conv->pitch*y + x*4) + 3] != 255)
539 internal_format = GL_RGB;
542 glGenTextures(1, &*tex);
543 glBindTexture(GL_TEXTURE_2D , *tex);
544 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
545 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
546 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
547 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
548 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
549 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
550 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
552 SDL_FreeSurface(conv);
556 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
558 float pw = power_of_two(w);
559 float ph = power_of_two(h);
561 if(effect & SEMI_TRANSPARENT)
564 glEnable(GL_TEXTURE_2D);
566 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
568 glColor4ub(alpha, alpha, alpha, alpha);
570 glBindTexture(GL_TEXTURE_2D, gl_texture);
574 if(effect & VERTICAL_FLIP)
577 glVertex2f(x, (float)h+y);
579 glTexCoord2f((float)w / pw, 0);
580 glVertex2f((float)w+x, (float)h+y);
582 glTexCoord2f((float)w / pw, (float)h / ph);
583 glVertex2f((float)w+x, y);
585 glTexCoord2f(0, (float)h / ph);
593 glTexCoord2f((float)w / pw, 0);
594 glVertex2f((float)w+x, y);
596 glTexCoord2f((float)w / pw, (float)h / ph);
597 glVertex2f((float)w+x, (float)h+y);
599 glTexCoord2f(0, (float)h / ph);
600 glVertex2f(x, (float)h+y);
604 glDisable(GL_TEXTURE_2D);
611 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
613 float pw = power_of_two(int(this->w));
614 float ph = power_of_two(int(this->h));
616 if(effect & SEMI_TRANSPARENT)
619 glBindTexture(GL_TEXTURE_2D, gl_texture);
622 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
624 glColor4ub(alpha, alpha, alpha, alpha);
626 glEnable(GL_TEXTURE_2D);
631 if(effect & VERTICAL_FLIP)
633 glTexCoord2f(sx / pw, sy / ph);
636 glTexCoord2f((float)(sx + w) / pw, sy / ph);
639 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
640 glVertex2f(w +x, h+y);
642 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
647 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
650 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
651 glVertex2f(w +x, h+y);
653 glTexCoord2f((float)(sx + w) / pw, sy / ph);
656 glTexCoord2f(sx / pw, sy / ph);
662 glDisable(GL_TEXTURE_2D);
669 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
671 if(effect & SEMI_TRANSPARENT)
674 float pw = power_of_two(sw);
675 float ph = power_of_two(sh);
677 if(effect & SEMI_TRANSPARENT)
680 glEnable(GL_TEXTURE_2D);
682 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
684 glColor4ub(alpha, alpha, alpha, alpha);
686 glBindTexture(GL_TEXTURE_2D, gl_texture);
690 if(effect & VERTICAL_FLIP)
693 glVertex2f(x, (float)sh+y);
695 glTexCoord2f((float)w / pw, 0);
696 glVertex2f((float)sw+x, (float)sh+y);
698 glTexCoord2f((float)w / pw, (float)h / ph);
699 glVertex2f((float)sw+x, y);
701 glTexCoord2f(0, (float)h / ph);
709 glTexCoord2f((float)w / pw, 0);
710 glVertex2f((float)sw+x, y);
712 glTexCoord2f((float)w / pw, (float)h / ph);
713 glVertex2f((float)sw+x, (float)sh+y);
715 glTexCoord2f(0, (float)h / ph);
716 glVertex2f(x, (float)sh+y);
720 glDisable(GL_TEXTURE_2D);
728 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
730 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
735 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
737 sdl_surface = sdl_surface_from_file(file, use_alpha);
742 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, bool use_alpha)
744 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
749 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
751 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
757 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
766 if(effect & SEMI_TRANSPARENT)
769 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
771 for(float sy = 0; sy < h; sy++)
772 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
779 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
780 to temp sur, blit the temp into the screen */
781 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
782 already have an alpha mask yet... */
784 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
785 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
786 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
787 sdl_surface->format->Bmask,
789 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
790 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
791 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
794 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
795 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
797 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
799 SDL_FreeSurface (sdl_surface_copy);
803 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
809 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
823 if(effect & SEMI_TRANSPARENT)
826 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
828 for(float sy_ = sy; sy_ < h; sy_++)
829 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
836 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
837 to temp sur, blit the temp into the screen */
838 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
839 already have an alpha mask yet... */
841 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
842 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
843 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
844 sdl_surface->format->Bmask,
846 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
847 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
848 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
851 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
852 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
854 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
856 SDL_FreeSurface (sdl_surface_copy);
860 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
866 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect)
875 if(effect & SEMI_TRANSPARENT)
878 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
879 sw, sh, sdl_surface->format->BitsPerPixel,
880 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
881 sdl_surface->format->Bmask,
884 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
885 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
888 SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha);
890 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
891 SDL_FreeSurface(sdl_surface_copy);
896 SurfaceSDL::~SurfaceSDL()