Skip to content

Commit

Permalink
Overlay: ImGuiOverlay - update API to avoid NewFrame/ EndFrame mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
paroj committed Jun 6, 2020
1 parent e245b0d commit 6ea11c9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Components/Overlay/include/OgreImGuiOverlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 8 additions & 2 deletions Components/Overlay/src/OgreImGuiOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <OgreRenderQueue.h>
#include <OgreFrameListener.h>
#include <OgreRoot.h>
#include <OgreTimer.h>

namespace Ogre
{
Expand Down Expand Up @@ -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<float>(
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;
Expand Down
16 changes: 7 additions & 9 deletions Samples/Simple/include/ImGuiDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImGuiInputListener> mImguiListener;
InputListenerChain mListenerChain;
Expand All @@ -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); }
Expand All @@ -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()});
Expand Down

0 comments on commit 6ea11c9

Please # to comment.