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_)
55 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
56 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
57 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 ) {
124 Surface::Surface(SDL_Surface* surf, int use_alpha)
125 : data(surf, use_alpha), w(0), h(0)
127 impl = data.create();
133 surfaces.push_back(this);
136 Surface::Surface(const std::string& file, int use_alpha)
137 : data(file, use_alpha), w(0), h(0)
139 impl = data.create();
145 surfaces.push_back(this);
148 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
149 : data(file, x, y, w, h, use_alpha), w(0), h(0)
151 impl = data.create();
157 surfaces.push_back(this);
164 impl = data.create();
176 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
183 printf("Error: Surface freed twice!!!\n");
185 surfaces.remove(this);
190 Surface::reload_all()
192 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
199 Surface::debug_check()
201 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
203 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
204 (*i)->data.file.c_str());
209 Surface::draw(float x, float y, Uint8 alpha, bool update)
213 if (impl->draw(x, y, alpha, update) == -2)
219 Surface::draw_bg(Uint8 alpha, bool update)
223 if (impl->draw_bg(alpha, update) == -2)
229 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
233 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
239 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
242 SDL_Surface * sdl_surface;
246 temp = IMG_Load(file.c_str());
249 st_abort("Can't load", file);
251 /* Set source rectangle for conv: */
258 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
262 temp->format->Amask);
264 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
265 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
268 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
271 SDL_SetAlpha(temp,0,0);
273 SDL_BlitSurface(temp, &src, conv, NULL);
274 if(use_alpha == IGNORE_ALPHA && !use_gl)
275 sdl_surface = SDL_DisplayFormat(conv);
277 sdl_surface = SDL_DisplayFormatAlpha(conv);
279 if (sdl_surface == NULL)
280 st_abort("Can't covert to display format", file);
282 if (use_alpha == IGNORE_ALPHA && !use_gl)
283 SDL_SetAlpha(sdl_surface, 0, 0);
285 SDL_FreeSurface(temp);
286 SDL_FreeSurface(conv);
292 sdl_surface_from_file(const std::string& file, int use_alpha)
294 SDL_Surface* sdl_surface;
297 temp = IMG_Load(file.c_str());
300 st_abort("Can't load", file);
302 if(use_alpha == IGNORE_ALPHA && !use_gl)
303 sdl_surface = SDL_DisplayFormat(temp);
305 sdl_surface = SDL_DisplayFormatAlpha(temp);
307 if (sdl_surface == NULL)
308 st_abort("Can't covert to display format", file);
310 if (use_alpha == IGNORE_ALPHA && !use_gl)
311 SDL_SetAlpha(sdl_surface, 0, 0);
313 SDL_FreeSurface(temp);
319 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
321 SDL_Surface* sdl_surface;
325 /* Save the alpha blending attributes */
326 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
327 saved_alpha = sdl_surf->format->alpha;
328 if ( (saved_flags & SDL_SRCALPHA)
331 SDL_SetAlpha(sdl_surf, 0, 0);
334 if(use_alpha == IGNORE_ALPHA && !use_gl)
335 sdl_surface = SDL_DisplayFormat(sdl_surf);
337 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
339 /* Restore the alpha blending attributes */
340 if ( (saved_flags & SDL_SRCALPHA)
343 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
346 if (sdl_surface == NULL)
347 st_abort("Can't covert to display format", "SURFACE");
349 if (use_alpha == IGNORE_ALPHA && !use_gl)
350 SDL_SetAlpha(sdl_surface, 0, 0);
355 //---------------------------------------------------------------------------
357 SurfaceImpl::SurfaceImpl()
361 SurfaceImpl::~SurfaceImpl()
363 SDL_FreeSurface(sdl_surface);
366 SDL_Surface* SurfaceImpl::get_sdl_surface() const
372 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
374 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
375 create_gl(sdl_surface,&gl_texture);
381 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
383 sdl_surface = sdl_surface_from_file(file, use_alpha);
384 create_gl(sdl_surface,&gl_texture);
390 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
392 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
393 create_gl(sdl_surface, &gl_texture);
399 SurfaceOpenGL::~SurfaceOpenGL()
401 glDeleteTextures(1, &gl_texture);
405 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
412 w = power_of_two(surf->w);
413 h = power_of_two(surf->h),
415 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
416 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
417 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
419 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
420 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
423 /* Save the alpha blending attributes */
424 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
425 saved_alpha = surf->format->alpha;
426 if ( (saved_flags & SDL_SRCALPHA)
429 SDL_SetAlpha(surf, 0, 0);
432 SDL_BlitSurface(surf, 0, conv, 0);
434 /* Restore the alpha blending attributes */
435 if ( (saved_flags & SDL_SRCALPHA)
438 SDL_SetAlpha(surf, saved_flags, saved_alpha);
441 glGenTextures(1, &*tex);
442 glBindTexture(GL_TEXTURE_2D , *tex);
443 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
444 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
445 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
446 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
447 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
448 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
449 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
451 SDL_FreeSurface(conv);
455 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
457 float pw = power_of_two(w);
458 float ph = power_of_two(h);
460 glEnable(GL_TEXTURE_2D);
462 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
464 glColor4ub(alpha, alpha, alpha, alpha);
466 glBindTexture(GL_TEXTURE_2D, gl_texture);
471 glTexCoord2f((float)w / pw, 0);
472 glVertex2f((float)w+x, y);
473 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
474 glTexCoord2f(0, (float)h / ph);
475 glVertex2f(x, (float)h+y);
478 glDisable(GL_TEXTURE_2D);
481 (void) update; // avoid compiler warning
487 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
489 float pw = power_of_two(w);
490 float ph = power_of_two(h);
492 glColor3ub(alpha, alpha, alpha);
494 glEnable(GL_TEXTURE_2D);
495 glBindTexture(GL_TEXTURE_2D, gl_texture);
500 glTexCoord2f((float)w / pw, 0);
501 glVertex2f(screen->w, 0);
502 glTexCoord2f((float)w / pw, (float)h / ph);
503 glVertex2f(screen->w, screen->h);
504 glTexCoord2f(0, (float)h / ph);
505 glVertex2f(0, screen->h);
508 glDisable(GL_TEXTURE_2D);
510 (void) update; // avoid compiler warning
516 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
518 float pw = power_of_two(int(this->w));
519 float ph = power_of_two(int(this->h));
521 glBindTexture(GL_TEXTURE_2D, gl_texture);
524 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
526 glColor4ub(alpha, alpha, alpha, alpha);
528 glEnable(GL_TEXTURE_2D);
532 glTexCoord2f(sx / pw, sy / ph);
534 glTexCoord2f((float)(sx + w) / pw, sy / ph);
536 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
537 glVertex2f(w +x, h+y);
538 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
542 glDisable(GL_TEXTURE_2D);
545 (void) update; // avoid warnings
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, bool update)
583 /* Copy the SDL surface, then make it using alpha and use it to blit into the screen */
584 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
585 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
586 sdl_surface->format->Rmask, sdl_surface->format->Gmask, sdl_surface->format->Bmask,
587 sdl_surface->format->Amask);
589 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
591 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
593 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
595 if (update == UPDATE)
596 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
598 SDL_FreeSurface (sdl_surface_copy) ;
602 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
604 if (update == UPDATE)
605 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
611 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
621 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
623 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
625 if (update == UPDATE)
626 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
632 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
647 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
649 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
651 if (update == UPDATE)
652 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
657 SurfaceSDL::~SurfaceSDL()