SFML community forums
Help => System => Topic started by: pighead10 on April 24, 2012, 08:24:00 pm
-
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:
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?
-
Hi,
You can fix it by using the last version of SFML 2.0RC
-
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?
-
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.
-
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
-
After upgrading (I decided to after it still wasn't working), this is my code:
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.
-
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.
-
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.
-
I mean that when you call sf::Sleep(1) your app might end up sleeping 15 ms for example.
-
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... :'(