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

Author Topic: [Help] move sf::RectangleShape sfml 2.1  (Read 1818 times)

0 Members and 1 Guest are viewing this topic.

Elekhyr

  • Guest
[Help] move sf::RectangleShape sfml 2.1
« 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 :)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: [Help] move sf::RectangleShape sfml 2.1
« Reply #1 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.

Elekhyr

  • Guest
Re: [Help] move sf::RectangleShape sfml 2.1
« Reply #2 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 ?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: [Help] move sf::RectangleShape sfml 2.1
« Reply #3 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)

Elekhyr

  • Guest
Re: [Help] move sf::RectangleShape sfml 2.1
« Reply #4 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 !

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: [Help] move sf::RectangleShape sfml 2.1
« Reply #5 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.

Elekhyr

  • Guest
Re: [Help] move sf::RectangleShape sfml 2.1
« Reply #6 on: May 16, 2014, 09:47:04 pm »
Oh okay, thanks :)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: [Help] move sf::RectangleShape sfml 2.1
« Reply #7 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).