SFML community forums

Help => Graphics => Topic started by: Nabeel Rehman on April 19, 2017, 08:34:19 am

Title: how to set background.
Post by: Nabeel Rehman on April 19, 2017, 08:34:19 am
Hi guys, i'm having trouble in setting up the background for my tank game, the image 'background.png' just don't show up!!!  ???
------------------------------------------------------------
// Image size is 1280x1024

        RenderWindow window(VideoMode(1024, 786, 32), "Test 32");

        Texture texture;
        texture.loadFromFile("background.png");
        Sprite sprite;
        Vector2u size = texture.getSize();
        sprite.setTexture(texture);
        sprite.setOrigin(size.x / 2, size.y / 2);

        while (window.isOpen())
        {
                Event e;
                while (window.pollEvent(e))
                {
                        if (e.type == Event::Closed)
                                window.close();
                }
                window.draw(sprite);
                window.display();
        }
Title: Re: how to set background.
Post by: sjaustirni on April 19, 2017, 02:26:59 pm
"image doesn't show up" is lacking information needed to diagnose the mistake you've made. Read this page (https://www.sfml-dev.org/tutorials/2.4/graphics-sprite.php) thoroughly, I believe it contains the solution to your problem (hint: it has something to do with working directories). If following the solution in the tutorial doesn't help, come back to the forum and explain what have you tried and what error messages you've got.

Good luck! ;)
Title: Re: how to set background.
Post by: Hapax on April 19, 2017, 08:57:11 pm
What is the returned value from texture.loadFromFile("background.png")?
Title: Re: how to set background.
Post by: chablahblah on April 23, 2017, 07:12:44 pm
It may be better to put an if statement around the load from file.

if(!texture.loadFromFile("background.png")) { // error: handle didn't load }
Title: Re: how to set background.
Post by: xYodax on July 13, 2021, 07:20:50 pm
The problem here is that while using the .setTexture() function and an sf::Texture the .setTexture() function expects to be given a pointer so:

RenderWindow window(VideoMode(1024, 786, 32), "Test 32");

        Texture texture;
        texture.loadFromFile("background.png");
        Sprite sprite;
        Vector2u size = texture.getSize();
        sprite.setTexture(&texture);  //This is where you add an & to designate texture as a pointer
        sprite.setOrigin(size.x / 2, size.y / 2);

        while (window.isOpen())
        {
                Event e;
                while (window.pollEvent(e))
                {
                        if (e.type == Event::Closed)
                                window.close();
                }
                window.draw(sprite);
                window.display();
        }
Title: Re: how to set background.
Post by: kojack on July 14, 2021, 03:52:33 am
setTexture() doesn't take a pointer. It's prototype is:
void setTexture(const Texture& texture, bool resetRect = false);
The texture parameter is a reference. Doing sprite.setTexture(&texture); would be a compile error.

Title: Re: how to set background.
Post by: Stauricus on July 14, 2021, 01:35:35 pm
The problem here is that while using the .setTexture() function and an sf::Texture the .setTexture() function expects to be given a pointer so:

after 4 years i think the poster either already solved it or gave up  ::)