added debug overlay class for showing fps and other infos

This commit is contained in:
Lendemor 2018-06-05 03:36:37 +02:00
parent afbab7711d
commit ec53ae08f2
6 changed files with 83 additions and 10 deletions

50
src/debugoverlay.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "debugoverlay.h"
#include "imgui/imgui.h"
#include "engine.h"
DebugOverlay::DebugOverlay():
m_enabled(false)
{
setID("overlay");
}
void DebugOverlay::update(){
gui();
SceneNode::update();
}
void DebugOverlay::gui()
{
if(m_enabled)
{
ImGuiIO& io = ImGui::GetIO();
// io.DeltaTime = float(getEngine().getDeltaTime()) / 1000.;
// ImGui::NewFrame();
const float DISTANCE = 10.0f;
ImVec2 window_pos = ImVec2(ImGui::GetIO().DisplaySize.x - DISTANCE,DISTANCE);
ImVec2 window_pos_pivot = ImVec2(1.0f, 0.0f);
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.0f, 0.0f, 0.0f, 0.3f)); // Transparent background
bool overlayEnabled = true;
ImGui::Begin("Debug Overlay",
&overlayEnabled,
ImGuiWindowFlags_NoTitleBar|
ImGuiWindowFlags_NoResize|
ImGuiWindowFlags_AlwaysAutoResize|
ImGuiWindowFlags_NoMove|
ImGuiWindowFlags_NoSavedSettings);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
ImGui::PopStyleColor();
// ImGui::EndFrame();
}
}
void DebugOverlay::toggle()
{
m_enabled = !m_enabled;
}

18
src/debugoverlay.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef DEBUGOVERLAY_H
#define DEBUGOVERLAY_H
#include "scene/scenenode.h"
class DebugOverlay : public SceneNode
{
bool m_enabled;
public:
DebugOverlay();
void update();
void gui();
void toggle();
};
#endif // DEBUGOVERLAY_H

View File

@ -15,6 +15,7 @@
#include "tools/loader.h" #include "tools/loader.h"
#include "editor.h" #include "editor.h"
#include "keymapper.h" #include "keymapper.h"
#include "debugoverlay.h"
#include "tools/loadingthread.h" #include "tools/loadingthread.h"
Engine::Engine() : Engine::Engine() :
@ -70,6 +71,7 @@ void Engine::createWindow(std::string title,
m_sparrowshell = new SparrowShell(m_window); m_sparrowshell = new SparrowShell(m_window);
m_editor = new Editor(); m_editor = new Editor();
m_keymapper = new KeyMapper(); m_keymapper = new KeyMapper();
m_overlay = new DebugOverlay();
m_loadingThread = LoadingThread::init(); m_loadingThread = LoadingThread::init();
} }
@ -103,7 +105,8 @@ void Engine::update()
if(settingsGuiOpen) if(settingsGuiOpen)
{ {
ImGui::Begin("Window Settings", &settingsGuiOpen); ImGui::Begin("Window Settings", &settingsGuiOpen);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); //moved to overlay
//ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
bool physicsDebugEnabled = (m_physicsDebugNode != nullptr); bool physicsDebugEnabled = (m_physicsDebugNode != nullptr);
if(ImGui::Checkbox("Toggle physics debug", &physicsDebugEnabled)) if(ImGui::Checkbox("Toggle physics debug", &physicsDebugEnabled))
{ {
@ -135,15 +138,6 @@ static void ShowExampleAppFixedOverlay(bool* p_open)
ImGui::Text("Simple overlay\nin the corner of the screen.\n(right-click to change position)"); ImGui::Text("Simple overlay\nin the corner of the screen.\n(right-click to change position)");
ImGui::Separator(); ImGui::Separator();
ImGui::Text("Mouse Position: (%.1f,%.1f)", ImGui::GetIO().MousePos.x, ImGui::GetIO().MousePos.y); ImGui::Text("Mouse Position: (%.1f,%.1f)", ImGui::GetIO().MousePos.x, ImGui::GetIO().MousePos.y);
if (ImGui::BeginPopupContextWindow())
{
if (ImGui::MenuItem("Top-left", NULL, corner == 0)) corner = 0;
if (ImGui::MenuItem("Top-right", NULL, corner == 1)) corner = 1;
if (ImGui::MenuItem("Bottom-left", NULL, corner == 2)) corner = 2;
if (ImGui::MenuItem("Bottom-right", NULL, corner == 3)) corner = 3;
if (p_open && ImGui::MenuItem("Close")) *p_open = false;
ImGui::EndPopup();
}
ImGui::End(); ImGui::End();
} }
ImGui::PopStyleColor(); ImGui::PopStyleColor();
@ -223,6 +217,7 @@ void Engine::setScene(std::string scene)
new_scene->getRootObject()->addChild(m_sparrowshell); new_scene->getRootObject()->addChild(m_sparrowshell);
new_scene->getRootObject()->addChild(m_editor); new_scene->getRootObject()->addChild(m_editor);
new_scene->getRootObject()->addChild(m_keymapper); new_scene->getRootObject()->addChild(m_keymapper);
new_scene->getRootObject()->addChild(m_overlay);
} }
void Engine::enablePhysicsDebug() void Engine::enablePhysicsDebug()

View File

@ -10,6 +10,7 @@ class SparrowShell;
class PhysicsDebugNode; class PhysicsDebugNode;
class Editor; class Editor;
class KeyMapper; class KeyMapper;
class DebugOverlay;
class LoadingThread; class LoadingThread;
namespace sf namespace sf
@ -56,6 +57,7 @@ public:
PhysicsDebugNode* getPhysicsDebug() const {return m_physicsDebugNode;} PhysicsDebugNode* getPhysicsDebug() const {return m_physicsDebugNode;}
Editor* getEditor() const {return m_editor;} Editor* getEditor() const {return m_editor;}
KeyMapper* getKeyMapper() const{return m_keymapper;} KeyMapper* getKeyMapper() const{return m_keymapper;}
DebugOverlay* getOverlay() const{return m_overlay;}
LoadingThread* getLoadingThread() const {return m_loadingThread;} LoadingThread* getLoadingThread() const {return m_loadingThread;}
SceneTree* getScene() const; SceneTree* getScene() const;
@ -83,6 +85,7 @@ private:
PhysicsDebugNode *m_physicsDebugNode; PhysicsDebugNode *m_physicsDebugNode;
SparrowRenderer* m_renderer; SparrowRenderer* m_renderer;
Editor* m_editor; Editor* m_editor;
DebugOverlay* m_overlay;
KeyMapper* m_keymapper; KeyMapper* m_keymapper;
LoadingThread* m_loadingThread; LoadingThread* m_loadingThread;

View File

@ -10,6 +10,7 @@
#include "tools/utils.h" #include "tools/utils.h"
#include "editor.h" #include "editor.h"
#include "keymapper.h" #include "keymapper.h"
#include "debugoverlay.h"
#define LUA_DEFINE(var) S[#var]=var #define LUA_DEFINE(var) S[#var]=var
#define LUA_SET_FUN(var) m_script.set_function(#var,&ScriptNode::var,this);this->functions_name.push_back(#var); #define LUA_SET_FUN(var) m_script.set_function(#var,&ScriptNode::var,this);this->functions_name.push_back(#var);
@ -30,6 +31,7 @@ ScriptNode::ScriptNode()
LUA_SET_FUN(resourcePackEditor); LUA_SET_FUN(resourcePackEditor);
LUA_SET_FUN(editor); LUA_SET_FUN(editor);
LUA_SET_FUN(keymapper); LUA_SET_FUN(keymapper);
LUA_SET_FUN(overlay);
m_script.new_usertype<Engine>("Engine", m_script.new_usertype<Engine>("Engine",
"time",&Engine::getTime, "time",&Engine::getTime,
@ -121,3 +123,7 @@ void ScriptNode::editor(){
void ScriptNode::keymapper(){ void ScriptNode::keymapper(){
this->getEngine().getKeyMapper()->toggle(); this->getEngine().getKeyMapper()->toggle();
} }
void ScriptNode::overlay(){
this->getEngine().getOverlay()->toggle();
}

View File

@ -29,6 +29,7 @@ public:
void resourcePackEditor(); void resourcePackEditor();
void editor(); void editor();
void keymapper(); void keymapper();
void overlay();
}; };
#endif // SCRIPTNODE_H #endif // SCRIPTNODE_H