I have an issue I want to solve:
When I move/drag/resize a window, my message pump get stuck and window content is not updated (well known problem with DefWIndowProc). I want to create another thread and make rendering there, but I don`t know how to correctly do that, because if I start rendering loop in other thread, messages seems not to reach window or something like that.
Here is some code. I use C++/CLI mixed mode and WPF window:
This is main. Here I start my engine
[STAThreadAttribute]
int main()
{
try
{
ManagedEngine^ managed = gcnew ManagedEngine();
managed->InitializeSystem();
ThreadStart^ threadDelegate = gcnew ThreadStart(managed, &ManagedEngine::StartEngine);
Thread^ newThread = gcnew Thread(threadDelegate);
newThread->Start();
//managed->StartEngine();
managed->Stop();
}
catch (Exception^ exception)
{
Console::WriteLine(exception->Message + exception->StackTrace + exception->TargetSite);
}
}
In initializeSystem() I do initialization of D3D, Camera, DirectInput
Rendering thread is in method Run()
void Engine::Run()
{
MSG msg;
bool done, result;
//////?????????????? ????????? MSG
ZeroMemory(&msg, sizeof(MSG));
//??????? ????, ??????? ????? ???????? ?? ??? ??? ???? ?? ???????
//????????? ?????????? ?????? ?? ???? ??? ?? ????????????
done = false;
int counter = 0;
while(!done)
{
Console::WriteLine("????? ? ???? ??????????");
//???????????? ????????? Windows
if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//???? ?????? ????????? ??????? ??????????, ????? ???????
if (msg.message == WM_QUIT)
{
done = true;
}
else
{
//????? ???????????? ????
result = Frame();
Console::WriteLine(counter);
if (!result)
{
done = true;
}
counter++;
}
}
}
I did my engine structure almost exactly as in rastertek lessons if you see them.
So, As I understand the main my problem is that I have message pump run in another thread, but window is main thread, so messages cant be read. That lead window to get stuck in that time when rendering goes well.
Now I want to understand how to organize thread to leave message pump outside (in main thread), but listen this messages inside second thread.
Correct me if I'm wrong, please. I need your help!