I'm creating an Win32 Window to serve as the primary surface to make DirectX and OpenGL calls. The problem is that is more intuitive to create the window first, initialize the API and load graphics resources (Shaders, Textures, Vertex Streams), and after that start with the simulation loop, but I don't know that if there is some internal problem - maybe with the Windows platform - in this create-load-show interval. Another thing is that I'm using the main thread to bufferize keyboard events. Eg.:
/*
* Initialize and Win32 Window to be showed later.
*/
BOOL CWindow::Init(const std::wstring& _sTitle, unsigned int _ui32Width, unsigned int _ui32Height, bool /*_bFullscreen*/) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = CWindow::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = ::GetModuleHandle(NULL);
wcex.hIcon = NULL;
wcex.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _sTitle.c_str();
wcex.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION);
if ( !::RegisterClassEx(&wcex) ) { return false; }
m_hWnd = ::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, wcex.lpszClassName, wcex.lpszClassName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
CW_USEDEFAULT, CW_USEDEFAULT, _ui32Width, _ui32Height,
NULL, NULL, wcex.hInstance, reinterpret_cast<LPVOID>(this) );
return m_hWnd ? true : false;
}
Looks more intuitive than the create-show-load approach, but I don't know if I'm going to lose window events in the interval of the above implementation so after the initialization I can call:
int CWindow::Run() {
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
MSG msg = { 0 };
while (m_bIsOpen) {
::WaitMessage();
while (::PeekMessage(&msg, m_hWnd, 0, 0, PM_REMOVE)) {
::DispatchMessage(&msg);
}
}
return static_cast<int>(msg.wParam);
}
At the highest level:
m_wWindow.Init(...);
CGraphics::InitApi( m_wWindow.Hwnd() );
m_wWindow.Show(...);
m_wWindow.Run();