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)
453 float pw = power_of_two(w);
454 float ph = power_of_two(h);
456 glEnable(GL_TEXTURE_2D);
458 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
460 glColor4ub(alpha, alpha, alpha, alpha);
462 glBindTexture(GL_TEXTURE_2D, gl_texture);
467 glTexCoord2f((float)w / pw, 0);
468 glVertex2f((float)w+x, y);
469 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
470 glTexCoord2f(0, (float)h / ph);
471 glVertex2f(x, (float)h+y);
474 glDisable(GL_TEXTURE_2D);
481 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha)
483 float pw = power_of_two(int(this->w));
484 float ph = power_of_two(int(this->h));
486 glBindTexture(GL_TEXTURE_2D, gl_texture);
489 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
491 glColor4ub(alpha, alpha, alpha, alpha);
493 glEnable(GL_TEXTURE_2D);
497 glTexCoord2f(sx / pw, sy / ph);
499 glTexCoord2f((float)(sx + w) / pw, sy / ph);
501 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
502 glVertex2f(w +x, h+y);
503 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
507 glDisable(GL_TEXTURE_2D);
515 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
517 float pw = power_of_two(int(this->w));
518 float ph = power_of_two(int(this->h));
520 glBindTexture(GL_TEXTURE_2D, gl_texture);
523 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
525 glColor4ub(alpha, alpha, alpha, alpha);
527 glEnable(GL_TEXTURE_2D);
533 glTexCoord2f((float)w / pw, 0);
535 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
536 glVertex2f(sw +x, sh+y);
537 glTexCoord2f(0, (float)h / ph);
541 glDisable(GL_TEXTURE_2D);
550 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
552 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
557 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
559 sdl_surface = sdl_surface_from_file(file, use_alpha);
564 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
566 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
572 SurfaceSDL::draw(float x, float y, Uint8 alpha)
583 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
584 to temp sur, blit the temp into the screen */
585 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
586 already have an alpha mask yet... */
588 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
589 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
590 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
591 sdl_surface->format->Bmask,
593 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
594 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
595 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
598 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
599 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
601 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
603 SDL_FreeSurface (sdl_surface_copy);
607 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
613 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha)
629 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
630 to temp sur, blit the temp into the screen */
631 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
632 already have an alpha mask yet... */
634 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
635 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
636 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
637 sdl_surface->format->Bmask,
639 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
640 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
641 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
644 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
645 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
647 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
649 SDL_FreeSurface (sdl_surface_copy);
653 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
660 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
670 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
673 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
674 sw, sh, sdl_surface->format->BitsPerPixel,
675 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
676 sdl_surface->format->Bmask,
679 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
680 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
682 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
683 SDL_FreeSurface(sdl_surface_copy);
685 if (update == UPDATE)
686 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
692 SurfaceSDL::~SurfaceSDL()