1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,182 @@ |
1 |
+/// Guards |
|
2 |
+ |
|
3 |
+#ifdef GLBACKEND_GLFW |
|
4 |
+#ifndef GLBACKEND_GLFW_HPP_ |
|
5 |
+#define GLBACKEND_GLFW_HPP_ |
|
6 |
+ |
|
7 |
+ |
|
8 |
+/// Includes |
|
9 |
+ |
|
10 |
+#include <array> |
|
11 |
+#include <string> |
|
12 |
+ |
|
13 |
+#include <glbackend.hpp> |
|
14 |
+ |
|
15 |
+#include <GLFW/glfw3.h> |
|
16 |
+ |
|
17 |
+ |
|
18 |
+/// Class definition |
|
19 |
+ |
|
20 |
+class GLBackendGLFW final : public GLBackend |
|
21 |
+{ |
|
22 |
+ |
|
23 |
+public: |
|
24 |
+ |
|
25 |
+ //// Special member functions |
|
26 |
+ |
|
27 |
+ explicit GLBackendGLFW( |
|
28 |
+ std::string const & title, |
|
29 |
+ std::array<int, 2> size = {0, 0}, |
|
30 |
+ std::array<int, 2> version = {0, 0}, |
|
31 |
+ int samples = 0, |
|
32 |
+ bool fullscreen = false, |
|
33 |
+ bool transparent = false |
|
34 |
+ ); |
|
35 |
+ ~GLBackendGLFW(); |
|
36 |
+ GLBackendGLFW(GLBackendGLFW &&) noexcept; |
|
37 |
+ GLBackendGLFW(GLBackendGLFW const &) = delete; |
|
38 |
+ GLBackendGLFW & operator=(GLBackendGLFW &&) = delete; |
|
39 |
+ GLBackendGLFW & operator=(GLBackendGLFW const &) = delete; |
|
40 |
+ |
|
41 |
+ //// Context |
|
42 |
+ |
|
43 |
+ void current() override; |
|
44 |
+ |
|
45 |
+ //// Render loop |
|
46 |
+ |
|
47 |
+ void swap() override; |
|
48 |
+ |
|
49 |
+ void events() override; |
|
50 |
+ |
|
51 |
+ bool running() const override; |
|
52 |
+ bool running(bool running) override; |
|
53 |
+ |
|
54 |
+ float time() const override; |
|
55 |
+ float time(float time) override; |
|
56 |
+ |
|
57 |
+ //// Input and output |
|
58 |
+ |
|
59 |
+ void lock(bool lock) override; |
|
60 |
+ |
|
61 |
+ bool key (std::string const & key) const override; |
|
62 |
+ bool button(int button) const override; |
|
63 |
+ |
|
64 |
+ //// Debug |
|
65 |
+ |
|
66 |
+ std::string debug_info() const override; |
|
67 |
+ |
|
68 |
+protected: |
|
69 |
+ |
|
70 |
+ //// Special member functions |
|
71 |
+ |
|
72 |
+ void destroy_(); |
|
73 |
+ |
|
74 |
+ //// Debug |
|
75 |
+ |
|
76 |
+ void static debug_glfw_error_callback_( |
|
77 |
+ int error, |
|
78 |
+ char const * message |
|
79 |
+ ); |
|
80 |
+ |
|
81 |
+protected: |
|
82 |
+ |
|
83 |
+ //// Context |
|
84 |
+ |
|
85 |
+ int static thread_local init_count_; |
|
86 |
+ |
|
87 |
+ GLFWwindow * window_; |
|
88 |
+ |
|
89 |
+}; |
|
90 |
+ |
|
91 |
+ |
|
92 |
+/// Inline definitions |
|
93 |
+ |
|
94 |
+inline void GLBackendGLFW::current() |
|
95 |
+{ |
|
96 |
+ glfwMakeContextCurrent(window_); |
|
97 |
+} |
|
98 |
+ |
|
99 |
+inline void GLBackendGLFW::events() |
|
100 |
+{ |
|
101 |
+ glfwPollEvents(); |
|
102 |
+} |
|
103 |
+ |
|
104 |
+inline void GLBackendGLFW::swap() |
|
105 |
+{ |
|
106 |
+ glfwSwapBuffers(window_); |
|
107 |
+} |
|
108 |
+ |
|
109 |
+inline bool GLBackendGLFW::running(bool running) |
|
110 |
+{ |
|
111 |
+ glfwSetWindowShouldClose(window_, !running); |
|
112 |
+ return running; |
|
113 |
+} |
|
114 |
+ |
|
115 |
+inline bool GLBackendGLFW::running() const |
|
116 |
+{ |
|
117 |
+ return !glfwWindowShouldClose(window_); |
|
118 |
+} |
|
119 |
+ |
|
120 |
+inline float GLBackendGLFW::time() const |
|
121 |
+{ |
|
122 |
+ return (float)glfwGetTime(); |
|
123 |
+} |
|
124 |
+ |
|
125 |
+inline float GLBackendGLFW::time(float time) |
|
126 |
+{ |
|
127 |
+ glfwSetTime((double)time); |
|
128 |
+ return time; |
|
129 |
+} |
|
130 |
+ |
|
131 |
+inline void GLBackendGLFW::lock(bool lock) |
|
132 |
+{ |
|
133 |
+ glfwSetInputMode( |
|
134 |
+ window_, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL |
|
135 |
+ ); |
|
136 |
+} |
|
137 |
+ |
|
138 |
+inline bool GLBackendGLFW::key(std::string const & key) const |
|
139 |
+{ |
|
140 |
+ if (key == "Left") |
|
141 |
+ return glfwGetKey(window_, GLFW_KEY_LEFT) == GLFW_PRESS; |
|
142 |
+ else if (key == "Right") |
|
143 |
+ return glfwGetKey(window_, GLFW_KEY_RIGHT) == GLFW_PRESS; |
|
144 |
+ else if (key == "Up") |
|
145 |
+ return glfwGetKey(window_, GLFW_KEY_UP) == GLFW_PRESS; |
|
146 |
+ else if (key == "Down") |
|
147 |
+ return glfwGetKey(window_, GLFW_KEY_DOWN) == GLFW_PRESS; |
|
148 |
+ else if (key == "Enter") |
|
149 |
+ return glfwGetKey(window_, GLFW_KEY_ENTER) == GLFW_PRESS; |
|
150 |
+ else if (key == "Escape") |
|
151 |
+ return glfwGetKey(window_, GLFW_KEY_ESCAPE) == GLFW_PRESS; |
|
152 |
+ else if (key == "Tab") |
|
153 |
+ return glfwGetKey(window_, GLFW_KEY_TAB) == GLFW_PRESS; |
|
154 |
+ else if (key == "Backspace") |
|
155 |
+ return glfwGetKey(window_, GLFW_KEY_BACKSPACE) == GLFW_PRESS; |
|
156 |
+ else if (key == "Control") |
|
157 |
+ return |
|
158 |
+ glfwGetKey(window_, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || |
|
159 |
+ glfwGetKey(window_, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS; |
|
160 |
+ else if (key == "Shift") |
|
161 |
+ return |
|
162 |
+ glfwGetKey(window_, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || |
|
163 |
+ glfwGetKey(window_, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS; |
|
164 |
+ else if (key == "Alt") |
|
165 |
+ return |
|
166 |
+ glfwGetKey(window_, GLFW_KEY_LEFT_ALT) == GLFW_PRESS || |
|
167 |
+ glfwGetKey(window_, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS; |
|
168 |
+ else if (key.length() == 1) |
|
169 |
+ return glfwGetKey(window_, key[0]) == GLFW_PRESS; |
|
170 |
+ return false; |
|
171 |
+} |
|
172 |
+ |
|
173 |
+inline bool GLBackendGLFW::button(int button) const |
|
174 |
+{ |
|
175 |
+ return glfwGetMouseButton(window_, button - 1) == GLFW_PRESS; |
|
176 |
+} |
|
177 |
+ |
|
178 |
+ |
|
179 |
+/// Guards |
|
180 |
+ |
|
181 |
+#endif |
|
182 |
+#endif |