I am trying to load texture using SFML and return sprite but textures won’t load with my path. Full error says: libc++abi: terminating with uncaught exception of type std::runtime_error: Could not find texture with path: /Users/username/Library/Developer/Xcode/DerivedData/Test-eqrudqhtua/Build/Products/Debug/Test.app/Contents/Resources/tile.png
So I’ve declared in my SpriteFactory.hpp
class SpriteFactory
{
//function when I load sprite for the first time
unsigned int LoadTexture(const std::string& texturePath);
//function to create a sprite
sf::Sprite GetSprite(unsigned int hash);
private:
//I’m using map to store textures
std::map<unsigned int, sf::Texture> textures;
};
Here the implementation in SpriteFactory.cpp
unsigned int SpriteFactory::LoadTexture(const std::string& texturePath)
{
//load texture if it's not loaded and return sprite
unsigned int hash = std::hash<std::string>{}(texturePath);
//if the hash of our texturePath exist in the textures
if (textures.find(hash) == textures.end())
{
// if it doesn't exist and reaches the end before texture
if (!textures[hash].loadFromFile(texturePath));
{
throw std::runtime_error("Could not find texture with path: " + texturePath);
}
}
return hash;
}
sf::Sprite SpriteFactory::GetSprite(unsigned int hash)
{
if (textures.find(hash) == textures.end())
{
throw std::runtime_error("Could not find texture with hash: " + hash);
}
return sf::Sprite(textures[hash]);
}
When I put code part above in a comments just black screen appears.
In my main.cpp
I load initial sprite textures
unsigned int tile = factory.LoadTexture(resourcePath() + "tile.png");
unsigned int tile_hl = factory.LoadTexture(resourcePath() + "tile.png");
Then I create a sprite sheet
Tile tilePart(factory.GetSprite(tile), factory.GetSprite(tile_hl),
sf::Vector2f(100.f, 100.f));
//draw it on screen
tilePart.Draw(window);
window.display();
My Tile.cpp
part implementation is here
Tile::Tile(sf::Sprite tile, sf::Sprite tile_hl, sf::Vector2f pos)
{
this->tile = tile;
this->tile_hl = tile_hl;
isHighlighted = (int)(pos.x /32) % 2 == 0;
//set position of a sprite
tile.setPosition(pos);
//reference points to address, tile I'm gonna to render
activeTile = isHighlighted ? &this->tile_hl : &this->tile;
}
Tile::Tile(const Tile& other)
{
tile = other.tile;
tile_hl = other.tile_hl;
isHighlighted = other.isHighlighted;
activeTile = isHighlighted ? &tile_hl : &tile;
}
void Tile::Draw(sf::RenderWindow& window)
{
if (isHighlighted)
{
activeTile = &this->tile_hl;
}
window.draw(*activeTile);
activeTile = &this->tile;
}
//whether we check highlights it will be set here
void Tile::SetHighlighted(bool flag)
{
// isHighlighted = flag;
}
Could you please help to find out which part I've missed. Path is correct, I’ve tried many different ways: absolute path, I‘ve checked my image and etc.
Aucun commentaire:
Enregistrer un commentaire