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

Author Topic: Clicking app titlebar makes objects skip  (Read 2230 times)

0 Members and 2 Guests are viewing this topic.

Atomsk

  • Newbie
  • *
  • Posts: 2
    • View Profile
Clicking app titlebar makes objects skip
« on: March 18, 2011, 03:38:01 pm »
I a simple program that just make a 32x32 ball bounce around the screen and when I click and hold the the application's titlebar the ball appears to be still, but when I release the ball seems to go past the set boundaries.

Code: [Select]

#include <SFML/Graphics.hpp>

int vx = 500;
int vy = 150;
const int windowWidth = 800;
const int windowHeight = 600;
const int ballSize = 32;


int main()
{
    sf::RenderWindow App(sf::VideoMode(800,600,32), "Physics Simulation");

    App.SetFramerateLimit(60);
    const sf::Input& Input = App.GetInput();

    sf::Image ballImg;

    if(!ballImg.LoadFromFile("Ball.png"))
        return EXIT_FAILURE;

    sf::Sprite Ball(ballImg);
    Ball.SetPosition(400,600-32);

    ///***
    while(App.IsOpened())
    {
      if(Ball.GetPosition().y > windowHeight-ballSize)
         vy *= -1;
if(Ball.GetPosition().y < 0)
vy *= -1;
if(Ball.GetPosition().x > windowWidth-ballSize)
vx *= -1;
if(Ball.GetPosition().x < 0)
vx *= -1;

Ball.Move(vx*App.GetFrameTime(), vy*App.GetFrameTime());

        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)
                App.Close();
        }

        App.Clear(sf::Color(100, 149, 237));
        App.Draw(Ball);
        App.Display();

    }
    return EXIT_SUCCESS;
}
[/code]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Clicking app titlebar makes objects skip
« Reply #1 on: March 18, 2011, 03:47:45 pm »
That's because your code is paused while the titlebar is clicked, and when it's released your code is resumed with a very high app.GetFrameTime() value. An easy workaround is to ignore the frame time when it's greater than, let's say, 200 ms.

But your algorithm is not robust anyway, when the ball bounces you must adjust its position as well, because if it's too far it will never be able to come back into the screen.
Laurent Gomila - SFML developer

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Clicking app titlebar makes objects skip
« Reply #2 on: March 18, 2011, 03:59:51 pm »
Ahhh. I was having the same thing happen but was holding off until I had some more stuff to ask about.

Thanks Atomsk & Laurent.  :D
{much better code}

Atomsk

  • Newbie
  • *
  • Posts: 2
    • View Profile
Clicking app titlebar makes objects skip
« Reply #3 on: March 18, 2011, 09:14:05 pm »
Oh, I can't believe I didn't think of that, thanks.

 

anything