|
| 1 | +## Disclaimer |
| 2 | + |
| 3 | +In this tutorial, we will be using GLFW to create and manage our window. Since this is not part of the core tutorial, we will only provide a small rundown of the header file without going into further detail. If you are new to GLFW, we recommend you check out [this tutorial on learnopengl.com](https://learnopengl.com/Getting-started/Hello-Window) |
| 4 | + |
| 5 | +The code below will open a small window which we will use in the next sections. |
| 6 | + |
| 7 | +## window.hpp |
| 8 | + |
| 9 | +```cpp |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <daxa/daxa.hpp> |
| 13 | +using namespace daxa::types; |
| 14 | + |
| 15 | +#include <GLFW/glfw3.h> |
| 16 | +#if defined(_WIN32) |
| 17 | +#define GLFW_EXPOSE_NATIVE_WIN32 |
| 18 | +#define GLFW_NATIVE_INCLUDE_NONE |
| 19 | +using HWND = void *; |
| 20 | +#elif defined(__linux__) |
| 21 | +#define GLFW_EXPOSE_NATIVE_X11 |
| 22 | +#define GLFW_EXPOSE_NATIVE_WAYLAND |
| 23 | +#endif |
| 24 | +#include <GLFW/glfw3native.h> |
| 25 | + |
| 26 | +struct Window{ |
| 27 | + GLFWwindow * glfw_window_ptr; |
| 28 | + u32 width, height; |
| 29 | + bool minimized = false; |
| 30 | + bool swapchain_out_of_date = false; |
| 31 | + |
| 32 | + explicit Window(char const * window_name, u32 sx = 800, u32 sy = 600) : width{sx}, height{sy} { |
| 33 | + // Initialize GLFW |
| 34 | + glfwInit(); |
| 35 | + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); |
| 36 | + |
| 37 | + // Tell GLFW to make the window resizable |
| 38 | + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); |
| 39 | + |
| 40 | + // Create the window |
| 41 | + glfw_window_ptr = glfwCreateWindow(static_cast<i32>(width), static_cast<i32>(height), window_name, nullptr, nullptr); |
| 42 | + |
| 43 | + // Set the user pointer to this window |
| 44 | + glfwSetWindowUserPointer(glfw_window_ptr, this); |
| 45 | + |
| 46 | + // Enable vsync (To limit the framerate to the refresh rate of the monitor) |
| 47 | + glfwSwapInterval(1); |
| 48 | + |
| 49 | + // When the window is resized, update the width and height and mark the swapchain as out of date |
| 50 | + glfwSetWindowContentScaleCallback(glfw_window_ptr, [](GLFWwindow* window, float xscale, float yscale){ |
| 51 | + auto* win = static_cast<Window*>(glfwGetWindowUserPointer(window)); |
| 52 | + win->width = static_cast<u32>(xscale); |
| 53 | + win->height = static_cast<u32>(yscale); |
| 54 | + win->swapchain_out_of_date = true; |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + ~Window() { |
| 59 | + glfwDestroyWindow(glfw_window_ptr); |
| 60 | + glfwTerminate(); |
| 61 | + } |
| 62 | + |
| 63 | + auto get_native_handle() const -> daxa::NativeWindowHandle |
| 64 | + { |
| 65 | +#if defined(_WIN32) |
| 66 | + return glfwGetWin32Window(glfw_window_ptr); |
| 67 | +#elif defined(__linux__) |
| 68 | + switch (get_native_platform()) |
| 69 | + { |
| 70 | + case daxa::NativeWindowPlatform::WAYLAND_API: |
| 71 | + return reinterpret_cast<daxa::NativeWindowHandle>(glfwGetWaylandWindow(glfw_window_ptr)); |
| 72 | + case daxa::NativeWindowPlatform::XLIB_API: |
| 73 | + default: |
| 74 | + return reinterpret_cast<daxa::NativeWindowHandle>(glfwGetX11Window(glfw_window_ptr)); |
| 75 | + } |
| 76 | +#endif |
| 77 | + } |
| 78 | + |
| 79 | + static auto get_native_platform() -> daxa::NativeWindowPlatform |
| 80 | + { |
| 81 | + switch(glfwGetPlatform()) |
| 82 | + { |
| 83 | + case GLFW_PLATFORM_WIN32: return daxa::NativeWindowPlatform::WIN32_API; |
| 84 | + case GLFW_PLATFORM_X11: return daxa::NativeWindowPlatform::XLIB_API; |
| 85 | + case GLFW_PLATFORM_WAYLAND: return daxa::NativeWindowPlatform::WAYLAND_API; |
| 86 | + default: return daxa::NativeWindowPlatform::UNKNOWN; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + inline void set_mouse_capture(bool should_capture) const |
| 91 | + { |
| 92 | + glfwSetCursorPos(glfw_window_ptr, static_cast<f64>(width / 2.), static_cast<f64>(height / 2.)); |
| 93 | + glfwSetInputMode(glfw_window_ptr, GLFW_CURSOR, should_capture ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); |
| 94 | + glfwSetInputMode(glfw_window_ptr, GLFW_RAW_MOUSE_MOTION, should_capture); |
| 95 | + } |
| 96 | + |
| 97 | + inline bool should_close() const |
| 98 | + { |
| 99 | + return glfwWindowShouldClose(glfw_window_ptr); |
| 100 | + } |
| 101 | + |
| 102 | + inline void update() const |
| 103 | + { |
| 104 | + glfwPollEvents(); |
| 105 | + glfwSwapBuffers(glfw_window_ptr); |
| 106 | + } |
| 107 | + |
| 108 | + inline GLFWwindow* get_glfw_window() const{ |
| 109 | + return glfw_window_ptr; |
| 110 | + } |
| 111 | + |
| 112 | + inline bool should_close() { |
| 113 | + return glfwWindowShouldClose(glfw_window_ptr); |
| 114 | + } |
| 115 | +}; |
| 116 | +``` |
| 117 | + |
| 118 | +## main.cpp |
| 119 | + |
| 120 | +```cpp |
| 121 | +#include "window.hpp" |
| 122 | + |
| 123 | +#include <iostream> |
| 124 | + |
| 125 | +#include <daxa/daxa.hpp> |
| 126 | +#include <daxa/utils/pipeline_manager.hpp> |
| 127 | +#include <daxa/utils/task_graph.hpp> |
| 128 | + |
| 129 | +using namespace daxa::task_resource_uses; |
| 130 | +using namespace daxa::types; |
| 131 | + |
| 132 | +int main(int argc, char const *argv[]) |
| 133 | +{ |
| 134 | + // Create a window |
| 135 | + auto window = Window("Learn Daxa", 860, 640); |
| 136 | + |
| 137 | + // Daxa code goes here... |
| 138 | + |
| 139 | + while (!window.should_close()) |
| 140 | + { |
| 141 | + window.update(); |
| 142 | + } |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | +``` |
0 commit comments