diff --git a/src/DevTools.cpp b/src/DevTools.cpp index e33b581..84d0f01 100644 --- a/src/DevTools.cpp +++ b/src/DevTools.cpp @@ -1,4 +1,3 @@ - #include #include "DevTools.hpp" #include "fonts/FeatherIcons.hpp" @@ -189,11 +188,15 @@ void DevTools::setupFonts() { void* font, size_t realSize, float size, const ImWchar* range ) { auto& io = ImGui::GetIO(); + // AddFontFromMemoryTTF assumes ownership of the passed data unless you configure it not to. + // Our font data has static lifetime, so we're handling the ownership. + ImFontConfig config; - config.MergeMode = true; + config.FontDataOwnedByAtlas = false; auto* result = io.Fonts->AddFontFromMemoryTTF( - font, realSize, size, nullptr, range + font, realSize, size, &config, range ); + config.MergeMode = true; io.Fonts->AddFontFromMemoryTTF( Font_FeatherIcons, sizeof(Font_FeatherIcons), size - 4.f, &config, icon_ranges ); @@ -213,7 +216,7 @@ void DevTools::setup() { IMGUI_CHECKVERSION(); - auto ctx = ImGui::CreateContext(); + ImGui::CreateContext(); auto& io = ImGui::GetIO(); io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; @@ -234,23 +237,27 @@ void DevTools::setup() { void DevTools::destroy() { if (!m_setup) return; - m_setup = false; - m_visible = false; + this->show(false); + auto& io = ImGui::GetIO(); + io.BackendPlatformUserData = nullptr; + m_fontTexture->release(); + m_fontTexture = nullptr; - // crashes :( - // ImGui::DestroyContext(); + ImGui::DestroyContext(); + m_setup = false; + m_reloadTheme = true; } void DevTools::show(bool visible) { m_visible = visible; + + auto& io = ImGui::GetIO(); + io.WantCaptureMouse = visible; + io.WantCaptureKeyboard = visible; } void DevTools::toggle() { this->show(!m_visible); - if (!m_visible) { - ImGui::GetIO().WantCaptureMouse = false; - ImGui::GetIO().WantCaptureKeyboard = false; - } } void DevTools::sceneChanged() { diff --git a/src/DevTools.hpp b/src/DevTools.hpp index 8b5816e..553c9f3 100644 --- a/src/DevTools.hpp +++ b/src/DevTools.hpp @@ -8,7 +8,6 @@ #include #include #include -#include using namespace geode::prelude; @@ -44,6 +43,7 @@ class DevTools { ImFont* m_smallFont = nullptr; ImFont* m_monoFont = nullptr; ImFont* m_boxFont = nullptr; + CCTexture2D* m_fontTexture = nullptr; Ref m_selectedNode; std::vector> m_toHighlight; diff --git a/src/backend.cpp b/src/backend.cpp index 2650d95..0804d23 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -12,8 +12,6 @@ using namespace cocos2d; // based off https://github.com/matcool/gd-imgui-cocos void DevTools::setupPlatform() { - ImGui::CreateContext(); - auto& io = ImGui::GetIO(); io.BackendPlatformUserData = this; @@ -29,13 +27,11 @@ void DevTools::setupPlatform() { int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); - auto* tex2d = new CCTexture2D; - tex2d->initWithData(pixels, kCCTexture2DPixelFormat_RGBA8888, width, height, CCSize(width, height)); - - // TODO: not leak this :-) - tex2d->retain(); + m_fontTexture = new CCTexture2D; + m_fontTexture->initWithData(pixels, kCCTexture2DPixelFormat_RGBA8888, width, height, CCSize(width, height)); + m_fontTexture->retain(); - io.Fonts->SetTexID(reinterpret_cast(static_cast(tex2d->getName()))); + io.Fonts->SetTexID(reinterpret_cast(static_cast(m_fontTexture->getName()))); } void DevTools::newFrame() {