Jump to content
  • Advertisement
Sign in to follow this  
billhsu

HELP! How to lock FPS Correctly

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

Help me!I am a kid new in game programing. I want to Lock my 3d game to 60 FPS.How to lock FPS Correctly. I tried timeGetTime() ,but it doesn't work properly Here is my code,but it didn't work properly . the game didn't run smoothly while(true) { DWORD start_time = GetTickCount(); if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } update(float(GetTickCount() - start_time)); while((GetTickCount() - start_time) < 30) Sleep(1); } How to lock FPS Correctly

Share this post


Link to post
Share on other sites
Advertisement
1. Are you absolutely sure you want to lock the FPS instead of making your game run at any frame rate?
2. Enable v-sync. That'll lock you to the monitors refresh rate (Although not necessarily 60 FPS).
3. You should be processing all messages before running a game tick, not just one.
4. GetTickCount() is only accurate to ~15ms. Using it to measure 30ms is a pretty high margin of error.
5. Sleep(1) will sleep for at least 1ms. Usually it'll sleep for between 10 and 250ms (>= One scheduler timeslice).
6. That looks suspiciously like DirectXTutorial source code, which is (IMO) a pretty terrible site to be learning from.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
1. Are you absolutely sure you want to lock the FPS instead of making your game run at any frame rate?


I just want to make sure that the game can run at a same speed no matter on a quick machine or a slow one,how to do it?

Share this post


Link to post
Share on other sites
Quote:
Original post by billhsu
Quote:
Original post by Evil Steve
1. Are you absolutely sure you want to lock the FPS instead of making your game run at any frame rate?


I just want to make sure that the game can run at a same speed no matter on a quick machine or a slow one,how to do it?
The best way is to base movement on time, not a fixed step. So, if you were previously moving a sprite 1 pixel per frame at 60 FPS, you'd want to move it 60 pixels per second. Then it doesn't matter if your app is running at 1000 FPS or 2 FPS, you'll still move things at the same speed.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by billhsu
Quote:
Original post by Evil Steve
1. Are you absolutely sure you want to lock the FPS instead of making your game run at any frame rate?


I just want to make sure that the game can run at a same speed no matter on a quick machine or a slow one,how to do it?
The best way is to base movement on time, not a fixed step. So, if you were previously moving a sprite 1 pixel per frame at 60 FPS, you'd want to move it 60 pixels per second. Then it doesn't matter if your app is running at 1000 FPS or 2 FPS, you'll still move things at the same speed.


Thanks,
but should i use timeGetTime()or QueryPerformanceCounter & QueryPerformanceFrequency

Share this post


Link to post
Share on other sites
Quote:
Original post by billhsu
but should i use timeGetTime()or QueryPerformanceCounter & QueryPerformanceFrequency
Usually timeGetTime() is sufficient. If you find you're running faster than you can measure with timeGetTime(), then you can start Sleep()ing or waiting longer.

There's been a few discussions about different types of timer on these forums, it should be fairly easy to track some of them down.

Share this post


Link to post
Share on other sites
Personally I am a big fan of fixed timesteps. Glenn Fiedler has a good article about that: Fix your timestep.

(sidenote: I brushed coffee over my display after reading about the internet stalker. I remember that guy from either gd.net or devmaster.net, but didn't know about his crusade :D)

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel(sidenote: I brushed coffee over my display after reading about the internet stalker. I remember that guy from either gd.net or devmaster.net, but didn't know about his crusade :D)


That's hilarious. [lol]

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
1. Are you absolutely sure you want to lock the FPS instead of making your game run at any frame rate?
2. Enable v-sync. That'll lock you to the monitors refresh rate (Although not necessarily 60 FPS).
3. You should be processing all messages before running a game tick, not just one.
4. GetTickCount() is only accurate to ~15ms. Using it to measure 30ms is a pretty high margin of error.
5. Sleep(1) will sleep for at least 1ms. Usually it'll sleep for between 10 and 250ms (>= One scheduler timeslice).
6. That looks suspiciously like DirectXTutorial source code, which is (IMO) a pretty terrible site to be learning from.


Maybe I'm doing something wrong but I let my game run as fast as it can. And when using the time based movement if the framerate drops then movement gets choppy because objects travel so much farther between frames.

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!