1+ #include " pch.h"
2+
3+ // Data
4+ static ID3D11Device* g_pd3dDevice = NULL ;
5+ static ID3D11DeviceContext* g_pd3dDeviceContext = NULL ;
6+ static IDXGISwapChain* g_pSwapChain = NULL ;
7+ static ID3D11RenderTargetView* g_mainRenderTargetView = NULL ;
8+
9+ // Forward declarations of helper functions
10+ bool CreateDeviceD3D (HWND hWnd);
11+ void CleanupDeviceD3D ();
12+ void CreateRenderTarget ();
13+ void CleanupRenderTarget ();
14+ LRESULT WINAPI WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
15+
16+ // Main code
17+ int dx11_test ()
18+ {
19+ // Create application window
20+ WNDCLASSEX wc = { sizeof (WNDCLASSEX), CS_CLASSDC, WndProc, 0L , 0L , GetModuleHandle (NULL ), NULL , NULL , NULL , NULL , L" fake_client" , NULL };
21+ ::RegisterClassEx (&wc);
22+ HWND hwnd = ::CreateWindow (wc.lpszClassName , L" Fake DX11 client" , WS_OVERLAPPEDWINDOW, 100 , 100 , 1280 , 800 , NULL , NULL , wc.hInstance , NULL );
23+
24+ // Initialize Direct3D
25+ if (!CreateDeviceD3D (hwnd))
26+ {
27+ CleanupDeviceD3D ();
28+ ::UnregisterClass (wc.lpszClassName, wc.hInstance);
29+ return 1 ;
30+ }
31+
32+ // Show the window
33+ ::ShowWindow (hwnd, SW_SHOWDEFAULT);
34+ ::UpdateWindow (hwnd);
35+
36+ // Main loop
37+ bool done = false ;
38+ while (!done)
39+ {
40+ // Poll and handle messages (inputs, window resize, etc.)
41+ // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
42+ // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
43+ // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
44+ // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
45+ MSG msg;
46+ while (::PeekMessage (&msg, NULL , 0U , 0U , PM_REMOVE))
47+ {
48+ ::TranslateMessage (&msg);
49+ ::DispatchMessage (&msg);
50+ if (msg.message == WM_QUIT)
51+ done = true ;
52+ }
53+ if (done)
54+ break ;
55+
56+ const float clear_color_with_alpha[4 ] = { 0 .0f , 0 .5f , 0 .7f , 1 .0f };
57+ g_pd3dDeviceContext->OMSetRenderTargets (1 , &g_mainRenderTargetView, NULL );
58+ g_pd3dDeviceContext->ClearRenderTargetView (g_mainRenderTargetView, clear_color_with_alpha);
59+
60+ g_pSwapChain->Present (1 , 0 ); // Present with vsync
61+ }
62+
63+ // Cleanup
64+ CleanupDeviceD3D ();
65+ ::DestroyWindow (hwnd);
66+ ::UnregisterClass (wc.lpszClassName, wc.hInstance);
67+
68+ return 0 ;
69+ }
70+
71+ // Helper functions
72+
73+ bool CreateDeviceD3D (HWND hWnd)
74+ {
75+ // Setup swap chain
76+ DXGI_SWAP_CHAIN_DESC sd;
77+ ZeroMemory (&sd, sizeof (sd));
78+ sd.BufferCount = 2 ;
79+ sd.BufferDesc .Width = 0 ;
80+ sd.BufferDesc .Height = 0 ;
81+ sd.BufferDesc .Format = DXGI_FORMAT_R8G8B8A8_UNORM;
82+ sd.BufferDesc .RefreshRate .Numerator = 60 ;
83+ sd.BufferDesc .RefreshRate .Denominator = 1 ;
84+ sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
85+ sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
86+ sd.OutputWindow = hWnd;
87+ sd.SampleDesc .Count = 1 ;
88+ sd.SampleDesc .Quality = 0 ;
89+ sd.Windowed = TRUE ;
90+ sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
91+
92+ UINT createDeviceFlags = 0 ;
93+ // createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
94+ D3D_FEATURE_LEVEL featureLevel;
95+ const D3D_FEATURE_LEVEL featureLevelArray[2 ] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
96+ if (D3D11CreateDeviceAndSwapChain (NULL , D3D_DRIVER_TYPE_HARDWARE, NULL , createDeviceFlags, featureLevelArray, 2 , D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
97+ return false ;
98+
99+ CreateRenderTarget ();
100+ return true ;
101+ }
102+
103+ void CleanupDeviceD3D ()
104+ {
105+ CleanupRenderTarget ();
106+ if (g_pSwapChain) { g_pSwapChain->Release (); g_pSwapChain = NULL ; }
107+ if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release (); g_pd3dDeviceContext = NULL ; }
108+ if (g_pd3dDevice) { g_pd3dDevice->Release (); g_pd3dDevice = NULL ; }
109+ }
110+
111+ void CreateRenderTarget ()
112+ {
113+ ID3D11Texture2D* pBackBuffer;
114+ g_pSwapChain->GetBuffer (0 , IID_PPV_ARGS (&pBackBuffer));
115+ g_pd3dDevice->CreateRenderTargetView (pBackBuffer, NULL , &g_mainRenderTargetView);
116+ pBackBuffer->Release ();
117+ }
118+
119+ void CleanupRenderTarget ()
120+ {
121+ if (g_mainRenderTargetView) { g_mainRenderTargetView->Release (); g_mainRenderTargetView = NULL ; }
122+ }
123+
124+ // Win32 message handler
125+ LRESULT WINAPI WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
126+ {
127+ switch (msg)
128+ {
129+ case WM_SIZE:
130+ if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
131+ {
132+ CleanupRenderTarget ();
133+ g_pSwapChain->ResizeBuffers (0 , (UINT)LOWORD (lParam), (UINT)HIWORD (lParam), DXGI_FORMAT_UNKNOWN, 0 );
134+ CreateRenderTarget ();
135+ }
136+ return 0 ;
137+ case WM_SYSCOMMAND:
138+ if ((wParam & 0xfff0 ) == SC_KEYMENU) // Disable ALT application menu
139+ return 0 ;
140+ break ;
141+ case WM_DESTROY:
142+ ::PostQuitMessage (0 );
143+ return 0 ;
144+ }
145+ return ::DefWindowProc (hWnd, msg, wParam, lParam);
146+ }
0 commit comments