-Surface::debug_check()
-{
- for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
- {
- printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
- (*i)->data.file.c_str());
- }
-}
-
-void
-apply_filter_to_surface(SDL_Surface* surface, int filter, Color color)
-{
- if(filter == HORIZONTAL_FLIP_FILTER) {
- SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
- SDL_BlitSurface(surface, NULL, sur_copy, NULL);
- SDL_SetAlpha(sur_copy,0,0);
-
- SDL_Rect src, dst;
- src.y = dst.y = 0;
- src.w = dst.w = 1;
- src.h = dst.h = sur_copy->h;
- for(int x = 0; x < sur_copy->w; x++)
- {
- src.x = x; dst.x = sur_copy->w-1 - x;
- SDL_BlitSurface(sur_copy, &src, surface, &dst);
- }
-
- SDL_FreeSurface(sur_copy);
- } else if(filter == MASK_FILTER) {
- SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true);
-
- Uint8 r,g,b,a;
-
- SDL_LockSurface(sur_copy);
- for(int x = 0; x < sur_copy->w; x++)
- for(int y = 0; y < sur_copy->h; y++) {
- SDL_GetRGBA(getpixel(sur_copy,x,y), sur_copy->format, &r,&g,&b,&a);
- if(a != 0) {
- putpixel(sur_copy, x,y, color.map_rgba(sur_copy));
- }
- }
- SDL_UnlockSurface(sur_copy);
-
- SDL_BlitSurface(sur_copy, NULL, surface, NULL);
- SDL_FreeSurface(sur_copy);
- }
-}
-
-SDL_Surface*
-sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha)
-{
- SDL_Rect src;
- SDL_Surface * sdl_surface;
- SDL_Surface * temp;
- SDL_Surface * conv;
-
- temp = IMG_Load(file.c_str());
-
- if (temp == NULL) {
- std::stringstream msg;
- msg << "Couldn't load '" << file << "': " << SDL_GetError();
- throw std::runtime_error(msg.str());
- }
-
- /* Set source rectangle for conv: */
-
- src.x = x;
- src.y = y;
- src.w = w;
- src.h = h;
-
- conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, temp->format->BitsPerPixel,
- temp->format->Rmask,
- temp->format->Gmask,
- temp->format->Bmask,
- temp->format->Amask);
-
- SDL_SetAlpha(temp,0,0);
-
- SDL_BlitSurface(temp, &src, conv, NULL);
- if(use_alpha == false && !config->use_gl)
- sdl_surface = SDL_DisplayFormat(conv);
- else
- sdl_surface = SDL_DisplayFormatAlpha(conv);
-
- if (sdl_surface == NULL) {
- std::stringstream msg;
- msg << "Can't convert file '" << file << "' to display format.";
- throw std::runtime_error(msg.str());
- }
-
- if (use_alpha == false && !config->use_gl)
- SDL_SetAlpha(sdl_surface, 0, 0);
-
- SDL_FreeSurface(temp);
- SDL_FreeSurface(conv);
-
- return sdl_surface;
-}
-
-SDL_Surface*
-sdl_surface_from_file(const std::string& file, bool use_alpha)
-{
- SDL_Surface* sdl_surface;
- SDL_Surface* temp;
-
- temp = IMG_Load(file.c_str());
-
- if (temp == NULL) {
- std::stringstream msg;
- msg << "Couldn't load file '" << file << "': " << SDL_GetError();
- throw std::runtime_error(msg.str());
- }
-
- if(use_alpha == false && !config->use_gl)
- sdl_surface = SDL_DisplayFormat(temp);
- else
- sdl_surface = SDL_DisplayFormatAlpha(temp);
-
- if (sdl_surface == NULL) {
- std::stringstream msg;
- msg << "Couldn't convert file '" << file << "' to display format";
- throw std::runtime_error(msg.str());
- }
-
- if (use_alpha == false && !config->use_gl)
- SDL_SetAlpha(sdl_surface, 0, 0);
-
- SDL_FreeSurface(temp);
-
- return sdl_surface;
-}
-
-SDL_Surface*
-sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha)
-{
- SDL_Surface* sdl_surface;
-#if 0
- Uint32 saved_flags;
- Uint8 saved_alpha;
-
- /* Save the alpha blending attributes */
- saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
- saved_alpha = sdl_surf->format->alpha;
- if ( (saved_flags & SDL_SRCALPHA)
- == SDL_SRCALPHA )
- {
- SDL_SetAlpha(sdl_surf, 0, 0);
- }
-#endif
-
- if(use_alpha == false && !config->use_gl)
- sdl_surface = SDL_DisplayFormat(sdl_surf);
- else
- sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
-
-#if 0
- /* Restore the alpha blending attributes */
- if ( (saved_flags & SDL_SRCALPHA)
- == SDL_SRCALPHA )
- {
- SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
- }
-#endif
-
- if (sdl_surface == NULL) {
- std::stringstream msg;
- msg << "Can't convert surface to display format.";
- throw std::runtime_error(msg.str());
- }
-
- if (use_alpha == false && !config->use_gl)
- SDL_SetAlpha(sdl_surface, 0, 0);
-
- return sdl_surface;
-}
-
-SDL_Surface*
-sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
-{
- SDL_Surface* sdl_surface
- = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
- screen->format->BitsPerPixel, screen->format->Rmask,
- screen->format->Gmask, screen->format->Bmask, 0);
-
- if(sdl_surface == 0)
- throw std::runtime_error("Can't create gradient surface");
-
- if(top == bottom) {
- SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
- top.red, top.green, top.blue));
- } else {
- float redstep = (float(bottom.red)-float(top.red)) / float(h);
- float greenstep = (float(bottom.green)-float(top.green)) / float(h);
- float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
-
- SDL_Rect rect;
- rect.x = 0;
- rect.w = w;
- rect.h = 1;
- for(float y = 0; y < h; y++) {
- rect.y = (int)y;
- SDL_FillRect(sdl_surface, &rect, SDL_MapRGB(sdl_surface->format,
- int(float(top.red) + redstep * y),
- int(float(top.green) + greenstep * y),
- int(float(top.blue) + bluestep * y)));
- }
- }
-
- return sdl_surface;
-}
-
-//---------------------------------------------------------------------------
-
-SurfaceImpl::SurfaceImpl()
-{}
-
-SurfaceImpl::~SurfaceImpl()
-{
- SDL_FreeSurface(sdl_surface);
-}
-
-SDL_Surface* SurfaceImpl::get_sdl_surface() const
-{
- return sdl_surface;
-}
-
-SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha)
-{
- sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
- create_gl(sdl_surface,&gl_texture);
-
- w = sdl_surface->w;
- h = sdl_surface->h;
-}
-
-SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha)
-{
- sdl_surface = sdl_surface_from_file(file, use_alpha);
- create_gl(sdl_surface,&gl_texture);
-
- w = sdl_surface->w;
- h = sdl_surface->h;
-}
-
-SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_,
- int w_, int h_, bool use_alpha_)
-{
- sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_);
-
- create_gl(sdl_surface, &gl_texture);
- w = sdl_surface->w;
- h = sdl_surface->h;
-}
-
-SurfaceOpenGL::SurfaceOpenGL(Color top_gradient, Color bottom_gradient,
- int _w, int _h)
-{
- sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h);
- create_gl(sdl_surface, &gl_texture);
- w = sdl_surface->w;
- h = sdl_surface->h;
-}
-
-SurfaceOpenGL::~SurfaceOpenGL()