1+ #pragma once
2+ #include < d3d11.h>
3+ // #include "DirectX/WICTextureLoader.h"
4+ // #include "resource.h"
5+
6+ class directx {
7+ private:
8+ ID3D11Device* g_pd3dDevice;
9+ ID3D11DeviceContext* g_pd3dDeviceContext;
10+ IDXGISwapChain* g_pSwapChain;
11+ ID3D11RenderTargetView* g_mainRenderTargetView;
12+ bool m_DPIScaleSet;
13+ float m_DPIScale;
14+ // ID3D11ShaderResourceView* m_icon;
15+ /* inline void LoadDXImage(int32_t file) {
16+ auto rc = FindResource(NULL, MAKEINTRESOURCE(file), "PNG");
17+ if (!rc)
18+ return;
19+ auto rcdata = LoadResource(NULL, rc);
20+ auto size = SizeofResource(NULL, rc);
21+ if (!rcdata)
22+ return;
23+ const uint8_t* data = static_cast<const uint8_t*>(LockResource(rcdata));
24+ DirectX::CreateWICTextureFromMemory(g_pd3dDevice, data, size, nullptr, &m_icon);
25+ }*/
26+ public:
27+ directx () :
28+ g_pd3dDevice (nullptr ),
29+ g_pd3dDeviceContext (nullptr ),
30+ g_pSwapChain (nullptr ),
31+ g_mainRenderTargetView (nullptr ),
32+ m_DPIScaleSet (false ),
33+ m_DPIScale (1 .f)
34+ {
35+ }
36+ ~directx () {
37+ CleanupDeviceD3D ();
38+ }
39+ inline ID3D11Device* GetDevice () {
40+ return g_pd3dDevice;
41+ }
42+ inline IDXGISwapChain* GetSwapChain () {
43+ return g_pSwapChain;
44+ }
45+ inline ID3D11DeviceContext* GetDeviceContext () {
46+ return g_pd3dDeviceContext;
47+ }
48+ inline ID3D11RenderTargetView* GetRenderTargetView () {
49+ return g_mainRenderTargetView;
50+ }
51+ inline float GetDPIScale () {
52+ if (!m_DPIScaleSet) {
53+ auto hDC = GetDC (NULL );
54+ auto dpix = GetDeviceCaps (hDC, LOGPIXELSX);
55+ ReleaseDC (NULL , hDC);
56+ auto DPI = MulDiv (100 , dpix, 96 );
57+ m_DPIScale = DPI / 100 .f ;
58+ m_DPIScaleSet = true ;
59+ }
60+ return m_DPIScale;
61+ }
62+ inline bool CreateDeviceD3D (HWND hWnd) {
63+ DXGI_SWAP_CHAIN_DESC sd;
64+ ZeroMemory (&sd, sizeof (sd));
65+ sd.BufferCount = 2 ;
66+ sd.BufferDesc .Width = 0 ;
67+ sd.BufferDesc .Height = 0 ;
68+ sd.BufferDesc .Format = DXGI_FORMAT_R8G8B8A8_UNORM;
69+ sd.BufferDesc .RefreshRate .Numerator = 60 ;
70+ sd.BufferDesc .RefreshRate .Denominator = 1 ;
71+ sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
72+ sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
73+ sd.OutputWindow = hWnd;
74+ sd.SampleDesc .Count = 1 ;
75+ sd.SampleDesc .Quality = 0 ;
76+ sd.Windowed = TRUE ;
77+ sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
78+
79+ UINT createDeviceFlags = 0 ;
80+ D3D_FEATURE_LEVEL featureLevel;
81+ const D3D_FEATURE_LEVEL featureLevelArray[3 ] = { D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0 };
82+ if (D3D11CreateDeviceAndSwapChain (NULL , D3D_DRIVER_TYPE_HARDWARE, NULL , createDeviceFlags, featureLevelArray, 3 , D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
83+ return false ;
84+
85+ CreateRenderTarget ();
86+ return true ;
87+ }
88+ inline void CleanupDeviceD3D () {
89+ CleanupRenderTarget ();
90+ if (g_pSwapChain) {
91+ g_pSwapChain->Release ();
92+ g_pSwapChain = NULL ;
93+ }
94+ if (g_pd3dDeviceContext) {
95+ g_pd3dDeviceContext->Release ();
96+ g_pd3dDeviceContext = NULL ;
97+ }
98+ if (g_pd3dDevice) {
99+ g_pd3dDevice->Release ();
100+ g_pd3dDevice = NULL ;
101+ }
102+ }
103+ inline void CreateRenderTarget () {
104+ ID3D11Texture2D* pBackBuffer;
105+ g_pSwapChain->GetBuffer (0 , __uuidof (ID3D11Texture2D), reinterpret_cast <LPVOID*>(&pBackBuffer));
106+ if (pBackBuffer) {
107+ g_pd3dDevice->CreateRenderTargetView (pBackBuffer, NULL , &g_mainRenderTargetView);
108+ pBackBuffer->Release ();
109+ }
110+ }
111+ inline void CleanupRenderTarget () {
112+ if (g_mainRenderTargetView) {
113+ g_mainRenderTargetView->Release ();
114+ g_mainRenderTargetView = NULL ;
115+ }
116+ }
117+ };
0 commit comments