Sir
Sir

Reputation: 8277

C++ Sprite not displaying - no errors either

I'm a bit lost to why my sprite won't display. I get no errors but the image doesn't display on the rendered window.

I load it like this:

main.cpp

//before main loop
sf::Sprite background = loadBG(theme["Background"]);
//in main loop
window.draw(background);

My function and its header is:

header

sf::Sprite loadBG(std::string);

cpp

sf::Sprite loadBG(std::string img){

sf::Texture texture;   
if (!texture.loadFromFile(img)){
   exit(1);
}

sf::Sprite sprite(texture); 
    return (sprite);
}

I have tested the value of theme["Background"] and it equals test.jpg

Am i missing something to get it to display ?

Upvotes: 2

Views: 1986

Answers (1)

Rapptz
Rapptz

Reputation: 21317

Your sf::Texture is out of scope.

Try the following code instead (no error checking):

void loadBG(sf::Texture& texture, sf::Sprite& sprite, const std::string& img) {
    if(texture.loadFromFile(img))
        sprite.setTexture(texture);
}

Upvotes: 3

Related Questions