#include "Graphics.h" #pragma comment(lib,"d3d11.lib") Graphics::Graphics( HWND hWnd ) { // 交换链的参数定义 DXGI_SWAP_CHAIN_DESC sd = {}; sd.BufferDesc.Width = 0; sd.BufferDesc.Height = 0; sd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; sd.BufferDesc.RefreshRate.Numerator = 0; sd.BufferDesc.RefreshRate.Denominator = 0; sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferCount = 1; sd.OutputWindow = hWnd; sd.Windowed = TRUE; sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; sd.Flags = 0; // create device and front/back buffers, and swap chain and rendering context // 创建Device,SwapChain和Context D3D11CreateDeviceAndSwapChain( nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0, D3D11_SDK_VERSION, &sd, &pSwap, &pDevice, nullptr, &pContext ); // gain access to texture subresource in swap chain (back buffer) // 定义指向backBuffer的指针 ID3D11Resource* pBackBuffer = nullptr; // 通过SwapChian,取得backBuffer pSwap->GetBuffer(0, __uuidof(ID3D11Resource), reinterpret_cast(&pBackBuffer)); // 取得指向属于backBuffer的View的指针 pDevice->CreateRenderTargetView( pBackBuffer, nullptr, &pTarget ); pBackBuffer->Release(); } Graphics::~Graphics() { // 释放View, Context, SwapChain和Device if (pTarget != nullptr) { pTarget->Release(); } if( pContext != nullptr ) { pContext->Release(); } if( pSwap != nullptr ) { pSwap->Release(); } if( pDevice != nullptr ) { pDevice->Release(); } } void Graphics::EndFrame() { // flipping,使用SwapChain将back buffer的内容展现到屏幕 pSwap->Present( 1u,0u ); // 同步频率1,代表屏幕每刷新一次,就进行图形刷新。 } void Graphics::ClearBuffer(float red, float green, float blue) noexcept { const float color[] = { red,green,blue,1.0f }; pContext->ClearRenderTargetView(pTarget, color); }