Commit bd929e85 authored by Administrator's avatar Administrator
Browse files

add d3d11 graphic display

parent fcfc26d9
......@@ -18,15 +18,12 @@ int App::Go()
return *ecode;
}
DoFrame();
// ȴ10룬ֹ
Sleep(10);
}
}
void App::DoFrame()
{
const float t = timer.Peek();
std::ostringstream oss;
oss << "Time elapsed: " << std::setprecision(1) << std::fixed << t << "s";
wnd.SetTitle(oss.str());
const float c = sin(timer.Peek()) / 2.0f + 0.5f;
wnd.Gfx().ClearBuffer(c, c, 1.0f);
wnd.Gfx().EndFrame();
}
\ No newline at end of file
#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<void**>(&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);
}
\ No newline at end of file
#pragma once
#include "ChiliWin.h"
#include <d3d11.h>
class Graphics
{
public:
Graphics( HWND hWnd );
Graphics( const Graphics& ) = delete;
Graphics& operator=( const Graphics& ) = delete;
~Graphics();
void EndFrame();
// 刷新RGB
void ClearBuffer(float red, float green, float blue) noexcept;
private:
// 指向Device的指针
ID3D11Device* pDevice = nullptr;
// 指向交换链的指针
IDXGISwapChain* pSwap = nullptr;
// 指向Context的指针
ID3D11DeviceContext* pContext = nullptr;
// 指向View的指针
ID3D11RenderTargetView* pTarget = nullptr;
};
\ No newline at end of file
......@@ -140,6 +140,7 @@
<ClCompile Include="App.cpp" />
<ClCompile Include="ChiliException.cpp" />
<ClCompile Include="ChiliTimer.cpp" />
<ClCompile Include="Graphics.cpp" />
<ClCompile Include="Keyboard.cpp" />
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="Window.cpp" />
......@@ -151,6 +152,7 @@
<ClInclude Include="ChiliException.h" />
<ClInclude Include="ChiliTimer.h" />
<ClInclude Include="ChiliWin.h" />
<ClInclude Include="Graphics.h" />
<ClInclude Include="Keyboard.h" />
<ClInclude Include="Mouse.h" />
<ClInclude Include="resource.h" />
......
......@@ -39,6 +39,9 @@
<ClCompile Include="ChiliTimer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Graphics.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChiliException.h">
......@@ -68,6 +71,9 @@
<ClInclude Include="ChiliTimer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Graphics.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="StudyDx.rc">
......
......@@ -112,8 +112,10 @@ Window::Window( int width,int height,const char* name )
{
throw CHWND_LAST_EXCEPT();
}
// show window
// newly created windows start off as hidden
ShowWindow( hWnd,SW_SHOWDEFAULT );
// create graphics object
pGfx = std::make_unique<Graphics>(hWnd);
}
Window::~Window()
......@@ -152,6 +154,11 @@ std::optional<int> Window::ProcessMessages()
return {};
}
Graphics& Window::Gfx()
{
return *pGfx;
}
LRESULT CALLBACK Window::HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept
{
// use create parameter passed in from CreateWindow() to store window class pointer at WinAPI side
......
......@@ -22,7 +22,9 @@
#include "ChiliException.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "Graphics.h"
#include <optional>
#include <memory>
class Window
......@@ -63,6 +65,7 @@ public:
Window& operator=(const Window&) = delete;
void SetTitle(const std::string& title);
static std::optional<int> ProcessMessages();
Graphics& Gfx();
private:
static LRESULT CALLBACK HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
static LRESULT CALLBACK HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
......@@ -74,6 +77,8 @@ private:
int width;
int height;
HWND hWnd;
// 成员变量gfx在Window的构造函数执行的过程中才会被初始化,因此定义指针变量,用于初始化gfx
std::unique_ptr<Graphics> pGfx;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment