175 lines
5.2 KiB
C++
175 lines
5.2 KiB
C++
#pragma once
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <functional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
|
|
namespace display {
|
|
|
|
/**
|
|
* Represents a window backed by GLFW.
|
|
* This abstraction considers this to be a physical
|
|
* window on the user's desktop.
|
|
*/
|
|
class Window {
|
|
public:
|
|
|
|
using ResizeFn = std::function<void(int width, int height)>;
|
|
using CloseFn = std::function<void()>;
|
|
|
|
using KeyFn = std::function<void(int key, int scancode, int action, int mods)>;
|
|
|
|
using CursorPosFn = std::function<void(double xpos, double ypos)>;
|
|
using MouseButtonFn = std::function<void(int button, int action, int mods)>;
|
|
using ScrollFn = std::function<void(double xoffset, double yoffset)>;
|
|
using CursorEnterFn = std::function<void(bool entered)>;
|
|
|
|
struct Callbacks {
|
|
|
|
ResizeFn onResize{ nullptr };
|
|
CloseFn onClose{ nullptr };
|
|
|
|
KeyFn onKey{ nullptr };
|
|
|
|
CursorPosFn onCursorPos{ nullptr };
|
|
MouseButtonFn onMouseButton{ nullptr };
|
|
ScrollFn onScroll{ nullptr };
|
|
CursorEnterFn onCursorEnter{ nullptr };
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
GLFWwindow* handle{ nullptr };
|
|
Callbacks callbacks{ nullptr };
|
|
|
|
public:
|
|
|
|
/**
|
|
* @brief Constructs an uninitialized, null Window wrapper.
|
|
*/
|
|
Window() noexcept = default;
|
|
|
|
/**
|
|
* @brief Constructs and initializes a physical GLFW window.
|
|
*
|
|
* @param width Initial width in screen coordinates.
|
|
* @param height Initial height in screen coordinates.
|
|
* @param title Title displayed in the window header bar.
|
|
* @param monitor Monitor to use for full screen, or nullptr for windowed mode.
|
|
* @param share Window whose context to share resources with, or nullptr.
|
|
*
|
|
* @throws std::runtime_error If window creation fails.
|
|
*/
|
|
Window(int width, int height, std::string_view title, GLFWmonitor* monitor = nullptr, GLFWwindow* share = nullptr);
|
|
|
|
/**
|
|
* @brief Destructor. Destroys the underlying GLFW window handle if valid.
|
|
*/
|
|
~Window() noexcept;
|
|
|
|
// --- Prevent Copying (RAII Single-Ownership) ---
|
|
Window(const Window&) = delete;
|
|
|
|
Window& operator=(const Window&) = delete;
|
|
|
|
// --- Enable Move Semantics ---
|
|
/**
|
|
* @brief Move constructor. Takes ownership of another Window's handle.
|
|
*/
|
|
Window(Window&& other) noexcept;
|
|
|
|
public:
|
|
|
|
/**
|
|
* @brief Move assignment operator. Safely cleans up current handle and steals other's handle.
|
|
*/
|
|
Window& operator=(Window&& other) noexcept;
|
|
|
|
public:
|
|
|
|
// --- Core Operations ---
|
|
|
|
/**
|
|
* @brief Checks if the underlying GLFW handle is non-null.
|
|
*/
|
|
bool isValid() const noexcept;
|
|
|
|
/**
|
|
* @brief Returns true if the user or system requested this window to close.
|
|
*/
|
|
bool shouldClose() const noexcept;
|
|
|
|
/**
|
|
* @brief Sets the close flag for the window.
|
|
*/
|
|
void setShouldClose(bool value) noexcept;
|
|
|
|
/**
|
|
* @brief Swaps the front and back color buffers.
|
|
*/
|
|
void swapBuffers() noexcept;
|
|
|
|
void setVisible(bool val) noexcept;
|
|
|
|
/**
|
|
* @brief Makes this window's OpenGL context current on the calling thread.
|
|
*/
|
|
void makeContextCurrent() noexcept;
|
|
|
|
// --- Window State Query & Mutators ---
|
|
|
|
std::pair<int, int> getSize() const noexcept;
|
|
|
|
std::pair<int, int> getFramebufferSize() const noexcept;
|
|
|
|
void setTitle(std::string_view title);
|
|
|
|
public: // --- Callbacks ---
|
|
|
|
/**
|
|
* @brief Attaches a modern C++ callback for window resize events.
|
|
*/
|
|
void setResizeCallback(ResizeFn callback);
|
|
|
|
void setCloseCallback(CloseFn callback);
|
|
|
|
void setKeyCallback(KeyFn callback);
|
|
|
|
void setCursorPosCallback(CursorPosFn fn);
|
|
|
|
void setMouseButtonCallback(MouseButtonFn fn);
|
|
|
|
void setScrollCallback(ScrollFn fn);
|
|
|
|
void setCursorEnterCallback(CursorEnterFn fn);
|
|
|
|
public: // --- Native Accessors ---
|
|
|
|
/**
|
|
* @brief Returns the raw, underlying GLFWwindow pointer for low-level or library integration.
|
|
*/
|
|
GLFWwindow* getNativeHandle() const noexcept;
|
|
|
|
/**
|
|
* @brief Implicit conversion operator to GLFWwindow* for ergonomic API interoperability.
|
|
*/
|
|
operator GLFWwindow* () const noexcept;
|
|
|
|
private:
|
|
|
|
static void glfwFramebufferSizeBridge(GLFWwindow* handle, int width, int height);
|
|
static void glfwWindowCloseBridge(GLFWwindow* handle);
|
|
static void glfwKeyBridge(GLFWwindow* handle, int key, int scancode, int action, int mods);
|
|
static void glfwCursorPosBridge(GLFWwindow* handle, double xpos, double ypos);
|
|
static void glfwMouseButtonBridge(GLFWwindow* handle, int button, int action, int mods);
|
|
static void glfwScrollBridge(GLFWwindow* handle, double xoffset, double yoffset);
|
|
static void glfwCursorEnterBridge(GLFWwindow* handle, int entered);
|
|
|
|
};
|
|
|
|
}
|