@ -29,8 +29,10 @@ void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data);
void ResetDeviceWGL ( ) ;
void ResetDeviceWGL ( ) ;
LRESULT WINAPI WndProc ( HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam ) ;
LRESULT WINAPI WndProc ( HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam ) ;
// Support function for multi-viewports
static void Win32_CreateWindow ( ImGuiViewport * viewport )
// Unlike most other backend combination, we need specific hooks to combine Win32+OpenGL.
// We could in theory decide to support Win32-specific code in OpenGL backend via e.g. an hypothetical ImGui_ImplOpenGL3_InitForRawWin32().
static void Hook_Renderer_CreateWindow ( ImGuiViewport * viewport )
{
{
assert ( viewport - > RendererUserData = = NULL ) ;
assert ( viewport - > RendererUserData = = NULL ) ;
@ -39,7 +41,7 @@ static void Win32_CreateWindow(ImGuiViewport* viewport)
viewport - > RendererUserData = data ;
viewport - > RendererUserData = data ;
}
}
static void Win32 _DestroyWindow( ImGuiViewport * viewport )
static void Hook_Renderer _DestroyWindow( ImGuiViewport * viewport )
{
{
if ( viewport - > RendererUserData ! = NULL )
if ( viewport - > RendererUserData ! = NULL )
{
{
@ -50,25 +52,17 @@ static void Win32_DestroyWindow(ImGuiViewport* viewport)
}
}
}
}
static void Win32 _RenderWindow( ImGuiViewport * viewport , void * )
static void Hook_Platform _RenderWindow( ImGuiViewport * viewport , void * )
{
{
WGL_WindowData * data = ( WGL_WindowData * ) viewport - > RendererUserData ;
if ( data )
{
// Activate the platform window DC in the OpenGL rendering context
// Activate the platform window DC in the OpenGL rendering context
if ( WGL_WindowData * data = ( WGL_WindowData * ) viewport - > RendererUserData )
wglMakeCurrent ( data - > hDC , g_hRC ) ;
wglMakeCurrent ( data - > hDC , g_hRC ) ;
}
}
}
static void Win32 _SwapBuffers( ImGuiViewport * viewport , void * )
static void Hook_Renderer _SwapBuffers( ImGuiViewport * viewport , void * )
{
{
WGL_WindowData * data = ( WGL_WindowData * ) viewport - > RendererUserData ;
if ( WGL_WindowData * data = ( WGL_WindowData * ) viewport - > RendererUserData )
: : SwapBuffers ( data - > hDC ) ;
if ( data )
{
SwapBuffers ( data - > hDC ) ;
}
}
}
// Main code
// Main code
@ -119,22 +113,20 @@ int main(int, char**)
ImGui_ImplWin32_InitForOpenGL ( hwnd ) ;
ImGui_ImplWin32_InitForOpenGL ( hwnd ) ;
ImGui_ImplOpenGL3_Init ( ) ;
ImGui_ImplOpenGL3_Init ( ) ;
// Win32+GL needs specific hooks for viewport, as there are specific things needed to tie Win32 and GL api.
if ( io . ConfigFlags & ImGuiConfigFlags_ViewportsEnable )
if ( io . ConfigFlags & ImGuiConfigFlags_ViewportsEnable )
{
{
ImGuiPlatformIO & platform_io = ImGui : : GetPlatformIO ( ) ;
ImGuiPlatformIO & platform_io = ImGui : : GetPlatformIO ( ) ;
IM_ASSERT ( platform_io . Renderer_CreateWindow = = NULL ) ;
// Store the hdc for this new window
IM_ASSERT ( platform_io . Renderer_DestroyWindow = = NULL ) ;
assert ( platform_io . Renderer_CreateWindow = = NULL ) ;
IM_ASSERT ( platform_io . Renderer_SwapBuffers = = NULL ) ;
platform_io . Renderer_CreateWindow = Win32_CreateWindow ;
IM_ASSERT ( platform_io . Platform_RenderWindow = = NULL ) ;
assert ( platform_io . Renderer_DestroyWindow = = NULL ) ;
platform_io . Renderer_CreateWindow = Hook_Renderer_CreateWindow ;
platform_io . Renderer_DestroyWindow = Win32_DestroyWindow ;
platform_io . Renderer_DestroyWindow = Hook_Renderer_DestroyWindow ;
assert ( platform_io . Renderer_SwapBuffers = = NULL ) ;
platform_io . Renderer_SwapBuffers = Hook_Renderer_SwapBuffers ;
platform_io . Renderer_SwapBuffers = Win32_SwapBuffers ;
platform_io . Platform_RenderWindow = Hook_Platform_RenderWindow ;
// We need to activate the context before drawing
assert ( platform_io . Platform_RenderWindow = = NULL ) ;
platform_io . Platform_RenderWindow = Win32_RenderWindow ;
}
}
// Load Fonts
// Load Fonts
// - 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.
// - 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.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
@ -227,6 +219,7 @@ int main(int, char**)
{
{
ImGui : : UpdatePlatformWindows ( ) ;
ImGui : : UpdatePlatformWindows ( ) ;
ImGui : : RenderPlatformWindowsDefault ( ) ;
ImGui : : RenderPlatformWindowsDefault ( ) ;
// Restore the OpenGL rendering context to the main window DC, since platform windows might have changed it.
// Restore the OpenGL rendering context to the main window DC, since platform windows might have changed it.
wglMakeCurrent ( g_MainWindow . hDC , g_hRC ) ;
wglMakeCurrent ( g_MainWindow . hDC , g_hRC ) ;
}
}