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

Author Topic: SFML 2 clock returning incorrectly when run in certain situations  (Read 5659 times)

0 Members and 1 Guest are viewing this topic.

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
I'm using SFML 2.0, but NOT the newest version of it. GetFrameTime() exists and GetElapsedTime() returns an sf::Uint32.

Here is the code:

Code: [Select]
void AppStateManager::start(AppState* state){
changeAppState(state);
double frameTime = 1;
int startTime = 0;
while(!m_bShutdown){
sf::Clock clock;
if(!SfmlFramework::Singleton()->window->IsOpened())m_bShutdown = true;
SfmlFramework::Singleton()->window->Clear();


//capture input and handle application-wide events
sf::Event evt;
while(SfmlFramework::Singleton()->window->PollEvent(evt)){
switch(evt.Type){
case sf::Event::KeyPressed:
m_ActiveStateStack.back()->keyPressed(evt.Key.Code);
break;
case sf::Event::KeyReleased:
m_ActiveStateStack.back()->keyReleased(evt.Key.Code);
break;
case sf::Event::MouseMoved:
m_ActiveStateStack.back()->mouseMoved(sf::Vector2f(evt.MouseMove.X,evt.MouseMove.Y));
break;
case sf::Event::MouseButtonPressed:
m_ActiveStateStack.back()->mousePressed(evt.MouseButton.Button);
break;
case sf::Event::MouseButtonReleased:
m_ActiveStateStack.back()->mouseReleased(evt.MouseButton.Button);
break;
case sf::Event::Closed:
m_bShutdown = true;
break;
default:
break;
}
}
if(!m_bShutdown)
m_ActiveStateStack.back()->update(frameTime);

frameTime = clock.GetElapsedTime();
clock.Reset();
std::cout << frameTime << std::endl;

SfmlFramework::Singleton()->window->Display();
}

SfmlFramework::Singleton()->window->Close();

}

When run in visual c++ 2010, it outputs correctly - 2 2 2 2 2 2 2 2 2. When run on SOME computers standalone, it also runs correctly. However, on some other computers - mine included - when run standalone it outputs 0 for a while then outputs something close to 15. This is obviously causing a massive problem with my game as a lot of people are experiencing an issue where the movement is incorrect.

How can I fix this?
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #1 on: April 24, 2012, 10:03:13 pm »
Hi,

You can fix it by using the last version of SFML 2.0RC

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #2 on: April 24, 2012, 10:17:02 pm »
Would that mean I have to change a lot of my code? The documentation has differences like capitalization and I have a decent amount of code in this project. Is this a known bug?
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #3 on: April 25, 2012, 08:10:21 am »
It's not a known bug, but timing functions were improved in the latest versions of SFML 2.

But don't expect frame times to be precise at such a high frequency, keep in mind that the OS and its timing functions have a limited resolution and precision (usually, you shouldn't rely on times < 15 ms). Does your game really need 500 updates per second? Limiting the frame rate to a decent amount (using vertical sync to avoid rendering artifacts) would probably be the best solution.
Laurent Gomila - SFML developer

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #4 on: April 25, 2012, 12:03:33 pm »
That hadn't occurred to me - I've never made anything that runs at more than 200 fps before, except this game has 2 bit graphics >.>

Thank you
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #5 on: April 25, 2012, 07:05:00 pm »
After upgrading (I decided to after it still wasn't working), this is my code:

Code: [Select]
void AppStateManager::start(AppState* state){
changeAppState(state);
double frameTime = 1;
int startTime = 0;
SfmlFramework::Singleton()->window->setFramerateLimit(200);
while(!m_bShutdown){
sf::Clock clock;
if(!SfmlFramework::Singleton()->window->isOpen())m_bShutdown = true;
SfmlFramework::Singleton()->window->clear();


//capture input and handle application-wide events
sf::Event evt;
while(SfmlFramework::Singleton()->window->pollEvent(evt)){
switch(evt.type){
case sf::Event::KeyPressed:
m_ActiveStateStack.back()->keyPressed(evt.key.code);
break;
case sf::Event::KeyReleased:
m_ActiveStateStack.back()->keyReleased(evt.key.code);
break;
case sf::Event::MouseMoved:
m_ActiveStateStack.back()->mouseMoved(sf::Vector2f(evt.mouseMove.x,evt.mouseMove.y));
break;
case sf::Event::MouseButtonPressed:
m_ActiveStateStack.back()->mousePressed(evt.mouseButton.button);
break;
case sf::Event::MouseButtonReleased:
m_ActiveStateStack.back()->mouseReleased(evt.mouseButton.button);
break;
case sf::Event::Closed:
m_bShutdown = true;
break;
default:
break;
}
}
if(!m_bShutdown)
m_ActiveStateStack.back()->update(frameTime);

frameTime = clock.restart().asMilliseconds();
std::cout << frameTime << std::endl;

SfmlFramework::Singleton()->window->display();
}

SfmlFramework::Singleton()->window->close();

}

In release, it runs at 240 fps and prints 1 a lot. However, when standalone, it runs at the same fps and prints "0" over and over and the occasional "4". Have I done somethnig to cause this?... (presumably this is the same problem as before, so on some computers it would work standalone). Also, the rest of the program that is based on frameTime like movement is running slower even in release mode.
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #6 on: April 25, 2012, 07:19:14 pm »
200 FPS is still a lot.

What's your OS?

Don't forget that setFramerateLimit uses sf::sleep internally, and that the resolution of the latter might not be low enough for 200 FPS, depending on your OS.
« Last Edit: April 25, 2012, 07:21:26 pm by Laurent »
Laurent Gomila - SFML developer

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #7 on: April 25, 2012, 07:52:23 pm »
200 FPS is still a lot.

What's your OS?

Don't forget that setFramerateLimit uses sf::sleep internally, and that the resolution of the latter might not be low enough for 200 FPS, depending on your OS.

I have windows 7. I'm not sure what you mean by your second sentence, though.
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #8 on: April 25, 2012, 08:10:06 pm »
I mean that when you call sf::Sleep(1) your app might end up sleeping 15 ms for example.
Laurent Gomila - SFML developer

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: SFML 2 clock returning incorrectly when run in certain situations
« Reply #9 on: April 25, 2012, 08:48:41 pm »
I tried limiting the FPS to 60, and everything that was related to frame time slowed down hugely. I'm assuming I have screwed it up somehow and it is not SFML's fault...  :'(
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk