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::resize(int w_, int h_)
216 if (impl->resize(w_,h_) == -2)
222 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
225 SDL_Surface * sdl_surface;
229 temp = IMG_Load(file.c_str());
232 st_abort("Can't load", file);
234 /* Set source rectangle for conv: */
241 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
245 temp->format->Amask);
247 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
248 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
251 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
254 SDL_SetAlpha(temp,0,0);
256 SDL_BlitSurface(temp, &src, conv, NULL);
257 if(use_alpha == IGNORE_ALPHA && !use_gl)
258 sdl_surface = SDL_DisplayFormat(conv);
260 sdl_surface = SDL_DisplayFormatAlpha(conv);
262 if (sdl_surface == NULL)
263 st_abort("Can't covert to display format", file);
265 if (use_alpha == IGNORE_ALPHA && !use_gl)
266 SDL_SetAlpha(sdl_surface, 0, 0);
268 SDL_FreeSurface(temp);
269 SDL_FreeSurface(conv);
275 sdl_surface_from_file(const std::string& file, int use_alpha)
277 SDL_Surface* sdl_surface;
280 temp = IMG_Load(file.c_str());
283 st_abort("Can't load", file);
285 if(use_alpha == IGNORE_ALPHA && !use_gl)
286 sdl_surface = SDL_DisplayFormat(temp);
288 sdl_surface = SDL_DisplayFormatAlpha(temp);
290 if (sdl_surface == NULL)
291 st_abort("Can't covert to display format", file);
293 if (use_alpha == IGNORE_ALPHA && !use_gl)
294 SDL_SetAlpha(sdl_surface, 0, 0);
296 SDL_FreeSurface(temp);
302 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
304 SDL_Surface* sdl_surface;
308 /* Save the alpha blending attributes */
309 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
310 saved_alpha = sdl_surf->format->alpha;
311 if ( (saved_flags & SDL_SRCALPHA)
314 SDL_SetAlpha(sdl_surf, 0, 0);
317 if(use_alpha == IGNORE_ALPHA && !use_gl)
318 sdl_surface = SDL_DisplayFormat(sdl_surf);
320 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
322 /* Restore the alpha blending attributes */
323 if ( (saved_flags & SDL_SRCALPHA)
326 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
329 if (sdl_surface == NULL)
330 st_abort("Can't covert to display format", "SURFACE");
332 if (use_alpha == IGNORE_ALPHA && !use_gl)
333 SDL_SetAlpha(sdl_surface, 0, 0);
338 //---------------------------------------------------------------------------
340 SurfaceImpl::SurfaceImpl()
343 SurfaceImpl::~SurfaceImpl()
345 SDL_FreeSurface(sdl_surface);
348 SDL_Surface* SurfaceImpl::get_sdl_surface() const
353 int SurfaceImpl::resize(int w_, int h_)
362 int ret = SDL_SoftStretch(sdl_surface, NULL,
368 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
370 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
371 create_gl(sdl_surface,&gl_texture);
377 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
379 sdl_surface = sdl_surface_from_file(file, use_alpha);
380 create_gl(sdl_surface,&gl_texture);
386 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
388 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
389 create_gl(sdl_surface, &gl_texture);
395 SurfaceOpenGL::~SurfaceOpenGL()
397 glDeleteTextures(1, &gl_texture);
401 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
408 w = power_of_two(surf->w);
409 h = power_of_two(surf->h),
411 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
412 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
413 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
415 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
416 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
419 /* Save the alpha blending attributes */
420 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
421 saved_alpha = surf->format->alpha;
422 if ( (saved_flags & SDL_SRCALPHA)
425 SDL_SetAlpha(surf, 0, 0);
428 SDL_BlitSurface(surf, 0, conv, 0);
430 /* Restore the alpha blending attributes */
431 if ( (saved_flags & SDL_SRCALPHA)
434 SDL_SetAlpha(surf, saved_flags, saved_alpha);
437 glGenTextures(1, &*tex);
438 glBindTexture(GL_TEXTURE_2D , *tex);
439 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
440 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
441 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
442 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
443 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
444 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
445 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
447 SDL_FreeSurface(conv);
451 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, uint32_t effect)
453 float pw = power_of_two(w);
454 float ph = power_of_two(h);
456 if(effect & SEMI_TRANSPARENT)
459 glEnable(GL_TEXTURE_2D);
461 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
463 glColor4ub(alpha, alpha, alpha, alpha);
465 glBindTexture(GL_TEXTURE_2D, gl_texture);
469 if(effect & VERTICAL_FLIP)
472 glVertex2f(x, (float)h+y);
474 glTexCoord2f((float)w / pw, 0);
475 glVertex2f((float)w+x, (float)h+y);
477 glTexCoord2f((float)w / pw, (float)h / ph);
478 glVertex2f((float)w+x, y);
480 glTexCoord2f(0, (float)h / ph);
488 glTexCoord2f((float)w / pw, 0);
489 glVertex2f((float)w+x, y);
491 glTexCoord2f((float)w / pw, (float)h / ph);
492 glVertex2f((float)w+x, (float)h+y);
494 glTexCoord2f(0, (float)h / ph);
495 glVertex2f(x, (float)h+y);
499 glDisable(GL_TEXTURE_2D);
506 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, uint32_t effect)
508 float pw = power_of_two(int(this->w));
509 float ph = power_of_two(int(this->h));
511 if(effect & SEMI_TRANSPARENT)
514 glBindTexture(GL_TEXTURE_2D, gl_texture);
517 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
519 glColor4ub(alpha, alpha, alpha, alpha);
521 glEnable(GL_TEXTURE_2D);
526 if(effect & VERTICAL_FLIP)
528 glTexCoord2f(sx / pw, sy / ph);
531 glTexCoord2f((float)(sx + w) / pw, sy / ph);
534 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
535 glVertex2f(w +x, h+y);
537 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
542 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
545 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
546 glVertex2f(w +x, h+y);
548 glTexCoord2f((float)(sx + w) / pw, sy / ph);
551 glTexCoord2f(sx / pw, sy / ph);
557 glDisable(GL_TEXTURE_2D);
565 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
567 float pw = power_of_two(int(this->w));
568 float ph = power_of_two(int(this->h));
570 glBindTexture(GL_TEXTURE_2D, gl_texture);
573 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
575 glColor4ub(alpha, alpha, alpha, alpha);
577 glEnable(GL_TEXTURE_2D);
583 glTexCoord2f((float)w / pw, 0);
585 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
586 glVertex2f(sw +x, sh+y);
587 glTexCoord2f(0, (float)h / ph);
591 glDisable(GL_TEXTURE_2D);
600 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
602 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
607 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
609 sdl_surface = sdl_surface_from_file(file, use_alpha);
614 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
616 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
622 SurfaceSDL::draw(float x, float y, Uint8 alpha, uint32_t effect)
631 if(effect & SEMI_TRANSPARENT)
634 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
636 for(float sy = 0; sy < h; sy++)
637 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
644 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
645 to temp sur, blit the temp into the screen */
646 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
647 already have an alpha mask yet... */
649 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
650 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
651 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
652 sdl_surface->format->Bmask,
654 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
655 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
656 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
659 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
660 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
662 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
664 SDL_FreeSurface (sdl_surface_copy);
668 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
674 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, uint32_t effect)
688 if(effect & SEMI_TRANSPARENT)
691 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
693 for(float sy_ = sy; sy_ < h; sy_++)
694 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
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 SDL_FreeSurface (sdl_surface_copy);
725 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
732 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
742 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
745 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
746 sw, sh, sdl_surface->format->BitsPerPixel,
747 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
748 sdl_surface->format->Bmask,
751 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
752 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
754 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
755 SDL_FreeSurface(sdl_surface_copy);
757 if (update == UPDATE)
758 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
764 SurfaceSDL::~SurfaceSDL()