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
24 #include "SDL_image.h"
29 Surface::Surfaces Surface::surfaces;
31 SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
32 : type(SURFACE), surface(0), use_alpha(use_alpha_)
34 // Copy the given surface and make sure that it is not stored in
36 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
38 temp->format->BitsPerPixel,
44 st_abort("No memory left.", "");
45 SDL_SetAlpha(temp,0,0);
46 SDL_BlitSurface(temp, NULL, surface, NULL);
49 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
50 : 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_)
60 SurfaceData::~SurfaceData()
62 SDL_FreeSurface(surface);
69 return create_SurfaceOpenGL();
71 return create_SurfaceSDL();
75 SurfaceData::create_SurfaceSDL()
80 return new SurfaceSDL(file, use_alpha);
82 return new SurfaceSDL(file, x, y, w, h, use_alpha);
84 return new SurfaceSDL(surface, use_alpha);
90 SurfaceData::create_SurfaceOpenGL()
95 return new SurfaceOpenGL(file, use_alpha);
97 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
99 return new SurfaceOpenGL(surface, use_alpha);
105 /* Quick utility function for texture creation */
106 static int power_of_two(int input)
110 while ( value < input ) {
116 Surface::Surface(SDL_Surface* surf, int use_alpha)
117 : data(surf, use_alpha), w(0), h(0)
119 impl = data.create();
125 surfaces.push_back(this);
128 Surface::Surface(const std::string& file, int use_alpha)
129 : data(file, use_alpha), w(0), h(0)
131 impl = data.create();
137 surfaces.push_back(this);
140 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
141 : data(file, x, y, w, h, use_alpha), w(0), h(0)
143 impl = data.create();
149 surfaces.push_back(this);
156 impl = data.create();
166 surfaces.remove(this);
171 Surface::reload_all()
173 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
180 Surface::debug_check()
182 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
184 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
185 (*i)->data.file.c_str());
190 Surface::draw(float x, float y, Uint8 alpha, bool update)
194 if (impl->draw(x, y, alpha, update) == -2)
200 Surface::draw_bg(Uint8 alpha, bool update)
204 if (impl->draw_bg(alpha, update) == -2)
210 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
214 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
220 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
223 SDL_Surface * sdl_surface;
227 temp = IMG_Load(file.c_str());
230 st_abort("Can't load", file);
232 /* Set source rectangle for conv: */
239 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
243 temp->format->Amask);
245 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
246 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
249 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
252 SDL_SetAlpha(temp,0,0);
254 SDL_BlitSurface(temp, &src, conv, NULL);
255 if(use_alpha == IGNORE_ALPHA && !use_gl)
256 sdl_surface = SDL_DisplayFormat(conv);
258 sdl_surface = SDL_DisplayFormatAlpha(conv);
260 if (sdl_surface == NULL)
261 st_abort("Can't covert to display format", file);
263 if (use_alpha == IGNORE_ALPHA && !use_gl)
264 SDL_SetAlpha(sdl_surface, 0, 0);
266 SDL_FreeSurface(temp);
267 SDL_FreeSurface(conv);
273 sdl_surface_from_file(const std::string& file, int use_alpha)
275 SDL_Surface* sdl_surface;
278 temp = IMG_Load(file.c_str());
281 st_abort("Can't load", file);
283 if(use_alpha == IGNORE_ALPHA && !use_gl)
284 sdl_surface = SDL_DisplayFormat(temp);
286 sdl_surface = SDL_DisplayFormatAlpha(temp);
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);
300 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
302 SDL_Surface* sdl_surface;
306 /* Save the alpha blending attributes */
307 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
308 saved_alpha = sdl_surf->format->alpha;
309 if ( (saved_flags & SDL_SRCALPHA)
312 SDL_SetAlpha(sdl_surf, 0, 0);
315 if(use_alpha == IGNORE_ALPHA && !use_gl)
316 sdl_surface = SDL_DisplayFormat(sdl_surf);
318 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
320 /* Restore the alpha blending attributes */
321 if ( (saved_flags & SDL_SRCALPHA)
324 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
327 if (sdl_surface == NULL)
328 st_abort("Can't covert to display format", "SURFACE");
330 if (use_alpha == IGNORE_ALPHA && !use_gl)
331 SDL_SetAlpha(sdl_surface, 0, 0);
336 //---------------------------------------------------------------------------
338 SurfaceImpl::SurfaceImpl()
342 SurfaceImpl::~SurfaceImpl()
344 SDL_FreeSurface(sdl_surface);
347 SDL_Surface* SurfaceImpl::get_sdl_surface() const
353 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
355 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
356 create_gl(sdl_surface,&gl_texture);
362 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
364 sdl_surface = sdl_surface_from_file(file, use_alpha);
365 create_gl(sdl_surface,&gl_texture);
371 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
373 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
374 create_gl(sdl_surface, &gl_texture);
380 SurfaceOpenGL::~SurfaceOpenGL()
382 glDeleteTextures(1, &gl_texture);
386 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
393 w = power_of_two(surf->w);
394 h = power_of_two(surf->h),
396 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
397 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
398 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
400 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
401 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
404 /* Save the alpha blending attributes */
405 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
406 saved_alpha = surf->format->alpha;
407 if ( (saved_flags & SDL_SRCALPHA)
410 SDL_SetAlpha(surf, 0, 0);
413 SDL_BlitSurface(surf, 0, conv, 0);
415 /* Restore the alpha blending attributes */
416 if ( (saved_flags & SDL_SRCALPHA)
419 SDL_SetAlpha(surf, saved_flags, saved_alpha);
422 glGenTextures(1, &*tex);
423 glBindTexture(GL_TEXTURE_2D , *tex);
424 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
425 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
426 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
427 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
428 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
429 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
430 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
432 SDL_FreeSurface(conv);
436 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
438 float pw = power_of_two(w);
439 float ph = power_of_two(h);
441 glEnable(GL_TEXTURE_2D);
443 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
445 glColor4ub(alpha, alpha, alpha, alpha);
447 glBindTexture(GL_TEXTURE_2D, gl_texture);
452 glTexCoord2f((float)w / pw, 0);
453 glVertex2f((float)w+x, y);
454 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
455 glTexCoord2f(0, (float)h / ph);
456 glVertex2f(x, (float)h+y);
459 glDisable(GL_TEXTURE_2D);
462 (void) update; // avoid compiler warning
468 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
470 float pw = power_of_two(w);
471 float ph = power_of_two(h);
473 glColor3ub(alpha, alpha, alpha);
475 glEnable(GL_TEXTURE_2D);
476 glBindTexture(GL_TEXTURE_2D, gl_texture);
481 glTexCoord2f((float)w / pw, 0);
482 glVertex2f(screen->w, 0);
483 glTexCoord2f((float)w / pw, (float)h / ph);
484 glVertex2f(screen->w, screen->h);
485 glTexCoord2f(0, (float)h / ph);
486 glVertex2f(0, screen->h);
489 glDisable(GL_TEXTURE_2D);
491 (void) update; // avoid compiler warning
497 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
499 float pw = power_of_two(int(this->w));
500 float ph = power_of_two(int(this->h));
502 glBindTexture(GL_TEXTURE_2D, gl_texture);
505 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
507 glColor4ub(alpha, alpha, alpha, alpha);
509 glEnable(GL_TEXTURE_2D);
513 glTexCoord2f(sx / pw, sy / ph);
515 glTexCoord2f((float)(sx + w) / pw, sy / ph);
517 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
518 glVertex2f(w +x, h+y);
519 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
523 glDisable(GL_TEXTURE_2D);
526 (void) update; // avoid warnings
531 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
533 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
538 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
540 sdl_surface = sdl_surface_from_file(file, use_alpha);
545 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
547 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
553 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
562 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
565 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
566 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
568 if (update == UPDATE)
569 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
575 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
585 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
587 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
589 if (update == UPDATE)
590 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
596 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
611 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
613 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
615 if (update == UPDATE)
616 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
621 SurfaceSDL::~SurfaceSDL()