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), 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,
43 SDL_SetAlpha(temp,0,0);
44 SDL_BlitSurface(temp, NULL, surface, NULL);
47 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
48 : type(LOAD), file(file_), use_alpha(use_alpha_)
52 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
53 : type(LOAD_PART), file(file_), use_alpha(use_alpha_),
54 x(x_), y(y_), w(w_), h(h_)
58 SurfaceData::~SurfaceData()
67 return create_SurfaceOpenGL();
69 return create_SurfaceSDL();
73 SurfaceData::create_SurfaceSDL()
78 return new SurfaceSDL(file, use_alpha);
80 return new SurfaceSDL(file, x, y, w, h, use_alpha);
82 return new SurfaceSDL(surface, use_alpha);
88 SurfaceData::create_SurfaceOpenGL()
93 return new SurfaceOpenGL(file, use_alpha);
95 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
97 return new SurfaceOpenGL(surface, use_alpha);
103 /* Quick utility function for texture creation */
104 static int power_of_two(int input)
108 while ( value < input ) {
114 Surface::Surface(SDL_Surface* surf, int use_alpha)
115 : data(surf, use_alpha), w(0), h(0)
117 impl = data.create();
123 surfaces.push_back(this);
126 Surface::Surface(const std::string& file, int use_alpha)
127 : data(file, use_alpha), w(0), h(0)
129 impl = data.create();
135 surfaces.push_back(this);
138 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
139 : data(file, x, y, w, h, use_alpha), w(0), h(0)
141 impl = data.create();
147 surfaces.push_back(this);
154 impl = data.create();
164 surfaces.remove(this);
169 Surface::reload_all()
171 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
178 Surface::draw(float x, float y, Uint8 alpha, bool update)
182 if (impl->draw(x, y, alpha, update) == -2)
188 Surface::draw_bg(Uint8 alpha, bool update)
192 if (impl->draw_bg(alpha, update) == -2)
198 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
202 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
208 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
211 SDL_Surface * sdl_surface;
215 temp = IMG_Load(file.c_str());
218 st_abort("Can't load", file);
220 /* Set source rectangle for conv: */
227 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
231 temp->format->Amask);
233 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
234 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
237 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
240 SDL_SetAlpha(temp,0,0);
242 SDL_BlitSurface(temp, &src, conv, NULL);
243 if(use_alpha == IGNORE_ALPHA && !use_gl)
244 sdl_surface = SDL_DisplayFormat(conv);
246 sdl_surface = SDL_DisplayFormatAlpha(conv);
248 if (sdl_surface == NULL)
249 st_abort("Can't covert to display format", file);
251 if (use_alpha == IGNORE_ALPHA && !use_gl)
252 SDL_SetAlpha(sdl_surface, 0, 0);
254 SDL_FreeSurface(temp);
255 SDL_FreeSurface(conv);
261 sdl_surface_from_file(const std::string& file, int use_alpha)
263 SDL_Surface* sdl_surface;
266 temp = IMG_Load(file.c_str());
269 st_abort("Can't load", file);
271 if(use_alpha == IGNORE_ALPHA && !use_gl)
272 sdl_surface = SDL_DisplayFormat(temp);
274 sdl_surface = SDL_DisplayFormatAlpha(temp);
276 if (sdl_surface == NULL)
277 st_abort("Can't covert to display format", file);
279 if (use_alpha == IGNORE_ALPHA && !use_gl)
280 SDL_SetAlpha(sdl_surface, 0, 0);
282 SDL_FreeSurface(temp);
288 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
290 SDL_Surface* sdl_surface;
294 /* Save the alpha blending attributes */
295 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
296 saved_alpha = sdl_surf->format->alpha;
297 if ( (saved_flags & SDL_SRCALPHA)
300 SDL_SetAlpha(sdl_surf, 0, 0);
303 if(use_alpha == IGNORE_ALPHA && !use_gl)
304 sdl_surface = SDL_DisplayFormat(sdl_surf);
306 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
308 /* Restore the alpha blending attributes */
309 if ( (saved_flags & SDL_SRCALPHA)
312 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
315 if (sdl_surface == NULL)
316 st_abort("Can't covert to display format", "SURFACE");
318 if (use_alpha == IGNORE_ALPHA && !use_gl)
319 SDL_SetAlpha(sdl_surface, 0, 0);
325 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
327 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
328 create_gl(sdl_surface,&gl_texture);
334 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
336 sdl_surface = sdl_surface_from_file(file, use_alpha);
337 create_gl(sdl_surface,&gl_texture);
343 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
345 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
346 create_gl(sdl_surface, &gl_texture);
352 SurfaceOpenGL::~SurfaceOpenGL()
354 SDL_FreeSurface(sdl_surface);
355 glDeleteTextures(1, &gl_texture);
359 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
366 w = power_of_two(surf->w);
367 h = power_of_two(surf->h),
369 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
370 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
371 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
373 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
374 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
377 /* Save the alpha blending attributes */
378 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
379 saved_alpha = surf->format->alpha;
380 if ( (saved_flags & SDL_SRCALPHA)
383 SDL_SetAlpha(surf, 0, 0);
386 SDL_BlitSurface(surf, 0, conv, 0);
388 /* Restore the alpha blending attributes */
389 if ( (saved_flags & SDL_SRCALPHA)
392 SDL_SetAlpha(surf, saved_flags, saved_alpha);
395 glGenTextures(1, &*tex);
396 glBindTexture(GL_TEXTURE_2D , *tex);
397 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
398 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
401 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
402 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
403 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
405 SDL_FreeSurface(conv);
409 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
411 float pw = power_of_two(w);
412 float ph = power_of_two(h);
414 glEnable(GL_TEXTURE_2D);
416 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
418 glColor4ub(alpha, alpha, alpha, alpha);
420 glBindTexture(GL_TEXTURE_2D, gl_texture);
425 glTexCoord2f((float)w / pw, 0);
426 glVertex2f((float)w+x, y);
427 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
428 glTexCoord2f(0, (float)h / ph);
429 glVertex2f(x, (float)h+y);
432 glDisable(GL_TEXTURE_2D);
435 /* Avoid compiler warnings */
443 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
445 float pw = power_of_two(w);
446 float ph = power_of_two(h);
448 glColor3ub(alpha, alpha, alpha);
450 glEnable(GL_TEXTURE_2D);
451 glBindTexture(GL_TEXTURE_2D, gl_texture);
456 glTexCoord2f((float)w / pw, 0);
457 glVertex2f(screen->w, 0);
458 glTexCoord2f((float)w / pw, (float)h / ph);
459 glVertex2f(screen->w, screen->h);
460 glTexCoord2f(0, (float)h / ph);
461 glVertex2f(0, screen->h);
464 glDisable(GL_TEXTURE_2D);
466 /* Avoid compiler warnings */
474 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
476 float pw = power_of_two(int(this->w));
477 float ph = power_of_two(int(this->h));
479 glBindTexture(GL_TEXTURE_2D, gl_texture);
482 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
484 glColor4ub(alpha, alpha, alpha, alpha);
486 glEnable(GL_TEXTURE_2D);
490 glTexCoord2f(sx / pw, sy / ph);
492 glTexCoord2f((float)(sx + w) / pw, sy / ph);
494 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
495 glVertex2f(w +x, h+y);
496 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
500 glDisable(GL_TEXTURE_2D);
503 /* Avoid compiler warnings */
510 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
512 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
517 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
519 sdl_surface = sdl_surface_from_file(file, use_alpha);
524 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
526 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
532 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
541 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
544 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
545 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
547 if (update == UPDATE)
548 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
554 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
564 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
566 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
568 if (update == UPDATE)
569 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
575 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
590 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
592 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
594 if (update == UPDATE)
595 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
600 SurfaceSDL::~SurfaceSDL()
602 SDL_FreeSurface(sdl_surface);