1 // dear imgui: standalone example application for DirectX 9
2 // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
3 
4 #include "imgui.h"
5 #include "imgui_impl_dx9.h"
6 #include "imgui_impl_win32.h"
7 #include <d3d9.h>
8 #define DIRECTINPUT_VERSION 0x0800
9 #include <dinput.h>
10 #include <tchar.h>
11 
12 // Data
13 static LPDIRECT3DDEVICE9        g_pd3dDevice = NULL;
14 static D3DPRESENT_PARAMETERS    g_d3dpp;
15 
16 extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)17 LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
18 {
19     if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
20         return true;
21 
22     switch (msg)
23     {
24     case WM_SIZE:
25         if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
26         {
27             ImGui_ImplDX9_InvalidateDeviceObjects();
28             g_d3dpp.BackBufferWidth  = LOWORD(lParam);
29             g_d3dpp.BackBufferHeight = HIWORD(lParam);
30             HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp);
31             if (hr == D3DERR_INVALIDCALL)
32                 IM_ASSERT(0);
33             ImGui_ImplDX9_CreateDeviceObjects();
34         }
35         return 0;
36     case WM_SYSCOMMAND:
37         if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
38             return 0;
39         break;
40     case WM_DESTROY:
41         PostQuitMessage(0);
42         return 0;
43     }
44     return DefWindowProc(hWnd, msg, wParam, lParam);
45 }
46 
main(int,char **)47 int main(int, char**)
48 {
49     // Create application window
50     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL };
51     RegisterClassEx(&wc);
52     HWND hwnd = CreateWindow(_T("ImGui Example"), _T("Dear ImGui DirectX9 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
53 
54     // Initialize Direct3D
55     LPDIRECT3D9 pD3D;
56     if ((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
57     {
58         UnregisterClass(_T("ImGui Example"), wc.hInstance);
59         return 0;
60     }
61     ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
62     g_d3dpp.Windowed = TRUE;
63     g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
64     g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
65     g_d3dpp.EnableAutoDepthStencil = TRUE;
66     g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
67     g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync
68     //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate
69 
70     // Create the D3DDevice
71     if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
72     {
73         pD3D->Release();
74         UnregisterClass(_T("ImGui Example"), wc.hInstance);
75         return 0;
76     }
77 
78     // Setup Dear ImGui binding
79     IMGUI_CHECKVERSION();
80     ImGui::CreateContext();
81     ImGuiIO& io = ImGui::GetIO(); (void)io;
82     //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;  // Enable Keyboard Controls
83     ImGui_ImplWin32_Init(hwnd);
84     ImGui_ImplDX9_Init(g_pd3dDevice);
85 
86     // Setup style
87     ImGui::StyleColorsDark();
88     //ImGui::StyleColorsClassic();
89 
90     // Load Fonts
91     // - 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.
92     // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
93     // - 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).
94     // - 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.
95     // - Read 'misc/fonts/README.txt' for more instructions and details.
96     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
97     //io.Fonts->AddFontDefault();
98     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
99     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
100     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
101     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
102     //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
103     //IM_ASSERT(font != NULL);
104 
105     bool show_demo_window = true;
106     bool show_another_window = false;
107     ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
108 
109     // Main loop
110     MSG msg;
111     ZeroMemory(&msg, sizeof(msg));
112     ShowWindow(hwnd, SW_SHOWDEFAULT);
113     UpdateWindow(hwnd);
114     while (msg.message != WM_QUIT)
115     {
116         // Poll and handle messages (inputs, window resize, etc.)
117         // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
118         // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
119         // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
120         // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
121         if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
122         {
123             TranslateMessage(&msg);
124             DispatchMessage(&msg);
125             continue;
126         }
127 
128         // Start the Dear ImGui frame
129         ImGui_ImplDX9_NewFrame();
130         ImGui_ImplWin32_NewFrame();
131         ImGui::NewFrame();
132 
133         // 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!).
134         if (show_demo_window)
135             ImGui::ShowDemoWindow(&show_demo_window);
136 
137         // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
138         {
139             static float f = 0.0f;
140             static int counter = 0;
141 
142             ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.
143 
144             ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
145             ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
146             ImGui::Checkbox("Another Window", &show_another_window);
147 
148             ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
149             ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
150 
151             if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
152                 counter++;
153             ImGui::SameLine();
154             ImGui::Text("counter = %d", counter);
155 
156             ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
157             ImGui::End();
158         }
159 
160         // 3. Show another simple window.
161         if (show_another_window)
162         {
163             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)
164             ImGui::Text("Hello from another window!");
165             if (ImGui::Button("Close Me"))
166                 show_another_window = false;
167             ImGui::End();
168         }
169 
170         // Rendering
171         ImGui::EndFrame();
172         g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
173         g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
174         g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
175         D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x*255.0f), (int)(clear_color.y*255.0f), (int)(clear_color.z*255.0f), (int)(clear_color.w*255.0f));
176         g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
177         if (g_pd3dDevice->BeginScene() >= 0)
178         {
179             ImGui::Render();
180             ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
181             g_pd3dDevice->EndScene();
182         }
183         HRESULT result = g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
184 
185         // Handle loss of D3D9 device
186         if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
187         {
188             ImGui_ImplDX9_InvalidateDeviceObjects();
189             g_pd3dDevice->Reset(&g_d3dpp);
190             ImGui_ImplDX9_CreateDeviceObjects();
191         }
192     }
193 
194     ImGui_ImplDX9_Shutdown();
195     ImGui_ImplWin32_Shutdown();
196     ImGui::DestroyContext();
197 
198     if (g_pd3dDevice) g_pd3dDevice->Release();
199     if (pD3D) pD3D->Release();
200     DestroyWindow(hwnd);
201     UnregisterClass(_T("ImGui Example"), wc.hInstance);
202 
203     return 0;
204 }
205