]> git.lizzy.rs Git - dragonblocks3d.git/blob - src/dragonblocks/window.cpp
Multithreading
[dragonblocks3d.git] / src / dragonblocks / window.cpp
1 #include <stdexcept>
2 #include "render_engine.hpp"
3 #include "window.hpp"
4
5 using namespace std;
6 using namespace glm;
7 using namespace dragonblocks;
8         
9 Window *Window::singleton = nullptr;
10
11 Window *Window::create(RenderEngine *r)
12 {
13         if (singleton)
14                 throw runtime_error("Window already exists");
15         return singleton = new Window(r);
16 }
17
18 void Window::windowPosCallback(GLFWwindow *id, int x, int y)
19 {
20         singleton->posInput(x, y);
21 }
22
23 void Window::framebufferSizeCallback(GLFWwindow *id, int width, int height)
24 {
25         glViewport(0, 0, width, height);
26         singleton->render_engine->updateProjectionMatrix();
27         singleton->sizeInput(width, height);
28 }
29
30 void Window::cursorPosCallback(GLFWwindow *id, double x, double y)
31 {
32         singleton->cursorInput(x, y);
33 }
34
35 void Window::setTitle(const string &title)
36 {
37         glfwSetWindowTitle(id, title.c_str());
38 }
39
40 void Window::setSize(int width, int height)
41 {
42         glfwSetWindowSize(id, width, height);
43 }
44
45 void Window::setPos(int x, int y)
46 {
47         glfwSetWindowPos(id, x, y);
48 }
49
50 void Window::toggleFullscreen()
51 {
52         fullscreen = ! fullscreen;
53         
54         if (fullscreen) {
55                 GLFWmonitor *monitor = glfwGetPrimaryMonitor();
56                 const GLFWvidmode *vidmode = glfwGetVideoMode(monitor);
57                 glfwSetWindowMonitor(id, monitor, 0, 0, vidmode->width, vidmode->height, 0);
58         }
59         else
60                 glfwSetWindowMonitor(id, nullptr, nfs_x, nfs_y, nfs_width, nfs_height, 0);
61 }
62
63 void Window::close()
64 {
65         glfwDestroyWindow(id);
66 }
67
68 void Window::swapBuffers()
69 {
70         glfwSwapBuffers(id);
71 }
72
73 bool Window::shouldClose() const
74 {
75         return glfwWindowShouldClose(id);
76 }
77
78 bool Window::wasKeyDown(int key) const
79 {
80         return glfwGetKey(id, key) == GLFW_PRESS;
81 }
82
83 void Window::makeContextCurrent() const
84 {
85         glfwMakeContextCurrent(id);
86 }
87
88 ivec2 Window::getSize() const
89 {
90         return ivec2(width, height);
91 }
92
93 ivec2 Window::getCursorPos() const
94 {
95         return ivec2(cursor_x, cursor_y);
96 }
97
98 ivec2 Window::getCursorDelta()
99 {
100         ivec2 delta(cursor_delta_x, cursor_delta_y);
101         cursor_delta_x = cursor_delta_y = 0;
102         return delta;
103 }
104
105 void Window::posInput(int x, int y)
106 {
107         if (! fullscreen) {
108                 nfs_x = x;
109                 nfs_y = y;
110         }
111 }
112
113 void Window::sizeInput(int w, int h)
114 {
115         width = w;
116         height = h;
117         if (! fullscreen) {
118                 nfs_width = width;
119                 nfs_height = height;
120         }
121 }
122
123 void Window::cursorInput(int x, int y)
124 {
125         int lx = cursor_x;
126         int ly = cursor_y;
127         cursor_delta_x = x - lx;
128         cursor_delta_y = y - ly;
129         cursor_x = x;
130         cursor_y = y;
131 }
132
133 Window::~Window()
134 {
135         close();
136 }
137
138 Window::Window(RenderEngine *r) : render_engine(r)
139 {
140         fullscreen = false;
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);
145         if (! id) {
146                 glfwTerminate();
147                 throw runtime_error("Failed to create GLFW window");
148         }
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);
154 }
155
156