HELP! How to lock FPS Correctly

Started by
18 comments, last by clashie 14 years, 9 months ago
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
http://hi.baidu.com/probill
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.
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?
http://hi.baidu.com/probill
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.
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
http://hi.baidu.com/probill
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.
thank you
http://hi.baidu.com/probill
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)
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]
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.

This topic is closed to new replies.

Advertisement