Ich arbeite mich gerade in die SFML ein und habe das Problem, das mein Sprite weiß bleibt. Ich lade die Textur über eine RessourceHolder-Klasse die es ermöglichen soll, allerlei Typen zu laden (Texture, Sprite, Font etc.)
Lade ich diese direkt auf mein Sprite funktioniert es, über die Holder-Klasse bleibt das Spieler-Sprite aber weiß.
Ich denke, dass es irgendwo an der Übergabe scheitert, denn die Texturen werden eigentlich korrekt geladen.
Die Implementation sieht so aus (nur relevanten Code kopiert)
Game.h
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
namespace Textures
{
enum ID { Landscape, Airplane, Missile };
}
class Game
{
public:
Game();
void run();
private:
void processEvents();
void update(sf::Time deltaTime);
void render();
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
void updateStatistics(sf::Time elapsedTime);
sf::RenderWindow mWindow;
bool mIsMovingUp;
bool mIsMovingDown;
bool mIsMovingLeft;
bool mIsMovingRight;
static const float PlayerSpeed;
static const sf::Time TimePerFrame;
sf::Texture mTexture;
sf::Sprite mPlayer;
sf::Text mStatisticsText;
sf::Time mStatisticsUpdateTime;
sf::Font mFont;
std::size_t mStatisticsNumFrames;
}
|
RessourceHolder.h
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
template <typename Ressource, typename Identifier>
class RessourceHolder
{
public:
void load(Identifier id, const std::string& filename);
template <typename Parameter>
void load(Identifier id, const std::string& filename, const Parameter& secondParam);
Ressource& get(Identifier id);
const Ressource& get(Identifier id) const;
private:
std::map<Identifier, std::unique_ptr<Ressource>> mRessourceMap;
};
|
Implementierung von load()
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
template <typename Ressource, typename Identifier>
void RessourceHolder<Ressource, Identifier>::load(Identifier id, const std::string& filename)
{
std::unique_ptr<Ressource> ressource(new Ressource());
if (!ressource->loadFromFile(filename))
throw std::runtime_error("RessourceHolder::load - Failed to load" + filename);
//Nachfolgendes gibt die passende Consolenausgabe "xy loaded" aus
if (ressource->loadFromFile(filename))
{
std::cout << filename << " loaded" << std::endl;
}
auto inserted = mRessourceMap.insert(std::make_pair(id, std::move(ressource)));
assert(inserted.second);
}
|
Implementierung von get()
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
|
template <typename Ressource, typename Identifier>
Ressource& RessourceHolder<Ressource, Identifier>::get(Identifier id)
{
auto found = mRessourceMap.find(id);
assert(found != mRessourceMap.end());
return *found->second;
}
|
Und zu guter letzt die Game.cpp ( Game::Game() )
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
|
{
mTexture.loadFromFile("Media/Textures/Eagle.png");
mPlayer.setTexture(mTexture); // So funktioniert es
RessourceHolder<sf::Texture, Textures::ID> textures;
textures.load(Textures::Airplane, "Media/Textures/Eagle.png");
mPlayer.setTexture(textures.get(Textures::Airplane)); // so funktionert es nicht!
}
|
Eigentlich liefert ja textures.get (letzte Zeile) korrekt eine Texture zurück, womit ich mir erwartet hätte, dass ich die dann mPlayer zuweisen kann. Aber irgendwie kommt da nur weiß an.
Jemand eine Idee?