From 6ea11c9aefae745543801b4c6e6522e75fcfac46 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Sat, 6 Jun 2020 15:50:31 +0200 Subject: [PATCH] Overlay: ImGuiOverlay - update API to avoid NewFrame/ EndFrame mismatch --- Components/Overlay/include/OgreImGuiOverlay.h | 3 ++- Components/Overlay/src/OgreImGuiOverlay.cpp | 10 ++++++++-- Samples/Simple/include/ImGuiDemo.h | 16 +++++++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Components/Overlay/include/OgreImGuiOverlay.h b/Components/Overlay/include/OgreImGuiOverlay.h index f78f6f80612..6aa12b69326 100644 --- a/Components/Overlay/include/OgreImGuiOverlay.h +++ b/Components/Overlay/include/OgreImGuiOverlay.h @@ -23,7 +23,8 @@ class _OgreOverlayExport ImGuiOverlay : public Overlay /// must be called before first show() ImFont* addFont(const String& name, const String& group OGRE_RESOURCE_GROUP_INIT); - static void NewFrame(const FrameEvent& evt); + static void NewFrame(); + OGRE_DEPRECATED static void NewFrame(const FrameEvent& evt) { NewFrame(); } void _findVisibleObjects(Camera* cam, RenderQueue* queue, Viewport* vp); diff --git a/Components/Overlay/src/OgreImGuiOverlay.cpp b/Components/Overlay/src/OgreImGuiOverlay.cpp index 68db4406dcb..97577bdb45e 100644 --- a/Components/Overlay/src/OgreImGuiOverlay.cpp +++ b/Components/Overlay/src/OgreImGuiOverlay.cpp @@ -19,6 +19,7 @@ #include #include #include +#include namespace Ogre { @@ -125,13 +126,18 @@ void ImGuiOverlay::ImGUIRenderable::createFontTexture() mFontTex->getBuffer()->blitFromMemory(PixelBox(Box(0, 0, width, height), PF_BYTE_RGBA, pixels)); } -void ImGuiOverlay::NewFrame(const FrameEvent& evt) +void ImGuiOverlay::NewFrame() { + static auto lastTime = Root::getSingleton().getTimer()->getMilliseconds(); + auto now = Root::getSingleton().getTimer()->getMilliseconds(); + ImGuiIO& io = ImGui::GetIO(); io.DeltaTime = std::max( - evt.timeSinceLastFrame, + float(now - lastTime)/1000, 1e-4f); // see https://github.com/ocornut/imgui/commit/3c07ec6a6126fb6b98523a9685d1f0f78ca3c40c + lastTime = now; + // Read keyboard modifiers inputs io.KeyAlt = false; io.KeySuper = false; diff --git a/Samples/Simple/include/ImGuiDemo.h b/Samples/Simple/include/ImGuiDemo.h index d9326642130..258fde65bf2 100644 --- a/Samples/Simple/include/ImGuiDemo.h +++ b/Samples/Simple/include/ImGuiDemo.h @@ -7,7 +7,7 @@ using namespace Ogre; using namespace OgreBites; -class _OgreSampleClassExport Sample_ImGui : public SdkSample +class _OgreSampleClassExport Sample_ImGui : public SdkSample, public RenderTargetListener { std::unique_ptr mImguiListener; InputListenerChain mListenerChain; @@ -21,16 +21,13 @@ class _OgreSampleClassExport Sample_ImGui : public SdkSample mInfo["Thumbnail"] = "thumb_imgui.png"; } - bool frameStarted(const FrameEvent& e) + void preViewportUpdate(const RenderTargetViewportEvent& evt) { - if(mWindow->isActive()) - { - ImGuiOverlay::NewFrame(e); + if(!evt.source->getOverlaysEnabled()) return; - ImGui::ShowDemoWindow(); - } + ImGuiOverlay::NewFrame(); - return SdkSample::frameStarted(e); + ImGui::ShowDemoWindow(); } bool keyPressed(const KeyboardEvent& evt) { return mListenerChain.keyPressed(evt); } @@ -50,11 +47,12 @@ class _OgreSampleClassExport Sample_ImGui : public SdkSample /* NOTE: - Custom apps will ASSERT on ImGuiOverlay::NewFrame(e) and not display any UI if they + Custom apps will ASSERT on ImGuiOverlay::NewFrame() and not display any UI if they have not registered the overlay system by calling mSceneMgr->addRenderQueueListener(mOverlaySystem). OgreBites::SampleBrowser does this on behalf of the ImGuiDemo but custom applications will need to call this themselves. See ApplicationContextBase::createDummyScene(). */ + mWindow->addListener(this); mImguiListener.reset(new ImGuiInputListener()); mListenerChain = InputListenerChain({mTrayMgr.get(), mImguiListener.get(), mCameraMan.get()});