2 #include "render_engine.hpp"
7 using namespace dragonblocks;
9 Window *Window::singleton = nullptr;
11 Window *Window::create(RenderEngine *r)
14 throw runtime_error("Window already exists");
16 return singleton = new Window(r);
19 void Window::windowPosCallback(GLFWwindow *id, int x, int y)
21 singleton->posInput(x, y);
24 void Window::framebufferSizeCallback(GLFWwindow *id, int width, int height)
26 glViewport(0, 0, width, height);
27 singleton->sizeInput(width, height);
30 void Window::cursorPosCallback(GLFWwindow *id, double x, double y)
32 singleton->cursorInput(x, y);
35 void Window::setTitle(const string &title)
37 glfwSetWindowTitle(id, title.c_str());
40 void Window::setSize(int width, int height)
42 glfwSetWindowSize(id, width, height);
45 void Window::setPos(int x, int y)
47 glfwSetWindowPos(id, x, y);
50 void Window::toggleFullscreen()
52 fullscreen = ! fullscreen;
55 GLFWmonitor *monitor = glfwGetPrimaryMonitor();
56 const GLFWvidmode *vidmode = glfwGetVideoMode(monitor);
57 glfwSetWindowMonitor(id, monitor, 0, 0, vidmode->width, vidmode->height, 0);
60 glfwSetWindowMonitor(id, nullptr, nfs_x, nfs_y, nfs_width, nfs_height, 0);
65 glfwDestroyWindow(id);
68 void Window::swapBuffers()
73 bool Window::shouldClose() const
75 return glfwWindowShouldClose(id);
78 bool Window::isKeyDown(int key) const
80 return glfwGetKey(id, key) == GLFW_PRESS;
83 void Window::makeContextCurrent() const
85 glfwMakeContextCurrent(id);
88 ivec2 Window::getSize() const
90 return ivec2(width, height);
93 ivec2 Window::getCursorPos() const
95 return ivec2(cursor_x, cursor_y);
98 ivec2 Window::getCursorDelta()
100 ivec2 delta(cursor_delta_x, cursor_delta_y);
101 cursor_delta_x = cursor_delta_y = 0;
105 void Window::posInput(int x, int y)
113 void Window::sizeInput(int w, int h)
123 void Window::cursorInput(int x, int y)
127 cursor_delta_x = x - lx;
128 cursor_delta_y = y - ly;
138 Window::Window(RenderEngine *r) : render_engine(r)
141 width = nfs_width = 100;
142 height = nfs_height = 100;
143 cursor_x = cursor_y = cursor_delta_x = cursor_delta_y = 0;
144 id = glfwCreateWindow(width, height, "libdragonblocks.so", NULL, NULL);
147 throw runtime_error("Failed to create GLFW window");
149 makeContextCurrent();
150 glfwSetInputMode(id, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
151 glfwSetWindowPosCallback(id, Window::windowPosCallback);
152 glfwSetFramebufferSizeCallback(id, Window::framebufferSizeCallback);
153 glfwSetCursorPosCallback(id, Window::cursorPosCallback);