diff options
Diffstat (limited to 'dxwindow/src/dxwindow.cc')
-rw-r--r-- | dxwindow/src/dxwindow.cc | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/dxwindow/src/dxwindow.cc b/dxwindow/src/dxwindow.cc new file mode 100644 index 0000000..8848a7e --- /dev/null +++ b/dxwindow/src/dxwindow.cc | |||
@@ -0,0 +1,80 @@ | |||
1 | #include "dxwindow.h" | ||
2 | |||
3 | #define GLFW_EXPOSE_NATIVE_WIN32 | ||
4 | #include <GLFW/glfw3native.h> | ||
5 | |||
6 | #include <cassert> | ||
7 | #include <cstdio> | ||
8 | |||
9 | namespace dx | ||
10 | { | ||
11 | |||
12 | static char glfw_error[1024] = {}; | ||
13 | |||
14 | static void glfw_error_callback(int error, const char* description) | ||
15 | { | ||
16 | sprintf_s(glfw_error, sizeof(glfw_error), | ||
17 | "GLFW error %d: %s", error, description); | ||
18 | } | ||
19 | |||
20 | Window::~Window() | ||
21 | { | ||
22 | if (m_window != nullptr) | ||
23 | { | ||
24 | glfwDestroyWindow(m_window); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | bool Window::Initialise(int width, int height, const char* title) | ||
29 | { | ||
30 | // GLFW by default creates an OpenGL context with the window. | ||
31 | // Use GLFW_NO_API to tell it not to do so. | ||
32 | glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); | ||
33 | |||
34 | if ((m_window = glfwCreateWindow( | ||
35 | width, height, title, /*monitor=*/NULL, /*share=*/NULL)) == nullptr) | ||
36 | { | ||
37 | return false; | ||
38 | } | ||
39 | |||
40 | return true; | ||
41 | } | ||
42 | |||
43 | HWND Window::GetWindowHandle() | ||
44 | { | ||
45 | if (!m_window) | ||
46 | { | ||
47 | return NULL; | ||
48 | } | ||
49 | return glfwGetWin32Window(m_window); | ||
50 | } | ||
51 | |||
52 | void Window::Update() | ||
53 | { | ||
54 | assert(m_window); | ||
55 | glfwPollEvents(); | ||
56 | } | ||
57 | |||
58 | bool Window::ShouldClose() const | ||
59 | { | ||
60 | assert(m_window); | ||
61 | return glfwWindowShouldClose(m_window) == GLFW_TRUE; | ||
62 | } | ||
63 | |||
64 | bool WindowInitialise() | ||
65 | { | ||
66 | glfwSetErrorCallback(glfw_error_callback); | ||
67 | return glfwInit() == GLFW_TRUE; | ||
68 | } | ||
69 | |||
70 | void WindowTerminate() | ||
71 | { | ||
72 | glfwTerminate(); | ||
73 | } | ||
74 | |||
75 | const char* GetWindowError() | ||
76 | { | ||
77 | return glfw_error; | ||
78 | } | ||
79 | |||
80 | } // namespace dx | ||