summaryrefslogtreecommitdiffstats
path: root/engine/flatsurface.cpp
diff options
context:
space:
mode:
authorancarola <raffaele.ancarola@epfl.ch>2019-01-12 23:24:05 +0100
committerancarola <raffaele.ancarola@epfl.ch>2019-01-12 23:24:05 +0100
commitc35dfbd0bb8a1abfef876bb6dbe20945867a5270 (patch)
tree895fbe6a09c24c45f09d33da7bf46a2e19e0155c /engine/flatsurface.cpp
parentinitialization (diff)
downloadflatland-c35dfbd0bb8a1abfef876bb6dbe20945867a5270.tar.gz
flatland-c35dfbd0bb8a1abfef876bb6dbe20945867a5270.zip
edas
Diffstat (limited to 'engine/flatsurface.cpp')
-rw-r--r--engine/flatsurface.cpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/engine/flatsurface.cpp b/engine/flatsurface.cpp
new file mode 100644
index 0000000..1970b71
--- /dev/null
+++ b/engine/flatsurface.cpp
@@ -0,0 +1,167 @@
+#include "flatsurface.h"
+
+#include <iostream>
+
+using namespace std;
+
+FlatSurface::FlatSurface(const char *filename, Uint32 format, SDL_Surface *parent)
+
+ : FlatObject(), parent(parent), hide(false)
+{
+ setID(filename);
+
+ cout << "FlatSurface: loading " << filename << endl;
+ surface = loadOptimizedSurface(filename, format);
+
+ if (!surface)
+ {
+ cout << "Warning: could not load surface " << filename << endl;
+ }
+
+ offset = new SDL_Rect;
+
+ offset->x = 0;
+ offset->y = 0;
+ offset->w = surface->w;
+ offset->h = surface->h;
+
+ viewport = new SDL_Rect;
+
+ viewport->x = 0;
+ viewport->y = 0;
+ viewport->w = surface->w;
+ viewport->h = surface->h;
+}
+
+FlatSurface::FlatSurface(SDL_Surface *surface, SDL_Surface *parent)
+
+ : FlatObject(), parent(parent), hide(false)
+{
+ this->surface = new SDL_Surface(*surface);
+
+ offset = new SDL_Rect;
+
+ offset->x = 0;
+ offset->y = 0;
+ offset->w = surface->w;
+ offset->h = surface->h;
+
+ viewport = new SDL_Rect;
+
+ viewport->x = 0;
+ viewport->y = 0;
+ viewport->w = surface->w;
+ viewport->h = surface->h;
+}
+
+FlatSurface::FlatSurface(const FlatSurface &sprite)
+
+ : FlatObject(sprite), parent(sprite.parent),
+ hide(sprite.hide)
+{
+ offset = new SDL_Rect(*sprite.offset);
+
+ viewport = new SDL_Rect(*sprite.viewport);
+
+ surface = copySurface(sprite.surface);
+}
+
+FlatSurface::~FlatSurface()
+{
+ SDL_FreeSurface(surface);
+
+ delete offset;
+ delete viewport;
+}
+
+void FlatSurface::setOffset(int x, int y, int w, int h)
+{
+ offset->x = x;
+ offset->y = y;
+
+ if (w > 0)
+ offset->w = w;
+
+ if (h > 0)
+ offset->h = h;
+}
+
+void FlatSurface::setViewport(int x, int y, int w, int h)
+{
+ viewport->x = x;
+ viewport->y = y;
+ viewport->w = w;
+ viewport->h = h;
+}
+
+void FlatSurface::setViewport(const SDL_Rect &rect)
+{
+ *viewport = rect;
+}
+
+
+void FlatSurface::setOffset(const SDL_Rect &offset)
+{
+ *this->offset = offset;
+}
+
+const SDL_Rect * FlatSurface::getOffset() const
+{
+ return offset;
+}
+
+const SDL_Rect * FlatSurface::getViewport() const
+{
+ return viewport;
+}
+
+void FlatSurface::setParent(SDL_Surface *parent)
+{
+ this->parent = parent;
+}
+
+SDL_Surface * FlatSurface::getParent()
+{
+ return parent;
+}
+
+SDL_Surface * FlatSurface::getSurface()
+{
+ return surface;
+}
+
+void FlatSurface::setHidden(bool flag)
+{
+ hide = flag;
+}
+
+bool FlatSurface::isHidden() const
+{
+ return hide;
+}
+
+void FlatSurface::blit()
+{
+ if (!hide)
+ SDL_BlitSurface(surface, viewport, parent, offset);
+}
+
+SDL_Surface * FlatSurface::loadOptimizedSurface(const char *filename, Uint32 format)
+{
+ SDL_Surface * optimized = 0;
+
+ SDL_Surface * loaded = SDL_LoadBMP(filename);
+
+ if (loaded)
+ {
+ optimized = SDL_ConvertSurfaceFormat(loaded, format, 0);
+ SDL_FreeSurface(loaded);
+ }
+
+ return optimized;
+}
+
+SDL_Surface * FlatSurface::copySurface(SDL_Surface *src)
+{
+ return SDL_ConvertSurface(src, src->format, src->flags);
+}