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"
32 Surface::Surfaces Surface::surfaces;
34 SurfaceData::SurfaceData(SDL_Surface* temp, int 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_, int 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_, int 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, int 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, int 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, int 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, w, h), w(0), h(0)
176 impl = data.create();
182 surfaces.push_back(this);
189 impl = data.create();
201 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
210 printf("Error: Surface freed twice!!!\n");
212 surfaces.remove(this);
217 Surface::reload_all()
219 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
226 Surface::debug_check()
228 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
230 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
231 (*i)->data.file.c_str());
236 Surface::resize(int w_, int h_)
242 if (impl->resize(w_,h_) == -2)
248 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
251 SDL_Surface * sdl_surface;
255 temp = IMG_Load(file.c_str());
258 st_abort("Can't load", file);
260 /* Set source rectangle for conv: */
267 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
271 temp->format->Amask);
273 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
274 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
277 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
280 SDL_SetAlpha(temp,0,0);
282 SDL_BlitSurface(temp, &src, conv, NULL);
283 if(use_alpha == IGNORE_ALPHA && !use_gl)
284 sdl_surface = SDL_DisplayFormat(conv);
286 sdl_surface = SDL_DisplayFormatAlpha(conv);
288 if (sdl_surface == NULL)
289 st_abort("Can't covert to display format", file);
291 if (use_alpha == IGNORE_ALPHA && !use_gl)
292 SDL_SetAlpha(sdl_surface, 0, 0);
294 SDL_FreeSurface(temp);
295 SDL_FreeSurface(conv);
301 sdl_surface_from_file(const std::string& file, int use_alpha)
303 SDL_Surface* sdl_surface;
306 temp = IMG_Load(file.c_str());
309 st_abort("Can't load", file);
311 if(use_alpha == IGNORE_ALPHA && !use_gl)
312 sdl_surface = SDL_DisplayFormat(temp);
314 sdl_surface = SDL_DisplayFormatAlpha(temp);
316 if (sdl_surface == NULL)
317 st_abort("Can't covert to display format", file);
319 if (use_alpha == IGNORE_ALPHA && !use_gl)
320 SDL_SetAlpha(sdl_surface, 0, 0);
322 SDL_FreeSurface(temp);
328 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
330 SDL_Surface* sdl_surface;
334 /* Save the alpha blending attributes */
335 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
336 saved_alpha = sdl_surf->format->alpha;
337 if ( (saved_flags & SDL_SRCALPHA)
340 SDL_SetAlpha(sdl_surf, 0, 0);
343 if(use_alpha == IGNORE_ALPHA && !use_gl)
344 sdl_surface = SDL_DisplayFormat(sdl_surf);
346 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
348 /* Restore the alpha blending attributes */
349 if ( (saved_flags & SDL_SRCALPHA)
352 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
355 if (sdl_surface == NULL)
356 st_abort("Can't covert to display format", "SURFACE");
358 if (use_alpha == IGNORE_ALPHA && !use_gl)
359 SDL_SetAlpha(sdl_surface, 0, 0);
365 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
367 SDL_Surface* sdl_surface;
369 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
370 screen->format->BitsPerPixel, screen->format->Rmask,
371 screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
373 if(sdl_surface == NULL)
374 st_abort("Cannot create surface for the gradient", "SURFACE");
378 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
379 top.red, top.green, top.blue));
383 float redstep = (float(bottom.red)-float(top.red)) / float(h);
384 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
385 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
391 for(float y = 0; y < h; y++)
394 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
395 int(float(top.red) + redstep * y),
396 int(float(top.green) + greenstep * y),
397 int(float(top.blue) + bluestep * y)));
404 //---------------------------------------------------------------------------
406 SurfaceImpl::SurfaceImpl()
409 SurfaceImpl::~SurfaceImpl()
411 SDL_FreeSurface(sdl_surface);
414 SDL_Surface* SurfaceImpl::get_sdl_surface() const
419 int SurfaceImpl::resize(int w_, int h_)
428 int ret = SDL_SoftStretch(sdl_surface, NULL,
434 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
436 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
437 create_gl(sdl_surface,&gl_texture);
443 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
445 sdl_surface = sdl_surface_from_file(file, use_alpha);
446 create_gl(sdl_surface,&gl_texture);
452 SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
454 sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
456 create_gl(sdl_surface, &gl_texture);
462 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
464 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
465 create_gl(sdl_surface, &gl_texture);
471 SurfaceOpenGL::~SurfaceOpenGL()
473 glDeleteTextures(1, &gl_texture);
477 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
484 w = power_of_two(surf->w);
485 h = power_of_two(surf->h),
487 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
488 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
489 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
491 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
492 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
495 /* Save the alpha blending attributes */
496 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
497 saved_alpha = surf->format->alpha;
498 if ( (saved_flags & SDL_SRCALPHA)
501 SDL_SetAlpha(surf, 0, 0);
504 SDL_BlitSurface(surf, 0, conv, 0);
506 /* Restore the alpha blending attributes */
507 if ( (saved_flags & SDL_SRCALPHA)
510 SDL_SetAlpha(surf, saved_flags, saved_alpha);
513 // We check all the pixels of the surface to figure out which
514 // internal format OpenGL should use for storing it, ie. if no alpha
515 // is present store in RGB instead of RGBA, this saves a few bytes
516 // of memory, but much more importantly it makes the game look
517 // *much* better in 16bit color mode
518 int internal_format = GL_RGB10_A2;
519 bool has_alpha = false;
521 unsigned char* buf = static_cast<unsigned char*>(conv->pixels);
522 for (int y = 0; y < surf->h; ++y)
523 for (int x = 0; x < surf->w; ++x)
525 if (buf[(conv->pitch*y + x*4) + 3] != 255)
534 internal_format = GL_RGB;
537 glGenTextures(1, &*tex);
538 glBindTexture(GL_TEXTURE_2D , *tex);
539 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
540 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
541 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
542 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
543 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
544 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
545 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
547 SDL_FreeSurface(conv);
551 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
553 float pw = power_of_two(w);
554 float ph = power_of_two(h);
556 if(effect & SEMI_TRANSPARENT)
559 glEnable(GL_TEXTURE_2D);
561 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
563 glColor4ub(alpha, alpha, alpha, alpha);
565 glBindTexture(GL_TEXTURE_2D, gl_texture);
569 if(effect & VERTICAL_FLIP)
572 glVertex2f(x, (float)h+y);
574 glTexCoord2f((float)w / pw, 0);
575 glVertex2f((float)w+x, (float)h+y);
577 glTexCoord2f((float)w / pw, (float)h / ph);
578 glVertex2f((float)w+x, y);
580 glTexCoord2f(0, (float)h / ph);
588 glTexCoord2f((float)w / pw, 0);
589 glVertex2f((float)w+x, y);
591 glTexCoord2f((float)w / pw, (float)h / ph);
592 glVertex2f((float)w+x, (float)h+y);
594 glTexCoord2f(0, (float)h / ph);
595 glVertex2f(x, (float)h+y);
599 glDisable(GL_TEXTURE_2D);
606 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
608 float pw = power_of_two(int(this->w));
609 float ph = power_of_two(int(this->h));
611 if(effect & SEMI_TRANSPARENT)
614 glBindTexture(GL_TEXTURE_2D, gl_texture);
617 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
619 glColor4ub(alpha, alpha, alpha, alpha);
621 glEnable(GL_TEXTURE_2D);
626 if(effect & VERTICAL_FLIP)
628 glTexCoord2f(sx / pw, sy / ph);
631 glTexCoord2f((float)(sx + w) / pw, sy / ph);
634 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
635 glVertex2f(w +x, h+y);
637 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
642 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
645 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
646 glVertex2f(w +x, h+y);
648 glTexCoord2f((float)(sx + w) / pw, sy / ph);
651 glTexCoord2f(sx / pw, sy / ph);
657 glDisable(GL_TEXTURE_2D);
665 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
667 float pw = power_of_two(int(this->w));
668 float ph = power_of_two(int(this->h));
670 glBindTexture(GL_TEXTURE_2D, gl_texture);
673 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
675 glColor4ub(alpha, alpha, alpha, alpha);
677 glEnable(GL_TEXTURE_2D);
683 glTexCoord2f((float)w / pw, 0);
685 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
686 glVertex2f(sw +x, sh+y);
687 glTexCoord2f(0, (float)h / ph);
691 glDisable(GL_TEXTURE_2D);
700 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
702 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
707 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
709 sdl_surface = sdl_surface_from_file(file, use_alpha);
714 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
716 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
721 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
723 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
729 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
738 if(effect & SEMI_TRANSPARENT)
741 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
743 for(float sy = 0; sy < h; sy++)
744 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
751 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
752 to temp sur, blit the temp into the screen */
753 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
754 already have an alpha mask yet... */
756 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
757 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
758 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
759 sdl_surface->format->Bmask,
761 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
762 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
763 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
766 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
767 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
769 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
771 SDL_FreeSurface (sdl_surface_copy);
775 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
781 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
795 if(effect & SEMI_TRANSPARENT)
798 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
800 for(float sy_ = sy; sy_ < h; sy_++)
801 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
808 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
809 to temp sur, blit the temp into the screen */
810 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
811 already have an alpha mask yet... */
813 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
814 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
815 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
816 sdl_surface->format->Bmask,
818 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
819 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
820 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
823 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
824 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
826 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
828 SDL_FreeSurface (sdl_surface_copy);
832 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
839 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
849 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
852 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
853 sw, sh, sdl_surface->format->BitsPerPixel,
854 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
855 sdl_surface->format->Bmask,
858 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
859 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
861 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
862 SDL_FreeSurface(sdl_surface_copy);
864 if (update == UPDATE)
865 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
871 SurfaceSDL::~SurfaceSDL()