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

Author Topic: Problem with sf::Clock  (Read 1582 times)

0 Members and 1 Guest are viewing this topic.

vietanh8286

  • Newbie
  • *
  • Posts: 9
    • Yahoo Instant Messenger - jaguar8286
    • View Profile
    • Email
Problem with sf::Clock
« on: August 03, 2012, 03:49:21 am »
Hi there,
I have a problem with below code:

       
         int Fps::getFps(sf::Time elapsedTime)
        {
                static int frameCounter = 0;
                static float frameTime = 0.0f;
                static int fps = 0;
                frameCounter++;
                frameTime += elapsedTime.asSeconds();
                if(frameTime >= 1.0f)
                {
                        fps = frameCounter;
                        frameCounter = 0;
                        frameTime -= 1.0f;
                }
                return fps;
        }
 

       
        void Game::processRenderThread()
        {
                if(renderWindow)
                {
                        renderWindow->setActive(true);
                        sf::Clock clock;
                        while(renderWindow->isOpen())
                        {
                                sf::Time elapsedTime = clock.getElapsedTime();
                                //clear
                                //draw something
                                //update and draw FPS
                                //display
                                sf::sleep(sf::milliseconds(1));
                                clock.restart();
                        }
                }
        }
 
If i use this code, the FPS is always equal 0

But, if i change:
                               
   sf::Time elapsedTime = clock.restart();
   //clear
   //draw something
   //update and draw FPS
   //display
   sf::sleep(sf::milliseconds(1));
 
It works!!!
Why???
« Last Edit: August 03, 2012, 03:57:39 am by vietanh8286 »

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Problem with sf::Clock
« Reply #1 on: August 03, 2012, 04:11:42 am »
Why would you wait for everything to be drawn and displayed before you reset the clock?
You're not doing anything else before getting the time or after resetting the clock, but in between. So this is what happens:

  • getElapsedTime()
  • drawing & displaying
  • clock.reset()
  • nothing else
  • getElapsedTime()
  • ...

You're practically restarting right before getting the time again, and therefore it will take quite a while for frameTime to become >= 1 seconds (if ever, not sure about the details).

But this isn't really relevant since your last snippet is how you should actually do it.
sf::Time elapsedTime = clock.restart();
« Last Edit: August 03, 2012, 02:38:41 pm by Perde »

lrx

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Problem with sf::Clock
« Reply #2 on: August 03, 2012, 06:31:42 am »
Why would you wait till everything is drawn and displayed before you reset the clock? If you're not doing much in the rest of the program, could it possibly be that this part consumes most of the time?

That could mean that something like this happens:
getElapsedTime()
drawing & displaying
clock.reset()
insignificant other stuff
getElapsedTime()
...

Now given that you're not doing much else you'd be practically resetting "right before" getting the time again, and therefore it will take a while for frameTime to become >= 1 seconds.

Does that even make sense? It's four in the morning right now.  ;D

I second this! This is exactly the same conclusion I just got to independently testing something else:) Well in my case I got nearly infinite(out of scale) fps, but that's just difference in way we calculate it
Just reset clock just after you've read time and it should work
« Last Edit: August 03, 2012, 06:34:08 am by lrx »

 

anything