Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
DataManager.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6Purpose:
7- DataManager is a singleton responsible for loading, caching and
8 releasing game resources (textures, sprites, animations, sounds,
9 level data, navigation/collision maps, game object data, etc.).
10- It provides simple file-based JSON save/load helpers used by
11 VideoGame/GameEntity and related systems to persist runtime data.
12- Resources are categorized by type and category so calling code can
13 list and query resources by semantic groups.
14
15Notes:
16- Resource loading functions in this initial implementation focus on
17 textures (BMP via SDL). Extend to support PNG/JPEG/OGG/etc. with
18 appropriate libraries as needed.
19- JSON serialization of complex objects is expected to be done by
20 calling code; DataManager provides file IO helpers and a directory
21 layout convention: "./Gamedata/{videogameName}/{objectName}.json".
22*/
23
24#pragma once
25
26#include <SDL3/SDL.h>
27#include <string>
28#include <unordered_map>
29#include <memory>
30#include <mutex>
31#include <vector>
32#include <map>
33
35
38
39// Cat�gories et types de ressources
41{
42 Unknown = 0,
43 Texture,
44 Sprite,
46 Sound,
47 Music,
48 FX,
49 Level,
50 Sector,
51 NavMap,
54};
55
57{
58 System = 0, // engine-level data
59 GameEntity, // data related to interactive objects
60 Level // level / map data
61};
62
63// Generic resource container
65{
68 std::string id; // logical identifier
69 std::string path; // filesystem path
70
71 // data payloads depending on the resource type
72 Sprite* sprite_texture = nullptr; // for texture/sprite resources
73 void* data = nullptr; // generic pointer for deferred objects
74
75 Resource() = default;
76 ~Resource() = default;
77};
78
80{
81public:
83 virtual ~DataManager();
84
85 // Singleton access
86 static DataManager& GetInstance();
87 static DataManager& Get() { return GetInstance(); }
88
89 void Initialize();
90 void Shutdown();
91
92 // Texture loading / retrieval / release
93 bool PreloadTexture(const std::string& id, const std::string& path, ResourceCategory category = ResourceCategory::System);
94 bool PreloadSprite(const std::string& id, const std::string& path, ResourceCategory category = ResourceCategory::GameEntity);
95 Sprite* GetTexture(const std::string& id) const;
96 Sprite* GetSprite(const std::string& id, const std::string& path, ResourceCategory category = ResourceCategory::GameEntity);
97 bool GetSprite_data(const std::string& id, const std::string& path, VisualSprite_data& outData);
98 bool GetSpriteEditor_data(const std::string& id, const std::string& path, VisualEditor_data& outData);
99 bool ReleaseResource(const std::string& id);
100
101
102 // Resource helpers
103 void UnloadAll();
104 bool HasResource(const std::string& id) const;
105
106 std::vector<std::string> ListResourcesByType(ResourceType type) const;
107 std::vector<std::string> ListResourcesByCategory(ResourceCategory category) const;
108
109 // JSON file helpers
110 // Save JSON content for an object inside the videogame folder.
111 // Path used: "./Gamedata/{videogameName}/{objectName}.json"
112 bool SaveJSONForObject(const std::string& videogameName, const std::string& objectName, const std::string& jsonContent) const;
113 bool LoadJSONForObject(const std::string& videogameName, const std::string& objectName, std::string& outJson) const;
114
115 // Generic file helpers
116 bool SaveTextFile(const std::string& filepath, const std::string& content) const;
117 bool LoadTextFile(const std::string& filepath, std::string& outContent) const;
118
119 // Ensure directory exists (creates intermediate dirs if necessary)
120 bool EnsureDirectoryExists(const std::string& dirpath) const;
121
122 // Helper to build the standard game data path
123 static std::string BuildGameDataPath(const std::string& videogameName, const std::string& objectName);
124
125 // Preload system resources from a configuration JSON file (e.g. "olympe.ini")
126 // Expected format:
127 // { "system_resources": [ { "id":"ui_icon", "path":"assets/ui/icon.bmp", "type":"texture" }, ... ] }
128 bool PreloadSystemResources(const std::string& configFilePath);
129
130 // ========================================================================
131 // PHASE 2: Batch Preloading System (for 3-Phase Level Loading)
132 // ========================================================================
133
135 {
140 std::vector<std::string> failedPaths;
141 std::map<std::string, std::string> fallbackPaths; // original -> actual
142
146
147 bool IsSuccess() const { return completelyFailed == 0; }
148 float GetSuccessRate() const
149 {
150 return totalRequested > 0 ?
151 static_cast<float>(successfullyLoaded + failedWithFallback) / totalRequested : 1.0f;
152 }
153 };
154
156 {
157 std::string sourceFile; // .tsj file path
158 std::string imageFile; // Main tileset image
159 std::vector<std::string> individualImages; // For collection tilesets
161
163 };
164
194
195 // Batch preload methods
196 PreloadStats PreloadTextures(const std::vector<std::string>& paths,
198 bool enableFallbackScan = true);
199
200 PreloadStats PreloadSprites(const std::vector<std::string>& paths,
202 bool enableFallbackScan = true);
203
204 PreloadStats PreloadAudioFiles(const std::vector<std::string>& paths,
205 bool enableFallbackScan = true);
206
207 PreloadStats PreloadTilesets(const std::vector<TilesetInfo>& tilesets,
208 bool enableFallbackScan = true);
209
210 // Fallback resource discovery
211 std::string FindResourceRecursive(const std::string& filename,
212 const std::string& rootDir = "GameData") const;
213
214private:
215 std::string name;
216 mutable std::mutex m_mutex_;
217 std::unordered_map<std::string, std::shared_ptr<Resource>> m_resources_;
219
220 // Platform-specific recursive search helpers
221#ifdef _WIN32
222 std::string FindResourceRecursive_Windows(const std::string& filename,
223 const std::string& rootDir) const;
224#else
225 std::string FindResourceRecursive_Unix(const std::string& filename,
226 const std::string& rootDir) const;
227#endif
228};
ResourceCategory
Definition DataManager.h:57
ResourceType
Definition DataManager.h:41
SDL_Texture Sprite
Definition DataManager.h:34
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static std::string BuildGameDataPath(const std::string &videogameName, const std::string &objectName)
std::string FindResourceRecursive_Unix(const std::string &filename, const std::string &rootDir) const
static DataManager & Get()
Definition DataManager.h:87
bool PreloadSystemResources(const std::string &configFilePath)
Sprite * GetSprite(const std::string &id, const std::string &path, ResourceCategory category=ResourceCategory::GameEntity)
void Initialize()
PreloadStats PreloadSprites(const std::vector< std::string > &paths, ResourceCategory category=ResourceCategory::GameEntity, bool enableFallbackScan=true)
PreloadStats PreloadTextures(const std::vector< std::string > &paths, ResourceCategory category=ResourceCategory::Level, bool enableFallbackScan=true)
virtual ~DataManager()
bool m_enableFallbackScan
Sprite * GetTexture(const std::string &id) const
bool EnsureDirectoryExists(const std::string &dirpath) const
std::string FindResourceRecursive(const std::string &filename, const std::string &rootDir="GameData") const
bool SaveTextFile(const std::string &filepath, const std::string &content) const
static DataManager & GetInstance()
std::vector< std::string > ListResourcesByType(ResourceType type) const
bool HasResource(const std::string &id) const
bool LoadJSONForObject(const std::string &videogameName, const std::string &objectName, std::string &outJson) const
std::unordered_map< std::string, std::shared_ptr< Resource > > m_resources_
std::mutex m_mutex_
bool PreloadTexture(const std::string &id, const std::string &path, ResourceCategory category=ResourceCategory::System)
PreloadStats PreloadTilesets(const std::vector< TilesetInfo > &tilesets, bool enableFallbackScan=true)
bool SaveJSONForObject(const std::string &videogameName, const std::string &objectName, const std::string &jsonContent) const
bool ReleaseResource(const std::string &id)
PreloadStats PreloadAudioFiles(const std::vector< std::string > &paths, bool enableFallbackScan=true)
bool LoadTextFile(const std::string &filepath, std::string &outContent) const
bool PreloadSprite(const std::string &id, const std::string &path, ResourceCategory category=ResourceCategory::GameEntity)
void Shutdown()
std::vector< std::string > ListResourcesByCategory(ResourceCategory category) const
std::string name
bool GetSprite_data(const std::string &id, const std::string &path, VisualSprite_data &outData)
bool GetSpriteEditor_data(const std::string &id, const std::string &path, VisualEditor_data &outData)
Definition Level.h:13
std::vector< std::string > failedPaths
std::map< std::string, std::string > fallbackPaths
float GetSuccessRate() const
std::vector< std::string > individualImages
ResourceCategory category
Definition DataManager.h:67
ResourceType type
Definition DataManager.h:66
std::string id
Definition DataManager.h:68
~Resource()=default
std::string path
Definition DataManager.h:69
Sprite * sprite_texture
Definition DataManager.h:72
Resource()=default
void * data
Definition DataManager.h:73