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::draw(float x, float y, Uint8 alpha, bool update)
184 if (impl->draw(x, y, alpha, update) == -2)
190 Surface::draw_bg(Uint8 alpha, bool update)
194 if (impl->draw_bg(alpha, update) == -2)
200 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
204 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
210 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
213 SDL_Surface * sdl_surface;
217 temp = IMG_Load(file.c_str());
220 st_abort("Can't load", file);
222 /* Set source rectangle for conv: */
229 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
233 temp->format->Amask);
235 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
236 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
239 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
242 SDL_SetAlpha(temp,0,0);
244 SDL_BlitSurface(temp, &src, conv, NULL);
245 if(use_alpha == IGNORE_ALPHA && !use_gl)
246 sdl_surface = SDL_DisplayFormat(conv);
248 sdl_surface = SDL_DisplayFormatAlpha(conv);
250 if (sdl_surface == NULL)
251 st_abort("Can't covert to display format", file);
253 if (use_alpha == IGNORE_ALPHA && !use_gl)
254 SDL_SetAlpha(sdl_surface, 0, 0);
256 SDL_FreeSurface(temp);
257 SDL_FreeSurface(conv);
263 sdl_surface_from_file(const std::string& file, int use_alpha)
265 SDL_Surface* sdl_surface;
268 temp = IMG_Load(file.c_str());
271 st_abort("Can't load", file);
273 if(use_alpha == IGNORE_ALPHA && !use_gl)
274 sdl_surface = SDL_DisplayFormat(temp);
276 sdl_surface = SDL_DisplayFormatAlpha(temp);
278 if (sdl_surface == NULL)
279 st_abort("Can't covert to display format", file);
281 if (use_alpha == IGNORE_ALPHA && !use_gl)
282 SDL_SetAlpha(sdl_surface, 0, 0);
284 SDL_FreeSurface(temp);
290 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
292 SDL_Surface* sdl_surface;
296 /* Save the alpha blending attributes */
297 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
298 saved_alpha = sdl_surf->format->alpha;
299 if ( (saved_flags & SDL_SRCALPHA)
302 SDL_SetAlpha(sdl_surf, 0, 0);
305 if(use_alpha == IGNORE_ALPHA && !use_gl)
306 sdl_surface = SDL_DisplayFormat(sdl_surf);
308 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
310 /* Restore the alpha blending attributes */
311 if ( (saved_flags & SDL_SRCALPHA)
314 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
317 if (sdl_surface == NULL)
318 st_abort("Can't covert to display format", "SURFACE");
320 if (use_alpha == IGNORE_ALPHA && !use_gl)
321 SDL_SetAlpha(sdl_surface, 0, 0);
326 //---------------------------------------------------------------------------
328 SurfaceImpl::SurfaceImpl()
332 SurfaceImpl::~SurfaceImpl()
334 SDL_FreeSurface(sdl_surface);
337 SDL_Surface* SurfaceImpl::get_sdl_surface() const
343 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
345 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
346 create_gl(sdl_surface,&gl_texture);
352 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
354 sdl_surface = sdl_surface_from_file(file, use_alpha);
355 create_gl(sdl_surface,&gl_texture);
361 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
363 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
364 create_gl(sdl_surface, &gl_texture);
370 SurfaceOpenGL::~SurfaceOpenGL()
372 glDeleteTextures(1, &gl_texture);
376 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
383 w = power_of_two(surf->w);
384 h = power_of_two(surf->h),
386 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
387 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
388 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
390 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
391 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
394 /* Save the alpha blending attributes */
395 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
396 saved_alpha = surf->format->alpha;
397 if ( (saved_flags & SDL_SRCALPHA)
400 SDL_SetAlpha(surf, 0, 0);
403 SDL_BlitSurface(surf, 0, conv, 0);
405 /* Restore the alpha blending attributes */
406 if ( (saved_flags & SDL_SRCALPHA)
409 SDL_SetAlpha(surf, saved_flags, saved_alpha);
412 glGenTextures(1, &*tex);
413 glBindTexture(GL_TEXTURE_2D , *tex);
414 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
415 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
416 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
417 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
418 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
419 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
420 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
422 SDL_FreeSurface(conv);
426 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
428 float pw = power_of_two(w);
429 float ph = power_of_two(h);
431 glEnable(GL_TEXTURE_2D);
433 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
435 glColor4ub(alpha, alpha, alpha, alpha);
437 glBindTexture(GL_TEXTURE_2D, gl_texture);
442 glTexCoord2f((float)w / pw, 0);
443 glVertex2f((float)w+x, y);
444 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
445 glTexCoord2f(0, (float)h / ph);
446 glVertex2f(x, (float)h+y);
449 glDisable(GL_TEXTURE_2D);
452 (void) update; // avoid compiler warning
458 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
460 float pw = power_of_two(w);
461 float ph = power_of_two(h);
463 glColor3ub(alpha, alpha, alpha);
465 glEnable(GL_TEXTURE_2D);
466 glBindTexture(GL_TEXTURE_2D, gl_texture);
471 glTexCoord2f((float)w / pw, 0);
472 glVertex2f(screen->w, 0);
473 glTexCoord2f((float)w / pw, (float)h / ph);
474 glVertex2f(screen->w, screen->h);
475 glTexCoord2f(0, (float)h / ph);
476 glVertex2f(0, screen->h);
479 glDisable(GL_TEXTURE_2D);
481 (void) update; // avoid compiler warning
487 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
489 float pw = power_of_two(int(this->w));
490 float ph = power_of_two(int(this->h));
492 glBindTexture(GL_TEXTURE_2D, gl_texture);
495 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
497 glColor4ub(alpha, alpha, alpha, alpha);
499 glEnable(GL_TEXTURE_2D);
503 glTexCoord2f(sx / pw, sy / ph);
505 glTexCoord2f((float)(sx + w) / pw, sy / ph);
507 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
508 glVertex2f(w +x, h+y);
509 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
513 glDisable(GL_TEXTURE_2D);
516 /* Avoid compiler warnings */
523 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
525 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
530 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
532 sdl_surface = sdl_surface_from_file(file, use_alpha);
537 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
539 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
545 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
554 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
557 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
558 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
560 if (update == UPDATE)
561 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
567 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
577 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
579 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
581 if (update == UPDATE)
582 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
588 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
603 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
605 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
607 if (update == UPDATE)
608 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
613 SurfaceSDL::~SurfaceSDL()