Sign in to follow this  
ekba89

Game Loop

Recommended Posts

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

[code]
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);
}
}
[/code]

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
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
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.

[quote]
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
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
One way of decoupling Update from Draw is to use a fixed timestep loop. This type of loop will draw in the "main" body of the while() loop, and performing Update in a nested loop that locks the updates to a fixed rate. In this way, you draw as fast as hardware will allow, taking advantage of interpolation to smooth things out, but your logic is fixed to a deterministic rate that doesn't vary due to draw rate variation.
[url="http://gafferongames.com/game-physics/fix-your-timestep"]http://gafferongames.com/game-physics/fix-your-timestep[/url]
[url="http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Canonical_Game_Loop"]http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Canonical_Game_Loop[/url]
[url="http://www.flipcode.com/archives/Main_Loop_with_Fixed_Time_Steps.shtml"]http://www.flipcode.com/archives/Main_Loop_with_Fixed_Time_Steps.shtml[/url]

Share this post


Link to post
Share on other sites
[quote name='WavyVirus' timestamp='1307387129' post='4820211']
I believe that the user can set VSync on or off through control panel / display settings, and that generally you should honour the setting they have chosen unless you can't avoid forcing it on/off.
[/quote]

That's a very good point. I've personally yet to find a way of detecting this in D3D however. A postcard from Ghastly Hack City tells me that I could create a temporary OpenGL context, run some dummy frames, time them, and make a decision based on that, but I fear that it would make Baby Jesus Cry.

Share this post


Link to post
Share on other sites
[quote name='PrestoChung' timestamp='1307396239' post='4820267']
Game Loop question:

Is it a bad idea to have virtual dispatch for functions that are called in the main loop?
[/quote]

Generally speaking, no.

There is a slight overhead involved but unless you are calling a function several thousand times per frame it shouldn't even be noticable. (Most modern game engines are calling interpreted scripts in the main loop, the overhead of virtual functions is negligable by comparison)

Share this post


Link to post
Share on other sites
[quote]
Is it a bad idea to have virtual dispatch for functions that are called in the main loop?
[/quote]
Probably not. If you are worried about performance I can guarantee you your bottleneck is elsewhere. Consider also that if you want to achieve dynamic behaviour in your main game loop you're going to have to implement something similar anyway.

Share this post


Link to post
Share on other sites
Thanks for the links JTippetts I'll check them out after writing this post :). Also i looked for VSync option in control panel/display i couldn't find but there was an option in nvidia control panel so it was forcing vsync.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this