SFML community forums

Help => Graphics => Topic started by: Elekhyr on May 16, 2014, 09:25:28 pm

Title: [Help] move sf::RectangleShape sfml 2.1
Post by: Elekhyr on May 16, 2014, 09:25:28 pm
Hi, i'm trying to move a sf::RectangleShape :
   

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
   {
      m_Rectangle.move(15 * -1, 0);
   }
   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
   {
      m_Rectangle.move(15, 0);
   }
 
     



but it's very jerky.
Plus when I press a Key there is a little latency.

Can you help me pls :)
Title: Re: [Help] move sf::RectangleShape sfml 2.1
Post by: G. on May 16, 2014, 09:27:05 pm
Did you put this code in the event loop? If yes, move it outside of the event loop.
Title: Re: [Help] move sf::RectangleShape sfml 2.1
Post by: Elekhyr on May 16, 2014, 09:30:31 pm
The problem is that my loop is :

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

                        window.clear();
                        mapManager.drawAll(window);


                        mapManager.updateAll(); // updateAll update all my object in mapManager (move, etc.)
                       
                       
                        window.display();

                }
        }
 

I can't put the code outside, no ?
Title: Re: [Help] move sf::RectangleShape sfml 2.1
Post by: G. on May 16, 2014, 09:33:12 pm
Clear draw display should also be outside of the event loop. Take a look (and read them and understand them) at the code in the tutorial : http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php (fr (http://www.sfml-dev.org/tutorials/2.1/graphics-draw-fr.php))
Title: Re: [Help] move sf::RectangleShape sfml 2.1
Post by: Elekhyr on May 16, 2014, 09:42:35 pm
You are a genius, thanks a lot ! I have read the tutorial and I think it's logical but I can't explain why (I'm a newbie in programming), if it doesn't bother you can you tell me why ?

Thanks a lot !
Title: Re: [Help] move sf::RectangleShape sfml 2.1
Post by: Ixrec on May 16, 2014, 09:44:57 pm
The biggest reason is that whenever there are no events for you to process, your clear/draw/display will never get executed.
Title: Re: [Help] move sf::RectangleShape sfml 2.1
Post by: Elekhyr on May 16, 2014, 09:47:04 pm
Oh okay, thanks :)
Title: Re: [Help] move sf::RectangleShape sfml 2.1
Post by: G. on May 16, 2014, 09:48:22 pm
You enter inside the event loop only when an event is triggered, you wouldn't want your window to display things only when an event a triggered isn't it? :p

And when you continuously move something, you want it to move every frame, not only when an event is triggered.
Putting your code inside the event loop gives the impression that it's executed every frame but it's not. It's because when you hold a key, the KeyPressed event is rapidly repeated (unless you disable key repeat).