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

Author Topic: Time handling and FPS q's  (Read 2433 times)

0 Members and 1 Guest are viewing this topic.

Sivak

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Time handling and FPS q's
« on: March 27, 2010, 08:31:24 pm »
Hey all.  I've begun the game and it obviously goes too fast without time controls, so I've done this at the end of the loop:

Code: [Select]

bool bWait = true;
while (bWait)
{ float Time = Clock.GetElapsedTime();
if (Time >= 0.0166)
bWait = false;
}
cmnFunc.CommonFunctionClass::DisplayFPS(Clock);
Clock.Reset();
App.Display();


Basically, the 0.0166 is around 60fps, and when I do send it to the DisplayFPS function, it does indeed show 60fps in the lower right.

I'm wondering if what I'm doing is a proper way of doing it?

I did see that one can supposedly lock the FPS rate, though that seemed to really bog down the App when doing it, but maybe I'm not doing something right in the implementation...?

Any advice appreciated, thanks.   8)

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Time handling and FPS q's
« Reply #1 on: March 27, 2010, 08:45:20 pm »
Why don't you just use sf::Window::SetFramerateLimit() or enable vsync?

Sivak

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Time handling and FPS q's
« Reply #2 on: March 27, 2010, 10:45:17 pm »
I've done this and it seems to work...  save for the routine I made for displaying the actual FPS...  It greatly fluctuates from 64 to 66 when I run the app instead of staying locked at 60...

Code: [Select]

void CommonFunctionClass::DisplayFPS(sf::Clock Clock)
{   sf::Sprite GFXframeDigits10(IMG_Digits);
sf::Sprite GFXframeDigits1(IMG_Digits);
sf::Sprite GFXframePeriod(IMG_Digits);
sf::Sprite GFXframeDigits01(IMG_Digits);
float Framerate = 1.f / Clock.GetElapsedTime();
Framerate *= 10;
int frameInt = (int)Framerate;
    int ShownFPS10 = frameInt / 100;
int ShownFPS1 = frameInt % 100 / 10;
int ShownFPS01 = frameInt % 10;
ShownFPS10 *= 16;
ShownFPS1 *= 16;
ShownFPS01 *= 16;
GFXframeDigits10.SetSubRect(sf::IntRect(ShownFPS10, 0, ShownFPS10+16, 20));
GFXframeDigits10.SetPosition(568, 450);
GFXframeDigits1.SetSubRect(sf::IntRect(ShownFPS1, 0, ShownFPS1+16, 20));
GFXframeDigits1.SetPosition(584, 450);
GFXframePeriod.SetSubRect(sf::IntRect(160, 0, 176, 20));
GFXframePeriod.SetPosition(600, 450);
GFXframeDigits01.SetSubRect(sf::IntRect(ShownFPS01, 0, ShownFPS01+16, 20));
GFXframeDigits01.SetPosition(616, 450);
App.Draw(GFXframeDigits10);
App.Draw(GFXframeDigits1);
App.Draw(GFXframePeriod);
App.Draw(GFXframeDigits01);
}

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Time handling and FPS q's
« Reply #3 on: March 27, 2010, 11:10:41 pm »
Well it happens the same to me. I don't know if Laurent will be fixing this soon. His clock implementation seems to have a precision problem as far as I know. Anyway, it works well.

BTW: Why don't you use sf::Font and sf::Text to display strings?

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Time handling and FPS q's
« Reply #4 on: March 27, 2010, 11:13:03 pm »
You can't precisely lock the framerate. You can try to use the Vertical Sync, but it will be close to 60fps only on 60Hz screens, with fluctuation.

You don't have any means to strictly lock the fps to 60, at least with OpenGL. (and SFML)

Some games success to lock it, but they mainly use DirectX, not OpenGL.

EDIT : The problem isn't in the clock, but in the Sleep function, wich is used with the SetFramerateLimit function. It's more about the OS than the SFML itself. Sleep() have a precision from 1 ou 20 ms on my computer, and even giving my game process a Real Time priority, it doesn't help.

 

anything