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
26 #include "SDL_image.h"
32 Surface::Surfaces Surface::surfaces;
34 SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
35 : type(SURFACE), surface(0), use_alpha(use_alpha_)
37 // Copy the given surface and make sure that it is not stored in
39 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
41 temp->format->BitsPerPixel,
47 st_abort("No memory left.", "");
48 SDL_SetAlpha(temp,0,0);
49 SDL_BlitSurface(temp, NULL, surface, NULL);
52 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
53 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
56 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
57 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
58 x(x_), y(y_), w(w_), h(h_)
61 SurfaceData::SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_)
62 : type(GRADIENT), surface(0), use_alpha(false), w(w_), h(h_)
64 top_gradient = top_gradient_;
65 bottom_gradient = bottom_gradient_;
69 SurfaceData::~SurfaceData()
71 SDL_FreeSurface(surface);
79 return create_SurfaceOpenGL();
81 return create_SurfaceSDL();
83 return create_SurfaceSDL();
88 SurfaceData::create_SurfaceSDL()
93 return new SurfaceSDL(file, use_alpha);
95 return new SurfaceSDL(file, x, y, w, h, use_alpha);
97 return new SurfaceSDL(surface, use_alpha);
99 return new SurfaceSDL(top_gradient, bottom_gradient, w, h);
105 SurfaceData::create_SurfaceOpenGL()
111 return new SurfaceOpenGL(file, use_alpha);
113 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
115 return new SurfaceOpenGL(surface, use_alpha);
117 return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h);
124 /* Quick utility function for texture creation */
125 static int power_of_two(int input)
129 while ( value < input )
137 Surface::Surface(SDL_Surface* surf, int use_alpha)
138 : data(surf, use_alpha), w(0), h(0)
140 impl = data.create();
146 surfaces.push_back(this);
149 Surface::Surface(const std::string& file, int use_alpha)
150 : data(file, use_alpha), w(0), h(0)
152 impl = data.create();
158 surfaces.push_back(this);
161 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
162 : data(file, x, y, w, h, use_alpha), w(0), h(0)
164 impl = data.create();
170 surfaces.push_back(this);
173 Surface::Surface(Color top_background, Color bottom_background, int w, int h)
174 : data(top_background, bottom_background, w, h), w(0), h(0)
176 impl = data.create();
182 surfaces.push_back(this);
189 impl = data.create();
201 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
210 printf("Error: Surface freed twice!!!\n");
212 surfaces.remove(this);
217 Surface::reload_all()
219 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
226 Surface::debug_check()
228 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
230 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
231 (*i)->data.file.c_str());
236 Surface::resize(int w_, int h_)
242 if (impl->resize(w_,h_) == -2)
248 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
251 SDL_Surface * sdl_surface;
255 temp = IMG_Load(file.c_str());
258 st_abort("Can't load", file);
260 /* Set source rectangle for conv: */
267 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
271 temp->format->Amask);
273 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
274 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
277 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
280 SDL_SetAlpha(temp,0,0);
282 SDL_BlitSurface(temp, &src, conv, NULL);
283 if(use_alpha == IGNORE_ALPHA && !use_gl)
284 sdl_surface = SDL_DisplayFormat(conv);
286 sdl_surface = SDL_DisplayFormatAlpha(conv);
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);
295 SDL_FreeSurface(conv);
301 sdl_surface_from_file(const std::string& file, int use_alpha)
303 SDL_Surface* sdl_surface;
306 temp = IMG_Load(file.c_str());
309 st_abort("Can't load", file);
311 if(use_alpha == IGNORE_ALPHA && !use_gl)
312 sdl_surface = SDL_DisplayFormat(temp);
314 sdl_surface = SDL_DisplayFormatAlpha(temp);
316 if (sdl_surface == NULL)
317 st_abort("Can't covert to display format", file);
319 if (use_alpha == IGNORE_ALPHA && !use_gl)
320 SDL_SetAlpha(sdl_surface, 0, 0);
322 SDL_FreeSurface(temp);
328 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
330 SDL_Surface* sdl_surface;
334 /* Save the alpha blending attributes */
335 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
336 saved_alpha = sdl_surf->format->alpha;
337 if ( (saved_flags & SDL_SRCALPHA)
340 SDL_SetAlpha(sdl_surf, 0, 0);
343 if(use_alpha == IGNORE_ALPHA && !use_gl)
344 sdl_surface = SDL_DisplayFormat(sdl_surf);
346 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
348 /* Restore the alpha blending attributes */
349 if ( (saved_flags & SDL_SRCALPHA)
352 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
355 if (sdl_surface == NULL)
356 st_abort("Can't covert to display format", "SURFACE");
358 if (use_alpha == IGNORE_ALPHA && !use_gl)
359 SDL_SetAlpha(sdl_surface, 0, 0);
365 sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
367 SDL_Surface* sdl_surface;
369 sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
370 screen->format->BitsPerPixel, screen->format->Rmask,
371 screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
373 if(sdl_surface == NULL)
374 st_abort("Cannot create surface for the gradient", "SURFACE");
378 SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
379 top.red, top.green, top.blue));
383 float redstep = (float(bottom.red)-float(top.red)) / float(h);
384 float greenstep = (float(bottom.green)-float(top.green)) / float(h);
385 float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
391 for(float y = 0; y < h; y++)
394 SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
395 int(float(top.red) + redstep * y),
396 int(float(top.green) + greenstep * y),
397 int(float(top.blue) + bluestep * y)));
404 //---------------------------------------------------------------------------
406 SurfaceImpl::SurfaceImpl()
409 SurfaceImpl::~SurfaceImpl()
411 SDL_FreeSurface(sdl_surface);
414 SDL_Surface* SurfaceImpl::get_sdl_surface() const
419 int SurfaceImpl::resize(int w_, int h_)
428 int ret = SDL_SoftStretch(sdl_surface, NULL,
434 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
436 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
437 create_gl(sdl_surface,&gl_texture);
443 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
445 sdl_surface = sdl_surface_from_file(file, use_alpha);
446 create_gl(sdl_surface,&gl_texture);
452 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
454 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
455 create_gl(sdl_surface, &gl_texture);
461 SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h)
463 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
464 create_gl(sdl_surface, &gl_texture);
470 SurfaceOpenGL::~SurfaceOpenGL()
472 glDeleteTextures(1, &gl_texture);
476 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
483 w = power_of_two(surf->w);
484 h = power_of_two(surf->h),
486 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
487 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
488 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
490 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
491 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
494 /* Save the alpha blending attributes */
495 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
496 saved_alpha = surf->format->alpha;
497 if ( (saved_flags & SDL_SRCALPHA)
500 SDL_SetAlpha(surf, 0, 0);
503 SDL_BlitSurface(surf, 0, conv, 0);
505 /* Restore the alpha blending attributes */
506 if ( (saved_flags & SDL_SRCALPHA)
509 SDL_SetAlpha(surf, saved_flags, saved_alpha);
512 glGenTextures(1, &*tex);
513 glBindTexture(GL_TEXTURE_2D , *tex);
514 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
515 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
516 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
517 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
518 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
519 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
520 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
522 SDL_FreeSurface(conv);
526 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, Uint32 effect)
528 float pw = power_of_two(w);
529 float ph = power_of_two(h);
531 if(effect & SEMI_TRANSPARENT)
534 glEnable(GL_TEXTURE_2D);
536 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
538 glColor4ub(alpha, alpha, alpha, alpha);
540 glBindTexture(GL_TEXTURE_2D, gl_texture);
544 if(effect & VERTICAL_FLIP)
547 glVertex2f(x, (float)h+y);
549 glTexCoord2f((float)w / pw, 0);
550 glVertex2f((float)w+x, (float)h+y);
552 glTexCoord2f((float)w / pw, (float)h / ph);
553 glVertex2f((float)w+x, y);
555 glTexCoord2f(0, (float)h / ph);
563 glTexCoord2f((float)w / pw, 0);
564 glVertex2f((float)w+x, y);
566 glTexCoord2f((float)w / pw, (float)h / ph);
567 glVertex2f((float)w+x, (float)h+y);
569 glTexCoord2f(0, (float)h / ph);
570 glVertex2f(x, (float)h+y);
574 glDisable(GL_TEXTURE_2D);
581 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
583 float pw = power_of_two(int(this->w));
584 float ph = power_of_two(int(this->h));
586 if(effect & SEMI_TRANSPARENT)
589 glBindTexture(GL_TEXTURE_2D, gl_texture);
592 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
594 glColor4ub(alpha, alpha, alpha, alpha);
596 glEnable(GL_TEXTURE_2D);
601 if(effect & VERTICAL_FLIP)
603 glTexCoord2f(sx / pw, sy / ph);
606 glTexCoord2f((float)(sx + w) / pw, sy / ph);
609 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
610 glVertex2f(w +x, h+y);
612 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
617 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
620 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
621 glVertex2f(w +x, h+y);
623 glTexCoord2f((float)(sx + w) / pw, sy / ph);
626 glTexCoord2f(sx / pw, sy / ph);
632 glDisable(GL_TEXTURE_2D);
640 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha)
642 float pw = power_of_two(int(this->w));
643 float ph = power_of_two(int(this->h));
645 glBindTexture(GL_TEXTURE_2D, gl_texture);
648 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
650 glColor4ub(alpha, alpha, alpha, alpha);
652 glEnable(GL_TEXTURE_2D);
658 glTexCoord2f((float)w / pw, 0);
660 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
661 glVertex2f(sw +x, sh+y);
662 glTexCoord2f(0, (float)h / ph);
666 glDisable(GL_TEXTURE_2D);
675 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
677 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
682 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
684 sdl_surface = sdl_surface_from_file(file, use_alpha);
689 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
691 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
696 SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h)
698 sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient, w, h);
704 SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect)
713 if(effect & SEMI_TRANSPARENT)
716 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
718 for(float sy = 0; sy < h; sy++)
719 if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2)
726 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
727 to temp sur, blit the temp into the screen */
728 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
729 already have an alpha mask yet... */
731 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
732 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
733 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
734 sdl_surface->format->Bmask,
736 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
737 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
738 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
741 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
742 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
744 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
746 SDL_FreeSurface (sdl_surface_copy);
750 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
756 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect)
770 if(effect & SEMI_TRANSPARENT)
773 if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack
775 for(float sy_ = sy; sy_ < h; sy_++)
776 if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2)
783 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
784 to temp sur, blit the temp into the screen */
785 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
786 already have an alpha mask yet... */
788 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
789 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
790 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
791 sdl_surface->format->Bmask,
793 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
794 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
795 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
798 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
799 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
801 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
803 SDL_FreeSurface (sdl_surface_copy);
807 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
814 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
824 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
827 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
828 sw, sh, sdl_surface->format->BitsPerPixel,
829 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
830 sdl_surface->format->Bmask,
833 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
834 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
836 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
837 SDL_FreeSurface(sdl_surface_copy);
839 if (update == UPDATE)
840 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
846 SurfaceSDL::~SurfaceSDL()