Untitled

 avatar
unknown
plain_text
4 years ago
2.7 kB
3
Indexable
bool CreateDeviceD3D(HWND hWindow)
{
    // Setup swap chain
    DXGI_SWAP_CHAIN_DESC chain;
    ZeroMemory(&chain, sizeof(chain));
    chain.BufferCount = 2;
    chain.BufferDesc.Width = 0;
    chain.BufferDesc.Height = 0;
    chain.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    chain.BufferDesc.RefreshRate.Numerator = 60;
    chain.BufferDesc.RefreshRate.Denominator = 1;
    chain.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
    chain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    chain.OutputWindow = hWindow;
    chain.SampleDesc.Count = 1;
    chain.SampleDesc.Quality = 0;
    chain.Windowed = TRUE;
    chain.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

    UINT createDeviceFlags = 0;
    //createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
    D3D_FEATURE_LEVEL featureLevel;
    const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0 };
    if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &chain, &swapChain, &device, &featureLevel, &deviceContext) != S_OK)
        return false;

    CreateRenderTarget();
    return true;
}
void DisposeDeviceD3D()
{
    DisposeRenderTarget();
    if (swapChain) { swapChain->Release(); swapChain = NULL; }
    if (deviceContext) { deviceContext->Release(); deviceContext = NULL; }
    if (device) { device->Release(); device = NULL; }
}
void CreateRenderTarget()
{
    ID3D11Texture2D* pBackBuffer;
    swapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
    device->CreateRenderTargetView(pBackBuffer, NULL, &renderTargetView);
    pBackBuffer->Release();
}
void DisposeRenderTarget()
{
    if (renderTargetView)
    {
        renderTargetView->Release();
        renderTargetView = NULL;
    }
}

LRESULT WINAPI WndProc(HWND hWindow, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (ImGui_ImplWin32_WndProcHandler(hWindow, message, wParam, lParam))
        return true;

    switch (message)
    {
        case WM_SIZE:
            if (device != NULL && wParam != SIZE_MINIMIZED)
            {
                DisposeRenderTarget();
                swapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
                CreateRenderTarget();
            }
            return 0;
        case WM_SYSCOMMAND:
            if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
                return 0;
            break;
        case WM_DESTROY:
            ::PostQuitMessage(0);
            return 0;
    }
    return ::DefWindowProc(hWindow, message, wParam, lParam);
}
Editor is loading...