]> git.localhorst.tv Git - space.git/blobdiff - src/graphics/Window.cpp
move to SDL2
[space.git] / src / graphics / Window.cpp
diff --git a/src/graphics/Window.cpp b/src/graphics/Window.cpp
new file mode 100644 (file)
index 0000000..7cf1b3a
--- /dev/null
@@ -0,0 +1,58 @@
+#include "Window.h"
+
+#include <algorithm>
+#include <cassert>
+#include <stdexcept>
+#include <string>
+
+using std::runtime_error;
+
+
+namespace space {
+
+const Vector<int> Window::POS_CENTER(
+       SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+const Vector<int> Window::POS_UNDEF(
+       SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED);
+
+
+Window::Window(
+       const char *title,
+       Vector<int> pos,
+       Vector<int> size,
+       Uint32 flags)
+: win(SDL_CreateWindow(title, pos.x, pos.y, size.x, size.y, flags)) {
+       if (!win) {
+               throw runtime_error(std::string("create window ") + title
+                       + ": " + SDL_GetError());
+       }
+}
+
+Window::~Window() {
+       if (win) SDL_DestroyWindow(win);
+}
+
+Window::Window(Window &&other)
+: win(other.win) {
+       other.win = nullptr;
+}
+
+Window &Window::operator =(Window &&other) {
+       std::swap(win, other.win);
+       return *this;
+}
+
+
+Vector<int> Window::Size() const {
+       assert(win);
+       Vector<int> size;
+       SDL_GetWindowSize(win, &size.x, &size.y);
+       return size;
+}
+
+
+Canvas Window::CreateCanvas(Uint32 flags) {
+       return Canvas(win, -1, flags);
+}
+
+}