diff --git a/libs/B-luga-graphics/include/B-luga-graphics/Raylib/Graphics/Graphics.hpp b/libs/B-luga-graphics/include/B-luga-graphics/Raylib/Graphics/Graphics.hpp index 7ad5cac..cbe1a99 100644 --- a/libs/B-luga-graphics/include/B-luga-graphics/Raylib/Graphics/Graphics.hpp +++ b/libs/B-luga-graphics/include/B-luga-graphics/Raylib/Graphics/Graphics.hpp @@ -159,6 +159,7 @@ namespace Raylib { class TextureManager { public: static TextureManager &getInstance(); + virtual void preloadTexture(const std::string &fileName) = 0; virtual void unloadTextures() = 0; }; diff --git a/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.cpp b/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.cpp index 84eaf1b..354aeb8 100644 --- a/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.cpp +++ b/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.cpp @@ -1,19 +1,28 @@ #include "Graphics.hpp" +#include "B-luga/PathResolver.hpp" #include namespace Raylib { - ::Texture2D &TextureManagerImpl::getTexture(const std::string &fileName) + ::Texture2D &TextureManagerImpl::getTexture(const std::string &path) { std::lock_guard lock(_mutex); - auto it = _textures.find(fileName); + preloadTexture(path); + auto fileName = PathResolver::resolve(path); - if (it == _textures.end()) { - _textures[fileName] = LoadTexture(fileName.c_str()); - } return _textures[fileName]; } + void preloadTexture(const std::string &path) + { + std::lock_guard lock(_mutex); + auto fileName = PathResolver::resolve(path); + + if (_textures.find(fileName) == _textures.end()) { + _textures[fileName] = LoadTexture(fileName.c_str()); + } + } + void TextureManagerImpl::unloadTextures() { std::lock_guard lock(_mutex); diff --git a/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.hpp b/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.hpp index 5bac99a..af29254 100644 --- a/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.hpp +++ b/libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.hpp @@ -51,6 +51,7 @@ namespace Raylib { static TextureManagerImpl &getInstance(); ::Texture2D &getTexture(const std::string &fileName); void unloadTextures() override; + void preloadTexture(const std::string &path) override; private: std::map _textures; diff --git a/libs/B-luga/include/B-luga/Json.hpp b/libs/B-luga/include/B-luga/Json.hpp index c8f512c..9c43e1a 100644 --- a/libs/B-luga/include/B-luga/Json.hpp +++ b/libs/B-luga/include/B-luga/Json.hpp @@ -28,8 +28,7 @@ class Json { void registerJsonFile(const std::string &path) { - const std::string path_resolved = PathResolver::resolve(path); - _jsonDatas.insert({path_resolved, loadJsonData(path_resolved)}); + getJsonData(path); } nlohmann::json @@ -50,8 +49,7 @@ class Json { template T getDataByJsonType(const std::string &dataType, const std::string &index) { - const std::string path_resolved = PathResolver::resolve(dataType); - nlohmann::json finalData(_jsonDatas[path_resolved]); + nlohmann::json finalData = getJsonData(dataType); finalData = finalData[index]; if (finalData == nullptr) { @@ -64,16 +62,14 @@ class Json { nlohmann::json getDataByJsonType(const std::string &dataType) { - const std::string path_resolved = PathResolver::resolve(dataType); - nlohmann::json data(_jsonDatas[path_resolved]); + nlohmann::json data = getJsonData(dataType); return (data); } nlohmann::basic_json<> getDataByJsonType(const std::string &dataType, const std::string &index) { - const std::string path_resolved = PathResolver::resolve(dataType); - nlohmann::basic_json<> finalData(_jsonDatas[path_resolved]); + nlohmann::basic_json<> finalData = getJsonData(dataType); finalData = finalData[index]; if (finalData == nullptr) { @@ -230,5 +226,15 @@ class Json { return jsonData; } + nlohmann::json getJsonData(const std::string &path) + { + const std::string path_resolved = PathResolver::resolve(dataType); + + if (_jsonDatas.find(path_resolved) == _jsonDatas.end()) { + _jsonDatas.insert({path_resolved, loadJsonData(path_resolved)}); + } + return _jsonDatas[path_resolved]; + } + std::unordered_map _jsonDatas; };