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
25 #include "SDL_image.h"
30 Surface::Surfaces Surface::surfaces;
32 SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
33 : type(SURFACE), surface(0), use_alpha(use_alpha_)
35 // Copy the given surface and make sure that it is not stored in
37 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
39 temp->format->BitsPerPixel,
45 st_abort("No memory left.", "");
46 SDL_SetAlpha(temp,0,0);
47 SDL_BlitSurface(temp, NULL, surface, NULL);
50 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
51 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
54 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
55 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
56 x(x_), y(y_), w(w_), h(h_)
59 SurfaceData::~SurfaceData()
61 SDL_FreeSurface(surface);
69 return create_SurfaceOpenGL();
71 return create_SurfaceSDL();
73 return create_SurfaceSDL();
78 SurfaceData::create_SurfaceSDL()
83 return new SurfaceSDL(file, use_alpha);
85 return new SurfaceSDL(file, x, y, w, h, use_alpha);
87 return new SurfaceSDL(surface, use_alpha);
93 SurfaceData::create_SurfaceOpenGL()
99 return new SurfaceOpenGL(file, use_alpha);
101 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
103 return new SurfaceOpenGL(surface, use_alpha);
110 /* Quick utility function for texture creation */
111 static int power_of_two(int input)
115 while ( value < input )
123 Surface::Surface(SDL_Surface* surf, int use_alpha)
124 : data(surf, use_alpha), w(0), h(0)
126 impl = data.create();
132 surfaces.push_back(this);
135 Surface::Surface(const std::string& file, int use_alpha)
136 : data(file, use_alpha), w(0), h(0)
138 impl = data.create();
144 surfaces.push_back(this);
147 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
148 : data(file, x, y, w, h, use_alpha), w(0), h(0)
150 impl = data.create();
156 surfaces.push_back(this);
163 impl = data.create();
175 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
184 printf("Error: Surface freed twice!!!\n");
186 surfaces.remove(this);
191 Surface::reload_all()
193 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
200 Surface::debug_check()
202 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
204 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
205 (*i)->data.file.c_str());
210 Surface::draw(float x, float y, Uint8 alpha, bool upside_down, bool update)
214 if(upside_down) // FIXME: this should be done by the SDL and OpenGL draw()
215 for(float sy = 0; sy < h; sy++)
216 impl->draw_part(0, sy, x, y+(h-sy), w, 1, alpha, update);
218 else if (impl->draw(x, y, alpha, update) == -2)
224 Surface::draw_bg(Uint8 alpha, bool update)
228 if (impl->draw_bg(alpha, update) == -2)
234 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
238 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
244 Surface::draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update)
248 if (impl->draw_stretched(x, y, w, h, alpha, update) == -2)
254 Surface::resize(int w_, int h_)
260 if (impl->resize(w_,h_) == -2)
265 Surface* Surface::CaptureScreen()
269 if (!(screen->flags & SDL_OPENGL))
271 cap_screen = new Surface(SDL_GetVideoSurface(),false);
278 unsigned char *pixels;
280 temp = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h, 24,
281 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
282 0x000000FF, 0x0000FF00, 0x00FF0000, 0
284 0x00FF0000, 0x0000FF00, 0x000000FF, 0
288 st_abort("Error while trying to capture the screen in OpenGL mode","");
290 pixels = (unsigned char*) malloc(3 * screen->w * screen->h);
293 SDL_FreeSurface(temp);
294 st_abort("Error while trying to capture the screen in OpenGL mode","");
297 glReadPixels(0, 0, screen->w, screen->h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
299 for (i=0; i<screen->h; i++)
300 memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3*screen->w * (screen->h-i-1), screen->w*3);
303 cap_screen = new Surface(temp,false);
304 SDL_FreeSurface(temp);
313 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
316 SDL_Surface * sdl_surface;
320 temp = IMG_Load(file.c_str());
323 st_abort("Can't load", file);
325 /* Set source rectangle for conv: */
332 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
336 temp->format->Amask);
338 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
339 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
342 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
345 SDL_SetAlpha(temp,0,0);
347 SDL_BlitSurface(temp, &src, conv, NULL);
348 if(use_alpha == IGNORE_ALPHA && !use_gl)
349 sdl_surface = SDL_DisplayFormat(conv);
351 sdl_surface = SDL_DisplayFormatAlpha(conv);
353 if (sdl_surface == NULL)
354 st_abort("Can't covert to display format", file);
356 if (use_alpha == IGNORE_ALPHA && !use_gl)
357 SDL_SetAlpha(sdl_surface, 0, 0);
359 SDL_FreeSurface(temp);
360 SDL_FreeSurface(conv);
366 sdl_surface_from_file(const std::string& file, int use_alpha)
368 SDL_Surface* sdl_surface;
371 temp = IMG_Load(file.c_str());
374 st_abort("Can't load", file);
376 if(use_alpha == IGNORE_ALPHA && !use_gl)
377 sdl_surface = SDL_DisplayFormat(temp);
379 sdl_surface = SDL_DisplayFormatAlpha(temp);
381 if (sdl_surface == NULL)
382 st_abort("Can't covert to display format", file);
384 if (use_alpha == IGNORE_ALPHA && !use_gl)
385 SDL_SetAlpha(sdl_surface, 0, 0);
387 SDL_FreeSurface(temp);
393 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
395 SDL_Surface* sdl_surface;
399 /* Save the alpha blending attributes */
400 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
401 saved_alpha = sdl_surf->format->alpha;
402 if ( (saved_flags & SDL_SRCALPHA)
405 SDL_SetAlpha(sdl_surf, 0, 0);
408 if(use_alpha == IGNORE_ALPHA && !use_gl)
409 sdl_surface = SDL_DisplayFormat(sdl_surf);
411 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
413 /* Restore the alpha blending attributes */
414 if ( (saved_flags & SDL_SRCALPHA)
417 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
420 if (sdl_surface == NULL)
421 st_abort("Can't covert to display format", "SURFACE");
423 if (use_alpha == IGNORE_ALPHA && !use_gl)
424 SDL_SetAlpha(sdl_surface, 0, 0);
429 //---------------------------------------------------------------------------
431 SurfaceImpl::SurfaceImpl()
434 SurfaceImpl::~SurfaceImpl()
436 SDL_FreeSurface(sdl_surface);
439 SDL_Surface* SurfaceImpl::get_sdl_surface() const
444 int SurfaceImpl::resize(int w_, int h_)
453 int ret = SDL_SoftStretch(sdl_surface, NULL,
459 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
461 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
462 create_gl(sdl_surface,&gl_texture);
468 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
470 sdl_surface = sdl_surface_from_file(file, use_alpha);
471 create_gl(sdl_surface,&gl_texture);
477 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
479 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
480 create_gl(sdl_surface, &gl_texture);
486 SurfaceOpenGL::~SurfaceOpenGL()
488 glDeleteTextures(1, &gl_texture);
492 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
499 w = power_of_two(surf->w);
500 h = power_of_two(surf->h),
502 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
503 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
504 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
506 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
507 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
510 /* Save the alpha blending attributes */
511 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
512 saved_alpha = surf->format->alpha;
513 if ( (saved_flags & SDL_SRCALPHA)
516 SDL_SetAlpha(surf, 0, 0);
519 SDL_BlitSurface(surf, 0, conv, 0);
521 /* Restore the alpha blending attributes */
522 if ( (saved_flags & SDL_SRCALPHA)
525 SDL_SetAlpha(surf, saved_flags, saved_alpha);
528 glGenTextures(1, &*tex);
529 glBindTexture(GL_TEXTURE_2D , *tex);
530 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
531 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
532 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
533 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
534 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
535 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
536 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
538 SDL_FreeSurface(conv);
542 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
544 float pw = power_of_two(w);
545 float ph = power_of_two(h);
547 glEnable(GL_TEXTURE_2D);
549 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
551 glColor4ub(alpha, alpha, alpha, alpha);
553 glBindTexture(GL_TEXTURE_2D, gl_texture);
558 glTexCoord2f((float)w / pw, 0);
559 glVertex2f((float)w+x, y);
560 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
561 glTexCoord2f(0, (float)h / ph);
562 glVertex2f(x, (float)h+y);
565 glDisable(GL_TEXTURE_2D);
568 (void) update; // avoid compiler warning
574 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
576 float pw = power_of_two(w);
577 float ph = power_of_two(h);
579 glColor3ub(alpha, alpha, alpha);
581 glEnable(GL_TEXTURE_2D);
582 glBindTexture(GL_TEXTURE_2D, gl_texture);
587 glTexCoord2f((float)w / pw, 0);
588 glVertex2f(screen->w, 0);
589 glTexCoord2f((float)w / pw, (float)h / ph);
590 glVertex2f(screen->w, screen->h);
591 glTexCoord2f(0, (float)h / ph);
592 glVertex2f(0, screen->h);
595 glDisable(GL_TEXTURE_2D);
597 (void) update; // avoid compiler warning
603 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
605 float pw = power_of_two(int(this->w));
606 float ph = power_of_two(int(this->h));
608 glBindTexture(GL_TEXTURE_2D, gl_texture);
611 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
613 glColor4ub(alpha, alpha, alpha, alpha);
615 glEnable(GL_TEXTURE_2D);
619 glTexCoord2f(sx / pw, sy / ph);
621 glTexCoord2f((float)(sx + w) / pw, sy / ph);
623 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
624 glVertex2f(w +x, h+y);
625 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
629 glDisable(GL_TEXTURE_2D);
632 (void) update; // avoid warnings
637 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
639 float pw = power_of_two(int(this->w));
640 float ph = power_of_two(int(this->h));
642 glBindTexture(GL_TEXTURE_2D, gl_texture);
645 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
647 glColor4ub(alpha, alpha, alpha, alpha);
649 glEnable(GL_TEXTURE_2D);
655 glTexCoord2f((float)w / pw, 0);
657 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
658 glVertex2f(sw +x, sh+y);
659 glTexCoord2f(0, (float)h / ph);
663 glDisable(GL_TEXTURE_2D);
666 (void) update; // avoid warnings
672 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
674 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
679 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
681 sdl_surface = sdl_surface_from_file(file, use_alpha);
686 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
688 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
694 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
705 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
706 to temp sur, blit the temp into the screen */
707 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
708 already have an alpha mask yet... */
710 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
711 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
712 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
713 sdl_surface->format->Bmask,
715 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
716 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
717 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
720 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
721 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
723 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
725 if (update == UPDATE)
726 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
728 SDL_FreeSurface (sdl_surface_copy);
732 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
734 if (update == UPDATE)
735 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
741 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
752 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
753 to temp sur, blit the temp into the screen */
754 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
755 already have an alpha mask yet... */
757 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
758 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
759 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
760 sdl_surface->format->Bmask,
762 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
763 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
764 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
767 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
768 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
770 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
772 if (update == UPDATE)
773 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
775 SDL_FreeSurface (sdl_surface_copy);
779 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
781 if (update == UPDATE)
782 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
788 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
804 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
805 to temp sur, blit the temp into the screen */
806 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
807 already have an alpha mask yet... */
809 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
810 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
811 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
812 sdl_surface->format->Bmask,
814 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
815 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
816 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
819 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
820 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
822 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
824 if (update == UPDATE)
825 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
827 SDL_FreeSurface (sdl_surface_copy);
831 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
833 if (update == UPDATE)
834 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
840 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
850 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
853 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
854 sw, sh, sdl_surface->format->BitsPerPixel,
855 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
856 sdl_surface->format->Bmask,
859 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
860 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
862 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
863 SDL_FreeSurface(sdl_surface_copy);
865 if (update == UPDATE)
866 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
871 SurfaceSDL::~SurfaceSDL()