2014-12-07 157 views
0

我这个代码的问题是,当我在Visual C++中运行它时弹出一个窗口 但它只是崩溃。它没有响应,我不能点击退出。我必须拉起 任务管理器才能摆脱窗口。我是新来的Windows编程和直接X. 下面我会发布是我认为问题是。Directx窗口不断崩溃

#include <d3d9.h> 

#include <time.h> 

#define APPTITLE "Direct3D_Windowed" 

LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM); 
ATOM MyRegisterClass(HINSTANCE); 
int Game_Init(HWND); 
void GAME_RUN(HWND); 
void GAME_END(HWND); 

LPDIRECT3D9 d3d = NULL; 
LPDIRECT3DDEVICE9 d3ddev = NULL; 

// Over here, after GAME_END() is called, I tried separating the POSTQUITMESSAGE But I 
    I just got an error. 

LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(msg) 
    { 
     case WM_DESTROY: 
     GAME_END(hWnd); 
     PostQuitMessage(0); 
     return 0; 
} 

return DefWindowProc(hWnd, msg, wParam, lParam); 
} 

ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
WNDCLASSEX wc; 
wc.cbSize = sizeof(WNDCLASSEX); 

wc.style = CS_HREDRAW | CS_VREDRAW; 
wc.lpfnWndProc = (WNDPROC)WinProc; 
wc.cbClsExtra = 0; 
wc.cbWndExtra = 0; 
wc.hInstance = hInstance; 
wc.hIcon = NULL; 
wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
wc.lpszMenuName = NULL; 
wc.lpszClassName = APPTITLE; 
wc.hIconSm = NULL; 

return RegisterClassEx(&wc); 
} 

// I got this code from a book that I am reading and realized that WinProc is not being 
    called in this function. Is this the potential problem? Were would I put the WinProc 
    function call if it is supposed to be here in WinMain 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
MSG msg = {0}; 
MyRegisterClass(hInstance); 
HWND hWnd; 

hWnd = CreateWindow(
    APPTITLE, 
    APPTITLE, 
    WS_OVERLAPPEDWINDOW, 
    CW_USEDEFAULT, 
    CW_USEDEFAULT, 
    500, 
    400, 
    NULL, 
    NULL, 
    hInstance, 
    NULL); 

if(!hWnd) 
    return FALSE; 

ShowWindow(hWnd, nCmdShow); 
UpdateWindow(hWnd); 

if(!Game_Init(hWnd)) 
    return 0; 

int done = 0; 

while(!done) 
{ 
    if(msg.message == WM_QUIT) 
    { 

     if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
     { 
      MessageBox(hWnd, "Recieve WM_QUIT message", "WinMain", MB_OK); 
      done = 1; 
     } 

     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 

    else 
     GAME_RUN(hWnd); 

} 

return msg.wParam; 
} 

int Game_Init(HWND hWnd) 
{ 
MessageBox(hWnd, "Program is about to start", "Game_Init", MB_OK); 

d3d = Direct3DCreate9(D3D_SDK_VERSION); 
if(d3d == NULL) 
{ 
    MessageBox(hWnd, "Error initializing Direct3D", "Error", MB_OK); 
    return 0; 
} 

D3DPRESENT_PARAMETERS d3dpp; 
ZeroMemory(&d3dpp, sizeof(d3dpp)); 
d3dpp.Windowed = TRUE; 
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; 

d3d->CreateDevice(
    D3DADAPTER_DEFAULT, 
    D3DDEVTYPE_HAL, 
    hWnd, 
    D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
    &d3dpp, 
    &d3ddev); 

if(d3ddev == NULL) 
{ 
    MessageBox(hWnd, "Error creating Direct device", "Error", MB_OK); 
    return 0; 
} 

srand(time(NULL)); 

return 1; 
} 

void GAME_RUN(HWND hWnd) 
{ 
if(d3ddev == NULL) 
    return; 

d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 0); 
if(d3ddev->BeginScene()) 
{ 
    d3ddev->EndScene(); 
} 

d3ddev->Present(NULL, NULL, NULL, NULL); 
} 

void GAME_END(HWND hWnd) 
{ 
MessageBox(hWnd, "Program is about to end", "Game End", MB_OK); 

if(d3ddev != NULL) 
    d3ddev->Release(); 

if(d3d != NULL) 
    d3d->Release(); 

} 

回答

0

看看这个吗?

if(msg.message == WM_QUIT) 

在你的while循环中。 也许改变,要,说:

if(true) 

原因:你希望你的应用程序传递的所有消息,不只是导致其退出的人。例如,当Windows想要你的应用程序绘制自己的时候。基本上,你当前的代码可以让你的应用程序做任何事情除了退出。

如果您想在应用程序退出时执行一些特殊操作,请在已有的case WM_DESTROY:WinProc()之后再添加一个case WM_QUIT:

GAME_RUN(hWnd);的当前位置不适合你。 你想要把它放在一个单独的线程中(最简单,性能最高)。或者你想使用一些定时器,并在case WM_DESTROY:之后用case WM_TIMER:来处理它。或者也可以编写自己的用户定义的消息。