Jump to content
  • Advertisement
Sign in to follow this  
OrcishCoder

Windowed mode DDraw game loop

This topic is 4845 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 am writing 2D game using DDraw. No 3D stuff, just typical 2D game and running under windowed mode (non-fullscreen) I wrote fullscreen game before and no problem with it. But now i am stucked: in windowed mode,I don want use typical PeekMessage gameloop because I dont want my game uses up all cpu resource. I was thinking using GetMessage,but then it will be difficult to keep constant frame updating. So, I came out this solution, hope you guys give me some advices and correct me if I am wrong: my game loop: while(1) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message==WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { if (g_bAppActive==1) { UpdateFrame(); Sleep(16); //Sleep 16 msec so it wont use up all cpu power } else { WaitMessage(); } } } Without Sleep(16),my cpu 100% consumed by this game and the system become very laggy. 16 because of 16 milliseconds and hence give resolution 60 times per second. Am I using a right concept? Oh, one more thing,UpdateFrame() is nothing but just Blitting a 800x600 background and a 71x96 moving sprite. I use time based animation,but i found sometime the sprite got shearing effect,I tried to add: pDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, NULL); before blitting primary surface,but it getting worse,it shears almost all the time,I dont know why. Really wonder how to make smooth animation under windowed mode. Thanks in advanced. regards, Martin

Share this post


Link to post
Share on other sites
Advertisement
Better would be to measure the time used by UpdateFrame and than to wait:

16 - updateFrameTime ms (if that value is larger 0)

Just take the following little example:
updateFrame takes 30ms (drawing world on a slow system)

now you will wait also 16ms, which make 46ms...which gives you only a rate of 24 frames per second, also you could reach 30 frames per second...

A better concept I think would be a timer, which creates events for updateFrame.

so that you would have something like:

WaitMessage
if( PeekMessage )
DispactMessage

Share this post


Link to post
Share on other sites
You could always just use GetTickCount() and compare with the last tick, to see if it is time to render the next frame or not..

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!