data_s - 1Fr3aK2/Cub3d GitHub Wiki
๐ data_s
Provides access to a single, persistent instance of the t_data structure (singleton pattern).
โ๏ธ Parameters
| Parameter | Type | Description |
|---|---|---|
| (none) | โ | This function does not take any parameters. |
๐ Returns
| Return value | Description |
|---|---|
t_data* |
Returns a pointer to a static t_data instance that persists for the programโs lifetime. |
๐ Description
The data_s function implements a singleton pattern for the t_data structure.
It defines a static variable of type t_data, ensuring that only one instance of this structure exists throughout the program.
Each call to data_s() returns a pointer to this same static instance, allowing global access to shared data without requiring external global variables.
This approach is particularly useful for managing global state in C applications, such as game engines or renderers, where multiple components need to access shared configuration or runtime data.
- The static variable is initialized only once, the first time the function is called.
- Subsequent calls return the same address, maintaining data consistency across the application.
๐ก Example Usage
t_data *data = data_s(); // Get pointer to the singleton data instance
data->width = 1920;
data->height = 1080;
t_data *same_data = data_s(); // Returns the same instance
printf("Resolution: %dx%d\n", same_data->width, same_data->height);