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::draw(float x, float y, Uint8 alpha, bool update)
214 if (impl->draw(x, y, alpha, update) == -2)
220 Surface::draw_bg(Uint8 alpha, bool update)
224 if (impl->draw_bg(alpha, update) == -2)
230 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
234 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
240 Surface::resize(int w_, int h_)
246 if (impl->resize(w_,h_) == -2)
252 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
255 SDL_Surface * sdl_surface;
259 temp = IMG_Load(file.c_str());
262 st_abort("Can't load", file);
264 /* Set source rectangle for conv: */
271 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
275 temp->format->Amask);
277 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
278 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
281 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
284 SDL_SetAlpha(temp,0,0);
286 SDL_BlitSurface(temp, &src, conv, NULL);
287 if(use_alpha == IGNORE_ALPHA && !use_gl)
288 sdl_surface = SDL_DisplayFormat(conv);
290 sdl_surface = SDL_DisplayFormatAlpha(conv);
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);
299 SDL_FreeSurface(conv);
305 sdl_surface_from_file(const std::string& file, int use_alpha)
307 SDL_Surface* sdl_surface;
310 temp = IMG_Load(file.c_str());
313 st_abort("Can't load", file);
315 if(use_alpha == IGNORE_ALPHA && !use_gl)
316 sdl_surface = SDL_DisplayFormat(temp);
318 sdl_surface = SDL_DisplayFormatAlpha(temp);
320 if (sdl_surface == NULL)
321 st_abort("Can't covert to display format", file);
323 if (use_alpha == IGNORE_ALPHA && !use_gl)
324 SDL_SetAlpha(sdl_surface, 0, 0);
326 SDL_FreeSurface(temp);
332 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
334 SDL_Surface* sdl_surface;
338 /* Save the alpha blending attributes */
339 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
340 saved_alpha = sdl_surf->format->alpha;
341 if ( (saved_flags & SDL_SRCALPHA)
344 SDL_SetAlpha(sdl_surf, 0, 0);
347 if(use_alpha == IGNORE_ALPHA && !use_gl)
348 sdl_surface = SDL_DisplayFormat(sdl_surf);
350 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
352 /* Restore the alpha blending attributes */
353 if ( (saved_flags & SDL_SRCALPHA)
356 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
359 if (sdl_surface == NULL)
360 st_abort("Can't covert to display format", "SURFACE");
362 if (use_alpha == IGNORE_ALPHA && !use_gl)
363 SDL_SetAlpha(sdl_surface, 0, 0);
368 //---------------------------------------------------------------------------
370 SurfaceImpl::SurfaceImpl()
373 SurfaceImpl::~SurfaceImpl()
375 SDL_FreeSurface(sdl_surface);
378 SDL_Surface* SurfaceImpl::get_sdl_surface() const
383 int SurfaceImpl::resize(int w_, int h_)
392 int ret = SDL_SoftStretch(sdl_surface, NULL,
398 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
400 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
401 create_gl(sdl_surface,&gl_texture);
407 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
409 sdl_surface = sdl_surface_from_file(file, use_alpha);
410 create_gl(sdl_surface,&gl_texture);
416 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
418 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
419 create_gl(sdl_surface, &gl_texture);
425 SurfaceOpenGL::~SurfaceOpenGL()
427 glDeleteTextures(1, &gl_texture);
431 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
438 w = power_of_two(surf->w);
439 h = power_of_two(surf->h),
441 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
442 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
443 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
445 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
446 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
449 /* Save the alpha blending attributes */
450 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
451 saved_alpha = surf->format->alpha;
452 if ( (saved_flags & SDL_SRCALPHA)
455 SDL_SetAlpha(surf, 0, 0);
458 SDL_BlitSurface(surf, 0, conv, 0);
460 /* Restore the alpha blending attributes */
461 if ( (saved_flags & SDL_SRCALPHA)
464 SDL_SetAlpha(surf, saved_flags, saved_alpha);
467 glGenTextures(1, &*tex);
468 glBindTexture(GL_TEXTURE_2D , *tex);
469 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
470 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
471 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
472 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
473 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
474 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
475 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
477 SDL_FreeSurface(conv);
481 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
483 float pw = power_of_two(w);
484 float ph = power_of_two(h);
486 glEnable(GL_TEXTURE_2D);
488 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
490 glColor4ub(alpha, alpha, alpha, alpha);
492 glBindTexture(GL_TEXTURE_2D, gl_texture);
497 glTexCoord2f((float)w / pw, 0);
498 glVertex2f((float)w+x, y);
499 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
500 glTexCoord2f(0, (float)h / ph);
501 glVertex2f(x, (float)h+y);
504 glDisable(GL_TEXTURE_2D);
507 (void) update; // avoid compiler warning
513 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
515 float pw = power_of_two(w);
516 float ph = power_of_two(h);
518 glColor3ub(alpha, alpha, alpha);
520 glEnable(GL_TEXTURE_2D);
521 glBindTexture(GL_TEXTURE_2D, gl_texture);
526 glTexCoord2f((float)w / pw, 0);
527 glVertex2f(screen->w, 0);
528 glTexCoord2f((float)w / pw, (float)h / ph);
529 glVertex2f(screen->w, screen->h);
530 glTexCoord2f(0, (float)h / ph);
531 glVertex2f(0, screen->h);
534 glDisable(GL_TEXTURE_2D);
536 (void) update; // avoid compiler warning
542 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
544 float pw = power_of_two(int(this->w));
545 float ph = power_of_two(int(this->h));
547 glBindTexture(GL_TEXTURE_2D, gl_texture);
550 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
552 glColor4ub(alpha, alpha, alpha, alpha);
554 glEnable(GL_TEXTURE_2D);
558 glTexCoord2f(sx / pw, sy / ph);
560 glTexCoord2f((float)(sx + w) / pw, sy / ph);
562 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
563 glVertex2f(w +x, h+y);
564 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
568 glDisable(GL_TEXTURE_2D);
571 (void) update; // avoid warnings
576 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
578 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
583 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
585 sdl_surface = sdl_surface_from_file(file, use_alpha);
590 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
592 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
598 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
609 /* Copy the SDL surface, then make it using alpha and use it to blit into the screen */
610 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
611 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
612 sdl_surface->format->Rmask, sdl_surface->format->Gmask, sdl_surface->format->Bmask,
613 sdl_surface->format->Amask);
615 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
617 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
619 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
621 if (update == UPDATE)
622 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
624 SDL_FreeSurface (sdl_surface_copy) ;
628 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
630 if (update == UPDATE)
631 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
637 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
647 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
649 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
651 if (update == UPDATE)
652 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
658 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
673 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
675 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
677 if (update == UPDATE)
678 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
683 SurfaceSDL::~SurfaceSDL()