Skip to content

Commit

Permalink
BLUGA-GRAPHICS: Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Nov 2, 2023
1 parent 589d1b1 commit 46ebb42
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,10 @@ namespace Raylib {
{
return std::make_unique<TextImpl>(text, position, fontSize, color);
}

TextureManager::getInstance()
{
static TextureManagerImpl instance;
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,9 @@ namespace Raylib {

class TextureManager {
public:
TextureManager(const TextureManager &) = delete;
TextureManager(TextureManager &&) = delete;
void operator=(const TextureManager &) = delete;
void operator=(TextureManager &&) = delete;

static TextureManager &getInstance();
~TextureManager();
::Texture2D &getTexture(const std::string &fileName);
void unloadTextures();

private:
TextureManager() = default;
std::map<std::string, ::Texture2D> _textures;
std::mutex _mutex;
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static TextureManager _instance;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
virtual void unloadTextures() = 0;
};

class RayImage {
Expand Down
21 changes: 21 additions & 0 deletions libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
#include "Graphics.hpp"
#include <mutex>

namespace Raylib {

::Texture2D &TextureManagerImpl::getTexture(const std::string &fileName)
{
std::lock_guard<std::mutex> lock(_mutex);
auto it = _textures.find(fileName);

if (it == _textures.end()) {
_textures[fileName] = LoadTexture(fileName.c_str());
}
return _textures[fileName];
}

void TextureManagerImpl::unloadTextures()
{
std::lock_guard<std::mutex> lock(_mutex);
for (auto &it : _textures) {
UnloadTexture(it.second);
}
_textures.clear();
}

RayImageImpl::RayImageImpl(const std::string &fileName)
: _image(LoadImage(fileName.c_str()))
{
Expand Down
10 changes: 10 additions & 0 deletions libs/B-luga-graphics/src/RaylibImpl/Graphics/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ namespace Raylib {
::Image _image;
};

class TextureManagerImpl : public TextureManager {
public:
::Texture2D &getTexture(const std::string &fileName);
void unloadTextures() override;

private:
std::map<std::string, ::Texture2D> _textures;
std::mutex _mutex;
};

class SpriteImpl : public Sprite {
public:
SpriteImpl(const std::string &fileName, float width, float height, std::size_t id);
Expand Down

0 comments on commit 46ebb42

Please sign in to comment.