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);
70 return create_SurfaceOpenGL();
72 return create_SurfaceSDL();
76 SurfaceData::create_SurfaceSDL()
81 return new SurfaceSDL(file, use_alpha);
83 return new SurfaceSDL(file, x, y, w, h, use_alpha);
85 return new SurfaceSDL(surface, use_alpha);
91 SurfaceData::create_SurfaceOpenGL()
96 return new SurfaceOpenGL(file, use_alpha);
98 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
100 return new SurfaceOpenGL(surface, use_alpha);
106 /* Quick utility function for texture creation */
107 static int power_of_two(int input)
111 while ( value < input ) {
117 Surface::Surface(SDL_Surface* surf, int use_alpha)
118 : data(surf, use_alpha), w(0), h(0)
120 impl = data.create();
126 surfaces.push_back(this);
129 Surface::Surface(const std::string& file, int use_alpha)
130 : data(file, use_alpha), w(0), h(0)
132 impl = data.create();
138 surfaces.push_back(this);
141 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
142 : data(file, x, y, w, h, use_alpha), w(0), h(0)
144 impl = data.create();
150 surfaces.push_back(this);
157 impl = data.create();
169 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
176 printf("Error: Surface freed twice!!!\n");
178 surfaces.remove(this);
183 Surface::reload_all()
185 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
192 Surface::debug_check()
194 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
196 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
197 (*i)->data.file.c_str());
202 Surface::draw(float x, float y, Uint8 alpha, bool update)
206 if (impl->draw(x, y, alpha, update) == -2)
212 Surface::draw_bg(Uint8 alpha, bool update)
216 if (impl->draw_bg(alpha, update) == -2)
222 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
226 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
232 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
235 SDL_Surface * sdl_surface;
239 temp = IMG_Load(file.c_str());
242 st_abort("Can't load", file);
244 /* Set source rectangle for conv: */
251 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
255 temp->format->Amask);
257 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
258 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
261 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
264 SDL_SetAlpha(temp,0,0);
266 SDL_BlitSurface(temp, &src, conv, NULL);
267 if(use_alpha == IGNORE_ALPHA && !use_gl)
268 sdl_surface = SDL_DisplayFormat(conv);
270 sdl_surface = SDL_DisplayFormatAlpha(conv);
272 if (sdl_surface == NULL)
273 st_abort("Can't covert to display format", file);
275 if (use_alpha == IGNORE_ALPHA && !use_gl)
276 SDL_SetAlpha(sdl_surface, 0, 0);
278 SDL_FreeSurface(temp);
279 SDL_FreeSurface(conv);
285 sdl_surface_from_file(const std::string& file, int use_alpha)
287 SDL_Surface* sdl_surface;
290 temp = IMG_Load(file.c_str());
293 st_abort("Can't load", file);
295 if(use_alpha == IGNORE_ALPHA && !use_gl)
296 sdl_surface = SDL_DisplayFormat(temp);
298 sdl_surface = SDL_DisplayFormatAlpha(temp);
300 if (sdl_surface == NULL)
301 st_abort("Can't covert to display format", file);
303 if (use_alpha == IGNORE_ALPHA && !use_gl)
304 SDL_SetAlpha(sdl_surface, 0, 0);
306 SDL_FreeSurface(temp);
312 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
314 SDL_Surface* sdl_surface;
318 /* Save the alpha blending attributes */
319 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
320 saved_alpha = sdl_surf->format->alpha;
321 if ( (saved_flags & SDL_SRCALPHA)
324 SDL_SetAlpha(sdl_surf, 0, 0);
327 if(use_alpha == IGNORE_ALPHA && !use_gl)
328 sdl_surface = SDL_DisplayFormat(sdl_surf);
330 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
332 /* Restore the alpha blending attributes */
333 if ( (saved_flags & SDL_SRCALPHA)
336 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
339 if (sdl_surface == NULL)
340 st_abort("Can't covert to display format", "SURFACE");
342 if (use_alpha == IGNORE_ALPHA && !use_gl)
343 SDL_SetAlpha(sdl_surface, 0, 0);
348 //---------------------------------------------------------------------------
350 SurfaceImpl::SurfaceImpl()
354 SurfaceImpl::~SurfaceImpl()
356 SDL_FreeSurface(sdl_surface);
359 SDL_Surface* SurfaceImpl::get_sdl_surface() const
365 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
367 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
368 create_gl(sdl_surface,&gl_texture);
374 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
376 sdl_surface = sdl_surface_from_file(file, use_alpha);
377 create_gl(sdl_surface,&gl_texture);
383 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
385 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
386 create_gl(sdl_surface, &gl_texture);
392 SurfaceOpenGL::~SurfaceOpenGL()
394 glDeleteTextures(1, &gl_texture);
398 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
405 w = power_of_two(surf->w);
406 h = power_of_two(surf->h),
408 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
409 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
410 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
412 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
413 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
416 /* Save the alpha blending attributes */
417 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
418 saved_alpha = surf->format->alpha;
419 if ( (saved_flags & SDL_SRCALPHA)
422 SDL_SetAlpha(surf, 0, 0);
425 SDL_BlitSurface(surf, 0, conv, 0);
427 /* Restore the alpha blending attributes */
428 if ( (saved_flags & SDL_SRCALPHA)
431 SDL_SetAlpha(surf, saved_flags, saved_alpha);
434 glGenTextures(1, &*tex);
435 glBindTexture(GL_TEXTURE_2D , *tex);
436 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
437 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
438 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
439 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
440 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
441 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
442 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
444 SDL_FreeSurface(conv);
448 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
450 float pw = power_of_two(w);
451 float ph = power_of_two(h);
453 glEnable(GL_TEXTURE_2D);
455 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
457 glColor4ub(alpha, alpha, alpha, alpha);
459 glBindTexture(GL_TEXTURE_2D, gl_texture);
464 glTexCoord2f((float)w / pw, 0);
465 glVertex2f((float)w+x, y);
466 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
467 glTexCoord2f(0, (float)h / ph);
468 glVertex2f(x, (float)h+y);
471 glDisable(GL_TEXTURE_2D);
474 (void) update; // avoid compiler warning
480 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
482 float pw = power_of_two(w);
483 float ph = power_of_two(h);
485 glColor3ub(alpha, alpha, alpha);
487 glEnable(GL_TEXTURE_2D);
488 glBindTexture(GL_TEXTURE_2D, gl_texture);
493 glTexCoord2f((float)w / pw, 0);
494 glVertex2f(screen->w, 0);
495 glTexCoord2f((float)w / pw, (float)h / ph);
496 glVertex2f(screen->w, screen->h);
497 glTexCoord2f(0, (float)h / ph);
498 glVertex2f(0, screen->h);
501 glDisable(GL_TEXTURE_2D);
503 (void) update; // avoid compiler warning
509 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
511 float pw = power_of_two(int(this->w));
512 float ph = power_of_two(int(this->h));
514 glBindTexture(GL_TEXTURE_2D, gl_texture);
517 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
519 glColor4ub(alpha, alpha, alpha, alpha);
521 glEnable(GL_TEXTURE_2D);
525 glTexCoord2f(sx / pw, sy / ph);
527 glTexCoord2f((float)(sx + w) / pw, sy / ph);
529 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
530 glVertex2f(w +x, h+y);
531 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
535 glDisable(GL_TEXTURE_2D);
538 (void) update; // avoid warnings
543 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
545 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
550 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
552 sdl_surface = sdl_surface_from_file(file, use_alpha);
557 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
559 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
565 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
574 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
577 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
578 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
580 if (update == UPDATE)
581 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
587 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
597 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
599 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
601 if (update == UPDATE)
602 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
608 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
623 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
625 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
627 if (update == UPDATE)
628 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
633 SurfaceSDL::~SurfaceSDL()