Game Loop

Started by
15 comments, last by ekba89 12 years, 10 months ago
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.
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.
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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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?
You will have to google how to disable VSynch on whatever framework your using whether it is XNA, DirectX, OpenGL, etc.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

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

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Thanks mhagain. D3DPRESENT_INTERVAL_IMMEDIATE worked. Also i appreciate suggestions to my game loop if you have any.

This topic is closed to new replies.

Advertisement