PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
window_internal.h
1#pragma once
2
3#include "pixelbullet/core/event.h"
4
5#include "pixelbullet/image/bitmap.h"
6#include "pixelbullet/input/input.h"
7#include "pixelbullet/input/key_codes.h"
8#include "pixelbullet/window/window_chrome.h"
9
10#include <glm/glm.hpp>
11
12#include <cstdint>
13#include <functional>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <utility>
18
19struct GLFWimage;
20struct GLFWwindow;
21
22namespace pixelbullet
23{
24class Filesystem;
25class VirtualPath;
26class Window;
27struct WindowConfig;
28namespace input
29{
30class InputState;
31}
32} // namespace pixelbullet
33
34namespace pixelbullet::window_internal
35{
37{
38 GLFWwindow* window = nullptr;
39 std::string title;
40 unsigned int width = 0;
41 unsigned int height = 0;
42 std::function<void(pixelbullet::core::Event&)> event_callback;
43 std::function<void(uint32_t)> framebuffer_resize_callback;
44
45 bool fullscreen = false;
46 bool focused = false;
47 glm::ivec2 size{ 0, 0 };
48 glm::ivec2 fullscreen_size{ 0, 0 };
49 glm::ivec2 position{ 0, 0 };
50 glm::vec2 content_scale{ 1.0f, 1.0f };
52 uint32_t id = 0;
53 bool cursor_capture_requested = false;
54 bool cursor_captured = false;
55 WindowFrameMode frame_mode = WindowFrameMode::SystemDecorated;
56 bool custom_chrome_supported = false;
57 bool custom_chrome_attached = false;
58 WindowChromeLayout custom_chrome_layout{};
59};
60
62{
63 glm::ivec2 framebuffer_size{ 0, 0 };
64 glm::ivec2 position{ 0, 0 };
65 bool focused = false;
66 glm::vec2 content_scale{ 1.0f, 1.0f };
67};
68
70{
71public:
72 virtual ~GlfwApi() = default;
73
74 virtual void AcquireRuntime() = 0;
75 virtual void ReleaseRuntime() noexcept = 0;
76 [[nodiscard]] virtual unsigned int NextWindowId() noexcept = 0;
77 virtual void DefaultWindowHints() = 0;
78 virtual void WindowHint(int hint, int value) = 0;
79 [[nodiscard]] virtual GLFWwindow* CreateWindow(int width, int height, const char* title) = 0;
80 virtual void DestroyWindow(GLFWwindow* window) = 0;
81 virtual void SetWindowUserPointer(GLFWwindow* window, void* pointer) = 0;
82 [[nodiscard]] virtual WindowSnapshot CaptureWindowSnapshot(GLFWwindow* window) = 0;
83 virtual void RegisterCallbacks(GLFWwindow* window) = 0;
84 virtual void PollEvents() noexcept = 0;
85 virtual void WaitEventsTimeout(double timeout_seconds) noexcept = 0;
86 virtual void SetWindowIcon(GLFWwindow* window, int count, const GLFWimage* images) = 0;
87 virtual void IconifyWindow(GLFWwindow* window) noexcept = 0;
88 virtual void MaximizeWindow(GLFWwindow* window) noexcept = 0;
89 virtual void RestoreWindow(GLFWwindow* window) noexcept = 0;
90 virtual void SetInputMode(GLFWwindow* window, int mode, int value) noexcept = 0;
91 virtual void SetClipboardString(GLFWwindow* window, const char* text) noexcept = 0;
92 [[nodiscard]] virtual const char* GetClipboardString(GLFWwindow* window) const noexcept = 0;
93 [[nodiscard]] virtual int GetWindowAttrib(GLFWwindow* window, int attrib) const noexcept = 0;
94 [[nodiscard]] virtual int GetKey(GLFWwindow* window, int key) const noexcept = 0;
95 [[nodiscard]] virtual glm::vec2 GetCursorPos(GLFWwindow* window) const noexcept = 0;
96};
97
99{
100public:
101 virtual ~WindowChromeApi() = default;
102
103 [[nodiscard]] virtual bool PlatformSupportsCustomChrome() const noexcept = 0;
104 [[nodiscard]] virtual bool AttachCustomChrome(Window& window) = 0;
105 virtual void DetachCustomChrome(Window& window) noexcept = 0;
106 virtual void OnLayoutChanged(Window& window) noexcept = 0;
107 virtual void ShowSystemMenuAt(Window& window, WindowPoint client_point) noexcept = 0;
108};
109
111{
112 [[nodiscard]] static GLFWwindow*& NativeWindow(Window& window) noexcept;
113 [[nodiscard]] static const GLFWwindow* NativeWindow(const Window& window) noexcept;
114 [[nodiscard]] static std::string& Title(Window& window) noexcept;
115 [[nodiscard]] static unsigned int& Width(Window& window) noexcept;
116 [[nodiscard]] static unsigned int& Height(Window& window) noexcept;
117 [[nodiscard]] static bool& Focused(Window& window) noexcept;
118 [[nodiscard]] static bool& Fullscreen(Window& window) noexcept;
119 [[nodiscard]] static glm::ivec2& Size(Window& window) noexcept;
120 [[nodiscard]] static glm::ivec2& FullscreenSize(Window& window) noexcept;
121 [[nodiscard]] static glm::ivec2& Position(Window& window) noexcept;
122 [[nodiscard]] static glm::vec2& ContentScale(Window& window) noexcept;
123 [[nodiscard]] static pixelbullet::input::InputState& InputState(Window& window) noexcept;
124 [[nodiscard]] static std::function<void(pixelbullet::core::Event&)>& EventCallback(Window& window) noexcept;
125 [[nodiscard]] static std::function<void(uint32_t)>& FramebufferResizeCallback(Window& window) noexcept;
126 [[nodiscard]] static uint32_t& Id(Window& window) noexcept;
127 [[nodiscard]] static bool& CursorCaptureRequested(Window& window) noexcept;
128 [[nodiscard]] static bool& CursorCaptured(Window& window) noexcept;
129 [[nodiscard]] static WindowFrameMode& FrameMode(Window& window) noexcept;
130 [[nodiscard]] static bool& CustomChromeSupported(Window& window) noexcept;
131 [[nodiscard]] static bool& CustomChromeAttached(Window& window) noexcept;
132 [[nodiscard]] static WindowChromeLayout& CustomChromeLayout(Window& window) noexcept;
133};
134
135class ScopedGlfwApiOverride
136{
137public:
138 explicit ScopedGlfwApiOverride(GlfwApi& api) noexcept;
139 ~ScopedGlfwApiOverride();
140
141 ScopedGlfwApiOverride(const ScopedGlfwApiOverride&) = delete;
142 ScopedGlfwApiOverride& operator=(const ScopedGlfwApiOverride&) = delete;
143
144private:
145 GlfwApi* previous_api_ = nullptr;
146};
147
148class ScopedWindowChromeApiOverride
149{
150public:
151 explicit ScopedWindowChromeApiOverride(WindowChromeApi& api) noexcept;
152 ~ScopedWindowChromeApiOverride();
153
154 ScopedWindowChromeApiOverride(const ScopedWindowChromeApiOverride&) = delete;
155 ScopedWindowChromeApiOverride& operator=(const ScopedWindowChromeApiOverride&) = delete;
156
157private:
158 WindowChromeApi* previous_api_ = nullptr;
159};
160
161using IconBitmapLoader = std::function<Bitmap(const Filesystem&, const VirtualPath&)>;
162
163class ScopedIconBitmapLoaderOverride
164{
165public:
166 explicit ScopedIconBitmapLoaderOverride(IconBitmapLoader loader);
167 ~ScopedIconBitmapLoaderOverride();
168
169 ScopedIconBitmapLoaderOverride(const ScopedIconBitmapLoaderOverride&) = delete;
170 ScopedIconBitmapLoaderOverride& operator=(const ScopedIconBitmapLoaderOverride&) = delete;
171
172private:
173 IconBitmapLoader previous_loader_;
174};
175
176[[nodiscard]] GlfwApi& GetGlfwApi();
177[[nodiscard]] WindowChromeApi& GetWindowChromeApi();
178[[nodiscard]] Bitmap LoadIconBitmap(const Filesystem& filesystem, const VirtualPath& icon_path);
179
180void InitializeWindow(Window& window, const WindowConfig& config);
181void ShutdownWindow(Window& window) noexcept;
182void BeginFrame(Window& window) noexcept;
183void PollEvents() noexcept;
184void WaitEventsTimeout(double timeout_seconds) noexcept;
185void SetIcon(Window& window, const Filesystem& filesystem, const VirtualPath& icon_path);
186void Minimize(Window& window) noexcept;
187void Maximize(Window& window) noexcept;
188void Restore(Window& window) noexcept;
189void RequestClose(Window& window) noexcept;
190[[nodiscard]] bool SetClipboardText(Window& window, std::string_view text);
191[[nodiscard]] std::optional<std::string> GetClipboardText(const Window& window);
192void ShowSystemMenuAt(Window& window, WindowPoint client_point) noexcept;
193[[nodiscard]] bool SupportsCustomChrome(const Window& window) noexcept;
194void SetCustomChromeLayout(Window& window, const WindowChromeLayout& layout);
195void ClearCustomChromeLayout(Window& window);
196void SetCursorCaptured(Window& window, bool captured) noexcept;
197void HandleFocusChanged(Window& window, bool focused) noexcept;
198[[nodiscard]] bool IsKeyDown(const Window& window, pixelbullet::input::KeyCode key) noexcept;
199[[nodiscard]] bool IsMinimized(const Window& window) noexcept;
200[[nodiscard]] bool IsMaximized(const Window& window) noexcept;
201} // namespace pixelbullet::window_internal
Packed RGBA8 CPU bitmap storage.
Definition bitmap.h:19
Definition filesystem.h:19
Definition virtual_path.h:10
Definition window.h:30
Definition event.h:108
Definition input.h:20
Definition window_internal.h:70
Definition window_internal.h:99
Definition window_chrome.h:34
Definition window_config.h:11
Definition window_chrome.h:28
Definition window_internal.h:111
Definition window_internal.h:62
Definition window_internal.h:37