Skip to content

Commit

Permalink
texture_manager: use a mutex for ref counted use
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathSpirit committed Dec 23, 2024
1 parent 0ca9a95 commit 13686bc
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions sources/manager/texture_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,28 @@
namespace fge::texture
{

namespace
{

std::mutex gSDLImageHandlerMutex;
unsigned int gSDLImageHandlerCounter = 0;

} // namespace

bool TextureManager::initialize()
{
if (this->isInitialized())
{
return true;
}

IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP | IMG_INIT_JXL | IMG_INIT_AVIF);
gSDLImageHandlerMutex.lock();
if (gSDLImageHandlerCounter == 0)
{
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP | IMG_INIT_JXL | IMG_INIT_AVIF);
}
++gSDLImageHandlerCounter;
gSDLImageHandlerMutex.unlock();

fge::Surface badSurface;

Expand Down Expand Up @@ -66,7 +80,12 @@ bool TextureManager::initialize()
void TextureManager::uninitialize()
{
BaseManager::uninitialize();
IMG_Quit();
gSDLImageHandlerMutex.lock();
if (--gSDLImageHandlerCounter == 0)
{
IMG_Quit();
}
gSDLImageHandlerMutex.unlock();
}

bool TextureManager::loadFromSurface(std::string_view name, fge::Surface const& surface)
Expand Down

0 comments on commit 13686bc

Please sign in to comment.