I've got a small problem with my game engine that I'm making. In windowed mode, when the user drags the window around on the screen, it's like the game processing continues, but at a much higher rate than normal.
It's hard to explain, and waaaay to much code to fully demonstrate. But. I have a Animation class that moves the position of an element based on the elapsed time. If I quickly click and release the window, the animation jumps to the very end, which I only assume means it processed extremely quickly all of a sudden.
I understand that the window doesn't draw when being dragged, which is fine. What I want to do is detect when the window is being dragged and then pause the simulation, only starting it up again when it's complete. WM_MOVING, WM_MOVE, and WM_EXITSIZEMOVE don't seem to work as expected. It's like there's no real way to determine when the window is being moved. I've also tried WM_NCLBUTTONDOWN to detect the possible start, which works, but then WM_NCLBUTTONUP doesn't seem to fire.
Here's my main loop. Any suggestions to fix this problem of continued animation while being dragged?
auto previousTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<float,std::milli> lag(0);
const std::chrono::duration<float,std::milli> tickRate(20);
bool paused = false;
MSG msg;
while(true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
else if(msg.message == WM_NCLBUTTONDOWN) // my attemps at detecting window moves
paused = true;
else if(msg.message == WM_NCLBUTTONUP)
paused = false;
else
birunji::messaging::process_wnd_msg(msg); // just posts all other relevant messages through a subscription queue
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
auto currentTime = std::chrono::high_resolution_clock::now();
auto elapsed = currentTime - previousTime;
previousTime = currentTime;
if(!paused) // my attempt at pausing the simulation
lag += std::chrono::duration_cast<std::chrono::duration<float,std::milli>>(elapsed);
if(!paused)
{
while(lag >= tickRate)
{
birunji::messaging::Postmaster::ProcessMessages(); // just executes the callbacks of the subscription queue above
uiManager.Update(lag.count()); // this is where the animation takes place that suddenly completes when dragging
lag -= tickRate;
}
uiManager.Draw();
birunji::renderer::Core::Draw();
window.Display();
}
}
}