Jump to content
  • Advertisement
Sign in to follow this  

Fix FPS problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. What I'm tryng is to set my game to specific FPS,
        static int sleep = 0; // in miliseconds
	if(CGlobalTimer::fFPS < 30.0f ) 
	else if(CGlobalTimer::fFPS > 30.0f ) 
	// value can't be negative
        if(sleep < 0) sleep = 0;


and in this example specificly to 30fps. But my log file show this: ... Sleep Time:31 FPS:21.20 Sleep Time:32 FPS:32.05 Sleep Time:31 FPS:21.37 Sleep Time:32 FPS:31.87 Sleep Time:31 FPS:21.35 Sleep Time:32 FPS:32.09 Sleep Time:31 FPS:21.31 Sleep Time:32 FPS:32.15 Sleep Time:31 FPS:21.26 Sleep Time:32 FPS:31.13 Sleep Time:31 FPS:20.34 Sleep Time:32 FPS:32.83 ... I tried to put one empty loop, but that either wont work, because for loop with 100.000 passes executes in very small time. Is there any other solution to this problem? Thanks in advance

Share this post

Link to post
Share on other sites
If you want to try to control FPS that way, take advantage of the fact that you know how many MS each frame should take. If you want 30 fps, each frame should take 33.3 ms. A slightly better solution would be

float desiredFPS = 30.0f;
int sleep = 0; // in miliseconds
if(CGlobalTimer::thisFrameTime < 1000.0f/desiredFPS )
Sleep(1000.0f/desiredFPS - CGlobalTimer::thisFrameTime);

You would call this code at the end of the frame and thisFrameTime would be the number of milliseconds it took to execute. You could also use lastFrameTime or something if you wanted.

Your code assumes that every frame is executing almost exactly the same and your trying to find that one sleep value that makes 30 fps. Your app will most certainly execute differently with each frame taking different times, however, due to cache, threading, etc., and obviously because of gamestate changing.

Note that a sleep time of 31 or 32 means that it's sleeping for most of the frame since 30 fps means each frame is taking about 33.3 ms. I'm not sure what to say about the joggling between 21 and and 32, as sleeping for 1 ms difference shouldn't make that much of a difference. My guess is there's something else in your app that is executing every other frame so that every other frame is taking a different time.

Share this post

Link to post
Share on other sites
Actually I dont know what to say. Now it works perfectly, even with old code. I tried to find what slows appliacation so much (when my FPS was joggling from 21 to 32) I disabbled almost whole code, but I didn't find nothing. Then on some later time it started to function normaly. Now my log with same code looks like:
Sleep Time:27 FPS:30.94
Sleep Time:28 FPS:30.04
Sleep Time:27 FPS:28.94
Sleep Time:28 FPS:30.33
Sleep Time:27 FPS:29.06
Sleep Time:26 FPS:29.33
Sleep Time:27 FPS:31.15
Sleep Time:28 FPS:30.26
Sleep Time:27 FPS:29.00

I don't have any idea what might be the posible problem.
Your code looks simplier than mine. I tried it, but it instead of 30 fps it gives me 26.

static float desiredFPS = 30.0f;
if(CGlobalTimer::fTimeElapsed < 1000.0f/desiredFPS )
Sleep(1000.0f/desiredFPS - CGlobalTimer::fTimeElapsed);

and my timer proc looks like:

void CGlobalTimer::Update()
__int64 cur;
fTimeElapsed = (float)((double)(cur-base)/(double)freq);
fFPS = (float)((double)freq/(double)(cur-base));
fTimeElapsed = (float)((double)(base-cur)/(double)freq);
fFPS = (float)((double)freq/(double)(base-cur));
base = cur;
fTime +=fTimeElapsed;

Why is fps 26 if desired fps is 30?
Am I missing something?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!