MayaFlux 0.1.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
7namespace MayaFlux::Core {
8
10 : m_config(config)
11{
13 "WindowManager initialized");
14}
15
26
27std::shared_ptr<Window> WindowManager::create_window(const WindowCreateInfo& create_info)
28{
29 if (m_window_lookup.count(create_info.title) > 0) {
31 "Window with title '{}' already exists", create_info.title);
32 return nullptr;
33 }
34
36 "Creating window '{}' ({}x{}), for platform {}", create_info.title, create_info.width, create_info.height, GLFWSingleton::get_platform());
37
38 auto window = create_window_internal(create_info);
39 if (!window) {
40 return nullptr;
41 }
42
43 m_windows.push_back(window);
44 m_window_lookup[create_info.title] = window;
45
46 if (window->get_create_info().register_for_processing) {
47 m_processing_windows.push_back(window);
48 }
49
51 "Created window '{}' - total: {}", create_info.title, m_windows.size());
52
53 return window;
54}
55
56void WindowManager::destroy_window(const std::shared_ptr<Window>& window)
57{
58 if (!window)
59 return;
60
61 auto pr_it = std::ranges::find(m_processing_windows, window);
62 if (pr_it != m_processing_windows.end()) {
63 m_processing_windows.erase(pr_it);
64 }
65
66 const std::string title = window->get_create_info().title;
67 remove_from_lookup(window);
68
69 auto it = std::ranges::find(m_windows, window);
70 if (it != m_windows.end()) {
71 m_windows.erase(it);
73 "Destroyed window '{}' - remaining: {}", title, m_windows.size());
74 }
75}
76
77bool WindowManager::destroy_window_by_title(const std::string& title)
78{
79 auto window = find_window(title);
80 if (window) {
81 destroy_window(window);
82 return true;
83 }
84 return false;
85}
86
87std::vector<std::shared_ptr<Window>> WindowManager::get_windows() const
88{
89 std::vector<std::shared_ptr<Window>> ptrs;
90 ptrs.reserve(m_windows.size());
91 for (const auto& w : m_windows) {
92 ptrs.push_back(w);
93 }
94 return ptrs;
95}
96
97std::shared_ptr<Window> WindowManager::find_window(const std::string& title) const
98{
99 auto it = m_window_lookup.find(title);
100 return (it != m_window_lookup.end()) ? it->second.lock() : nullptr;
101}
102
103std::shared_ptr<Window> WindowManager::get_window(size_t index) const
104{
105 if (index >= m_windows.size()) {
106 return nullptr;
107 }
108 return m_windows[index];
109}
110
112{
113 return std::ranges::any_of(m_windows,
114 [](const auto& w) { return w->should_close(); });
115}
116
118{
119 size_t destroyed_count = 0;
120
121 std::vector<std::shared_ptr<Window>> to_destroy;
122 for (const auto& window : m_windows) {
123 if (window->should_close()) {
124 to_destroy.push_back(window);
125 }
126 }
127
128 for (auto& window : to_destroy) {
129 destroy_window(window);
130 destroyed_count++;
131 }
132
133 if (destroyed_count > 0) {
135 "Destroyed {} closed window(s)", destroyed_count);
136 }
137
138 return destroyed_count;
139}
140
142 const WindowCreateInfo& create_info)
143{
144 switch (m_config.windowing_backend) {
146 return std::make_unique<GlfwWindow>(create_info, m_config.surface_info,
148
151 "SDL backend not implemented");
152 return nullptr;
153
156 "Native backend not implemented");
157 return nullptr;
158
159 default:
161 "Unknown windowing backend: {}",
162 static_cast<int>(m_config.windowing_backend));
163 return nullptr;
164 }
165}
166
167void WindowManager::remove_from_lookup(const std::shared_ptr<Window>& window)
168{
169 const auto& title = window->get_create_info().title;
170 m_window_lookup.erase(title);
171}
172
174{
175 glfwPollEvents();
176
177 {
178 std::lock_guard<std::mutex> lock(m_hooks_mutex);
179 for (const auto& [name, hook] : m_frame_hooks) {
180 hook();
181 }
182 }
183
185
186 return window_count() > 0;
187}
188
189void WindowManager::register_frame_hook(const std::string& name,
190 std::function<void()> hook)
191{
192 std::lock_guard<std::mutex> lock(m_hooks_mutex);
193 m_frame_hooks[name] = std::move(hook);
194}
195
196void WindowManager::unregister_frame_hook(const std::string& name)
197{
198 std::lock_guard<std::mutex> lock(m_hooks_mutex);
199 m_frame_hooks.erase(name);
200}
201
202} // namespace MayaFlux::Core
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_PRINT(comp, ctx,...)
#define MF_WARN(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.
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 destroy_window(const std::shared_ptr< Window > &window)
Destroys a window by pointer.
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.
@ WindowingSubsystem
Windowing system operations (GLFW, SDL)
@ Core
Core engine, backend, subsystems.
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.