Skip to main content
GameDev.net gamedev.net
🔒 Locked

Windowed mode DDraw game loop

Started by OrcishCoder Aug 9, 2005 at 2:14 AM 1 replies 700+ views
Original Post
OrcishCoder
OrcishCoder
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
Sparhawk42
Sparhawk42
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

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.