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,950 questions

51,892 answers

573 users

How to render a Direct3D11 blank frame with C++ Win32 API

1 Answer

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

static IDXGISwapChain* swapchain = nullptr;
static ID3D11Device* dev = nullptr;
static ID3D11DeviceContext* devcon = nullptr;
static ID3D11RenderTargetView* backbuffer = nullptr;

void InitD3D11(HWND hWnd) {
	// create a struct for swap chain
	DXGI_SWAP_CHAIN_DESC scd;

	// clear struct 
	ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

	// fill the swap chain struct
	scd.BufferCount = 1;                                    // one 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;                               // 4 multisamples
	scd.Windowed = TRUE;                                    

	auto result = D3D11CreateDeviceAndSwapChain(
		NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		NULL,
		NULL,
		NULL,
		D3D11_SDK_VERSION,
		&scd,
		&swapchain,
		&dev,
		NULL,
		&devcon);

	if (result != S_OK) {
		MessageBox(nullptr, L"Error creating D3D11CreateDeviceAndSwapChain", L"Error", MB_OK);
		exit(0);
	}
	
	// set the render target
	ID3D11Texture2D* pBackBuffer;
	swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

	dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
	pBackBuffer->Release();

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

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

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

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

// render a single frame
void RenderFrame(void) {
	// clear the back buffer to RGB color
	float color[] = { .15f, .35f, 0.5, 1 };
	devcon->ClearRenderTargetView(backbuffer, color);

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

void ReleaseD3D11() {
    swapchain->Release();
	backbuffer->Release();
    dev->Release();
    devcon->Release();
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

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

	const wchar_t szClassName[] = L"directx11";

	WNDCLASSEX wc = { };
	wc.cbSize = sizeof(wc);
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.hInstance = hInstance;
	wc.lpszClassName = szClassName;

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

	HWND hWnd = CreateWindowEx(
		0, szClassName, L"Window Name",
		WS_CAPTION | WS_MAXIMIZEBOX | WS_SYSMENU | WS_MINIMIZEBOX,
		300, 300, 640, 480,
		NULL, NULL, hInstance, NULL);

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

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	MSG msg = { 0 };
	BOOL result;

	InitD3D11(hWnd);
	RenderFrame();
	ReleaseD3D11();

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

	if (result == -1)
		return -1;

	return (int)msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
		case WM_CLOSE: {
			PostQuitMessage(0);
			break;
		}
		default: {
			//OutputDebugStringA("default\n");
			break;
		}
	}

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





/*
run:


*/

 



answered Aug 13, 2023 by avibootz
...