Using win32 API, why does deltaTime always end up as zero? Code (Text): int startTime = timeGetTime(); // start message loop while(msg.message != WM_QUIT) { if(::PeekMessage(&msg,0,0,0,PM_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } else { int t = timeGetTime(); int deltaTime = (t - startTime); // For each frame update and render our game app.Update(deltaTime); app.Render(); startTime = t; } }
I'm not sure if it'll fix your problem but try declaring your variables t and deltaTime to NULL before the loop.
Remember that the granularity of timeGetTime() can be greater than 5 milliseconds. That might be an issue depending on how fast your PC is.
Also, you'll probably need to change your message loop to: Code (Text): while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE) { translate.. dispatch... } //update game logic ... Otherwise you might find that you're not processing windows events fast enough and your event queue will just keep growing.
I have a very fast laptop so that may be the issue. My question now is, how do I fix it? That was something I didn't know. Thanks.
Well, I seemed to have fixed it by moving into the timing variables into the app.Update() I am not sure as to why it works though and would love to understand that. Here is my main loop now: Code (Text): int startTime = timeGetTime(); // start message loop while(msg.message != WM_QUIT) { while(::PeekMessage(&msg,0,0,0,PM_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } // For each frame update and render our game app.Update(startTime); app.Render(); } These lines: Code (Text): int t = timeGetTime(); int deltaTime = (t - startTime); take place in the app.Update function now.