diff options
Diffstat (limited to 'dxwindow')
-rw-r--r-- | dxwindow/CMakeLists.txt | 10 | ||||
-rw-r--r-- | dxwindow/include/dxwindow.h | 50 | ||||
-rw-r--r-- | dxwindow/src/dxwindow.cc | 80 |
3 files changed, 140 insertions, 0 deletions
diff --git a/dxwindow/CMakeLists.txt b/dxwindow/CMakeLists.txt new file mode 100644 index 0000000..c29e098 --- /dev/null +++ b/dxwindow/CMakeLists.txt | |||
@@ -0,0 +1,10 @@ | |||
1 | cmake_minimum_required(VERSION 3.0) | ||
2 | |||
3 | add_library(dxwindow | ||
4 | src/dxwindow.cc) | ||
5 | |||
6 | target_include_directories(dxwindow PUBLIC | ||
7 | include/) | ||
8 | |||
9 | target_link_libraries(dxwindow PUBLIC | ||
10 | glfw) | ||
diff --git a/dxwindow/include/dxwindow.h b/dxwindow/include/dxwindow.h new file mode 100644 index 0000000..e8f551a --- /dev/null +++ b/dxwindow/include/dxwindow.h | |||
@@ -0,0 +1,50 @@ | |||
1 | #pragma once | ||
2 | |||
3 | // Include Windows.h before GLFW to avoid macro redefinition warnings. | ||
4 | #define WIN32_LEAN_AND_MEAN | ||
5 | #include <Windows.h> | ||
6 | |||
7 | #define GLFW_INCLUDE_NONE // Do not include OpenGL headers. | ||
8 | #include <GLFW/glfw3.h> | ||
9 | |||
10 | namespace dx | ||
11 | { | ||
12 | |||
13 | class Window | ||
14 | { | ||
15 | public: | ||
16 | ~Window(); | ||
17 | |||
18 | /// Creates the window. | ||
19 | bool Initialise(int width, int height, const char* title); | ||
20 | |||
21 | /// Returns the native window handle. | ||
22 | /// If the window has not been initialized, returns an invalid handle. | ||
23 | HWND GetWindowHandle(); | ||
24 | |||
25 | /// Updates the window by polling for user input. | ||
26 | void Update(); | ||
27 | |||
28 | /// Returns true if the user tried to close the window, false otherwise. | ||
29 | bool ShouldClose() const; | ||
30 | |||
31 | private: | ||
32 | GLFWwindow* m_window = nullptr; | ||
33 | }; | ||
34 | |||
35 | /// Initialise the window subsystem. | ||
36 | /// | ||
37 | /// This function must be called at the start of your application before any | ||
38 | /// Windows are created. | ||
39 | bool WindowInitialise(); | ||
40 | |||
41 | /// Terminate the window subsystem. | ||
42 | /// | ||
43 | /// This function should be called at the end of your application. Any existing | ||
44 | /// Windows are destroyed and are invalid beyond this call. | ||
45 | void WindowTerminate(); | ||
46 | |||
47 | /// Returns the last Window error. | ||
48 | const char* GetWindowError(); | ||
49 | |||
50 | } // namespace dx | ||
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 | ||