Reputation: 1507
This is my WinMain:
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
if(SUCCEEDED(CoInitialize(NULL)))
{
{
Game game;
game.CreateRessources(hInst);
game.ShowMainScreen();
HWND hwnd=game.Getm_hWnd();
HWND* pHWND=&hwnd;
game.pWinsock->Initialize(hwnd);
ShowWindow(game.Getm_hWnd(),SW_MAXIMIZE);
UpdateWindow(game.Getm_hWnd());
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
}
return 0;
}
There is my window creation class:
void Game::CreateWindowClass(HINSTANCE hInst)
{
// Create a window class
WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=WinProc;
wClass.lpszClassName="Window Class";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,"Failed to register window class","Window Class Failed",MB_ICONERROR);
}
m_hWnd=CreateWindowEx(NULL,
"Window Class",
"Game",
WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
this);
if(!m_hWnd)
{
int nResult=GetLastError();
MessageBox(NULL,"Window class creation failed","Window Class Failed",MB_ICONERROR);
}
}
This is Game::CreateRessources:
void Game::CreateRessources(HINSTANCE hInst)
{
CreateWindowClass(hInst);
pD2DResources=CreateD2DResources(m_hWnd);
pMessageLog=CreateMessageLog();
pWinsock=CreateWinsock();
}
And finally, my WinProc up to slightly after the lines: if(pGame==NULL) MessageBox(NULL,"Pointer pGame is NULL","Error",NULL);
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg==WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
Game* pGame = (Game*)pcs->lpCreateParams;
::SetWindowLongPtrW(
hWnd,
GWLP_USERDATA,
PtrToUlong(pGame)
);
}
else if(msg==WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
else
{
Game* pGame = reinterpret_cast<Game*>(static_cast<LONG_PTR>(
::GetWindowLongPtrW(
hWnd,
GWLP_USERDATA
)));
if(pGame==NULL)
MessageBox(NULL,"Pointer pGame is NULL","Error",NULL);
if(pGame->pD2DResources!=NULL)
{
switch(msg)
{
For some reason, pGame is NULL since the MessageBox pops up. Why so?
Edit: By the way, I am dynamically creating a few objects and private pointers inside my game object are assigned to these dynamically created objects. Could it have to do with the problem?
Upvotes: 0
Views: 259
Reputation: 2349
Because WM_CREATE
is not 1st message the window procedure gets. See WM_NCCREATE
.
Upvotes: 4