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

Author Topic: [SFML2.0] Drawing multiple instances of one sprite  (Read 5888 times)

0 Members and 1 Guest are viewing this topic.

Roggie

  • Newbie
  • *
  • Posts: 8
    • View Profile
[SFML2.0] Drawing multiple instances of one sprite
« on: June 28, 2012, 08:39:35 am »
Hello!

I'm a beginner to SFML, and I've just learned to draw a sprite in 2.0 . Now I can move it with my arrow keys (which I'm very proud of :3), I'd like to draw another sprite. I loaded the second sprite (a square) the same way as I did with the first sprite (a circle) and set the position to 0,0. So far so good. Then, I drew it, and succeeded in doing that as well. After that, I wanted to draw multiple instances of that one sprite, but couldn't find a way to. Is the only way to do this by making hundreds of sprites (sprsquare1, sprsquare2, sprsquare45, etc.) or is there any way of drawing the same sprite on different places?

Roggie

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML2.0] Drawing multiple instances of one sprite
« Reply #1 on: June 28, 2012, 09:14:41 am »
sprite.setPosition(0, 0);
window.draw(sprite);

sprite.setPosition(100, 100);
window.draw(sprite);

...
Laurent Gomila - SFML developer

Roggie

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: [SFML2.0] Drawing multiple instances of one sprite
« Reply #2 on: June 28, 2012, 10:12:46 am »
Well, I guess I tried to do it in the wrong way then, because I did try this:
Quote
    sf::Texture squaretex;
    if (!squaretex.loadFromFile(resourcePath() + "square.gif"))
        return EXIT_FAILURE;
    sf::Sprite sprsquare;
    sprsquare.setTexture(squaretex);
    sprsquare.setColor(sf::Color(255, 255, 255, 200));
    sprsquare.setPosition(0, 0);

//loads of code here

    while (window.isOpen())
    {
       // Process events
       sf::Event event;
       while (window.pollEvent(event))
       {
          // Close window : exit
          if (event.type == sf::Event::Closed)
             window.close();
           
          // Escape pressed : exit
          if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
             window.close();
       }
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
            sprcircle.move(-1,0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            sprcircle.move(1,0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
            sprcircle.move(0,-1);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
            sprcircle.move(0,1);
        }

       // Clear screen
       window.clear();
       
       // Draw the sprite
       window.draw(sprite);
        window.draw(sprcircle);
        window.draw(sprsquare);   
        sprsquare.setPosition(32, 0);
        window.draw(sprsquare);

       // Update the window
       window.display();
    }

   return EXIT_SUCCESS;
}

Anyways, thank you!

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: [SFML2.0] Drawing multiple instances of one sprite
« Reply #3 on: June 28, 2012, 10:26:03 am »
Quote
Well, I guess I tried to do it in the wrong way then
The problem with that code is that you set the square to position (0, 0) at the beginning of your code and not right before it is drawn. So the first frame will be ok, but for the second frame it will still be on position (32, 0) were you drew your second square. Therefore you only saw one square on the screen.

So just move the "sprsquare.setPosition(0, 0)" to right before the first "window.draw(sprsquare)" and it should work.
TGUI: C++ SFML GUI

 

anything