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)
262 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
265 SDL_Surface * sdl_surface;
269 temp = IMG_Load(file.c_str());
272 st_abort("Can't load", file);
274 /* Set source rectangle for conv: */
281 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
285 temp->format->Amask);
287 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
288 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
291 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
294 SDL_SetAlpha(temp,0,0);
296 SDL_BlitSurface(temp, &src, conv, NULL);
297 if(use_alpha == IGNORE_ALPHA && !use_gl)
298 sdl_surface = SDL_DisplayFormat(conv);
300 sdl_surface = SDL_DisplayFormatAlpha(conv);
302 if (sdl_surface == NULL)
303 st_abort("Can't covert to display format", file);
305 if (use_alpha == IGNORE_ALPHA && !use_gl)
306 SDL_SetAlpha(sdl_surface, 0, 0);
308 SDL_FreeSurface(temp);
309 SDL_FreeSurface(conv);
315 sdl_surface_from_file(const std::string& file, int use_alpha)
317 SDL_Surface* sdl_surface;
320 temp = IMG_Load(file.c_str());
323 st_abort("Can't load", file);
325 if(use_alpha == IGNORE_ALPHA && !use_gl)
326 sdl_surface = SDL_DisplayFormat(temp);
328 sdl_surface = SDL_DisplayFormatAlpha(temp);
330 if (sdl_surface == NULL)
331 st_abort("Can't covert to display format", file);
333 if (use_alpha == IGNORE_ALPHA && !use_gl)
334 SDL_SetAlpha(sdl_surface, 0, 0);
336 SDL_FreeSurface(temp);
342 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
344 SDL_Surface* sdl_surface;
348 /* Save the alpha blending attributes */
349 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
350 saved_alpha = sdl_surf->format->alpha;
351 if ( (saved_flags & SDL_SRCALPHA)
354 SDL_SetAlpha(sdl_surf, 0, 0);
357 if(use_alpha == IGNORE_ALPHA && !use_gl)
358 sdl_surface = SDL_DisplayFormat(sdl_surf);
360 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
362 /* Restore the alpha blending attributes */
363 if ( (saved_flags & SDL_SRCALPHA)
366 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
369 if (sdl_surface == NULL)
370 st_abort("Can't covert to display format", "SURFACE");
372 if (use_alpha == IGNORE_ALPHA && !use_gl)
373 SDL_SetAlpha(sdl_surface, 0, 0);
378 //---------------------------------------------------------------------------
380 SurfaceImpl::SurfaceImpl()
383 SurfaceImpl::~SurfaceImpl()
385 SDL_FreeSurface(sdl_surface);
388 SDL_Surface* SurfaceImpl::get_sdl_surface() const
393 int SurfaceImpl::resize(int w_, int h_)
402 int ret = SDL_SoftStretch(sdl_surface, NULL,
408 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
410 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
411 create_gl(sdl_surface,&gl_texture);
417 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
419 sdl_surface = sdl_surface_from_file(file, use_alpha);
420 create_gl(sdl_surface,&gl_texture);
426 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
428 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
429 create_gl(sdl_surface, &gl_texture);
435 SurfaceOpenGL::~SurfaceOpenGL()
437 glDeleteTextures(1, &gl_texture);
441 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
448 w = power_of_two(surf->w);
449 h = power_of_two(surf->h),
451 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
452 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
453 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
455 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
456 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
459 /* Save the alpha blending attributes */
460 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
461 saved_alpha = surf->format->alpha;
462 if ( (saved_flags & SDL_SRCALPHA)
465 SDL_SetAlpha(surf, 0, 0);
468 SDL_BlitSurface(surf, 0, conv, 0);
470 /* Restore the alpha blending attributes */
471 if ( (saved_flags & SDL_SRCALPHA)
474 SDL_SetAlpha(surf, saved_flags, saved_alpha);
477 glGenTextures(1, &*tex);
478 glBindTexture(GL_TEXTURE_2D , *tex);
479 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
480 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
481 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
482 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
483 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
484 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
485 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
487 SDL_FreeSurface(conv);
491 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
493 float pw = power_of_two(w);
494 float ph = power_of_two(h);
496 glEnable(GL_TEXTURE_2D);
498 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
500 glColor4ub(alpha, alpha, alpha, alpha);
502 glBindTexture(GL_TEXTURE_2D, gl_texture);
507 glTexCoord2f((float)w / pw, 0);
508 glVertex2f((float)w+x, y);
509 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
510 glTexCoord2f(0, (float)h / ph);
511 glVertex2f(x, (float)h+y);
514 glDisable(GL_TEXTURE_2D);
517 (void) update; // avoid compiler warning
523 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
525 float pw = power_of_two(w);
526 float ph = power_of_two(h);
528 glColor3ub(alpha, alpha, alpha);
530 glEnable(GL_TEXTURE_2D);
531 glBindTexture(GL_TEXTURE_2D, gl_texture);
536 glTexCoord2f((float)w / pw, 0);
537 glVertex2f(screen->w, 0);
538 glTexCoord2f((float)w / pw, (float)h / ph);
539 glVertex2f(screen->w, screen->h);
540 glTexCoord2f(0, (float)h / ph);
541 glVertex2f(0, screen->h);
544 glDisable(GL_TEXTURE_2D);
546 (void) update; // avoid compiler warning
552 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
554 float pw = power_of_two(int(this->w));
555 float ph = power_of_two(int(this->h));
557 glBindTexture(GL_TEXTURE_2D, gl_texture);
560 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
562 glColor4ub(alpha, alpha, alpha, alpha);
564 glEnable(GL_TEXTURE_2D);
568 glTexCoord2f(sx / pw, sy / ph);
570 glTexCoord2f((float)(sx + w) / pw, sy / ph);
572 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
573 glVertex2f(w +x, h+y);
574 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
578 glDisable(GL_TEXTURE_2D);
581 (void) update; // avoid warnings
586 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
588 float pw = power_of_two(int(this->w));
589 float ph = power_of_two(int(this->h));
591 glBindTexture(GL_TEXTURE_2D, gl_texture);
594 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
596 glColor4ub(alpha, alpha, alpha, alpha);
598 glEnable(GL_TEXTURE_2D);
604 glTexCoord2f((float)w / pw, 0);
606 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
607 glVertex2f(sw +x, sh+y);
608 glTexCoord2f(0, (float)h / ph);
612 glDisable(GL_TEXTURE_2D);
615 (void) update; // avoid warnings
621 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
623 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
628 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
630 sdl_surface = sdl_surface_from_file(file, use_alpha);
635 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
637 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
643 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
654 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
655 to temp sur, blit the temp into the screen */
656 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
657 already have an alpha mask yet... */
659 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
660 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
661 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
662 sdl_surface->format->Bmask,
664 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
665 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
666 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
669 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
670 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
672 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
674 if (update == UPDATE)
675 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
677 SDL_FreeSurface (sdl_surface_copy);
681 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
683 if (update == UPDATE)
684 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
690 SurfaceSDL::draw_bg(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_SoftStretch(sdl_surface, NULL, screen, &dest);
730 if (update == UPDATE)
731 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
737 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
753 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
754 to temp sur, blit the temp into the screen */
755 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
756 already have an alpha mask yet... */
758 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
759 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
760 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
761 sdl_surface->format->Bmask,
763 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
764 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
765 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
768 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
769 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
771 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
773 if (update == UPDATE)
774 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
776 SDL_FreeSurface (sdl_surface_copy);
780 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
782 if (update == UPDATE)
783 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
789 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
799 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
802 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
803 sw, sh, sdl_surface->format->BitsPerPixel,
804 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
805 sdl_surface->format->Bmask,
808 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
809 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
811 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
812 SDL_FreeSurface(sdl_surface_copy);
814 if (update == UPDATE)
815 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
820 SurfaceSDL::~SurfaceSDL()