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 Uint32 rmask, gmask, bmask, amask;
371 /* SDL interprets each pixel as a 32-bit number, so our masks must depend
372 on the endianness (byte order) of the machine */
373 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
385 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
386 screen->format->BitsPerPixel, rmask, gmask, bmask, amask);
388 if(sdl_surface == NULL)
389 st_abort("Cannot create surface for the gradient", "SURFACE");
391 float redstep = (float(bottom.red)-float(top.red)) / float(h);
392 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
393 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
399 for(float y = 0; y < h; y++)
402 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
403 int(float(top.red) + redstep * y),
404 int(float(top.green) + greenstep * y),
405 int(float(top.blue) + bluestep * y)));
411 //---------------------------------------------------------------------------
413 SurfaceImpl::SurfaceImpl()
416 SurfaceImpl::~SurfaceImpl()
418 SDL_FreeSurface(sdl_surface);
421 SDL_Surface* SurfaceImpl::get_sdl_surface() const
426 int SurfaceImpl::resize(int w_, int h_)
435 int ret = SDL_SoftStretch(sdl_surface, NULL,
441 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
443 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
444 create_gl(sdl_surface,&gl_texture);
450 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
452 sdl_surface = sdl_surface_from_file(file, use_alpha);
453 create_gl(sdl_surface,&gl_texture);
459 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
461 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
462 create_gl(sdl_surface, &gl_texture);
468 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
470 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
471 create_gl(sdl_surface, &gl_texture);
477 SurfaceOpenGL::~SurfaceOpenGL()
479 glDeleteTextures(1, &gl_texture);
483 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
490 w = power_of_two(surf->w);
491 h = power_of_two(surf->h),
493 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
494 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
495 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
497 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
498 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
501 /* Save the alpha blending attributes */
502 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
503 saved_alpha = surf->format->alpha;
504 if ( (saved_flags & SDL_SRCALPHA)
507 SDL_SetAlpha(surf, 0, 0);
510 SDL_BlitSurface(surf, 0, conv, 0);
512 /* Restore the alpha blending attributes */
513 if ( (saved_flags & SDL_SRCALPHA)
516 SDL_SetAlpha(surf, saved_flags, saved_alpha);
519 glGenTextures(1, &*tex);
520 glBindTexture(GL_TEXTURE_2D , *tex);
521 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
522 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
523 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
524 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
525 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
526 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
527 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
529 SDL_FreeSurface(conv);
533 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
535 float pw = power_of_two(w);
536 float ph = power_of_two(h);
538 if(effect & SEMI_TRANSPARENT)
541 glEnable(GL_TEXTURE_2D);
543 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
545 glColor4ub(alpha, alpha, alpha, alpha);
547 glBindTexture(GL_TEXTURE_2D, gl_texture);
551 if(effect & VERTICAL_FLIP)
554 glVertex2f(x, (float)h+y);
556 glTexCoord2f((float)w / pw, 0);
557 glVertex2f((float)w+x, (float)h+y);
559 glTexCoord2f((float)w / pw, (float)h / ph);
560 glVertex2f((float)w+x, y);
562 glTexCoord2f(0, (float)h / ph);
570 glTexCoord2f((float)w / pw, 0);
571 glVertex2f((float)w+x, y);
573 glTexCoord2f((float)w / pw, (float)h / ph);
574 glVertex2f((float)w+x, (float)h+y);
576 glTexCoord2f(0, (float)h / ph);
577 glVertex2f(x, (float)h+y);
581 glDisable(GL_TEXTURE_2D);
588 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
590 float pw = power_of_two(int(this->w));
591 float ph = power_of_two(int(this->h));
593 if(effect & SEMI_TRANSPARENT)
596 glBindTexture(GL_TEXTURE_2D, gl_texture);
599 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
601 glColor4ub(alpha, alpha, alpha, alpha);
603 glEnable(GL_TEXTURE_2D);
608 if(effect & VERTICAL_FLIP)
610 glTexCoord2f(sx / pw, sy / ph);
613 glTexCoord2f((float)(sx + w) / pw, sy / ph);
616 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
617 glVertex2f(w +x, h+y);
619 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
624 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
627 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
628 glVertex2f(w +x, h+y);
630 glTexCoord2f((float)(sx + w) / pw, sy / ph);
633 glTexCoord2f(sx / pw, sy / ph);
639 glDisable(GL_TEXTURE_2D);
647 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
649 float pw = power_of_two(int(this->w));
650 float ph = power_of_two(int(this->h));
652 glBindTexture(GL_TEXTURE_2D, gl_texture);
655 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
657 glColor4ub(alpha, alpha, alpha, alpha);
659 glEnable(GL_TEXTURE_2D);
665 glTexCoord2f((float)w / pw, 0);
667 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
668 glVertex2f(sw +x, sh+y);
669 glTexCoord2f(0, (float)h / ph);
673 glDisable(GL_TEXTURE_2D);
682 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
684 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
689 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
691 sdl_surface = sdl_surface_from_file(file, use_alpha);
696 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
698 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
703 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
705 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
711 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
720 if(effect & SEMI_TRANSPARENT)
723 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
725 for(float sy = 0; sy < h; sy++)
726 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
733 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
734 to temp sur, blit the temp into the screen */
735 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
736 already have an alpha mask yet... */
738 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
739 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
740 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
741 sdl_surface->format->Bmask,
743 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
744 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
745 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
748 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
749 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
751 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
753 SDL_FreeSurface (sdl_surface_copy);
757 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
763 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
777 if(effect & SEMI_TRANSPARENT)
780 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
782 for(float sy_ = sy; sy_ < h; sy_++)
783 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
790 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
791 to temp sur, blit the temp into the screen */
792 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
793 already have an alpha mask yet... */
795 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
796 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
797 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
798 sdl_surface->format->Bmask,
800 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
801 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
802 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
805 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
806 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
808 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
810 SDL_FreeSurface (sdl_surface_copy);
814 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
821 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
831 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
834 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
835 sw, sh, sdl_surface->format->BitsPerPixel,
836 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
837 sdl_surface->format->Bmask,
840 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
841 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
843 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
844 SDL_FreeSurface(sdl_surface_copy);
846 if (update == UPDATE)
847 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
853 SurfaceSDL::~SurfaceSDL()