Hi!
When I set the window's framerate limit to 60 I get ~35 FPS. When I set the limit to 120 I get ~65 FPS. When I don't define a limit I get over 8000 FPS.
Now, I want my game to run at 60 FPS. I can't really set the framerate limit to 60 though because then for some reason it stays at ~35 FPS.
Can someone please help me? What am I doing wrong?!
Edit: I made a quick example code (untested)
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test", sf::Style::Close);
window.setFramerateLimit(60);
sf::Font font;
if (!font.loadFromMemory(fontChar, fontSize)) {
return -1;
}
sf::Clock c;
sf::Text t = sf::Text("", font, 30);
t.setColor(sf::Color(0, 0, 0));
while (window.isOpen()) {
sf::Event evt;
while (window.pollEvent(evt)) {
switch (evt.type) {
case sf::Event::Closed:
window.close();
break;
default:
break;
}
}
window.clear(sf::Color(255, 255, 255));
float fps = 1.f / c.restart().asSeconds();
std::stringstream ss;
ss << "FPS: " << (int)fps;
t.setString(ss.str());
window.draw(t);
window.display();
}
return 0;
}
I suppose this is quite inaccurate
float fps = 1.f / c.restart().asSeconds();
You should rather try:
int fps = 1000000 / c.restart().asMicroseconds();
(untested) ;-)
I suppose this is quite inaccurate
float fps = 1.f / c.restart().asSeconds();
You should rather try:
int fps = 1000000 / c.restart().asMicroseconds();
(untested) ;-)
It yields the same results for me.
Am I calculating the FPS correctly? Maybe that's the issue here but I don't see what's wrong with my code.
What version of SFML are you using? Not too long ago we've had an improvement to the inaccuracy of sf::sleep, which is the cause of the issue here. I think it's already included in SFML 2.1, but I'm not certain - might want to look at the commit history.
Overall the framelimit with sf::sleep will always be a bit inaccurate, so if possible one should use VSync, but since that can be deactivated in the driver, one will still want to use framelimit.
Nexus implemented for Zloxx II an interesting idea, where you measure the frametime in the beginning and if it's not at the desired point, you can move the framelimit up. That way you could start with a fixed 60 limit and if the measured time is too far away from 1/60, you could move the limit up (or down) a bit to get closer to 60fps.
I suppose this is quite inaccurate
float fps = 1.f / c.restart().asSeconds();
You should rather try:
int fps = 1000000 / c.restart().asMicroseconds();
It's not really that inaccurate given that we're dealing with frametimes of 0.008 - 0.01s and don't care too much about the microseconds. And if you want to be accurate than your integer division isn't the best way to go about it either... ;)