SFML community forums

Help => Graphics => Topic started by: wizh on June 08, 2013, 07:06:04 pm

Title: Style question.
Post by: wizh on June 08, 2013, 07:06:04 pm
I read in another thread that you should never make textures local temporary objects. So I was wondering, is this the correct way to handle drawing a sprite from another method?

#include <SFML/Graphics.hpp>

void drawSprite(sf::RenderWindow &window, sf::Texture texture) {

        sf::Sprite sprite;
        sprite.setTexture(texture);
    window.draw(sprite);
}


int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");

        sf::Texture texture;
        texture.loadFromFile("pTexture.png");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
                drawSprite(window, texture);
        window.display();
    }

    return 0;
}
Title: Re: Style question.
Post by: Laurent on June 09, 2013, 08:56:22 am
The function should take a reference, otherwise it will copy the entire texture every time it is called.
Title: Re: Style question.
Post by: wizh on June 09, 2013, 12:26:39 pm
I see, thanks.