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 update)
214 if (impl->draw(x, y, alpha, update) == -2)
220 Surface::draw_bg(Uint8 alpha, bool update)
224 if (impl->draw_bg(alpha, update) == -2)
230 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
234 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
240 Surface::draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update)
244 if (impl->draw_stretched(x, y, w, h, alpha, update) == -2)
250 Surface::resize(int w_, int h_)
256 if (impl->resize(w_,h_) == -2)
261 Surface* Surface::CaptureScreen()
265 if (!(screen->flags & SDL_OPENGL))
267 cap_screen = new Surface(SDL_GetVideoSurface(),false);
274 unsigned char *pixels;
276 temp = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h, 24,
277 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
278 0x000000FF, 0x0000FF00, 0x00FF0000, 0
280 0x00FF0000, 0x0000FF00, 0x000000FF, 0
284 st_abort("Error while trying to capture the screen in OpenGL mode","");
286 pixels = (unsigned char*) malloc(3 * screen->w * screen->h);
289 SDL_FreeSurface(temp);
290 st_abort("Error while trying to capture the screen in OpenGL mode","");
293 glReadPixels(0, 0, screen->w, screen->h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
295 for (i=0; i<screen->h; i++)
296 memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3*screen->w * (screen->h-i-1), screen->w*3);
299 cap_screen = new Surface(temp,false);
300 SDL_FreeSurface(temp);
309 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
312 SDL_Surface * sdl_surface;
316 temp = IMG_Load(file.c_str());
319 st_abort("Can't load", file);
321 /* Set source rectangle for conv: */
328 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
332 temp->format->Amask);
334 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
335 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
338 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
341 SDL_SetAlpha(temp,0,0);
343 SDL_BlitSurface(temp, &src, conv, NULL);
344 if(use_alpha == IGNORE_ALPHA && !use_gl)
345 sdl_surface = SDL_DisplayFormat(conv);
347 sdl_surface = SDL_DisplayFormatAlpha(conv);
349 if (sdl_surface == NULL)
350 st_abort("Can't covert to display format", file);
352 if (use_alpha == IGNORE_ALPHA && !use_gl)
353 SDL_SetAlpha(sdl_surface, 0, 0);
355 SDL_FreeSurface(temp);
356 SDL_FreeSurface(conv);
362 sdl_surface_from_file(const std::string& file, int use_alpha)
364 SDL_Surface* sdl_surface;
367 temp = IMG_Load(file.c_str());
370 st_abort("Can't load", file);
372 if(use_alpha == IGNORE_ALPHA && !use_gl)
373 sdl_surface = SDL_DisplayFormat(temp);
375 sdl_surface = SDL_DisplayFormatAlpha(temp);
377 if (sdl_surface == NULL)
378 st_abort("Can't covert to display format", file);
380 if (use_alpha == IGNORE_ALPHA && !use_gl)
381 SDL_SetAlpha(sdl_surface, 0, 0);
383 SDL_FreeSurface(temp);
389 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
391 SDL_Surface* sdl_surface;
395 /* Save the alpha blending attributes */
396 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
397 saved_alpha = sdl_surf->format->alpha;
398 if ( (saved_flags & SDL_SRCALPHA)
401 SDL_SetAlpha(sdl_surf, 0, 0);
404 if(use_alpha == IGNORE_ALPHA && !use_gl)
405 sdl_surface = SDL_DisplayFormat(sdl_surf);
407 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
409 /* Restore the alpha blending attributes */
410 if ( (saved_flags & SDL_SRCALPHA)
413 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
416 if (sdl_surface == NULL)
417 st_abort("Can't covert to display format", "SURFACE");
419 if (use_alpha == IGNORE_ALPHA && !use_gl)
420 SDL_SetAlpha(sdl_surface, 0, 0);
425 //---------------------------------------------------------------------------
427 SurfaceImpl::SurfaceImpl()
430 SurfaceImpl::~SurfaceImpl()
432 SDL_FreeSurface(sdl_surface);
435 SDL_Surface* SurfaceImpl::get_sdl_surface() const
440 int SurfaceImpl::resize(int w_, int h_)
449 int ret = SDL_SoftStretch(sdl_surface, NULL,
455 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
457 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
458 create_gl(sdl_surface,&gl_texture);
464 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
466 sdl_surface = sdl_surface_from_file(file, use_alpha);
467 create_gl(sdl_surface,&gl_texture);
473 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
475 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
476 create_gl(sdl_surface, &gl_texture);
482 SurfaceOpenGL::~SurfaceOpenGL()
484 glDeleteTextures(1, &gl_texture);
488 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
495 w = power_of_two(surf->w);
496 h = power_of_two(surf->h),
498 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
499 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
500 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
502 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
503 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
506 /* Save the alpha blending attributes */
507 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
508 saved_alpha = surf->format->alpha;
509 if ( (saved_flags & SDL_SRCALPHA)
512 SDL_SetAlpha(surf, 0, 0);
515 SDL_BlitSurface(surf, 0, conv, 0);
517 /* Restore the alpha blending attributes */
518 if ( (saved_flags & SDL_SRCALPHA)
521 SDL_SetAlpha(surf, saved_flags, saved_alpha);
524 glGenTextures(1, &*tex);
525 glBindTexture(GL_TEXTURE_2D , *tex);
526 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
527 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
528 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
529 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
530 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
531 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
532 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
534 SDL_FreeSurface(conv);
538 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
540 float pw = power_of_two(w);
541 float ph = power_of_two(h);
543 glEnable(GL_TEXTURE_2D);
545 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
547 glColor4ub(alpha, alpha, alpha, alpha);
549 glBindTexture(GL_TEXTURE_2D, gl_texture);
554 glTexCoord2f((float)w / pw, 0);
555 glVertex2f((float)w+x, y);
556 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
557 glTexCoord2f(0, (float)h / ph);
558 glVertex2f(x, (float)h+y);
561 glDisable(GL_TEXTURE_2D);
564 (void) update; // avoid compiler warning
570 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
572 float pw = power_of_two(w);
573 float ph = power_of_two(h);
575 glColor3ub(alpha, alpha, alpha);
577 glEnable(GL_TEXTURE_2D);
578 glBindTexture(GL_TEXTURE_2D, gl_texture);
583 glTexCoord2f((float)w / pw, 0);
584 glVertex2f(screen->w, 0);
585 glTexCoord2f((float)w / pw, (float)h / ph);
586 glVertex2f(screen->w, screen->h);
587 glTexCoord2f(0, (float)h / ph);
588 glVertex2f(0, screen->h);
591 glDisable(GL_TEXTURE_2D);
593 (void) update; // avoid compiler warning
599 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
601 float pw = power_of_two(int(this->w));
602 float ph = power_of_two(int(this->h));
604 glBindTexture(GL_TEXTURE_2D, gl_texture);
607 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
609 glColor4ub(alpha, alpha, alpha, alpha);
611 glEnable(GL_TEXTURE_2D);
615 glTexCoord2f(sx / pw, sy / ph);
617 glTexCoord2f((float)(sx + w) / pw, sy / ph);
619 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
620 glVertex2f(w +x, h+y);
621 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
625 glDisable(GL_TEXTURE_2D);
628 (void) update; // avoid warnings
633 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
635 float pw = power_of_two(int(this->w));
636 float ph = power_of_two(int(this->h));
638 glBindTexture(GL_TEXTURE_2D, gl_texture);
641 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
643 glColor4ub(alpha, alpha, alpha, alpha);
645 glEnable(GL_TEXTURE_2D);
651 glTexCoord2f((float)w / pw, 0);
653 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
654 glVertex2f(sw +x, sh+y);
655 glTexCoord2f(0, (float)h / ph);
659 glDisable(GL_TEXTURE_2D);
662 (void) update; // avoid warnings
668 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
670 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
675 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
677 sdl_surface = sdl_surface_from_file(file, use_alpha);
682 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
684 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
690 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
701 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
702 to temp sur, blit the temp into the screen */
703 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
704 already have an alpha mask yet... */
706 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
707 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
708 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
709 sdl_surface->format->Bmask,
711 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
712 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
713 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
716 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
717 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
719 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
721 if (update == UPDATE)
722 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
724 SDL_FreeSurface (sdl_surface_copy);
728 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
730 if (update == UPDATE)
731 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
737 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
748 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
749 to temp sur, blit the temp into the screen */
750 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
751 already have an alpha mask yet... */
753 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
754 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
755 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
756 sdl_surface->format->Bmask,
758 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
759 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
760 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
763 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
764 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
766 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
768 if (update == UPDATE)
769 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
771 SDL_FreeSurface (sdl_surface_copy);
775 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
777 if (update == UPDATE)
778 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
784 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
800 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
801 to temp sur, blit the temp into the screen */
802 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
803 already have an alpha mask yet... */
805 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
806 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
807 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
808 sdl_surface->format->Bmask,
810 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
811 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
812 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
815 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
816 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
818 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
820 if (update == UPDATE)
821 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
823 SDL_FreeSurface (sdl_surface_copy);
827 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
829 if (update == UPDATE)
830 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
836 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
846 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
849 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
850 sw, sh, sdl_surface->format->BitsPerPixel,
851 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
852 sdl_surface->format->Bmask,
855 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
856 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
858 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
859 SDL_FreeSurface(sdl_surface_copy);
861 if (update == UPDATE)
862 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
867 SurfaceSDL::~SurfaceSDL()