Directx Window保持崩溃

时间:2014-12-07 20:55:20

标签: directx

我对此代码的问题在于,当我在visual C ++中运行它时会弹出一个窗口 但它只是崩溃了。它没有响应,我无法单击退出。我必须拉起来 任务经理摆脱窗口。我是Windows编程和直接X的新手。 下面我会发帖,我认为问题是。

#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();

}

1 个答案:

答案 0 :(得分:0)

看看这个?

if(msg.message == WM_QUIT)

在你的循环中。 也许改变一下,说:

if(true)

原因:您希望应用程序传递所有消息,而不仅仅是导致其退出的消息。例如,当Windows希望您的应用程序绘制自己时。基本上,您当前的代码最让您的应用程序可以执行任何除了退出。

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

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