问题描述
我正在和一个朋友制作一个小游戏,一个问题是当你拖动窗口时,它会冻结并且程序完全停止,直到你松开为止.我寻找了一个简单的解决方案,但我只发现它发生在所有事情上.此外,它搞砸了增量时间,因为它就像一个非常长的帧.有什么方法可以让程序在移动时继续运行,或者如果这太复杂,请修复增量时间?谢谢?
I'm making a small game with a friend and one problem is that when you drag the window around, it freezes and the program completely stops until you let go. I searched for a simple solution but I only found out that it happens with everything. Also, it screws up the delta time since it acts like a really long frame. Is there any way to either simply have the program continue running while you move it or if that's too complicated, fix the delta time? Thanks?
推荐答案
您的应用程序很可能冻结",因为它有一个类似于以下内容的 WinMain 循环:
Your application is most likely "freezing" because it has a WinMain loop similar to this:
while (true)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
TickGame();
}
}
所以不是勾选它而是处理 WM_MOVE 消息 .一个简单的解决方法是在移动消息中调用您的游戏滴答函数,或者当您收到第一个移动消息时暂停游戏并在您一两秒钟没有收到时取消暂停可能是有意义的.也就是说,人们真的会在播放时拖动窗口,不太可能.
So instead of ticking it is processing the WM_MOVE message . One simple work around would be just to call your games tick function within the move message, or perhaps it makes sense to pause the game when you get the first move message and unpause it if you haven't gotten one for a second or two. That is, are people really going to be dragging the window WHILE playing, unlikely.
要回答您的第二个问题,通常游戏(尤其是物理引擎)会使用固定时间步长来稳定模拟,请阅读有关固定时间步长的更多信息 此处.
To answer your second question, typically games (physics engines especially) will use a fixed time step to stabilize the simulation, read more about fixed time step here.
这篇关于当您在 SDL2 中移动窗口时,如何修复程序冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!