cpp
#include <d3d11.h>
#include <dxgi1_2.h>
#include <iostream>
int main()
{
// 创建 D3D 设备和 D3D 设备上下文
ID3D11Device* pDevice = nullptr;
ID3D11DeviceContext* pDeviceContext = nullptr;
D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0 };
D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, D3D11_CREATE_DEVICE_BGRA_SUPPORT,
featureLevels, 1, D3D11_SDK_VERSION, &pDevice, nullptr, &pDeviceContext);
// 获取 DXGI 适配器
IDXGIDevice* pDXGIDevice = nullptr;
pDevice->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&pDXGIDevice));
IDXGIAdapter* pDXGIAdapter = nullptr;
pDXGIDevice->GetAdapter(&pDXGIAdapter);
// 获取 DXGI 设备
IDXGIFactory2* pDXGIFactory = nullptr;
pDXGIAdapter->GetParent(__uuidof(IDXGIFactory2), reinterpret_cast<void**>(&pDXGIFactory));
// 创建交换链并启用硬件加速
IDXGISwapChain1* pSwapChain = nullptr;
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
swapChainDesc.BufferCount = 2;
swapChainDesc.Width = 800;
swapChainDesc.Height = 600;
swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
pDXGIFactory->CreateSwapChainForHwnd(pDevice, GetActiveWindow(), &swapChainDesc, nullptr, nullptr, &pSwapChain);
pDXGIDevice->SetMaximumFrameLatency(1);
// 使用硬件加速绘制三角形
pDeviceContext->OMSetRenderTargets(1, nullptr, nullptr);
D3D11_VIEWPORT viewport = {};
viewport.Width = 800;
viewport.Height = 600;
viewport.MaxDepth = 1.0f;
pDeviceContext->RSSetViewports(1, &viewport);
float clearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
pDeviceContext->ClearRenderTargetView(nullptr, clearColor);
pSwapChain->Present(1, 0);
// 释放资源
pSwapChain->Release();
pDXGIDevice->Release();
pDXGIAdapter->Release();
pDXGIFactory->Release();
pDeviceContext->Release();
pDevice->Release();
return 0;
}