D3D初始化

#include 
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include 
#pragma warning( default : 4996 )


bool InitD3D(HWND hWnd, bool fullscreen);
void RenderScene();
void ShutDown();

LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;


LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
        break;
    case WM_KEYUP:
        if (wParam == VK_ESCAPE)
            PostQuitMessage(0);
        break;
    default:
        break;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevInst, LPSTR cmdLine, int show)
{
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0, hInst, NULL, NULL, NULL, NULL, L"UGPDX", NULL };

    RegisterClassEx(&wc);

    HWND hWnd = CreateWindow(L"UGPDX", L"Blank D3D Window", WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, GetDesktopWindow(), NULL, wc.hInstance, NULL);

    if (InitD3D(hWnd, false))
    {
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);

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

        while (msg.message!= WM_QUIT)
        {
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            else
            {
                RenderScene();
            }
        }
    }

    ShutDown();
    UnregisterClass(L"UGPDX", wc.hInstance);
    return 0;
}

bool InitD3D(HWND hWnd, bool fullscreen)
{
    D3DDISPLAYMODE displayMode;

    g_D3D = Direct3DCreate9(D3D9b_SDK_VERSION);

    if (g_D3D == NULL)
        return false;

    if (FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
        return false;

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    if (fullscreen)
    {
        d3dpp.Windowed = FALSE;
        d3dpp.BackBufferWidth = 640;
        d3dpp.BackBufferHeight = 480;
    }
    else
        d3dpp.Windowed = TRUE;

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = displayMode.Format;

    if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice)))
        return false;

    return true;

}

void RenderScene()
{
    g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    g_D3DDevice->BeginScene();

    g_D3DDevice->EndScene();

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

void ShutDown()
{
    if (g_D3DDevice != NULL)
        g_D3DDevice->Release();
    if (g_D3D != NULL)
        g_D3D->Release();

    g_D3D = NULL;
    g_D3DDevice = NULL;
}

你可能感兴趣的:(DX)