Use shared font atlas and fix crash when opening overlay in some games.

This commit is contained in:
Mr_Goldberg 2022-08-21 03:45:11 -04:00
parent de6805dd8b
commit c9a102ee30
No known key found for this signature in database
GPG key ID: 8597D87419DEF278
9 changed files with 25 additions and 20 deletions

View file

@ -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<void()> OverlayProc;
std::function<void(bool)> OverlayHookReady;
void *ImGuiFontAtlas;
virtual bool StartHook(std::function<bool(bool)> key_combination_callback, std::set<ToggleKey> 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

View file

@ -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.

View file

@ -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<ImWchar> 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<ingame_overlay::ToggleKey> 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);
}

View file

@ -102,6 +102,7 @@ class Steam_Overlay
std::string show_url;
std::vector<Overlay_Achievement> achievements;
bool show_achievements, show_settings;
void *fonts_atlas;
bool disable_forced, local_save, warning_forced;
uint32_t appid;

View file

@ -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);

View file

@ -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);

View file

@ -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());

View file

@ -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;

View file

@ -86,7 +86,7 @@ void OpenGL_Hook::_PrepareForOverlay(HDC hDC)
if (!_Initialized)
{
ImGui::CreateContext();
ImGui::CreateContext((ImFontAtlas *)ImGuiFontAtlas);
ImGui_ImplOpenGL3_Init();
_LastWindow = hWnd;