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 Surface::Surfaces Surface::surfaces;
34 SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_)
35 : type(SURFACE), surface(0), use_alpha(use_alpha_)
37 // Copy the given surface and make sure that it is not stored in
39 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
41 temp->format->BitsPerPixel,
47 st_abort("No memory left.", "");
48 SDL_SetAlpha(temp,0,0);
49 SDL_BlitSurface(temp, NULL, surface, NULL);
52 SurfaceData::SurfaceData(const std::string& file_, bool use_alpha_)
53 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
56 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
57 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
58 x(x_), y(y_), w(w_), h(h_)
61 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_)
62 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
64 top_gradient = top_gradient_;
65 bottom_gradient = bottom_gradient_;
69 SurfaceData::~SurfaceData()
71 SDL_FreeSurface(surface);
79 return create_SurfaceOpenGL();
81 return create_SurfaceSDL();
83 return create_SurfaceSDL();
88 SurfaceData::create_SurfaceSDL()
93 return new SurfaceSDL(file, use_alpha);
95 return new SurfaceSDL(file, x, y, w, h, use_alpha);
97 return new SurfaceSDL(surface, use_alpha);
99 return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
105 SurfaceData::create_SurfaceOpenGL()
111 return new SurfaceOpenGL(file, use_alpha);
113 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
115 return new SurfaceOpenGL(surface, use_alpha);
117 return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
124 /* Quick utility function for texture creation */
125 static int power_of_two(int input)
129 while ( value < input )
137 Surface::Surface(SDL_Surface* surf, bool use_alpha)
138 : data(surf, use_alpha), w(0), h(0)
140 impl = data.create();
146 surfaces.push_back(this);
149 Surface::Surface(const std::string& file, bool use_alpha)
150 : data(file, use_alpha), w(0), h(0)
152 impl = data.create();
158 surfaces.push_back(this);
161 Surface::Surface(const std::string& file, int x, int y, int w, int h, bool use_alpha)
162 : data(file, x, y, w, h, use_alpha), w(0), h(0)
164 impl = data.create();
170 surfaces.push_back(this);
173 Surface::Surface(Color top_background, Color bottom_background, int w_, int h_)
174 : data(top_background, bottom_background, 0, 0), w(0), h(0)
176 // FIXME: Gradient surfaces currently don't accept width/height
177 // If nonzero values are passed to data.create(), supertux
179 impl = data.create();
185 surfaces.push_back(this);
192 impl = data.create();
204 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
213 printf("Error: Surface freed twice!!!\n");
215 surfaces.remove(this);
220 Surface::reload_all()
222 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
229 Surface::debug_check()
231 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
233 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
234 (*i)->data.file.c_str());
239 Surface::resize(int w_, int h_)
245 if (impl->resize(w_,h_) == -2)
251 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
254 SDL_Surface * sdl_surface;
258 temp = IMG_Load(file.c_str());
261 st_abort("Can't load", file);
263 /* Set source rectangle for conv: */
270 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
274 temp->format->Amask);
276 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
277 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
280 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
283 SDL_SetAlpha(temp,0,0);
285 SDL_BlitSurface(temp, &src, conv, NULL);
286 if(use_alpha == false && !use_gl)
287 sdl_surface = SDL_DisplayFormat(conv);
289 sdl_surface = SDL_DisplayFormatAlpha(conv);
291 if (sdl_surface == NULL)
292 st_abort("Can't covert to display format", file);
294 if (use_alpha == false && !use_gl)
295 SDL_SetAlpha(sdl_surface, 0, 0);
297 SDL_FreeSurface(temp);
298 SDL_FreeSurface(conv);
304 sdl_surface_from_file(const std::string& file, bool use_alpha)
306 SDL_Surface* sdl_surface;
309 temp = IMG_Load(file.c_str());
312 st_abort("Can't load", file);
314 if(use_alpha == false && !use_gl)
315 sdl_surface = SDL_DisplayFormat(temp);
317 sdl_surface = SDL_DisplayFormatAlpha(temp);
319 if (sdl_surface == NULL)
320 st_abort("Can't covert to display format", file);
322 if (use_alpha == false && !use_gl)
323 SDL_SetAlpha(sdl_surface, 0, 0);
325 SDL_FreeSurface(temp);
331 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
333 SDL_Surface* sdl_surface;
337 /* Save the alpha blending attributes */
338 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
339 saved_alpha = sdl_surf->format->alpha;
340 if ( (saved_flags & SDL_SRCALPHA)
343 SDL_SetAlpha(sdl_surf, 0, 0);
346 if(use_alpha == false && !use_gl)
347 sdl_surface = SDL_DisplayFormat(sdl_surf);
349 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
351 /* Restore the alpha blending attributes */
352 if ( (saved_flags & SDL_SRCALPHA)
355 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
358 if (sdl_surface == NULL)
359 st_abort("Can't covert to display format", "SURFACE");
361 if (use_alpha == false && !use_gl)
362 SDL_SetAlpha(sdl_surface, 0, 0);
368 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
370 SDL_Surface* sdl_surface;
372 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
373 screen->format->BitsPerPixel, screen->format->Rmask,
374 screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
376 if(sdl_surface == NULL)
377 st_abort("Cannot create surface for the gradient", "SURFACE");
381 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
382 top.red, top.green, top.blue));
386 float redstep = (float(bottom.red)-float(top.red)) / float(h);
387 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
388 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
394 for(float y = 0; y < h; y++)
397 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
398 int(float(top.red) + redstep * y),
399 int(float(top.green) + greenstep * y),
400 int(float(top.blue) + bluestep * y)));
407 //---------------------------------------------------------------------------
409 SurfaceImpl::SurfaceImpl()
412 SurfaceImpl::~SurfaceImpl()
414 SDL_FreeSurface(sdl_surface);
417 SDL_Surface* SurfaceImpl::get_sdl_surface() const
422 int SurfaceImpl::resize(int w_, int h_)
431 int ret = SDL_SoftStretch(sdl_surface, NULL,
437 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
439 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
440 create_gl(sdl_surface,&gl_texture);
446 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
448 sdl_surface = sdl_surface_from_file(file, use_alpha);
449 create_gl(sdl_surface,&gl_texture);
455 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_)
457 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
459 create_gl(sdl_surface, &gl_texture);
465 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
467 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
468 create_gl(sdl_surface, &gl_texture);
474 SurfaceOpenGL::~SurfaceOpenGL()
476 glDeleteTextures(1, &gl_texture);
480 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
487 w = power_of_two(surf->w);
488 h = power_of_two(surf->h),
490 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
491 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
492 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
494 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
495 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
498 /* Save the alpha blending attributes */
499 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
500 saved_alpha = surf->format->alpha;
501 if ( (saved_flags & SDL_SRCALPHA)
504 SDL_SetAlpha(surf, 0, 0);
507 SDL_BlitSurface(surf, 0, conv, 0);
509 /* Restore the alpha blending attributes */
510 if ( (saved_flags & SDL_SRCALPHA)
513 SDL_SetAlpha(surf, saved_flags, saved_alpha);
516 // We check all the pixels of the surface to figure out which
517 // internal format OpenGL should use for storing it, ie. if no alpha
518 // is present store in RGB instead of RGBA, this saves a few bytes
519 // of memory, but much more importantly it makes the game look
520 // *much* better in 16bit color mode
521 int internal_format = GL_RGB10_A2;
522 bool has_alpha = false;
524 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
525 for (int y = 0; y < surf->h; ++y)
526 for (int x = 0; x < surf->w; ++x)
528 if (buf[(conv->pitch*y + x*4) + 3] != 255)
537 internal_format = GL_RGB;
540 glGenTextures(1, &*tex);
541 glBindTexture(GL_TEXTURE_2D , *tex);
542 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
543 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
544 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
545 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
546 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
547 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
548 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
550 SDL_FreeSurface(conv);
554 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
556 float pw = power_of_two(w);
557 float ph = power_of_two(h);
559 if(effect & SEMI_TRANSPARENT)
562 glEnable(GL_TEXTURE_2D);
564 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
566 glColor4ub(alpha, alpha, alpha, alpha);
568 glBindTexture(GL_TEXTURE_2D, gl_texture);
572 if(effect & VERTICAL_FLIP)
575 glVertex2f(x, (float)h+y);
577 glTexCoord2f((float)w / pw, 0);
578 glVertex2f((float)w+x, (float)h+y);
580 glTexCoord2f((float)w / pw, (float)h / ph);
581 glVertex2f((float)w+x, y);
583 glTexCoord2f(0, (float)h / ph);
591 glTexCoord2f((float)w / pw, 0);
592 glVertex2f((float)w+x, y);
594 glTexCoord2f((float)w / pw, (float)h / ph);
595 glVertex2f((float)w+x, (float)h+y);
597 glTexCoord2f(0, (float)h / ph);
598 glVertex2f(x, (float)h+y);
602 glDisable(GL_TEXTURE_2D);
609 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
611 float pw = power_of_two(int(this->w));
612 float ph = power_of_two(int(this->h));
614 if(effect & SEMI_TRANSPARENT)
617 glBindTexture(GL_TEXTURE_2D, gl_texture);
620 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
622 glColor4ub(alpha, alpha, alpha, alpha);
624 glEnable(GL_TEXTURE_2D);
629 if(effect & VERTICAL_FLIP)
631 glTexCoord2f(sx / pw, sy / ph);
634 glTexCoord2f((float)(sx + w) / pw, sy / ph);
637 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
638 glVertex2f(w +x, h+y);
640 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
645 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
648 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
649 glVertex2f(w +x, h+y);
651 glTexCoord2f((float)(sx + w) / pw, sy / ph);
654 glTexCoord2f(sx / pw, sy / ph);
660 glDisable(GL_TEXTURE_2D);
668 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
670 float pw = power_of_two(int(this->w));
671 float ph = power_of_two(int(this->h));
673 glBindTexture(GL_TEXTURE_2D, gl_texture);
676 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
678 glColor4ub(alpha, alpha, alpha, alpha);
680 glEnable(GL_TEXTURE_2D);
686 glTexCoord2f((float)w / pw, 0);
688 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
689 glVertex2f(sw +x, sh+y);
690 glTexCoord2f(0, (float)h / ph);
694 glDisable(GL_TEXTURE_2D);
703 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha)
705 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
710 SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha)
712 sdl_surface = sdl_surface_from_file(file, use_alpha);
717 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, bool use_alpha)
719 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
724 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
726 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
732 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
741 if(effect & SEMI_TRANSPARENT)
744 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
746 for(float sy = 0; sy < h; sy++)
747 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
754 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
755 to temp sur, blit the temp into the screen */
756 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
757 already have an alpha mask yet... */
759 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
760 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
761 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
762 sdl_surface->format->Bmask,
764 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
765 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
766 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
769 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
770 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
772 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
774 SDL_FreeSurface (sdl_surface_copy);
778 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
784 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
798 if(effect & SEMI_TRANSPARENT)
801 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
803 for(float sy_ = sy; sy_ < h; sy_++)
804 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
811 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
812 to temp sur, blit the temp into the screen */
813 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
814 already have an alpha mask yet... */
816 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
817 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
818 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
819 sdl_surface->format->Bmask,
821 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
822 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
823 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
826 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
827 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
829 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
831 SDL_FreeSurface (sdl_surface_copy);
835 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
842 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
852 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
855 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
856 sw, sh, sdl_surface->format->BitsPerPixel,
857 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
858 sdl_surface->format->Bmask,
861 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
862 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
864 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
865 SDL_FreeSurface(sdl_surface_copy);
867 if (update == UPDATE)
868 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
874 SurfaceSDL::~SurfaceSDL()