Hey everyone
I have a problem trying to get the code from chapter 2 in the sfml game development book to work.
The problem is the whitebox issue, I understand what causes the whitebox (a loss of the texture) but i simply cant figure out why im losing my texture?
Here is my ResourceHolder
#pragma once
#include <map>
#include <memory>
#include <cassert>
template<typename Resource, typename Identifier>
class ResourceHolder
{
public:
void load(Identifier id, const std::string& filename)
{
std::unique_ptr<Resource> resource(new Resource());
if (!resource->loadFromFile(filename))
throw std::runtime_error("ResourceHolder::load - failed to load " + filename);
auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource)));
assert(inserted.second);
}
Resource get(Identifier id)
{
auto found = mResourceMap.find(id);
assert(found != mResourceMap.end());
return *found->second;
}
const Resource& get(Identifier id) const
{
auto found = mResourceMap.find(id);
assert(found != mResourceMap.end());
return *found->second;
}
private:
std::map<Identifier, std::unique_ptr<Resource>> mResourceMap;
};
And this is a snippet from Game.cpp, where i use the Resource holder:
Game::Game() : mTextures() ,mPlayer() ,mWindow(sf::VideoMode(640, 480), "SFML Application")
{
try
{
mTextures.load(Textures::Airplane, "Eagle.png");
}
catch (std::runtime_error& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
mPlayer.setTexture(mTextures.get(Textures::Airplane));
mPlayer.setPosition(200.f, 200.f);
}
for reference here is Game.hpp
#include <SFML/Graphics.hpp>
#include "ResourceHolder.hpp"
namespace Textures
{
enum ID
{
Landscape,
Airplane,
Missile
};
}
class Game
{
public:
Game();
void run();
private:
void processEvents();
void update(sf::Time deltaTime);
void render();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
public:
sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
private:
ResourceHolder<sf::Texture, Textures::ID> mTextures;
sf::Texture mTexture;
sf::Sprite mPlayer;
sf::RenderWindow mWindow;
bool mIsMovingUp = false;
bool mIsMovingDown = false;
bool mIsMovingLeft = false;
bool mIsMovingRight = false;
};
Hope one of you guys can help me out, because im really stuck