MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
WindowManager.cpp
Go to the documentation of this file.
1#include "WindowManager.hpp"
2
6
8
10
11namespace MayaFlux::Core {
12
14 : m_config(config)
15{
17 "WindowManager initialized");
18}
19
28
29std::shared_ptr<Window> WindowManager::create_window(const WindowCreateInfo& create_info)
30{
31 if (m_window_lookup.count(create_info.title) > 0) {
33 "Window with title '{}' already exists", create_info.title);
34 return nullptr;
35 }
36
38 "Creating window '{}' ({}x{}), for platform {}", create_info.title, create_info.width, create_info.height, get_platform_name());
39
40 auto window = create_window_internal(create_info);
41 if (!window) {
42 return nullptr;
43 }
44
45 if (!window->get_native_handle()) {
47 "Window '{}' has no native handle after creation, discarding", create_info.title);
48 return nullptr;
49 }
50
51 m_windows.push_back(window);
52 m_window_lookup[create_info.title] = window;
53
54 if (window->get_create_info().register_for_processing) {
56 }
57
59 "Created window '{}' - total: {}", create_info.title, m_windows.size());
60
61 return window;
62}
63
64void WindowManager::destroy_window(const std::shared_ptr<Window>& window, bool cleanup_backend)
65{
66 if (!window)
67 return;
68
69 auto pr_it = std::ranges::find(m_processing_windows, window);
70 if (pr_it != m_processing_windows.end()) {
71 m_processing_windows.erase(pr_it);
72 }
73
74 const std::string title = window->get_create_info().title;
76
77 auto it = std::ranges::find(m_windows, window);
78 if (it != m_windows.end()) {
79 m_windows.erase(it);
81 "Destroyed window '{}' - remaining: {}", title, m_windows.size());
82 }
83
84 if (!cleanup_backend) {
85 return;
86 }
87
88 if (m_terminate.load()) {
89 window->destroy();
90 } else {
92 window->destroy();
93 });
94 }
96 "Backend resources for window '{}' cleaned up", title);
97}
98
99bool WindowManager::destroy_window_by_title(const std::string& title)
100{
101 auto window = find_window(title);
102 if (window) {
104 return true;
105 }
106 return false;
107}
108
109std::vector<std::shared_ptr<Window>> WindowManager::get_windows() const
110{
111 std::vector<std::shared_ptr<Window>> ptrs;
112 ptrs.reserve(m_windows.size());
113 for (const auto& w : m_windows) {
114 ptrs.push_back(w);
115 }
116 return ptrs;
117}
118
119std::shared_ptr<Window> WindowManager::find_window(const std::string& title) const
120{
121 auto it = m_window_lookup.find(title);
122 return (it != m_window_lookup.end()) ? it->second.lock() : nullptr;
123}
124
125std::shared_ptr<Window> WindowManager::get_window(size_t index) const
126{
127 if (index >= m_windows.size()) {
128 return nullptr;
129 }
130 return m_windows[index];
131}
132
134{
135 return std::ranges::any_of(m_windows,
136 [](const auto& w) { return w->should_close(); });
137}
138
140{
141 size_t destroyed_count = 0;
142
143 std::vector<std::shared_ptr<Window>> to_destroy;
144 for (const auto& window : m_windows) {
145 if (window->should_close()) {
146 to_destroy.push_back(window);
147 }
148 }
149
150 for (auto& window : to_destroy) {
151 destroy_window(window, true);
152 destroyed_count++;
153 }
154
155 if (destroyed_count > 0) {
157 "Destroyed {} closed window(s)", destroyed_count);
158 }
159
160 return destroyed_count;
161}
162
164 const WindowCreateInfo& create_info)
165{
166 switch (m_config.windowing_backend) {
168#if defined(MAYAFLUX_PLATFORM_WINDOWS)
169 return std::make_shared<Win32Window>(create_info, m_config);
170#else
172 "Native Win32 backend not implemented on this platform");
173 return nullptr;
174#endif
175
177#if defined(MAYAFLUX_PLATFORM_LINUX)
178 return std::make_shared<WaylandWindow>(create_info, m_config);
179#else
181 "Native Wayland backend not implemented on this platform");
182 return nullptr;
183#endif
184
186#if defined(MAYAFLUX_PLATFORM_MACOS)
187 return std::make_shared<CocoaWindow>(create_info, m_config);
188#else
190 "Native Cocoa backend not implemented on this platform");
191 return nullptr;
192#endif
193
194 default:
196 "Unknown windowing backend: {}",
197 static_cast<int>(m_config.windowing_backend));
198 return nullptr;
199 }
200}
201
202void WindowManager::remove_from_lookup(const std::shared_ptr<Window>& window)
203{
204 const auto& title = window->get_create_info().title;
205 m_window_lookup.erase(title);
206}
207
209{
210 for (auto& w : m_processing_windows)
211 w->poll();
212
213 {
214 std::lock_guard<std::mutex> lock(m_hooks_mutex);
215 for (const auto& [name, hook] : m_frame_hooks) {
216 hook();
217 }
218 }
219
221
222 return window_count() > 0;
223}
224
226 std::function<void()> hook)
227{
228 std::lock_guard<std::mutex> lock(m_hooks_mutex);
229 m_frame_hooks[name] = std::move(hook);
230}
231
233{
234 std::lock_guard<std::mutex> lock(m_hooks_mutex);
235 m_frame_hooks.erase(name);
236}
237
238} // namespace MayaFlux::Core
#define MF_INFO(comp, ctx,...)
#define MF_LOG(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::weak_ptr< Core::Window > window
Definition Picker.cpp:23
std::string name
Definition VKDevice.cpp:123
uint32_t index
Definition VKDevice.cpp:122
std::shared_ptr< Window > get_window(size_t index) const
Gets window by index.
std::atomic< bool > m_terminate
bool process()
Process windows for one frame.
std::mutex m_hooks_mutex
Mutex for protecting m_windows and m_window_lookup.
std::vector< std::shared_ptr< Window > > get_windows() const
Gets all active windows.
std::vector< std::shared_ptr< Window > > m_windows
std::shared_ptr< Window > find_window(const std::string &title) const
Finds window by title.
WindowManager(const GlobalGraphicsConfig &config)
Constructs WindowManager with global graphics config.
std::shared_ptr< Window > create_window_internal(const WindowCreateInfo &create_info)
Factory for creating backend-specific windows.
bool any_window_should_close() const
Checks if any window should close.
void register_frame_hook(const std::string &name, std::function< void()> hook)
Register a hook that runs every frame.
size_t destroy_closed_windows()
Destroys all windows that should close.
void remove_from_lookup(const std::shared_ptr< Window > &window)
Removes window from lookup table.
std::shared_ptr< Window > create_window(const WindowCreateInfo &create_info)
Creates a new window.
GlobalGraphicsConfig m_config
std::unordered_map< std::string, std::function< void()> > m_frame_hooks
Calls all registered frame hooks.
std::unordered_map< std::string, std::weak_ptr< Window > > m_window_lookup
bool destroy_window_by_title(const std::string &title)
Destroys a window by title.
std::vector< std::shared_ptr< Window > > m_processing_windows
size_t window_count() const
Gets number of active windows.
void unregister_frame_hook(const std::string &name)
Unregister a previously registered frame hook.
void destroy_window(const std::shared_ptr< Window > &window, bool cleanup_backend=false)
Destroys a window by pointer.
@ WindowingSubsystem
Windowing system operations (Win32, Wayland, Cocoa)
@ Core
Core engine, backend, subsystems.
auto dispatch_main_sync(Func &&func, Args &&... args) -> decltype(auto)
Definition Dispatch.hpp:141
WindowingBackend windowing_backend
Selected windowing backend.
std::string title
Window title/identifier.
uint32_t width
Initial window dimensions.
Configuration for creating a single window instance.