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