How do you fix a program from freezing when you move the window in SDL2?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 01:53:10

问题


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?


回答1:


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();
    }
}

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.



来源:https://stackoverflow.com/questions/29552658/how-do-you-fix-a-program-from-freezing-when-you-move-the-window-in-sdl2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!