MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GlfwSingleton.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef GLFW_BACKEND
6
7namespace MayaFlux::Core {
8
9struct GlfwPreInitConfig;
10
11/**
12 * @class GLFWSingleton
13 * @brief Singleton utility for managing global GLFW initialization and termination
14 *
15 * GLFWSingleton ensures that the GLFW library is initialized exactly once per process,
16 * and is properly terminated when no more windows are in use. This prevents redundant
17 * initialization and resource leaks, and provides a safe, centralized mechanism for
18 * managing the GLFW global state.
19 *
20 * Usage:
21 * - Call GLFWSingleton::initialize() before creating any GLFW windows or contexts.
22 * - Use mark_window_created() and mark_window_destroyed() to track window lifetimes.
23 * - Call GLFWSingleton::terminate() when all windows are destroyed to clean up resources.
24 *
25 * This class is not intended to be instantiated; all methods and state are static.
26 */
27class GLFWSingleton {
28public:
29 /**
30 * @brief Initializes the GLFW library if not already initialized
31 * @return True if initialization succeeded or was already done, false on failure
32 *
33 * Sets up the GLFW error callback and calls glfwInit() if needed.
34 * Safe to call multiple times; initialization occurs only once.
35 */
36 static bool initialize();
37
38 /**
39 * @brief Terminates the GLFW library if initialized and no windows remain
40 *
41 * Calls glfwTerminate() only if GLFW was previously initialized and all
42 * tracked windows have been destroyed. Resets the initialization state.
43 */
44 static void terminate();
45
46 /**
47 * @brief Configures GLFW with pre-initialization hints
48 * @param config Configuration parameters to set before initialization
49 *
50 * Must be called before initialize() to take effect. Sets various GLFW
51 * windowing and context hints based on the provided configuration.
52 * If called after initialization, the hints will be ignored.
53 */
54 static void configure(const GlfwPreInitConfig& config);
55
56 /**
57 * @brief Increments the count of active GLFW windows
58 *
59 * Should be called whenever a new GLFW window is created.
60 */
61 static void mark_window_created() { s_window_count++; }
62
63 /**
64 * @brief Decrements the count of active GLFW windows
65 *
66 * Should be called whenever a GLFW window is destroyed.
67 * Ensures the window count does not go below zero.
68 */
69 static void mark_window_destroyed()
70 {
71 if (s_window_count > 0)
72 s_window_count--;
73 }
74
75 /**
76 * @brief Enumerates all connected monitors and their information
77 * @return A vector of MonitorInfo structs for each connected monitor
78 *
79 * Queries GLFW for the list of connected monitors and retrieves their
80 * properties such as name, dimensions, and current video mode.
81 */
82 static std::vector<MonitorInfo> enumerate_monitors();
83
84 /**
85 * @brief Retrieves information about the primary monitor
86 * @return MonitorInfo struct for the primary monitor
87 *
88 * Queries GLFW for the primary monitor and retrieves its properties.
89 */
90 static MonitorInfo get_primary_monitor();
91
92 /**
93 * @brief Retrieves information about a specific monitor by ID
94 * @param id The ID of the monitor to query
95 * @return MonitorInfo struct for the specified monitor, or an empty struct if not found
96 *
97 * Queries GLFW for the list of monitors and returns the one matching the given ID.
98 */
99 static MonitorInfo get_monitor(int32_t id);
100
101 /**
102 * @brief Sets a custom error callback for GLFW errors
103 * @param callback A function to be called on GLFW errors, receiving an error code and description
104 *
105 * Overrides the default GLFW error callback with the provided function.
106 * The callback will be invoked whenever a GLFW error occurs.
107 */
108 static void set_error_callback(std::function<void(int, const char*)> callback);
109
110 /**
111 * @brief Checks if GLFW has been initialized
112 * @return True if initialized, false otherwise
113 */
114 static bool is_initialized() { return s_initialized; }
115
116 /**
117 * @brief Gets the current count of active GLFW windows
118 * @return The number of currently active GLFW windows
119 */
120 static uint32_t get_window_count() { return s_window_count; }
121
122 /**
123 * @brief Gets the current GLFW platform (Wayland, X11, etc.)
124 * @return Platform identifier, or empty string if GLFW < 3.4
125 */
126 static std::string get_platform();
127
128 /**
129 * @brief Retrieves the list of required Vulkan instance extensions for GLFW
130 * @return A vector of extension names required by GLFW for Vulkan surface creation
131 *
132 * Calls glfwGetRequiredInstanceExtensions() to get the list of Vulkan
133 * instance extensions that must be enabled to create surfaces with GLFW.
134 */
135 static std::vector<const char*> get_required_instance_extensions();
136
137private:
138 /**
139 * @brief Tracks whether GLFW has been initialized
140 */
141 static bool s_initialized;
142
143 /**
144 * @brief Number of currently active GLFW windows
145 */
146 static uint32_t s_window_count;
147
148 /**
149 * @brief Internal GLFW error callback that forwards to the user-defined callback if set
150 * @param error The GLFW error code
151 * @param description A human-readable description of the error
152 */
153 static std::function<void(int, const char*)> s_error_callback;
154
155 static bool s_configured;
156
157 static GlfwPreInitConfig s_preinit_config;
158};
159
160}
161
162#endif // GLFW_BACKEND
void initialize()
Definition main.cpp:11
bool is_initialized()
Checks if the default engine has been initialized.
Definition Core.cpp:52