From c9a102ee302f1cb95bf8fe2d999bf2045ba78476 Mon Sep 17 00:00:00 2001 From: Mr_Goldberg Date: Sun, 21 Aug 2022 03:45:11 -0400 Subject: [PATCH] Use shared font atlas and fix crash when opening overlay in some games. --- overlay_experimental/Renderer_Hook.h | 4 ++- overlay_experimental/linux/OpenGLX_Hook.cpp | 2 +- overlay_experimental/steam_overlay.cpp | 28 +++++++++++--------- overlay_experimental/steam_overlay.h | 1 + overlay_experimental/windows/DX10_Hook.cpp | 2 +- overlay_experimental/windows/DX11_Hook.cpp | 2 +- overlay_experimental/windows/DX12_Hook.cpp | 2 +- overlay_experimental/windows/DX9_Hook.cpp | 2 +- overlay_experimental/windows/OpenGL_Hook.cpp | 2 +- 9 files changed, 25 insertions(+), 20 deletions(-) diff --git a/overlay_experimental/Renderer_Hook.h b/overlay_experimental/Renderer_Hook.h index a14b148..78e702b 100644 --- a/overlay_experimental/Renderer_Hook.h +++ b/overlay_experimental/Renderer_Hook.h @@ -39,7 +39,8 @@ class Renderer_Hook public: Renderer_Hook(): OverlayProc(&DefaultOverlayProc), - OverlayHookReady(&DefaultOverlayHookReady) + OverlayHookReady(&DefaultOverlayHookReady), + ImGuiFontAtlas(nullptr) {} static void DefaultOverlayProc() {} @@ -47,6 +48,7 @@ public: std::function OverlayProc; std::function OverlayHookReady; + void *ImGuiFontAtlas; virtual bool StartHook(std::function key_combination_callback, std::set toggle_keys) = 0; virtual bool IsStarted() = 0; // Returns a Handle to the renderer image ressource or nullptr if it failed to create the resource, the handle can be used in ImGui's Image calls, image_buffer must be RGBA ordered diff --git a/overlay_experimental/linux/OpenGLX_Hook.cpp b/overlay_experimental/linux/OpenGLX_Hook.cpp index c313874..9c9f5fd 100644 --- a/overlay_experimental/linux/OpenGLX_Hook.cpp +++ b/overlay_experimental/linux/OpenGLX_Hook.cpp @@ -84,7 +84,7 @@ void OpenGLX_Hook::_PrepareForOverlay(Display* display, GLXDrawable drawable) { if( !_Initialized ) { - ImGui::CreateContext(); + ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas); ImGui_ImplOpenGL3_Init(); //int attributes[] = { //can't be const b/c X11 doesn't like it. Not sure if that's intentional or just stupid. diff --git a/overlay_experimental/steam_overlay.cpp b/overlay_experimental/steam_overlay.cpp index 0f04e84..db166d0 100644 --- a/overlay_experimental/steam_overlay.cpp +++ b/overlay_experimental/steam_overlay.cpp @@ -128,7 +128,8 @@ Steam_Overlay::Steam_Overlay(Settings* settings, SteamCallResults* callback_resu i_have_lobby(false), show_achievements(false), show_settings(false), - _renderer(nullptr) + _renderer(nullptr), + fonts_atlas(nullptr) { strncpy(username_text, settings->get_local_name(), sizeof(username_text)); @@ -709,11 +710,10 @@ void Steam_Overlay::BuildNotifications(int width, int height) void Steam_Overlay::CreateFonts() { - //TODO: remove from dx_whatever hook - if(ImGui::GetCurrentContext() == nullptr) - ImGui::CreateContext(); + if (fonts_atlas) return; + + ImFontAtlas *Fonts = new ImFontAtlas(); - ImGuiIO& io = ImGui::GetIO(); ImFontConfig fontcfg; float font_size = 16.0; @@ -727,7 +727,7 @@ void Steam_Overlay::CreateFonts() font_builder.AddText(x.description.c_str()); } - font_builder.AddRanges(io.Fonts->GetGlyphRangesDefault()); + font_builder.AddRanges(Fonts->GetGlyphRangesDefault()); ImVector ranges; font_builder.BuildRanges(&ranges); @@ -744,11 +744,11 @@ void Steam_Overlay::CreateFonts() ImFont *font = NULL; #if defined(__WINDOWS__) - font = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\micross.ttf", font_size, &fontcfg); + font = Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\micross.ttf", font_size, &fontcfg); #endif if (!font) { - font = io.Fonts->AddFontDefault(&fontcfg); + font = Fonts->AddFontDefault(&fontcfg); } font_notif = font_default = font; @@ -757,15 +757,16 @@ void Steam_Overlay::CreateFonts() PRINT_DEBUG("loading extra fonts\n"); fontcfg.MergeMode = true; #if defined(__WINDOWS__) - io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\simsun.ttc", font_size, &fontcfg); - io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\malgun.ttf", font_size, &fontcfg); + Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\simsun.ttc", font_size, &fontcfg); + Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\malgun.ttf", font_size, &fontcfg); #endif } - io.Fonts->Build(); + Fonts->Build(); + fonts_atlas = (void *)Fonts; - ImGuiStyle& style = ImGui::GetStyle(); - style.WindowRounding = 0.0; // Disable round window + // ImGuiStyle& style = ImGui::GetStyle(); + // style.WindowRounding = 0.0; // Disable round window reset_LastError(); } @@ -1048,6 +1049,7 @@ void Steam_Overlay::RunCallbacks() auto callback = std::bind(&Steam_Overlay::OpenOverlayHook, this, std::placeholders::_1); PRINT_DEBUG("start renderer\n", _renderer); std::set keys = {ingame_overlay::ToggleKey::SHIFT, ingame_overlay::ToggleKey::TAB}; + _renderer->ImGuiFontAtlas = fonts_atlas; bool started = _renderer->StartHook(callback, keys); PRINT_DEBUG("tried to start renderer %u\n", started); } diff --git a/overlay_experimental/steam_overlay.h b/overlay_experimental/steam_overlay.h index 20c7efb..ca33e04 100644 --- a/overlay_experimental/steam_overlay.h +++ b/overlay_experimental/steam_overlay.h @@ -102,6 +102,7 @@ class Steam_Overlay std::string show_url; std::vector achievements; bool show_achievements, show_settings; + void *fonts_atlas; bool disable_forced, local_save, warning_forced; uint32_t appid; diff --git a/overlay_experimental/windows/DX10_Hook.cpp b/overlay_experimental/windows/DX10_Hook.cpp index 9abefe2..14e8ec9 100644 --- a/overlay_experimental/windows/DX10_Hook.cpp +++ b/overlay_experimental/windows/DX10_Hook.cpp @@ -115,7 +115,7 @@ void DX10_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain) pDevice->CreateRenderTargetView(pBackBuffer, nullptr, &mainRenderTargetView); pBackBuffer->Release(); - ImGui::CreateContext(); + ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas); ImGui_ImplDX10_Init(pDevice); Windows_Hook::Inst()->SetInitialWindowSize(desc.OutputWindow); diff --git a/overlay_experimental/windows/DX11_Hook.cpp b/overlay_experimental/windows/DX11_Hook.cpp index 6f5fbec..14111fe 100644 --- a/overlay_experimental/windows/DX11_Hook.cpp +++ b/overlay_experimental/windows/DX11_Hook.cpp @@ -147,7 +147,7 @@ void DX11_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain) return; if(ImGui::GetCurrentContext() == nullptr) - ImGui::CreateContext(); + ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas); ImGui_ImplDX11_Init(pDevice, pContext); diff --git a/overlay_experimental/windows/DX12_Hook.cpp b/overlay_experimental/windows/DX12_Hook.cpp index b3819a1..5f2d85c 100644 --- a/overlay_experimental/windows/DX12_Hook.cpp +++ b/overlay_experimental/windows/DX12_Hook.cpp @@ -264,7 +264,7 @@ void DX12_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain, ID3D12CommandQueu //auto heaps = std::move(get_free_texture_heap()); - ImGui::CreateContext(); + ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas); ImGui_ImplDX12_Init(pDevice, bufferCount, DXGI_FORMAT_R8G8B8A8_UNORM, pSrvDescHeap, pSrvDescHeap->GetCPUDescriptorHandleForHeapStart(), pSrvDescHeap->GetGPUDescriptorHandleForHeapStart()); diff --git a/overlay_experimental/windows/DX9_Hook.cpp b/overlay_experimental/windows/DX9_Hook.cpp index 90fb470..c3d00fb 100644 --- a/overlay_experimental/windows/DX9_Hook.cpp +++ b/overlay_experimental/windows/DX9_Hook.cpp @@ -133,7 +133,7 @@ void DX9_Hook::_PrepareForOverlay(IDirect3DDevice9 *pDevice, HWND destWindow) pDevice->AddRef(); _pDevice = pDevice; - ImGui::CreateContext(); + ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas); ImGui_ImplDX9_Init(pDevice); _LastWindow = destWindow; diff --git a/overlay_experimental/windows/OpenGL_Hook.cpp b/overlay_experimental/windows/OpenGL_Hook.cpp index 6a258b5..e88de9d 100644 --- a/overlay_experimental/windows/OpenGL_Hook.cpp +++ b/overlay_experimental/windows/OpenGL_Hook.cpp @@ -86,7 +86,7 @@ void OpenGL_Hook::_PrepareForOverlay(HDC hDC) if (!_Initialized) { - ImGui::CreateContext(); + ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas); ImGui_ImplOpenGL3_Init(); _LastWindow = hWnd;