//
//
+#include <assert.h>
#include "SDL.h"
#include "SDL_image.h"
#include "texture.h"
#include "globals.h"
#include "setup.h"
+Surface::Surfaces Surface::surfaces;
+
+SurfaceData::SurfaceData(SDL_Surface* surf, int use_alpha_)
+ : type(SURFACE), surface(surf), use_alpha(use_alpha_)
+{
+}
+
+SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
+ : type(LOAD), file(file_), use_alpha(use_alpha_)
+{
+}
+
+SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
+ : type(LOAD_PART), file(file_), use_alpha(use_alpha_),
+ x(x_), y(y_), w(w_), h(h_)
+{
+}
+
+SurfaceImpl*
+SurfaceData::create()
+{
+ if (use_gl)
+ return create_SurfaceOpenGL();
+ else
+ return create_SurfaceSDL();
+}
+
+SurfaceSDL*
+SurfaceData::create_SurfaceSDL()
+{
+ switch(type)
+ {
+ case LOAD:
+ return new SurfaceSDL(file, use_alpha);
+ case LOAD_PART:
+ return new SurfaceSDL(file, x, y, w, h, use_alpha);
+ case SURFACE:
+ return 0; //new SurfaceSDL(surface, use_alpha);
+ }
+ assert(0);
+}
+
+SurfaceOpenGL*
+SurfaceData::create_SurfaceOpenGL()
+{
+ switch(type)
+ {
+ case LOAD:
+ return new SurfaceOpenGL(file, use_alpha);
+ case LOAD_PART:
+ return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
+ case SURFACE:
+ return 0; //new SurfaceOpenGL(surface, use_alpha);
+ }
+ assert(0);
+}
+
+
/* Quick utility function for texture creation */
static int power_of_two(int input)
{
}
Surface::Surface(SDL_Surface* surf, int use_alpha)
+ : data(surf, use_alpha), w(0), h(0)
{
- if (use_gl)
- impl = new SurfaceOpenGL(surf, use_alpha);
- else
- impl = new SurfaceSDL(surf, use_alpha);
-
- w = impl->w;
- h = impl->h;
+ impl = data.create();
+ if (impl)
+ {
+ w = impl->w;
+ h = impl->h;
+ }
+ surfaces.push_back(this);
}
Surface::Surface(const std::string& file, int use_alpha)
+ : data(file, use_alpha), w(0), h(0)
{
- if (use_gl)
- impl = new SurfaceOpenGL(file, use_alpha);
- else
- impl = new SurfaceSDL(file, use_alpha);
-
- w = impl->w;
- h = impl->h;
+ impl = data.create();
+ if (impl)
+ {
+ w = impl->w;
+ h = impl->h;
+ }
+ surfaces.push_back(this);
}
Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
+ : data(file, x, y, w, h, use_alpha), w(0), h(0)
{
- if (use_gl)
- impl = new SurfaceOpenGL(file, x, y, w, h, use_alpha);
- else
- impl = new SurfaceSDL(file, x, y, w, h, use_alpha);
+ impl = data.create();
+ if (impl)
+ {
+ w = impl->w;
+ h = impl->h;
+ }
+ surfaces.push_back(this);
+}
- w = impl->w;
- h = impl->h;
+void
+Surface::reload()
+{
+ delete impl;
+ impl = data.create();
+ if (impl)
+ {
+ w = impl->w;
+ h = impl->h;
+ }
}
Surface::~Surface()
{
+ surfaces.remove(this);
delete impl;
}
void
+Surface::reload_all()
+{
+ for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
+ {
+ (*i)->reload();
+ }
+}
+
+void
Surface::draw(float x, float y, Uint8 alpha, bool update)
{
if (impl) impl->draw(x, y, alpha, update);
dest.h = h;
if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
- return;
+ return;
SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
dest.h = screen->h;
if(alpha != 255)
- SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
+ SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
if (update == UPDATE)
dest.h = (int)h;
if(alpha != 255)
- SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
+ SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
SDL_BlitSurface(sdl_surface, &src, screen, &dest);
#include <SDL_opengl.h>
#endif
+#include <list>
#include "screen.h"
class SurfaceImpl;
+class SurfaceSDL;
+class SurfaceOpenGL;
+
+/** This class holds all the data necessary to construct a surface */
+class SurfaceData
+{
+public:
+ enum ConstructorType { LOAD, LOAD_PART, SURFACE };
+ ConstructorType type;
+ SDL_Surface* surface;
+ std::string file;
+ int use_alpha;
+ int x;
+ int y;
+ int w;
+ int h;
+
+ 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_);
+
+ SurfaceSDL* create_SurfaceSDL();
+ SurfaceOpenGL* create_SurfaceOpenGL();
+ SurfaceImpl* create();
+};
/** Container class that holds a surface, necessary so that we can
switch Surface implementations (OpenGL, SDL) on the fly */
class Surface
{
public:
+ SurfaceData data;
SurfaceImpl* impl;
int w;
int h;
+
+ typedef std::list<Surface*> Surfaces;
+ static Surfaces surfaces;
public:
+ static void reload_all();
+
Surface(SDL_Surface* surf, int use_alpha);
Surface(const std::string& file, int use_alpha);
Surface(const std::string& file, int x, int y, int w, int h, int use_alpha);
~Surface();
+
+ /** Reload the surface, which is necesarry in case of a mode swich */
+ void reload();
void draw(float x, float y, Uint8 alpha = 255, bool update = false);
void draw_bg(Uint8 alpha = 255, bool update = false);