MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
WindowManager.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Core {
7
8/**
9 * @class WindowManager
10 * @brief Manages window lifecycle and GLFW event polling
11 *
12 * Responsibilities:
13 * - Create/destroy windows
14 * - Poll GLFW events (calls glfwPollEvents)
15 * - Query windows by title/index
16 */
17class MAYAFLUX_API WindowManager {
18public:
19 /**
20 * @brief Constructs WindowManager with global graphics config
21 * @param config Global graphics configuration for window defaults
22 */
23 explicit WindowManager(const GlobalGraphicsConfig& config);
24
26
27 WindowManager(const WindowManager&) = delete;
29 WindowManager(WindowManager&&) noexcept = delete;
30 WindowManager& operator=(WindowManager&&) noexcept = delete;
31
32 /**
33 * @brief Creates a new window
34 * @param create_info Window creation parameters
35 * @return Pointer to created window (owned by manager)
36 */
37 std::shared_ptr<Window> create_window(const WindowCreateInfo& create_info);
38
39 /**
40 * @brief Destroys a window by pointer
41 * @param window Pointer to window to destroy
42 * @param cleanup_backend If true, also cleans up backend resources
43 */
44 void destroy_window(const std::shared_ptr<Window>& window, bool cleanup_backend = false);
45
46 /**
47 * @brief Destroys a window by title
48 * @param title Title of window to destroy
49 * @return True if window was found and destroyed
50 */
51 bool destroy_window_by_title(const std::string& title);
52
53 /**
54 * @brief Gets all active windows
55 */
56 std::vector<std::shared_ptr<Window>> get_windows() const;
57
58 /**
59 * @brief Finds window by title
60 * @return Pointer to window, or nullptr if not found
61 */
62 std::shared_ptr<Window> find_window(const std::string& title) const;
63
64 /**
65 * @brief Gets window by index
66 * @return Pointer to window, or nullptr if index out of bounds
67 */
68 std::shared_ptr<Window> get_window(size_t index) const;
69
70 /**
71 * @brief Gets number of active windows
72 */
73 size_t window_count() const { return m_windows.size(); }
74
75 /**
76 * @brief Checks if any window should close
77 * @return True if at least one window has close requested
78 */
79 bool any_window_should_close() const;
80
81 /**
82 * @brief Destroys all windows that should close
83 * @return Number of windows destroyed
84 */
85 size_t destroy_closed_windows();
86
87 /**
88 * @brief Gets the global graphics configuration
89 */
90 const GraphicsSurfaceInfo& get_config() const { return m_config.surface_info; }
91
92 /**
93 * @brief Process windows for one frame
94 *
95 * This is the main per-frame operation that should be called
96 * from the application's main loop. It:
97 * 1. Polls GLFW events (triggers EventSource)
98 * 2. Cleans up closed windows
99 * 3. Optionally runs per-frame hooks
100 *
101 * @return True if processing should continue, false if all windows closed
102 */
103 bool process();
104
105 /**
106 * @brief Register a hook that runs every frame
107 * @param name Hook identifier
108 * @param hook Function to call each frame
109 */
110 void register_frame_hook(const std::string& name,
111 std::function<void()> hook);
112
113 /**
114 * @brief Unregister a previously registered frame hook
115 * @param name Hook identifier to remove
116 */
117 void unregister_frame_hook(const std::string& name);
118
119 /**
120 * @brief Get windows registered for processing
121 */
122 std::vector<std::shared_ptr<Window>> get_processing_windows() const { return m_processing_windows; }
123
124 /**
125 * @brief Set terminate flag to stop event loop thread
126 *
127 * This flag is also checked during destroy closed windows to check
128 * if backend resources should be released.
129 */
130 void set_terminate() { m_terminate.store(true); }
131
132private:
134 std::vector<std::shared_ptr<Window>> m_windows;
135 std::unordered_map<std::string, std::weak_ptr<Window>> m_window_lookup;
136
137 /**
138 * @brief Factory for creating backend-specific windows
139 */
140 std::shared_ptr<Window> create_window_internal(const WindowCreateInfo& create_info);
141
142 /** @brief Atomic flag to signal event loop thread to stop */
143 std::atomic<bool> m_should_stop { false };
144
145 /**
146 * @brief Removes window from lookup table
147 */
148 void remove_from_lookup(const std::shared_ptr<Window>& window);
149
150 /**
151 * @brief Calls all registered frame hooks
152 */
153 std::unordered_map<std::string, std::function<void()>> m_frame_hooks;
154
155 /** @brief Mutex for protecting m_windows and m_window_lookup */
156 mutable std::mutex m_hooks_mutex;
157
158 std::vector<std::shared_ptr<Window>> m_processing_windows;
159
160 std::atomic<bool> m_terminate { false };
161};
162
163} // namespace MayaFlux::Core
void set_terminate()
Set terminate flag to stop event loop thread.
std::vector< std::shared_ptr< Window > > get_processing_windows() const
Get windows registered for processing.
const GraphicsSurfaceInfo & get_config() const
Gets the global graphics configuration.
WindowManager & operator=(const WindowManager &)=delete
WindowManager(const WindowManager &)=delete
std::mutex m_hooks_mutex
Mutex for protecting m_windows and m_window_lookup.
std::vector< std::shared_ptr< Window > > m_windows
WindowManager(WindowManager &&) noexcept=delete
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
std::vector< std::shared_ptr< Window > > m_processing_windows
Manages window lifecycle and GLFW event polling.
Platform-agnostic window wrapper.
Definition Window.hpp:22
System-wide configuration for visual stream processing.
Configuration for creating a single window instance.