#pragma once #include #include #include #include #include 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; using CloseFn = std::function; using KeyFn = std::function; using CursorPosFn = std::function; using MouseButtonFn = std::function; using ScrollFn = std::function; using CursorEnterFn = std::function; 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 getSize() const noexcept; std::pair 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); }; }