1// Dear ImGui: standalone example application for Win32 + OpenGL 3
2
3// Learn about Dear ImGui:
4// - FAQ https://dearimgui.com/faq
5// - Getting Started https://dearimgui.com/getting-started
6// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
7// - Introduction, links and more at the top of imgui.cpp
8
9// This is provided for completeness, however it is strongly recommended you use OpenGL with SDL or GLFW.
10
11#include "imgui.h"
12#include "imgui_impl_opengl3.h"
13#include "imgui_impl_win32.h"
14#ifndef WIN32_LEAN_AND_MEAN
15#define WIN32_LEAN_AND_MEAN
16#endif
17#include <windows.h>
18#include <GL/GL.h>
19#include <tchar.h>
20
21// Data stored per platform window
22struct WGL_WindowData { HDC hDC; };
23
24// Data
25static HGLRC g_hRC;
26static WGL_WindowData g_MainWindow;
27static int g_Width;
28static int g_Height;
29
30// Forward declarations of helper functions
31bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data);
32void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data);
33void ResetDeviceWGL();
34LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
35
36// Support function for multi-viewports
37// Unlike most other backend combination, we need specific hooks to combine Win32+OpenGL.
38// We could in theory decide to support Win32-specific code in OpenGL backend via e.g. an hypothetical ImGui_ImplOpenGL3_InitForRawWin32().
39static void Hook_Renderer_CreateWindow(ImGuiViewport* viewport)
40{
41 assert(viewport->RendererUserData == NULL);
42
43 WGL_WindowData* data = IM_NEW(WGL_WindowData);
44 CreateDeviceWGL((HWND)viewport->PlatformHandle, data);
45 viewport->RendererUserData = data;
46}
47
48static void Hook_Renderer_DestroyWindow(ImGuiViewport* viewport)
49{
50 if (viewport->RendererUserData != NULL)
51 {
52 WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData;
53 CleanupDeviceWGL((HWND)viewport->PlatformHandle, data);
54 IM_DELETE(p: data);
55 viewport->RendererUserData = NULL;
56 }
57}
58
59static void Hook_Platform_RenderWindow(ImGuiViewport* viewport, void*)
60{
61 // Activate the platform window DC in the OpenGL rendering context
62 if (WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData)
63 wglMakeCurrent(data->hDC, g_hRC);
64}
65
66static void Hook_Renderer_SwapBuffers(ImGuiViewport* viewport, void*)
67{
68 if (WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData)
69 ::SwapBuffers(data->hDC);
70}
71
72// Main code
73int main(int, char**)
74{
75 // Create application window
76 //ImGui_ImplWin32_EnableDpiAwareness();
77 WNDCLASSEXW wc = { sizeof(wc), CS_OWNDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
78 ::RegisterClassExW(&wc);
79 HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+OpenGL3 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
80
81 // Initialize OpenGL
82 if (!CreateDeviceWGL(hwnd, &g_MainWindow))
83 {
84 CleanupDeviceWGL(hwnd, &g_MainWindow);
85 ::DestroyWindow(hwnd);
86 ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
87 return 1;
88 }
89 wglMakeCurrent(g_MainWindow.hDC, g_hRC);
90
91 // Show the window
92 ::ShowWindow(hwnd, SW_SHOWDEFAULT);
93 ::UpdateWindow(hwnd);
94
95 // Setup Dear ImGui context
96 IMGUI_CHECKVERSION();
97 ImGui::CreateContext();
98 ImGuiIO& io = ImGui::GetIO(); (void)io;
99 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
100 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
101 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
102 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
103
104 // Setup Dear ImGui style
105 ImGui::StyleColorsDark();
106 //ImGui::StyleColorsClassic();
107
108 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
109 ImGuiStyle& style = ImGui::GetStyle();
110 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
111 {
112 style.WindowRounding = 0.0f;
113 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
114 }
115
116 // Setup Platform/Renderer backends
117 ImGui_ImplWin32_InitForOpenGL(hwnd);
118 ImGui_ImplOpenGL3_Init();
119
120 // Win32+GL needs specific hooks for viewport, as there are specific things needed to tie Win32 and GL api.
121 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
122 {
123 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
124 IM_ASSERT(platform_io.Renderer_CreateWindow == NULL);
125 IM_ASSERT(platform_io.Renderer_DestroyWindow == NULL);
126 IM_ASSERT(platform_io.Renderer_SwapBuffers == NULL);
127 IM_ASSERT(platform_io.Platform_RenderWindow == NULL);
128 platform_io.Renderer_CreateWindow = Hook_Renderer_CreateWindow;
129 platform_io.Renderer_DestroyWindow = Hook_Renderer_DestroyWindow;
130 platform_io.Renderer_SwapBuffers = Hook_Renderer_SwapBuffers;
131 platform_io.Platform_RenderWindow = Hook_Platform_RenderWindow;
132 }
133
134 // Load Fonts
135 // - 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.
136 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
137 // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
138 // - 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.
139 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
140 // - Read 'docs/FONTS.md' for more instructions and details.
141 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
142 //io.Fonts->AddFontDefault();
143 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
144 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
145 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
146 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
147 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
148 //IM_ASSERT(font != nullptr);
149
150 // Our state
151 bool show_demo_window = true;
152 bool show_another_window = false;
153 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
154
155 // Main loop
156 bool done = false;
157 while (!done)
158 {
159 // Poll and handle messages (inputs, window resize, etc.)
160 // See the WndProc() function below for our to dispatch events to the Win32 backend.
161 MSG msg;
162 while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
163 {
164 ::TranslateMessage(&msg);
165 ::DispatchMessage(&msg);
166 if (msg.message == WM_QUIT)
167 done = true;
168 }
169 if (done)
170 break;
171
172 // Start the Dear ImGui frame
173 ImGui_ImplOpenGL3_NewFrame();
174 ImGui_ImplWin32_NewFrame();
175 ImGui::NewFrame();
176
177 // 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!).
178 if (show_demo_window)
179 ImGui::ShowDemoWindow(p_open: &show_demo_window);
180
181 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
182 {
183 static float f = 0.0f;
184 static int counter = 0;
185
186 ImGui::Begin(name: "Hello, world!"); // Create a window called "Hello, world!" and append into it.
187
188 ImGui::Text(fmt: "This is some useful text."); // Display some text (you can use a format strings too)
189 ImGui::Checkbox(label: "Demo Window", v: &show_demo_window); // Edit bools storing our window open/close state
190 ImGui::Checkbox(label: "Another Window", v: &show_another_window);
191
192 ImGui::SliderFloat(label: "float", v: &f, v_min: 0.0f, v_max: 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
193 ImGui::ColorEdit3(label: "clear color", col: (float*)&clear_color); // Edit 3 floats representing a color
194
195 if (ImGui::Button(label: "Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
196 counter++;
197 ImGui::SameLine();
198 ImGui::Text(fmt: "counter = %d", counter);
199
200 ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
201 ImGui::End();
202 }
203
204 // 3. Show another simple window.
205 if (show_another_window)
206 {
207 ImGui::Begin(name: "Another Window", p_open: &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
208 ImGui::Text(fmt: "Hello from another window!");
209 if (ImGui::Button(label: "Close Me"))
210 show_another_window = false;
211 ImGui::End();
212 }
213
214 // Rendering
215 ImGui::Render();
216 glViewport(0, 0, g_Width, g_Height);
217 glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
218 glClear(GL_COLOR_BUFFER_BIT);
219 ImGui_ImplOpenGL3_RenderDrawData(draw_data: ImGui::GetDrawData());
220
221 // Update and Render additional Platform Windows
222 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
223 {
224 ImGui::UpdatePlatformWindows();
225 ImGui::RenderPlatformWindowsDefault();
226
227 // Restore the OpenGL rendering context to the main window DC, since platform windows might have changed it.
228 wglMakeCurrent(g_MainWindow.hDC, g_hRC);
229 }
230
231 // Present
232 ::SwapBuffers(g_MainWindow.hDC);
233 }
234
235 ImGui_ImplOpenGL3_Shutdown();
236 ImGui_ImplWin32_Shutdown();
237 ImGui::DestroyContext();
238
239 CleanupDeviceWGL(hwnd, &g_MainWindow);
240 wglDeleteContext(g_hRC);
241 ::DestroyWindow(hwnd);
242 ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
243
244 return 0;
245}
246
247// Helper functions
248bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data)
249{
250 HDC hDc = ::GetDC(hWnd);
251 PIXELFORMATDESCRIPTOR pfd = { 0 };
252 pfd.nSize = sizeof(pfd);
253 pfd.nVersion = 1;
254 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
255 pfd.iPixelType = PFD_TYPE_RGBA;
256 pfd.cColorBits = 32;
257
258 const int pf = ::ChoosePixelFormat(hDc, &pfd);
259 if (pf == 0)
260 return false;
261 if (::SetPixelFormat(hDc, pf, &pfd) == FALSE)
262 return false;
263 ::ReleaseDC(hWnd, hDc);
264
265 data->hDC = ::GetDC(hWnd);
266 if (!g_hRC)
267 g_hRC = wglCreateContext(data->hDC);
268 return true;
269}
270
271void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data)
272{
273 wglMakeCurrent(nullptr, nullptr);
274 ::ReleaseDC(hWnd, data->hDC);
275}
276
277// Forward declare message handler from imgui_impl_win32.cpp
278extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
279
280// Win32 message handler
281// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
282// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
283// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
284// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
285LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
286{
287 if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
288 return true;
289
290 switch (msg)
291 {
292 case WM_SIZE:
293 if (wParam != SIZE_MINIMIZED)
294 {
295 g_Width = LOWORD(lParam);
296 g_Height = HIWORD(lParam);
297 }
298 return 0;
299 case WM_SYSCOMMAND:
300 if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
301 return 0;
302 break;
303 case WM_DESTROY:
304 ::PostQuitMessage(0);
305 return 0;
306 }
307 return ::DefWindowProcW(hWnd, msg, wParam, lParam);
308}
309

source code of imgui/examples/example_win32_opengl3/main.cpp