Skip to content

Commit

Permalink
BLUGA: Remove registerJson + preload texture
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Nov 4, 2023
1 parent 0b0487e commit 3a1595e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ namespace Raylib {
class TextureManager {
public:
static TextureManager &getInstance();
virtual void preloadTexture(const std::string &fileName) = 0;
virtual void unloadTextures() = 0;
};

Expand Down
19 changes: 14 additions & 5 deletions libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
#include "Graphics.hpp"
#include "B-luga/PathResolver.hpp"
#include <mutex>

namespace Raylib {

::Texture2D &TextureManagerImpl::getTexture(const std::string &fileName)
::Texture2D &TextureManagerImpl::getTexture(const std::string &path)
{
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> lock(_mutex);
Expand Down
1 change: 1 addition & 0 deletions libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, ::Texture2D> _textures;
Expand Down
22 changes: 14 additions & 8 deletions libs/B-luga/include/B-luga/Json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,8 +49,7 @@ class Json {
template <typename T>
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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<std::string, nlohmann::json> _jsonDatas;
};

0 comments on commit 3a1595e

Please sign in to comment.