2011-12-22 179 views
0

這是創建一個簡單窗口並初始化一個簡單的direct3d設備的代碼。但每次程序到達render()函數應用程序終止。我不知道爲什麼會發生。有人能解釋我這種奇怪的行爲嗎?謝謝!!direct3d初始化失敗/ C++

//==================================================================================================== 

#include <windows.h> 
#include <d3d9.h> 

//==================================================================================================== 

HINSTANCE hInst; 
HWND wndHandle; 

//==================================================================================================== 

LPDIRECT3D9 pD3D; // the Direct3D object 
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device 

//==================================================================================================== 

bool initWindow(HINSTANCE hInstance); 
bool initDirect3D(void); 
void cleanUp (void); 
void render(void); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 

//==================================================================================================== 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    if (!initWindow(hInstance)) return false; 

    if (!initDirect3D()) return false; 

    MSG msg; 
    ZeroMemory(&msg, sizeof(msg)); 

    if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) 
    { 
     TranslateMessage (&msg); 
     DispatchMessage (&msg); 
    } else { 
     render(); // i think this is the problem ... 
    } 

    return static_cast<int>(msg.wParam); 
} 

bool initWindow(HINSTANCE hInstance) 
{ 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 
    wcex.style = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra = 0; 
    wcex.cbWndExtra = 0; 
    wcex.hInstance = hInstance; 
    wcex.hIcon = LoadIcon(0, IDI_APPLICATION); 
    wcex.hCursor = LoadCursor(0, IDC_ARROW); 
    wcex.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); 
    wcex.lpszMenuName = 0L; 
    wcex.lpszClassName = L"DirectXTemplate"; 
    wcex.hIconSm = 0; 
    RegisterClassEx(&wcex); 

    wndHandle = CreateWindow(L"DirectXTemplate", L"DirectX Template", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); 

    if (!wndHandle) return false; 

    ShowWindow(wndHandle, SW_SHOW); 
    UpdateWindow(wndHandle); 

    return true; 
} 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
     case WM_DESTROY: 
      PostQuitMessage(0); 
      break; 

     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
} 

bool initDirect3D(void) 
{ 
    pD3D = NULL; 
    pd3dDevice = NULL; 

    // create the DirectX object 
    if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return false; 

    // fill the presentation parameters structure 
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp)); 

    d3dpp.Windowed   = TRUE; 
    d3dpp.SwapEffect  = D3DSWAPEFFECT_DISCARD; 
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; 
    d3dpp.BackBufferCount = 1; 
    d3dpp.BackBufferHeight = 480; 
    d3dpp.BackBufferWidth = 640; 
    d3dpp.hDeviceWindow  = wndHandle; 

    // create a default DirectX device 
    if (FAILED(pD3D -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice))) return false; 

    return true; 
} 

void render(void) 
{ 
    // Check to make sure you have a valid Direct3D device 
    if(NULL == pd3dDevice) return; // clear the back buffer to a blue color 
    pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0); 

    // Present the back buffer contents to the display 
    pd3dDevice -> Present(NULL, NULL, NULL, NULL); 
} 

void cleanUp (void) 
{ 
    // Release the device and the Direct3D object 
    if (pd3dDevice != NULL) pd3dDevice -> Release(); 
    if (pD3D != NULL) pD3D -> Release(); 
} 

回答

3

@DuckMaestro是對的。你的程序正在經歷msg/render過程,然後結束。它應該只在msg退出程序時結束。嘗試把在一個循環是這樣的:

while(msg.message!=WM_QUIT){ 
    if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) 
    { 
     TranslateMessage (&msg); 
     DispatchMessage (&msg); 
    } else { 
     render(); // i think this is the problem ... 
    } 
} 
+0

它的工作!非常感謝) – 2011-12-22 00:57:58

2

你...

if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) 
{ 
    TranslateMessage (&msg); 
    DispatchMessage (&msg); 
} else { 
    render(); // i think this is the problem ... 
} 

...需要在一個while循環,不是嗎?用調試器逐句執行代碼,按語句聲明。 Win32應用程序需要一個while循環來保持活躍,可以說話。

+0

我試過了 - 而且,只是爲了測試它,我把'渲染()''中的功能WM_PAINT'。程序仍然終止。 – 2011-12-22 00:52:49

+0

這很奇怪,因爲在書中一切正常。 – 2011-12-22 00:53:45