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