From fba8184491e0b7ae6fab7ac01b4600d230dc4569 Mon Sep 17 00:00:00 2001 From: marsunet Date: Tue, 21 Dec 2021 17:04:22 -0800 Subject: Initial commit with window demo. --- dxwindow/src/dxwindow.cc | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 dxwindow/src/dxwindow.cc (limited to 'dxwindow/src/dxwindow.cc') 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 @@ +#include "dxwindow.h" + +#define GLFW_EXPOSE_NATIVE_WIN32 +#include + +#include +#include + +namespace dx +{ + +static char glfw_error[1024] = {}; + +static void glfw_error_callback(int error, const char* description) +{ + sprintf_s(glfw_error, sizeof(glfw_error), + "GLFW error %d: %s", error, description); +} + +Window::~Window() +{ + if (m_window != nullptr) + { + glfwDestroyWindow(m_window); + } +} + +bool Window::Initialise(int width, int height, const char* title) +{ + // GLFW by default creates an OpenGL context with the window. + // Use GLFW_NO_API to tell it not to do so. + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + + if ((m_window = glfwCreateWindow( + width, height, title, /*monitor=*/NULL, /*share=*/NULL)) == nullptr) + { + return false; + } + + return true; +} + +HWND Window::GetWindowHandle() +{ + if (!m_window) + { + return NULL; + } + return glfwGetWin32Window(m_window); +} + +void Window::Update() +{ + assert(m_window); + glfwPollEvents(); +} + +bool Window::ShouldClose() const +{ + assert(m_window); + return glfwWindowShouldClose(m_window) == GLFW_TRUE; +} + +bool WindowInitialise() +{ + glfwSetErrorCallback(glfw_error_callback); + return glfwInit() == GLFW_TRUE; +} + +void WindowTerminate() +{ + glfwTerminate(); +} + +const char* GetWindowError() +{ + return glfw_error; +} + +} // namespace dx -- cgit v1.2.3