Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to create a window and render frame with specific color using Direct3D 11 in C++ Win32 API

1 Answer

0 votes
#include <windows.h>
#include <d3d11_3.h>

#pragma comment(lib, "d3d11.lib")

IDXGISwapChain *swapchain;            
ID3D11Device *device;                    
ID3D11DeviceContext *devicecontext;
ID3D11RenderTargetView *backbuffer;

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void InitD3D(HWND hWnd);
void CleanD3D(void);
void RenderFrame(void);

int CALLBACK WinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPSTR lpCmdLine,
    _In_ int nCmdShow) {

    static TCHAR szClassName[] = L"directx";

    WNDCLASSEX wc = { 0 };
    wc.cbSize = sizeof(wc);
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = NULL;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szClassName;
    wc.hIconSm = NULL;

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, L"RegisterClassEx Error", L"Title", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }
    
    RECT rc = { 0, 0, 800, 600 };
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);

    HWND hWnd = CreateWindowEx(
        0, szClassName, L"Window Name",
        WS_CAPTION | WS_MAXIMIZEBOX | WS_SYSMENU | WS_MINIMIZEBOX,
        300, 300, 
        rc.right - rc.left, // Window Width
        rc.bottom - rc.top, // Window Height
        NULL, NULL, hInstance, NULL);

    if (!hWnd) {
        MessageBox(NULL, L"hWnd Error", L"Title", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

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

    InitD3D(hWnd);
    RenderFrame();

    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_DESTROY:
            CleanD3D();
            PostQuitMessage(0);
            return 0;
    }
    
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

void InitD3D(HWND hWnd) {
    // Direct3D init

    // swap chain struct
    DXGI_SWAP_CHAIN_DESC scd;

    // clear out the struct for use
    ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

    // swap chain struct
    scd.BufferCount = 1;                                    // 1 back buffer
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;     // 32-bit color
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;      
    scd.OutputWindow = hWnd;                                
    scd.SampleDesc.Count = 4;                               
    scd.Windowed = TRUE;                                    
    
    D3D11CreateDeviceAndSwapChain(NULL,
        D3D_DRIVER_TYPE_HARDWARE,
        NULL,
        0,
        NULL,
        0,
        D3D11_SDK_VERSION,
        &scd,
        &swapchain,
        &device,
        NULL,
        &devicecontext);

    // Set the render target - back buffer

    ID3D11Texture2D *pBackBuffer = NULL;
    
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
    device->CreateRenderTargetView(_In_ pBackBuffer, NULL, &backbuffer);
    pBackBuffer->Release();

    devicecontext->OMSetRenderTargets(1, &backbuffer, NULL);

    // Set the viewport

    D3D11_VIEWPORT viewport;
    ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    viewport.Width = 800;
    viewport.Height = 600;

    devicecontext->RSSetViewports(1, &viewport);
}

void RenderFrame(void) {
    // clear back buffer to a deep blue
    float color[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
    devicecontext->ClearRenderTargetView(backbuffer, color);

    // switch the back buffer and the front buffer
    swapchain->Present(0, 0);
}

void CleanD3D(void) {
    swapchain->Release();
    backbuffer->Release();
    device->Release();
    devicecontext->Release();
}




/*
run:


*/

 



answered Jun 22, 2021 by avibootz
edited Nov 20, 2022 by avibootz
...