Public Group

# Easy FPS question

This topic is 4350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is probably one of the most common and easy things to do but I just can't get it to work. I want to make the program timer/computer independent so it doesn't matter on which computer you play the game and also show the current fps. I've searched a lot and found about 27 different ways of doing this (some of them didn't make any sense at all), but none of them worked and they did result in many different ways on top of that. This is really frustruating, especially when none of the examples written in any tutorial seems to work. Is there an easy way to do this? This is about everything that I know works for now that i'm using:
int framestart = SDL_GetTicks();
deltaTime = (float)(framestart-lastTime);
lastTime = framestart;


How do I calculate the fps from here on? Some tutorials said to divide deltaTime with 1000 like this: deltaTime = (float)(framestart-lastTime)/1000.0f; and some don't. I don't know which i'm supposed to use. If I divide with 1000 the movements of the player becomes too slow and if I don't they become too fast, when I use the movement code like this:
pX += xSpeed * deltaTime;
pY += ySpeed * deltaTime;


Some people say that you should divide 1000 with deltaTime other people say that you should do this and that and someone else say something totally different and this is repeated several times on different places. In the end none of the code they pasted works in any way. Anyone who can post code that actually works and calculates the fps? this is killing me.. Appreciate any help.

##### Share on other sites
To get the fps simply take the time your frame started and subtract it from when it ended. Then you have the ms per frame. From there on in simply see how many frames it would take for those ms to add up to a second, and you're done.

For time based movement, do what you were doing, multiply the answer by 0.001 (or divide by 1000, same thing) (assuming you had milliseconds to begin with), and multiply that delta (that is now in seconds, since you divided by 1000) by your speeds. Done - time based movement.

The reason it might seem slow is that your movement is supposed to be unit per second, not unit per frame. So you gotta fix that.

##### Share on other sites
I found this at libSDL, it ought to suit your needs.

##### Share on other sites
Whether you divide by 1000 or not is up to you. The important thing is that you are consistent in all of your units. If you do divide, the delta value is in seconds. If not, it is in milliseconds. You're quite free to divide by 604800000 and maintain a delta value in weeks. I'm not sure quite what that says about your expected framerate though [rolleyes].

I find it very useful to keep track of a set of units in my game (usually SI units). That way, I can guess at approximate values for constants without any need for trial-and-error:
If all positions are stored in metres, times in seconds (so divide your GetTickCount()-derived deltas by 1000) and masses in kilogrammes then velocities will naturally be in metres/second, forces in Newtons, energies in Joules etc etc. This way, I can throw 90000 in as the momentum of my Car object, and know I'll get fairly decent results.

However, if you store distances in pixels, times in clock cycles and masses in moons, then how many compiles will it take to get the acceleration of your tennis ball object right?

Supposing that you use GetTickCount() and don't divide, the delta value will be in milliseconds per frame. So to calculate the instantaneous framerate, divide by 1000 to get seconds per frame and then take the reciprocal for frames per second. There are much better ways to get fps (without necessarily calculating it each frame) though.

Regards

##### Share on other sites
Thanks, I think I got it to work. I used the following:
// globalsdouble deltaTime=0.0, lastTime=0.0, currentFPS;int frameCounter=1;

// main codelastTime = SDL_GetTicks();

        /* This is in the game loop */        frameCounter++;        if (lastTime < SDL_GetTicks() - 1.0*1000) {            lastTime = SDL_GetTicks();            currentFPS = frameCounter;                        frameCounter=0;                     }                stringstream ss;        ss << "FPS: " << currentFPS;        DrawText(25, 85, ss.str());

I get about 64-65 FPS, does that sound correct in your option?

edit:
I don't know where I got 0.5 from but I edited it to 1.0, now the frame rate is 127-128.

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 81
• ### Forum Statistics

• Total Topics
632144
• Total Posts
3004414

×