//
#include <assert.h>
+#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "texture.h"
Surface::Surfaces Surface::surfaces;
-SurfaceData::SurfaceData(SDL_Surface* surf, int use_alpha_)
- : type(SURFACE), surface(surf), use_alpha(use_alpha_)
-{
+SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
+ : type(SURFACE), use_alpha(use_alpha_)
+{
+ // Copy the given surface and make sure that it is not stored in
+ // video memory
+ surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
+ temp->w, temp->h,
+ temp->format->BitsPerPixel,
+ temp->format->Rmask,
+ temp->format->Gmask,
+ temp->format->Bmask,
+ temp->format->Amask);
+ SDL_SetAlpha(temp,0,0);
+ SDL_BlitSurface(temp, NULL, surface, NULL);
}
SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
{
}
+SurfaceData::~SurfaceData()
+{
+
+}
+
SurfaceImpl*
SurfaceData::create()
{
case LOAD_PART:
return new SurfaceSDL(file, x, y, w, h, use_alpha);
case SURFACE:
- return 0; //new SurfaceSDL(surface, use_alpha);
+ return new SurfaceSDL(surface, use_alpha);
}
assert(0);
}
case LOAD_PART:
return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
case SURFACE:
- return 0; //new SurfaceOpenGL(surface, use_alpha);
+ return new SurfaceOpenGL(surface, use_alpha);
}
assert(0);
}
void
Surface::draw(float x, float y, Uint8 alpha, bool update)
{
- if (impl) impl->draw(x, y, alpha, update);
+ if (impl)
+ {
+ if (impl->draw(x, y, alpha, update) == -2)
+ reload();
+ }
}
void
Surface::draw_bg(Uint8 alpha, bool update)
{
- if (impl) impl->draw_bg(alpha, update);
+ if (impl)
+ {
+ if (impl->draw_bg(alpha, update) == -2)
+ reload();
+ }
}
void
Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
{
- if (impl) impl->draw_part(sx, sy, x, y, w, h, alpha, update);
+ if (impl)
+ {
+ if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
+ reload();
+ }
}
SDL_Surface*
SDL_FreeSurface(conv);
}
-void
+int
SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
{
float pw = power_of_two(w);
/* Avoid compiler warnings */
if(update)
{}
+
+ return 0;
}
-void
+int
SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
{
float pw = power_of_two(w);
/* Avoid compiler warnings */
if(update)
{}
+
+ return 0;
}
-void
+int
SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
{
float pw = power_of_two(int(this->w));
/* Avoid compiler warnings */
if(update)
{}
+ return 0;
}
#endif
h = sdl_surface->h;
}
-void
+int
SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
{
SDL_Rect dest;
dest.h = h;
if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
- return;
+ return -1;
SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
- SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
+ int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
if (update == UPDATE)
SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
+
+ return ret;
}
-void
+int
SurfaceSDL::draw_bg(Uint8 alpha, bool update)
{
SDL_Rect dest;
if(alpha != 255)
SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
- SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
+
+ int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
if (update == UPDATE)
SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
+
+ return ret;
}
-void
+int
SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
{
SDL_Rect src, dest;
if(alpha != 255)
SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
- SDL_BlitSurface(sdl_surface, &src, screen, &dest);
+ int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
if (update == UPDATE)
update_rect(screen, dest.x, dest.y, dest.w, dest.h);
+
+ return ret;
}
SurfaceSDL::~SurfaceSDL()
SurfaceData(SDL_Surface* surf, int use_alpha_);
SurfaceData(const std::string& file_, int use_alpha_);
SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_);
+ ~SurfaceData();
SurfaceSDL* create_SurfaceSDL();
SurfaceOpenGL* create_SurfaceOpenGL();
int h;
public:
- virtual void draw(float x, float y, Uint8 alpha, bool update) = 0;
- virtual void draw_bg(Uint8 alpha, bool update) = 0;
- virtual void draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update) = 0;
+ /** Return 0 on success, -2 if surface needs to be reloaded */
+ virtual int draw(float x, float y, Uint8 alpha, bool update) = 0;
+ virtual int draw_bg(Uint8 alpha, bool update) = 0;
+ virtual int draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update) = 0;
};
class SurfaceSDL : public SurfaceImpl
SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha);
virtual ~SurfaceSDL();
- void draw(float x, float y, Uint8 alpha, bool update);
- void draw_bg(Uint8 alpha, bool update);
- void draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update);
+ int draw(float x, float y, Uint8 alpha, bool update);
+ int draw_bg(Uint8 alpha, bool update);
+ int draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update);
};
class SurfaceOpenGL : public SurfaceImpl
SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha);
virtual ~SurfaceOpenGL();
- void draw(float x, float y, Uint8 alpha, bool update);
- void draw_bg(Uint8 alpha, bool update);
- void draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update);
+ int draw(float x, float y, Uint8 alpha, bool update);
+ int draw_bg(Uint8 alpha, bool update);
+ int draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update);
private:
void create_gl(SDL_Surface * surf, GLuint * tex);