-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
306 lines (251 loc) · 10.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Core.h>
#include <winrt/Windows.Graphics.Display.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.UI.Input.h>
#include <imgui.h>
#include <imgui_impl_uwp.h>
#include <imgui_impl_dx11.h>
#include <d3d11.h>
#include <dxgi1_4.h>
using namespace winrt;
using namespace Windows;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Foundation::Numerics;
using namespace Windows::Graphics::Display;
using namespace Windows::UI;
using namespace Windows::UI::Core;
struct App : implements<App, IFrameworkViewSource, IFrameworkView>
{
bool m_windowClosed = false;
bool m_windowVisible = true;
com_ptr<ID3D11Device> m_pd3dDevice;
com_ptr<ID3D11DeviceContext> m_pd3dDeviceContext;
com_ptr<IDXGISwapChain1> m_pSwapChain;
com_ptr<ID3D11RenderTargetView> m_mainRenderTargetView;
UINT m_ResizeWidth = 0, m_ResizeHeight = 0;
Windows::Foundation::Size m_OldSize;
IFrameworkView CreateView()
{
return *this;
}
void Initialize(CoreApplicationView const &)
{
}
void Load(hstring const&)
{
}
void Uninitialize()
{
}
void Run()
{
CoreWindow window = CoreWindow::GetForCurrentThread();
CoreDispatcher dispatcher = window.Dispatcher();
window.Activate();
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
// High DPI scaling
DisplayInformation currentDisplayInformation = DisplayInformation::GetForCurrentView();
float dpi = currentDisplayInformation.LogicalDpi() / 96.0f;
io.DisplayFramebufferScale = { dpi, dpi }; // TODO: Handle DPI change
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplUwp_InitForCurrentView();
ImGui_ImplDX11_Init(m_pd3dDevice.get(), m_pd3dDeviceContext.get());
// Our state
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
while (!m_windowClosed)
{
if (m_windowVisible)
{
dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
// Handle window resize (we don't resize directly in the SizeChanged handler)
if (m_ResizeWidth != 0 && m_ResizeHeight != 0)
{
CleanupRenderTarget();
HRESULT hr = m_pSwapChain->ResizeBuffers(2, lround(m_ResizeWidth * io.DisplayFramebufferScale.x), lround(m_ResizeHeight * io.DisplayFramebufferScale.y), DXGI_FORMAT_UNKNOWN, 0);
m_ResizeWidth = m_ResizeHeight = 0;
CreateRenderTarget();
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplUwp_NewFrame();
ImGui::NewFrame();
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
// 3. Show another simple window.
if (show_another_window)
{
ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me"))
show_another_window = false;
ImGui::End();
}
// Rendering
ImGui::Render();
auto renderTargetView = m_mainRenderTargetView.get();
const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
m_pd3dDeviceContext->OMSetRenderTargets(1, &renderTargetView, nullptr);
m_pd3dDeviceContext->ClearRenderTargetView(renderTargetView, clear_color_with_alpha);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
m_pSwapChain->Present(1, 0);
}
else
{
dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
}
}
// Cleanup
ImGui_ImplDX11_Shutdown();
ImGui_ImplUwp_Shutdown();
ImGui::DestroyContext();
}
void SetWindow(CoreWindow const & window)
{
window.Closed({ this, &App::OnWindowClosed });
window.SizeChanged({ this, &App::OnSizeChanged });
window.VisibilityChanged({ this, &App::OnVisibiltyChanged });
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_12_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
D3D_FEATURE_LEVEL featureLevel;
HRESULT hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
0,
0,
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION,
m_pd3dDevice.put(),
&featureLevel,
m_pd3dDeviceContext.put()
);
if (FAILED(hr))
{
winrt::check_hresult(
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_WARP,
0,
0,
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION,
m_pd3dDevice.put(),
&featureLevel,
m_pd3dDeviceContext.put()
)
);
}
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = { 0 };
swapChainDesc.Width = 0;
swapChainDesc.Height = 0;
swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.Stereo = false;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 2;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
// This sequence obtains the DXGI factory that was used to create the Direct3D device above.
com_ptr<IDXGIDevice3> dxgiDevice;
m_pd3dDevice.as(dxgiDevice);
com_ptr<IDXGIAdapter> dxgiAdapter;
winrt::check_hresult(
dxgiDevice->GetAdapter(dxgiAdapter.put())
);
com_ptr<IDXGIFactory4> dxgiFactory;
winrt::check_hresult(
dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory))
);
winrt::check_hresult(
dxgiFactory->CreateSwapChainForCoreWindow(
m_pd3dDevice.get(),
winrt::get_unknown(window),
&swapChainDesc,
nullptr,
m_pSwapChain.put()
)
);
winrt::check_hresult(
dxgiDevice->SetMaximumFrameLatency(1)
);
CreateRenderTarget();
}
void OnSizeChanged(IInspectable const &, WindowSizeChangedEventArgs const & args)
{
auto size = args.Size();
// Prevent unnecessary resize
if (size != m_OldSize)
{
m_ResizeWidth = size.Width;
m_ResizeHeight = size.Height;
}
m_OldSize = size;
}
void OnVisibiltyChanged(IInspectable const&, VisibilityChangedEventArgs const& args)
{
m_windowVisible = args.Visible();
}
void OnWindowClosed(IInspectable const&, CoreWindowEventArgs const& args)
{
m_windowClosed = true;
}
void CreateRenderTarget()
{
com_ptr<ID3D11Texture2D> pBackBuffer;
m_pSwapChain->GetBuffer(0, IID_PPV_ARGS(pBackBuffer.put()));
m_pd3dDevice->CreateRenderTargetView(pBackBuffer.get(), nullptr, m_mainRenderTargetView.put());
}
void CleanupRenderTarget()
{
m_mainRenderTargetView = nullptr;
}
};
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
CoreApplication::Run(make<App>());
}