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()
63 SDL_FreeSurface(surface);
71 return create_SurfaceOpenGL();
73 return create_SurfaceSDL();
75 return create_SurfaceSDL();
80 SurfaceData::create_SurfaceSDL()
85 return new SurfaceSDL(file, use_alpha);
87 return new SurfaceSDL(file, x, y, w, h, use_alpha);
89 return new SurfaceSDL(surface, use_alpha);
95 SurfaceData::create_SurfaceOpenGL()
101 return new SurfaceOpenGL(file, use_alpha);
103 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
105 return new SurfaceOpenGL(surface, use_alpha);
112 /* Quick utility function for texture creation */
113 static int power_of_two(int input)
117 while ( value < input )
125 Surface::Surface(SDL_Surface* surf, int use_alpha)
126 : data(surf, use_alpha), w(0), h(0)
128 impl = data.create();
134 surfaces.push_back(this);
137 Surface::Surface(const std::string& file, int use_alpha)
138 : data(file, use_alpha), w(0), h(0)
140 impl = data.create();
146 surfaces.push_back(this);
149 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
150 : data(file, x, y, w, h, use_alpha), w(0), h(0)
152 impl = data.create();
158 surfaces.push_back(this);
165 impl = data.create();
177 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
186 printf("Error: Surface freed twice!!!\n");
188 surfaces.remove(this);
193 Surface::reload_all()
195 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
202 Surface::debug_check()
204 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
206 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
207 (*i)->data.file.c_str());
212 Surface::resize(int w_, int h_)
218 if (impl->resize(w_,h_) == -2)
224 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
227 SDL_Surface * sdl_surface;
231 temp = IMG_Load(file.c_str());
234 st_abort("Can't load", file);
236 /* Set source rectangle for conv: */
243 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
247 temp->format->Amask);
249 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
250 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
253 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
256 SDL_SetAlpha(temp,0,0);
258 SDL_BlitSurface(temp, &src, conv, NULL);
259 if(use_alpha == IGNORE_ALPHA && !use_gl)
260 sdl_surface = SDL_DisplayFormat(conv);
262 sdl_surface = SDL_DisplayFormatAlpha(conv);
264 if (sdl_surface == NULL)
265 st_abort("Can't covert to display format", file);
267 if (use_alpha == IGNORE_ALPHA && !use_gl)
268 SDL_SetAlpha(sdl_surface, 0, 0);
270 SDL_FreeSurface(temp);
271 SDL_FreeSurface(conv);
277 sdl_surface_from_file(const std::string& file, int use_alpha)
279 SDL_Surface* sdl_surface;
282 temp = IMG_Load(file.c_str());
285 st_abort("Can't load", file);
287 if(use_alpha == IGNORE_ALPHA && !use_gl)
288 sdl_surface = SDL_DisplayFormat(temp);
290 sdl_surface = SDL_DisplayFormatAlpha(temp);
292 if (sdl_surface == NULL)
293 st_abort("Can't covert to display format", file);
295 if (use_alpha == IGNORE_ALPHA && !use_gl)
296 SDL_SetAlpha(sdl_surface, 0, 0);
298 SDL_FreeSurface(temp);
304 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
306 SDL_Surface* sdl_surface;
310 /* Save the alpha blending attributes */
311 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
312 saved_alpha = sdl_surf->format->alpha;
313 if ( (saved_flags & SDL_SRCALPHA)
316 SDL_SetAlpha(sdl_surf, 0, 0);
319 if(use_alpha == IGNORE_ALPHA && !use_gl)
320 sdl_surface = SDL_DisplayFormat(sdl_surf);
322 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
324 /* Restore the alpha blending attributes */
325 if ( (saved_flags & SDL_SRCALPHA)
328 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
331 if (sdl_surface == NULL)
332 st_abort("Can't covert to display format", "SURFACE");
334 if (use_alpha == IGNORE_ALPHA && !use_gl)
335 SDL_SetAlpha(sdl_surface, 0, 0);
340 //---------------------------------------------------------------------------
342 SurfaceImpl::SurfaceImpl()
345 SurfaceImpl::~SurfaceImpl()
347 SDL_FreeSurface(sdl_surface);
350 SDL_Surface* SurfaceImpl::get_sdl_surface() const
355 int SurfaceImpl::resize(int w_, int h_)
364 int ret = SDL_SoftStretch(sdl_surface, NULL,
370 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
372 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
373 create_gl(sdl_surface,&gl_texture);
379 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
381 sdl_surface = sdl_surface_from_file(file, use_alpha);
382 create_gl(sdl_surface,&gl_texture);
388 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
390 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
391 create_gl(sdl_surface, &gl_texture);
397 SurfaceOpenGL::~SurfaceOpenGL()
399 glDeleteTextures(1, &gl_texture);
403 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
410 w = power_of_two(surf->w);
411 h = power_of_two(surf->h),
413 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
414 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
415 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
417 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
418 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
421 /* Save the alpha blending attributes */
422 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
423 saved_alpha = surf->format->alpha;
424 if ( (saved_flags & SDL_SRCALPHA)
427 SDL_SetAlpha(surf, 0, 0);
430 SDL_BlitSurface(surf, 0, conv, 0);
432 /* Restore the alpha blending attributes */
433 if ( (saved_flags & SDL_SRCALPHA)
436 SDL_SetAlpha(surf, saved_flags, saved_alpha);
439 glGenTextures(1, &*tex);
440 glBindTexture(GL_TEXTURE_2D , *tex);
441 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
442 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
443 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
444 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
445 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
446 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
447 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
449 SDL_FreeSurface(conv);
453 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
455 float pw = power_of_two(w);
456 float ph = power_of_two(h);
458 if(effect & SEMI_TRANSPARENT)
461 glEnable(GL_TEXTURE_2D);
463 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
465 glColor4ub(alpha, alpha, alpha, alpha);
467 glBindTexture(GL_TEXTURE_2D, gl_texture);
471 if(effect & VERTICAL_FLIP)
474 glVertex2f(x, (float)h+y);
476 glTexCoord2f((float)w / pw, 0);
477 glVertex2f((float)w+x, (float)h+y);
479 glTexCoord2f((float)w / pw, (float)h / ph);
480 glVertex2f((float)w+x, y);
482 glTexCoord2f(0, (float)h / ph);
490 glTexCoord2f((float)w / pw, 0);
491 glVertex2f((float)w+x, y);
493 glTexCoord2f((float)w / pw, (float)h / ph);
494 glVertex2f((float)w+x, (float)h+y);
496 glTexCoord2f(0, (float)h / ph);
497 glVertex2f(x, (float)h+y);
501 glDisable(GL_TEXTURE_2D);
508 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
510 float pw = power_of_two(int(this->w));
511 float ph = power_of_two(int(this->h));
513 if(effect & SEMI_TRANSPARENT)
516 glBindTexture(GL_TEXTURE_2D, gl_texture);
519 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
521 glColor4ub(alpha, alpha, alpha, alpha);
523 glEnable(GL_TEXTURE_2D);
528 if(effect & VERTICAL_FLIP)
530 glTexCoord2f(sx / pw, sy / ph);
533 glTexCoord2f((float)(sx + w) / pw, sy / ph);
536 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
537 glVertex2f(w +x, h+y);
539 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
544 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
547 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
548 glVertex2f(w +x, h+y);
550 glTexCoord2f((float)(sx + w) / pw, sy / ph);
553 glTexCoord2f(sx / pw, sy / ph);
559 glDisable(GL_TEXTURE_2D);
567 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
569 float pw = power_of_two(int(this->w));
570 float ph = power_of_two(int(this->h));
572 glBindTexture(GL_TEXTURE_2D, gl_texture);
575 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
577 glColor4ub(alpha, alpha, alpha, alpha);
579 glEnable(GL_TEXTURE_2D);
585 glTexCoord2f((float)w / pw, 0);
587 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
588 glVertex2f(sw +x, sh+y);
589 glTexCoord2f(0, (float)h / ph);
593 glDisable(GL_TEXTURE_2D);
602 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
604 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
609 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
611 sdl_surface = sdl_surface_from_file(file, use_alpha);
616 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
618 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
624 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
633 if(effect & SEMI_TRANSPARENT)
636 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
638 for(float sy = 0; sy < h; sy++)
639 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
646 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
647 to temp sur, blit the temp into the screen */
648 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
649 already have an alpha mask yet... */
651 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
652 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
653 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
654 sdl_surface->format->Bmask,
656 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
657 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
658 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
661 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
662 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
664 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
666 SDL_FreeSurface (sdl_surface_copy);
670 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
676 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
690 if(effect & SEMI_TRANSPARENT)
693 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
695 for(float sy_ = sy; sy_ < h; sy_++)
696 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
703 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
704 to temp sur, blit the temp into the screen */
705 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
706 already have an alpha mask yet... */
708 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
709 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
710 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
711 sdl_surface->format->Bmask,
713 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
714 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
715 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
718 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
719 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
721 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
723 SDL_FreeSurface (sdl_surface_copy);
727 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
734 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
744 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
747 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
748 sw, sh, sdl_surface->format->BitsPerPixel,
749 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
750 sdl_surface->format->Bmask,
753 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
754 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
756 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
757 SDL_FreeSurface(sdl_surface_copy);
759 if (update == UPDATE)
760 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
766 SurfaceSDL::~SurfaceSDL()