Skip to main content
GameDev.net gamedev.net
🔒 Locked

Timing Code Frustrations

Started by knine Dec 21, 2002 at 6:18 PM 1 replies 750+ views
Original Post
knine
knine
I''m having a devil of a time with my timing code. I thought it was going to be easy enough to get time based movement. But I can''t even get the fps to display correctly. here is my timing code.
    float fTimeElapsed = timeGetTime() - gfLastTime;
    gfLastTime = timeGetTime();


    float fSecsPerFrame = (float)(fTimeElapsed/1000.0);
    float fFPS = 1.0/fSecsPerFrame;
Am I missing something? And for the movement it should be
    x += fSecsPerFrame * xvel;
right? with xvel being the amount to move over 1 second. It ends up that I have to set that value at around 80 or it just seems too slow. Thanks in advance for your help! knine
randomZ
randomZ
Oh my god, it''s hungarian notation! *HAACHOO*
Sorry, but I''m an allergic person

Back to the subject:
I''m not sure, but you could try

  
float fSecsPerFrame = fTimeElapsed / (float)1000.0;

float fFPS = (float)1.0 / fSecsPerFrame;



"Reality is merely an illusion, albeit a very persistent one." (Albert Einstein)
My Homepage
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
MAD_Mask
MAD_Mask
Hey , I agree with the first part of your code (the first 2 lines ) . But after that I realy do not know if you do things correctly and since you say that it do not shows the FPS corectly then.... it seams that you don''t Anyway this is how I get a FPS count and it seams to work .


void ShowFrameRate()
{
static DWORD timeTotal = 0;
static DWORD FrameCounter = 0;
static CHAR str[100];
LONG FPS = 0;

timeTotal += Time->TimeElapsed;
FrameCounter++;
if (timeTotal > 1000)
{
FPS = (LONG)( (float)FrameCounter/((float)timeTotal / 1000.0) );
sprintf(str,"Frame Rate: %d fps",FPS);
timeTotal = 0;
FrameCounter = 0;
}
// you cand replace this line with the function that you use
// to display things on the screen.
DrawString(pScreen, font , 400 , 20 , str );
you ca

}



As for time based movement well things are the same:
after you calculate the " TimeElapsed " you just do something like this :
   x = x + XVel * TimeElapsed;   


just like you''ve said. But you have to be carful about unit of mesurment so if your TimeElapsed is mesured in miliseconds then your XVel must be "adjusted" to be pixel/miliseconds NOT pixel/second.

hoppe it helps.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.