Hi guys.
I'm trying to make a simple game loop, where the
usual window message pump is in the initial thread,
while the directx rendering is at another thread.
The code is something like that:
// The next method runs at the initial thread
// it is called after DirectX and win32 initialization.
int Run()
{
std::thread tRender(&ThreadProc_Render);
if (tRender.joinable())
tRender.detach();
MSG msg;
while (this->m_bRun)
{
BOOL bGetMsg(::GetMessage(&msg, NULL, 0, 0));
if (bGetMsg == -1) // Error
{
this->m_bRun = false;
}
else if (bGetMsg == 0) // WM_QUIT
this->m_bRun = false;
else
{
// ::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
return (msg.wParam);
}
ThreadProc_Render:
void ThreadProc_Render()
{
StopWatch sw;
// Render Loop
while (pWnd->m_bRun)
{
uint32 nMS = sw.Restart();
HRESULT hr(gpSwapChain->Present(0, 0));
// ::Sleep(15);
std::this_thread::yield();
}
}
in case you're wondering, StopWatch is a class of mine. Restart()
sets the watch old time to now, and return the different between the old and the new time.
The problem is, the window it self is not responding (can't move or resize it)
It seems as if the other thread is taking all the GetMessage() thread resources.
However, as you can see, the function call to sleep with 15 ms is commented out.
when it's not commented out, the program runs fine.
Also, if I comment out the Present() call, same thing - window seem to "work" again
(can resize, move).
Also, when looking at task manager, the application doesn't seem to consume
high cpu at all.
As far as I can see, it looks like a blocking issue. The thing is, there is no blocking at all.
I even tried replacing GetMessage() wit PeekMessage(),
and calling 'yield' when there are no messages. That gave me the same result though,
window stuck unless Present() line is commented out (or Sleep isn't).
Why is this even happening? What am I missing here?