I'm having trouble using the AssetManager class from the book SFML Essentials.
The class is a singleton meant to hold sf::Texture. I slightly modified it to store sf::Image instead.
It's causing a strange problem: The code compiles successfully in visual studio 2015 community debug mode, the console window launches, but the sfml window never does. It's stuck in the console window indefinitely, meaning I can't debug it.
Relevant code:
AssetManager.h
public:
AssetManager();
static sf::Image& get_image(std::string const& filename);
static void erase_image(std::string const& filename);
private:
std::map<std::string, sf::Image> m_Images;
static AssetManager* sInstance;
AssetManager.cpp
AssetManager* AssetManager::sInstance = nullptr;
AssetManager::AssetManager()
{
assert(sInstance == nullptr);
sInstance = this;
}
sf::Image& AssetManager::get_image(std::string const& filename)
{
auto& iMap = sInstance->m_Images;
//See if the texture is already loaded
auto pairFound = iMap.find(filename);
//If yes, return the texture
if (pairFound != iMap.end())
{
return pairFound->second;
}
else //Else, load the texture and return it
{
//Create an element in the texture map
auto& image = iMap[filename];
image.loadFromFile(filename);
return image;
}
}
void AssetManager::erase_image(std::string const& filename)
{
auto& iMap = sInstance->m_Images;
iMap.erase(filename);
}
Enemy.h
Enemy();
~Enemy();
std::map<std::string, sf::Image> Images;
Enemy.cpp
Enemy::Enemy()
{
Images["Spawn"] = AssetManager::get_image("Spawn.png");
}
Enemy::~Enemy()
{
AssetManager::erase_image("Spawn.png");
}
Here's the really strange part, broken into (steps):
(1) If I remove Enemy's destructor, it still doesn't run.
(2) If I then also remove / comment out the get_image line in Enemy constructor, then it runs but obviously I don't have the image in-game.
(3) If I THEN re-add / uncomment that same line (in the constructor), THEN IT RUNS AND I GET THE IMAGE IN-GAME
(4) If I then re-add the destructor it stops running.
(5) If I then remove the destructor again, it doesn't run! EVEN THOUGH THE EXACT SAME CODE RAN SUCCESSFULLY 2 STEPS AGO! (in step 3)
Does anybody have a clue why this is happening?
I also welcome any opinions on whether this is a good/bad resource manager and if I should switch to some other resource manager.