Jump to content
  • Advertisement
Sign in to follow this  
ekba89

Game Loop

This topic is 2568 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

I've a simple gameloop. My problem is I can't get my FPS higher than 60.


elapsedTime = 0;
lastTime = 0;
while(gameWindow->CheckMessages())
{
lastTime = timeGetTime();

fpsCounter->Update((float)elapsedTime);

Update(elapsedTime);
Draw();

elapsedTime = timeGetTime() - lastTime;

if(elapsedTime < MILLISECONDPERFRAME)
{
sleepTime = MILLISECONDPERFRAME - elapsedTime;
elapsedTime += sleepTime;
Sleep((DWORD)sleepTime);
}
}


I tried to remove sleep part of the loop but still i get 60 fps. Then i tried to remove some part of the draw code to make it faster but still i can't get higher than 60 fps. Is it because of timeGetTime() resolution or i did something wrong? By the way my MILLISECONDPERFRAME variable is just 1000.0f/60.0f. Also i tried to change 60.0f here to a higher value but still i get 60fps.And lastly i tired to lower the fps and it works.

Share this post


Link to post
Share on other sites
Advertisement
you have VSync on. You set it up (apparently inadvertently) so that your backbuffer being written to the display is synchronized to your monitor's refresh rate. You can read about VSync on the internet.


I recommend decoupling your game logic update from your graphics presentation. That will let your logic update as often as it can, while allowing you to keep VSync on.

Share this post


Link to post
Share on other sites
Sleeping in a game loop is never a good idea by the way - check out the documentation for Sleep on MSDN. The sleep time is nothing more than a guaranteed minimum, and you could in fact sleep for any arbitrary amount of time greater than that.

Share this post


Link to post
Share on other sites
Thanks for fast reply. Do you have any suggestion how to decouple it from my game loop since i should use draw in a loop. Also isn't there a way to close VSync. And i'm having problem with this DWORD float conversions. Is there a more convenient way for gameloop?

Share this post


Link to post
Share on other sites
You will have to google how to disable VSynch on whatever framework your using whether it is XNA, DirectX, OpenGL, etc.

Share this post


Link to post
Share on other sites
Thanks for the suggestion mhagain. I know sleep is not a good idea but i use it to let other threads work. If i use empty while i prevent other threads from working. Is there any other way?


I'm using DirectX. I found D3DPRESENT_DONOTWAIT disables VSync. But it didn't work.

Share this post


Link to post
Share on other sites
Why do you want more than 60FPS. You probably do not need to render more than 60 FPS.

D3DPRESENT_DONOTWAIT is a hint, not a command. Your driver might override this. Look at your display adaptor settings in Windows.


Sleeping in a game loop is never a good idea by the way...
[/quote]
"Never" is very strong. On laptops and other battery powered devices it can preserve battery if you Sleep() when you've no useful processing to do. One option is to Sleep() when you have more than a scheduler quantum of time before you want to do anything. As you said, you have no guarantees, but at least this way you're letting any background processes take the CPU when you need it least.

Share this post


Link to post
Share on other sites
As you said i probably don't need more than 60 FPS but still to have full control over the loop and to learn i asked my question.

Share this post


Link to post
Share on other sites
Set your presentation interval in your initial present parameters and vsync will enable/disable as appropriate. D3DPRESENT_INTERVAL_DEFAULT or D3DPRESENT_INTERVAL_ONE for vsync enabled, D3DPRESENT_INTERVAL_IMMEDIATE for vsync disabled.

Share this post


Link to post
Share on other sites
Thanks mhagain. D3DPRESENT_INTERVAL_IMMEDIATE worked. Also i appreciate suggestions to my game loop if you have any.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!