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 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
370 screen->format->BitsPerPixel, screen->format->Rmask,
371 screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
373 if(sdl_surface == NULL)
374 st_abort("Cannot create surface for the gradient", "SURFACE");
376 float redstep = (float(bottom.red)-float(top.red)) / float(h);
377 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
378 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
384 for(float y = 0; y < h; y++)
387 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
388 int(float(top.red) + redstep * y),
389 int(float(top.green) + greenstep * y),
390 int(float(top.blue) + bluestep * y)));
396 //---------------------------------------------------------------------------
398 SurfaceImpl::SurfaceImpl()
401 SurfaceImpl::~SurfaceImpl()
403 SDL_FreeSurface(sdl_surface);
406 SDL_Surface* SurfaceImpl::get_sdl_surface() const
411 int SurfaceImpl::resize(int w_, int h_)
420 int ret = SDL_SoftStretch(sdl_surface, NULL,
426 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
428 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
429 create_gl(sdl_surface,&gl_texture);
435 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
437 sdl_surface = sdl_surface_from_file(file, use_alpha);
438 create_gl(sdl_surface,&gl_texture);
444 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
446 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
447 create_gl(sdl_surface, &gl_texture);
453 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
455 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
456 create_gl(sdl_surface, &gl_texture);
462 SurfaceOpenGL::~SurfaceOpenGL()
464 glDeleteTextures(1, &gl_texture);
468 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
475 w = power_of_two(surf->w);
476 h = power_of_two(surf->h),
478 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
479 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
480 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
482 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
483 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
486 /* Save the alpha blending attributes */
487 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
488 saved_alpha = surf->format->alpha;
489 if ( (saved_flags & SDL_SRCALPHA)
492 SDL_SetAlpha(surf, 0, 0);
495 SDL_BlitSurface(surf, 0, conv, 0);
497 /* Restore the alpha blending attributes */
498 if ( (saved_flags & SDL_SRCALPHA)
501 SDL_SetAlpha(surf, saved_flags, saved_alpha);
504 glGenTextures(1, &*tex);
505 glBindTexture(GL_TEXTURE_2D , *tex);
506 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
507 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
508 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
509 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
510 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
511 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
512 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
514 SDL_FreeSurface(conv);
518 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
520 float pw = power_of_two(w);
521 float ph = power_of_two(h);
523 if(effect & SEMI_TRANSPARENT)
526 glEnable(GL_TEXTURE_2D);
528 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
530 glColor4ub(alpha, alpha, alpha, alpha);
532 glBindTexture(GL_TEXTURE_2D, gl_texture);
536 if(effect & VERTICAL_FLIP)
539 glVertex2f(x, (float)h+y);
541 glTexCoord2f((float)w / pw, 0);
542 glVertex2f((float)w+x, (float)h+y);
544 glTexCoord2f((float)w / pw, (float)h / ph);
545 glVertex2f((float)w+x, y);
547 glTexCoord2f(0, (float)h / ph);
555 glTexCoord2f((float)w / pw, 0);
556 glVertex2f((float)w+x, y);
558 glTexCoord2f((float)w / pw, (float)h / ph);
559 glVertex2f((float)w+x, (float)h+y);
561 glTexCoord2f(0, (float)h / ph);
562 glVertex2f(x, (float)h+y);
566 glDisable(GL_TEXTURE_2D);
573 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
575 float pw = power_of_two(int(this->w));
576 float ph = power_of_two(int(this->h));
578 if(effect & SEMI_TRANSPARENT)
581 glBindTexture(GL_TEXTURE_2D, gl_texture);
584 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
586 glColor4ub(alpha, alpha, alpha, alpha);
588 glEnable(GL_TEXTURE_2D);
593 if(effect & VERTICAL_FLIP)
595 glTexCoord2f(sx / pw, sy / ph);
598 glTexCoord2f((float)(sx + w) / pw, sy / ph);
601 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
602 glVertex2f(w +x, h+y);
604 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
609 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
612 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
613 glVertex2f(w +x, h+y);
615 glTexCoord2f((float)(sx + w) / pw, sy / ph);
618 glTexCoord2f(sx / pw, sy / ph);
624 glDisable(GL_TEXTURE_2D);
632 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
634 float pw = power_of_two(int(this->w));
635 float ph = power_of_two(int(this->h));
637 glBindTexture(GL_TEXTURE_2D, gl_texture);
640 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
642 glColor4ub(alpha, alpha, alpha, alpha);
644 glEnable(GL_TEXTURE_2D);
650 glTexCoord2f((float)w / pw, 0);
652 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
653 glVertex2f(sw +x, sh+y);
654 glTexCoord2f(0, (float)h / ph);
658 glDisable(GL_TEXTURE_2D);
667 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
669 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
674 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
676 sdl_surface = sdl_surface_from_file(file, use_alpha);
681 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
683 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
688 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
690 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
696 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
705 if(effect & SEMI_TRANSPARENT)
708 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
710 for(float sy = 0; sy < h; sy++)
711 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
718 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
719 to temp sur, blit the temp into the screen */
720 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
721 already have an alpha mask yet... */
723 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
724 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
725 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
726 sdl_surface->format->Bmask,
728 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
729 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
730 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
733 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
734 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
736 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
738 SDL_FreeSurface (sdl_surface_copy);
742 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
748 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
762 if(effect & SEMI_TRANSPARENT)
765 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
767 for(float sy_ = sy; sy_ < h; sy_++)
768 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
775 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
776 to temp sur, blit the temp into the screen */
777 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
778 already have an alpha mask yet... */
780 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
781 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
782 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
783 sdl_surface->format->Bmask,
785 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
786 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
787 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
790 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
791 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
793 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
795 SDL_FreeSurface (sdl_surface_copy);
799 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
806 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
816 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
819 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
820 sw, sh, sdl_surface->format->BitsPerPixel,
821 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
822 sdl_surface->format->Bmask,
825 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
826 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
828 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
829 SDL_FreeSurface(sdl_surface_copy);
831 if (update == UPDATE)
832 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
838 SurfaceSDL::~SurfaceSDL()