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

Author Topic: Why will some sprites not been drawed?  (Read 952 times)

0 Members and 1 Guest are viewing this topic.

nischu

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Why will some sprites not been drawed?
« on: April 18, 2018, 11:03:25 pm »
Here my code:
int main()
{
    sf::Texture texture;
    if (!texture.loadFromFile("/home/nico/Pictures/sheets/64x32_tictactoe.png"))
    {
        std::cerr << "Texture file couldn't loaded!\n";
        return EXIT_FAILURE;
    }
    sf::Sprite cross(texture,sf::Rect<int>(0,0,32,32));
    cross.setPosition(0.0f,0.0f);

    sf::Sprite ring(texture);
    ring.setTextureRect( sf::Rect<int>(32,0,32, 32));
    ring.setPosition(32.0f, 0.0f );

    std::array<sf::Sprite,9> sprite;
    for( auto sp : sprite)
    {
        sp = sf::Sprite(texture, sf::Rect<int>(0,0,32,32));
        sp.setPosition(100,100);
    }
    sf::RenderWindow window(sf::VideoMode(400, 400), "TicTacToe");

    while (window.isOpen())
    {
        window.clear();

        window.draw(cross);  // works
        window.draw(ring);  // works
        for (auto sp : sprite) window.draw(sp); // doesn't work

        window.display();

        sf::Event event;

        while (window.waitEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }
    }
    return EXIT_SUCCESS;
}
Any idea why the sprites of the std::array will not drawed? Thanks for help.
« Last Edit: April 18, 2018, 11:21:04 pm by nischu »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Why will some sprites not been drawed?
« Reply #1 on: April 19, 2018, 12:07:20 am »
Because you copy your sprites inside both for loops, use auto& instead.
Failing to succeed does not mean failing to progress!