1 // Dear ImGui: standalone example application for DirectX 10
2 // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
3 // Read online: https://github.com/ocornut/imgui/tree/master/docs
4
5 #include "imgui.h"
6 #include "imgui_impl_win32.h"
7 #include "imgui_impl_dx10.h"
8 #include <d3d10_1.h>
9 #include <d3d10.h>
10 #include <tchar.h>
11
12 // Data
13 static ID3D10Device* g_pd3dDevice = NULL;
14 static IDXGISwapChain* g_pSwapChain = NULL;
15 static ID3D10RenderTargetView* g_mainRenderTargetView = NULL;
16
17 // Forward declarations of helper functions
18 bool CreateDeviceD3D(HWND hWnd);
19 void CleanupDeviceD3D();
20 void CreateRenderTarget();
21 void CleanupRenderTarget();
22 LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
23
24 // Main code
main(int,char **)25 int main(int, char**)
26 {
27 // Create application window
28 //ImGui_ImplWin32_EnableDpiAwareness();
29 WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL };
30 ::RegisterClassEx(&wc);
31 HWND hwnd = ::CreateWindow(wc.lpszClassName, _T("Dear ImGui DirectX10 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
32
33 // Initialize Direct3D
34 if (!CreateDeviceD3D(hwnd))
35 {
36 CleanupDeviceD3D();
37 ::UnregisterClass(wc.lpszClassName, wc.hInstance);
38 return 1;
39 }
40
41 // Show the window
42 ::ShowWindow(hwnd, SW_SHOWDEFAULT);
43 ::UpdateWindow(hwnd);
44
45 // Setup Dear ImGui context
46 IMGUI_CHECKVERSION();
47 ImGui::CreateContext();
48 ImGuiIO& io = ImGui::GetIO(); (void)io;
49 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
50 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
51 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
52 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
53 //io.ConfigViewportsNoAutoMerge = true;
54 //io.ConfigViewportsNoTaskBarIcon = true;
55
56 // Setup Dear ImGui style
57 ImGui::StyleColorsDark();
58 //ImGui::StyleColorsClassic();
59
60 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
61 ImGuiStyle& style = ImGui::GetStyle();
62 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
63 {
64 style.WindowRounding = 0.0f;
65 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
66 }
67
68 // Setup Platform/Renderer backends
69 ImGui_ImplWin32_Init(hwnd);
70 ImGui_ImplDX10_Init(g_pd3dDevice);
71
72 // Load Fonts
73 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
74 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
75 // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
76 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
77 // - Read 'docs/FONTS.md' for more instructions and details.
78 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
79 //io.Fonts->AddFontDefault();
80 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
81 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
82 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
83 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
84 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
85 //IM_ASSERT(font != NULL);
86
87 // Our state
88 bool show_demo_window = true;
89 bool show_another_window = false;
90 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
91
92 // Main loop
93 bool done = false;
94 while (!done)
95 {
96 // Poll and handle messages (inputs, window resize, etc.)
97 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
98 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
99 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
100 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
101 MSG msg;
102 while (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
103 {
104 ::TranslateMessage(&msg);
105 ::DispatchMessage(&msg);
106 if (msg.message == WM_QUIT)
107 done = true;
108 }
109 if (done)
110 break;
111
112 // Start the Dear ImGui frame
113 ImGui_ImplDX10_NewFrame();
114 ImGui_ImplWin32_NewFrame();
115 ImGui::NewFrame();
116
117 // 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!).
118 if (show_demo_window)
119 ImGui::ShowDemoWindow(&show_demo_window);
120
121 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
122 {
123 static float f = 0.0f;
124 static int counter = 0;
125
126 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
127
128 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
129 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
130 ImGui::Checkbox("Another Window", &show_another_window);
131
132 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
133 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
134
135 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
136 counter++;
137 ImGui::SameLine();
138 ImGui::Text("counter = %d", counter);
139
140 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
141 ImGui::End();
142 }
143
144 // 3. Show another simple window.
145 if (show_another_window)
146 {
147 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)
148 ImGui::Text("Hello from another window!");
149 if (ImGui::Button("Close Me"))
150 show_another_window = false;
151 ImGui::End();
152 }
153
154 // Rendering
155 ImGui::Render();
156 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 };
157 g_pd3dDevice->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
158 g_pd3dDevice->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
159 ImGui_ImplDX10_RenderDrawData(ImGui::GetDrawData());
160
161 // Update and Render additional Platform Windows
162 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
163 {
164 ImGui::UpdatePlatformWindows();
165 ImGui::RenderPlatformWindowsDefault();
166 }
167
168 g_pSwapChain->Present(1, 0); // Present with vsync
169 //g_pSwapChain->Present(0, 0); // Present without vsync
170 }
171
172 ImGui_ImplDX10_Shutdown();
173 ImGui_ImplWin32_Shutdown();
174 ImGui::DestroyContext();
175
176 CleanupDeviceD3D();
177 ::DestroyWindow(hwnd);
178 ::UnregisterClass(wc.lpszClassName, wc.hInstance);
179
180 return 0;
181 }
182
183 // Helper functions
184
CreateDeviceD3D(HWND hWnd)185 bool CreateDeviceD3D(HWND hWnd)
186 {
187 // Setup swap chain
188 DXGI_SWAP_CHAIN_DESC sd;
189 ZeroMemory(&sd, sizeof(sd));
190 sd.BufferCount = 2;
191 sd.BufferDesc.Width = 0;
192 sd.BufferDesc.Height = 0;
193 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
194 sd.BufferDesc.RefreshRate.Numerator = 60;
195 sd.BufferDesc.RefreshRate.Denominator = 1;
196 sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
197 sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
198 sd.OutputWindow = hWnd;
199 sd.SampleDesc.Count = 1;
200 sd.SampleDesc.Quality = 0;
201 sd.Windowed = TRUE;
202 sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
203
204 UINT createDeviceFlags = 0;
205 //createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
206 if (D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, D3D10_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice) != S_OK)
207 return false;
208
209 CreateRenderTarget();
210 return true;
211 }
212
CleanupDeviceD3D()213 void CleanupDeviceD3D()
214 {
215 CleanupRenderTarget();
216 if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
217 if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
218 }
219
CreateRenderTarget()220 void CreateRenderTarget()
221 {
222 ID3D10Texture2D* pBackBuffer;
223 g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
224 g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
225 pBackBuffer->Release();
226 }
227
CleanupRenderTarget()228 void CleanupRenderTarget()
229 {
230 if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
231 }
232
233 // Forward declare message handler from imgui_impl_win32.cpp
234 extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
235
236 // Win32 message handler
WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)237 LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
238 {
239 if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
240 return true;
241
242 switch (msg)
243 {
244 case WM_SIZE:
245 if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
246 {
247 CleanupRenderTarget();
248 g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
249 CreateRenderTarget();
250 }
251 return 0;
252 case WM_SYSCOMMAND:
253 if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
254 return 0;
255 break;
256 case WM_DESTROY:
257 ::PostQuitMessage(0);
258 return 0;
259 }
260 return ::DefWindowProc(hWnd, msg, wParam, lParam);
261 }
262