From fba8184491e0b7ae6fab7ac01b4600d230dc4569 Mon Sep 17 00:00:00 2001
From: marsunet <marc.sunet@amd.com>
Date: Tue, 21 Dec 2021 17:04:22 -0800
Subject: Initial commit with window demo.

---
 dxwindow/CMakeLists.txt     | 10 ++++++
 dxwindow/include/dxwindow.h | 50 ++++++++++++++++++++++++++++
 dxwindow/src/dxwindow.cc    | 80 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 140 insertions(+)
 create mode 100644 dxwindow/CMakeLists.txt
 create mode 100644 dxwindow/include/dxwindow.h
 create mode 100644 dxwindow/src/dxwindow.cc

(limited to 'dxwindow')

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 @@
+cmake_minimum_required(VERSION 3.0)
+
+add_library(dxwindow
+    src/dxwindow.cc)
+
+target_include_directories(dxwindow PUBLIC
+    include/)
+
+target_link_libraries(dxwindow PUBLIC
+    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 @@
+#pragma once
+
+// Include Windows.h before GLFW to avoid macro redefinition warnings.
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+
+#define GLFW_INCLUDE_NONE  // Do not include OpenGL headers.
+#include <GLFW/glfw3.h>
+
+namespace dx
+{
+
+class Window
+{
+public:
+    ~Window();
+
+    /// Creates the window.
+    bool Initialise(int width, int height, const char* title);
+
+    /// Returns the native window handle.
+    /// If the window has not been initialized, returns an invalid handle.
+    HWND GetWindowHandle();
+
+    /// Updates the window by polling for user input.
+    void Update();
+
+    /// Returns true if the user tried to close the window, false otherwise.
+    bool ShouldClose() const;
+
+private:
+    GLFWwindow* m_window = nullptr;
+};
+
+/// Initialise the window subsystem.
+///
+/// This function must be called at the start of your application before any
+/// Windows are created.
+bool WindowInitialise();
+
+/// Terminate the window subsystem.
+///
+/// This function should be called at the end of your application. Any existing
+/// Windows are destroyed and are invalid beyond this call.
+void WindowTerminate();
+
+/// Returns the last Window error.
+const char* GetWindowError();
+
+}  // 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 @@
+#include "dxwindow.h"
+
+#define GLFW_EXPOSE_NATIVE_WIN32
+#include <GLFW/glfw3native.h>
+
+#include <cassert>
+#include <cstdio>
+
+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