Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Style question.  (Read 1130 times)

0 Members and 1 Guest are viewing this topic.

wizh

  • Newbie
  • *
  • Posts: 19
    • View Profile
Style question.
« 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;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Style question.
« Reply #1 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.
Laurent Gomila - SFML developer

wizh

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Style question.
« Reply #2 on: June 09, 2013, 12:26:39 pm »
I see, thanks.