MayaFlux 0.2.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
9namespace MayaFlux::Core {
10
12 : m_config(config)
13{
15 "WindowManager initialized");
16}
17
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, GLFWSingleton::get_platform());
39
40 auto window = create_window_internal(create_info);
41 if (!window) {
42 return nullptr;
43 }
44
45 m_windows.push_back(window);
46 m_window_lookup[create_info.title] = window;
47
48 if (window->get_create_info().register_for_processing) {
49 m_processing_windows.push_back(window);
50 }
51
53 "Created window '{}' - total: {}", create_info.title, m_windows.size());
54
55 return window;
56}
57
58void WindowManager::destroy_window(const std::shared_ptr<Window>& window, bool cleanup_backend)
59{
60 if (!window)
61 return;
62
63 auto pr_it = std::ranges::find(m_processing_windows, window);
64 if (pr_it != m_processing_windows.end()) {
65 m_processing_windows.erase(pr_it);
66 }
67
68 const std::string title = window->get_create_info().title;
69 remove_from_lookup(window);
70
71 auto it = std::ranges::find(m_windows, window);
72 if (it != m_windows.end()) {
73 m_windows.erase(it);
75 "Destroyed window '{}' - remaining: {}", title, m_windows.size());
76 }
77
78 if (!cleanup_backend) {
79 return;
80 }
81
82 if (m_terminate.load()) {
83 window->destroy();
84 } else {
86 window->destroy();
87 });
88 }
90 "Backend resources for window '{}' cleaned up", title);
91}
92
93bool WindowManager::destroy_window_by_title(const std::string& title)
94{
95 auto window = find_window(title);
96 if (window) {
97 destroy_window(window);
98 return true;
99 }
100 return false;
101}
102
103std::vector<std::shared_ptr<Window>> WindowManager::get_windows() const
104{
105 std::vector<std::shared_ptr<Window>> ptrs;
106 ptrs.reserve(m_windows.size());
107 for (const auto& w : m_windows) {
108 ptrs.push_back(w);
109 }
110 return ptrs;
111}
112
113std::shared_ptr<Window> WindowManager::find_window(const std::string& title) const
114{
115 auto it = m_window_lookup.find(title);
116 return (it != m_window_lookup.end()) ? it->second.lock() : nullptr;
117}
118
119std::shared_ptr<Window> WindowManager::get_window(size_t index) const
120{
121 if (index >= m_windows.size()) {
122 return nullptr;
123 }
124 return m_windows[index];
125}
126
128{
129 return std::ranges::any_of(m_windows,
130 [](const auto& w) { return w->should_close(); });
131}
132
134{
135 size_t destroyed_count = 0;
136
137 std::vector<std::shared_ptr<Window>> to_destroy;
138 for (const auto& window : m_windows) {
139 if (window->should_close()) {
140 to_destroy.push_back(window);
141 }
142 }
143
144 for (auto& window : to_destroy) {
145 destroy_window(window);
146 destroyed_count++;
147 }
148
149 if (destroyed_count > 0) {
151 "Destroyed {} closed window(s)", destroyed_count);
152 }
153
154 return destroyed_count;
155}
156
158 const WindowCreateInfo& create_info)
159{
160 switch (m_config.windowing_backend) {
162 return std::make_unique<GlfwWindow>(create_info, m_config.surface_info,
164
167 "SDL backend not implemented");
168 return nullptr;
169
172 "Native backend not implemented");
173 return nullptr;
174
175 default:
177 "Unknown windowing backend: {}",
178 static_cast<int>(m_config.windowing_backend));
179 return nullptr;
180 }
181}
182
183void WindowManager::remove_from_lookup(const std::shared_ptr<Window>& window)
184{
185 const auto& title = window->get_create_info().title;
186 m_window_lookup.erase(title);
187}
188
190{
192 glfwPollEvents();
193 });
194
195 {
196 std::lock_guard<std::mutex> lock(m_hooks_mutex);
197 for (const auto& [name, hook] : m_frame_hooks) {
198 hook();
199 }
200 }
201
203
204 return window_count() > 0;
205}
206
207void WindowManager::register_frame_hook(const std::string& name,
208 std::function<void()> hook)
209{
210 std::lock_guard<std::mutex> lock(m_hooks_mutex);
211 m_frame_hooks[name] = std::move(hook);
212}
213
214void WindowManager::unregister_frame_hook(const std::string& name)
215{
216 std::lock_guard<std::mutex> lock(m_hooks_mutex);
217 m_frame_hooks.erase(name);
218}
219
220} // namespace MayaFlux::Core
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_PRINT(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
static std::string get_platform()
Gets the current GLFW platform (Wayland, X11, etc.)
static void terminate()
Terminates the GLFW library if initialized and no windows remain.
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 (GLFW, SDL)
@ Core
Core engine, backend, subsystems.
auto dispatch_main_sync(Func &&func, Args &&... args) -> decltype(auto)
Definition Parallel.hpp:110
GraphicsSurfaceInfo surface_info
System-wide configuration for visual stream processing.
@ NATIVE
Platform-native (Win32/X11/Cocoa, if implemented)
GlfwPreInitConfig glfw_preinit_config
Pre-initialization configuration for GLFW.
WindowingBackend windowing_backend
Selected windowing backend.
GraphicsApi requested_api
Selected graphics API for rendering.
std::string title
Window title/identifier.
uint32_t width
Initial window dimensions.
Configuration for creating a single window instance.