我的 WinMain 是这样开始的:
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
if(SUCCEEDED(CoInitialize(NULL)))
{
{
HRESULT hr = S_OK;
Game game;
D2DResources d2DResources;
game.SetPointer(d2DResources);
hr = d2DResources.Initialize(hInst);
我的编译器将在中断未处理的异常之前执行上面显示的最后一行。我知道它不会走得更远,因为我在 hr = d2DResources.Initialize(hInst) 之前添加了一个 MessageBox 命令,之后又添加了一个,并且只有第一个出现。
所以,D2DResources::Initialisize(HINSTANCE) 看起来像这样:
HRESULT D2DResources::Initialize(HINSTANCE hInst)
{
HRESULT hr;
// Create factory
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
// Create WIC factory
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pIWICIF)
);
}
// 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", // Replace with gameName
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);
}
RECT rc;
GetClientRect(m_hWnd,&rc);
// Creates render target
if(SUCCEEDED(hr))
{
pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
m_hWnd,
D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top)),
&pRT);
}
D2D1_SIZE_F pRTSize = pRT->GetSize();
RECT rect = {50, (long)pRTSize.height-(LogLineSize*5), (long)pRTSize.width, (long)pRTSize.height};
logArea = rect;
if(SUCCEEDED(hr))
{
pRT->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&pWhiteBrush
);
}
if(SUCCEEDED(hr))
{
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(pWF),
reinterpret_cast<IUnknown**>(&pWF)
);
}
if(SUCCEEDED(hr))
{
hr = pWF->CreateTextFormat(
L"Verdana",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
14.0f,
L"",
&pTextFormat
);
}
return hr;
}
重载的 Game::SetPointer 在使用 D2DResources 对象调用时看起来像这样:
void Game::SetPointer(D2DResources& p)
{
pD2DResources=&p;
}
pD2DResources 是一个 D2DResources*。
当 WM_SIZE 消息发送到我的 WinProc 时,问题发生在 D2DResources::Initialization(HINSTANCE) 期间。所以,有一些我的WinProc:
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
{
Game* pGame = reinterpret_cast<Game*>(static_cast<LONG_PTR>(
::GetWindowLongPtrW(
hWnd,
GWLP_USERDATA
)));
switch(msg)
{
case WM_SIZE:
{
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
pGame->pD2DResources->OnResize(width, height);
}
break;
当调用 OnResize 时,就会出现问题。
void D2DResources::OnResize(UINT width, UINT height)
{
if(pRT)
{
HRESULT hr = pRT->Resize(D2D1::SizeU(width, height));
if(hr!=0) MessageBox(NULL,"Render target could not be resized","ID2D1HwndRenterTarget Error",MB_ICONERROR);
}
}
编译器中断,并且有一个黄色箭头指向 if(pRT)(pRT 是指向渲染目标的指针)行。
有趣的是,在调试过程中查看汽车显示了这一点。
- 这 0x00000000 {log=0x00000000 nLogLines=??? logArea={...} ...} D2DResources * const
- 日志 0x00000000 wchar_t [511] nLogLines CXX0030:错误:无法评估表达式
- logArea {顶部=???底部=???左=???right=???} tagRECT m_hWnd CXX0017:错误:符号“”未找到 pD2DFactory CXX0017:错误:符号“”未找到 pIWICIF CXX0017:错误:符号“”未找到 pWF CXX0017:错误:符号“”未找到 pRT CXX0030:错误:无法评估
表达式 pCurrentScreen CXX0017:错误:未找到符号“” pWhiteBrush CXX0017:错误:未找到符号“” pTextFormat CXX0017:错误:未找到符号“”
那么,问题是什么,解决方案是什么?