From b0142f3348441fb5131fbaea32cad9ec22bc9943 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Fri, 10 Sep 2021 16:45:20 -0400 Subject: [PATCH] Added basic debug log window --- CMakeLists.txt | 1 + docs/Tasks.todo | 7 ++- src/core/core.cpp | 23 ++++++- src/graphics/gui/gui.cpp | 19 +++--- src/graphics/gui/gui.h | 1 + src/graphics/gui/logGui.cpp | 116 ++++++++++++++++++++++++++++++++++++ src/graphics/gui/logGui.h | 57 ++++++++++++++++++ 7 files changed, 211 insertions(+), 13 deletions(-) create mode 100644 src/graphics/gui/logGui.cpp create mode 100644 src/graphics/gui/logGui.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fa19780..dc2b92c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ set(LUNARIUM_SRC "src/input/keyboard.cpp" "src/input/inputManager.cpp" "src/graphics/gui/gui.cpp" +"src/graphics/gui/logGui.cpp" ) # add the executable diff --git a/docs/Tasks.todo b/docs/Tasks.todo index eb0059f..7a261fb 100644 --- a/docs/Tasks.todo +++ b/docs/Tasks.todo @@ -13,8 +13,11 @@ Core: ✔ Implement the Image creation methods @done (9/9/2021, 2:50:20 PM) - Dear ImGui: - ☐ Dear ImGui class with basic initialization + GUI: + ✔ Dear ImGui class with basic initialization @done (9/10/2021, 1:42:19 PM) + ✔ Debug log window @done (9/10/2021, 4:44:48 PM) + ☐ Add key to show debug log window +. Input: ✔ Port over the Element2D input system and adjust it to use glfw @done (9/8/2021, 8:20:07 PM) diff --git a/src/core/core.cpp b/src/core/core.cpp index 4ce5495..1687b1a 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace lunarium { @@ -42,6 +43,7 @@ namespace lunarium Logger::Log(LogCategory::CORE, LogLevel::INFO, "Lunarium is shutting down!"); // Shutdown subsystems + LogGui::FreeInstance(); GUI::GetInstance().Shutdown(); GUI::FreeInstance(); @@ -83,6 +85,15 @@ namespace lunarium if (mErrorLogFile.is_open()) Logger::GetInstance()->AddListener(new FileListener(mErrorLogFile, LogLevel::ERROR | LogLevel::FATAL_ERROR)); + // Init the Debug log window + OpRes result; + result = LogGui::GetInstance().Initialize(); + if (Failed(result)) + { + Logger::Log(LogCategory::CORE, LogLevel::WARNING, + "Could not initialized the debug log window: %s", result.Description); + } + Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running Lunarium version %s", Version::GetVersion().ToString().c_str()); // Attempt to load the engine state file. This file should be placed in the same directory as the lunarium program. @@ -100,8 +111,6 @@ namespace lunarium std::vector sd; mpArgs = new Args(argc, argv, '-', sd); - OpRes result; - // Init Graphics/Window system mpWindow = new Window; result = mpWindow->Initialize(mState); @@ -151,6 +160,12 @@ namespace lunarium // GUI mGUI.Initialize(mpWindow); + result = LogGui::GetInstance().Initialize(); + if (Failed(result)) + { + Logger::Log(LogCategory::CORE, LogLevel::WARNING, + "Could not initialized the debug log window: %s", result.Description); + } mbIsInit = true; } @@ -208,13 +223,15 @@ namespace lunarium mpGraphics->BeginDraw(); // DEBUG: Render test ImGUI window - mGUI.ShowDemoWindow(); + // mGUI.ShowDemoWindow(); + LogGui::GetInstance().Show(); // DEBUG: Graphics tests mpGraphics->DrawFilledEllipse(glm::vec2(600, 300), glm::vec2(100, 150), Color(1.0f, 0.0f, 1.0f, 1.0f), 100); mpGraphics->DrawString("This is a test of the text renderer!", Rectangle(100, 200, width, 300), Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f, mpGraphics->DefaultFont()); + mGUI.EndFrame(); mpGraphics->EndDraw(); } } diff --git a/src/graphics/gui/gui.cpp b/src/graphics/gui/gui.cpp index 671fc29..6f4b0a2 100644 --- a/src/graphics/gui/gui.cpp +++ b/src/graphics/gui/gui.cpp @@ -98,22 +98,25 @@ namespace lunarium ImGui::NewFrame(); } - void GUI::ShowDemoWindow() + void GUI::EndFrame() { - if (!mbIsInit) - return; - - - ImGui::ShowDemoWindow(&mbShowDemo); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { - //GLFWwindow* backup_current_context = glfwGetCurrentContext(); + GLFWwindow* backup_current_context = glfwGetCurrentContext(); ImGui::UpdatePlatformWindows(); ImGui::RenderPlatformWindowsDefault(); - //glfwMakeContextCurrent(backup_current_context); + glfwMakeContextCurrent(backup_current_context); } } + + void GUI::ShowDemoWindow() + { + if (!mbIsInit) + return; + + ImGui::ShowDemoWindow(&mbShowDemo); + } } \ No newline at end of file diff --git a/src/graphics/gui/gui.h b/src/graphics/gui/gui.h index 235fdcf..1c8707b 100644 --- a/src/graphics/gui/gui.h +++ b/src/graphics/gui/gui.h @@ -23,6 +23,7 @@ namespace lunarium void Shutdown(); void NewFrame(); + void EndFrame(); void ShowDemoWindow(); private: diff --git a/src/graphics/gui/logGui.cpp b/src/graphics/gui/logGui.cpp new file mode 100644 index 0000000..56f1a28 --- /dev/null +++ b/src/graphics/gui/logGui.cpp @@ -0,0 +1,116 @@ +/****************************************************************************** +* File - logGui.cpp +* Author - Joey Pollack +* Date - 2021/09/09 (y/m/d) +* Mod Date - 2021/09/10 (y/m/d) +* Description - A debug log window +******************************************************************************/ + +#include "logGui.h" +#include +#include +#include +#include + +namespace lunarium +{ + //////////////////////////////////////////////////////////// + // GUI LOG LISTENER + //////////////////////////////////////////////////////////// + GuiListener::GuiListener(LogGui* pGui, uint32_t acceptedLogLevels, uint32_t acceptedLogCategories, const char* myName) + : LogListener(acceptedLogLevels, acceptedLogCategories, myName), mpGui(pGui) + { + + } + + bool GuiListener::Log(LogMessage& message) + { + if (!LevelIsSet(message.LogLevel) || + !CategoryIsSet(message.LogCategory)) + return false; + + std::ostringstream oss; + + oss << std::endl << Logger::TimeStamp() << Logger::GetCategoryName(message.LogCategory) + << Logger::GetLevelName(message.LogLevel) << message.Message << std::flush; + + mpGui->mMsgHistory.push_back(oss.str()); + return true; + } + + //////////////////////////////////////////////////////////// + // GUI LOGGER + //////////////////////////////////////////////////////////// + LogGui* LogGui::mpInstance = nullptr; + LogGui::LogGui() + : mbShow(true), mListener(this) + { + + } + + LogGui& LogGui::GetInstance() + { + if (!mpInstance) + { + mpInstance = new LogGui; + } + + return *mpInstance; + } + + void LogGui::FreeInstance() + { + Logger::GetInstance()->RemoveListener(&mpInstance->mListener); + + delete mpInstance; + mpInstance = nullptr; + } + + OpRes LogGui::Initialize() + { + Logger::GetInstance()->AddListener(&mListener); + return OpRes::OK(); + } + + void LogGui::Show() + { + if (!mbShow) + return; + + ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver); + if (!ImGui::Begin("Debug Log", &mbShow)) + { + ImGui::End(); + return; + } + + ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); + for (int i = 0; i < mMsgHistory.size(); i++) + { + const char* msg = mMsgHistory[i].c_str(); + int len = strlen(msg); + ImGui::TextUnformatted(msg); + } + + if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) + ImGui::SetScrollHereY(1.0f); + + ImGui::EndChild(); + ImGui::End(); + } + + void LogGui::Clear() + { + mMsgHistory.clear(); + } + + void LogGui::SetShow(bool show) + { + mbShow = show; + } + + bool LogGui::IsShown() const + { + return mbShow; + } +} \ No newline at end of file diff --git a/src/graphics/gui/logGui.h b/src/graphics/gui/logGui.h new file mode 100644 index 0000000..ae3280a --- /dev/null +++ b/src/graphics/gui/logGui.h @@ -0,0 +1,57 @@ +/****************************************************************************** +* File - logGui.h +* Author - Joey Pollack +* Date - 2021/09/09 (y/m/d) +* Mod Date - 2021/09/10 (y/m/d) +* Description - A debug log window +******************************************************************************/ + +#ifndef LOG_GUI_H_ +#define LOG_GUI_H_ + +#include +#include +#include +#include + +namespace lunarium +{ + class LogGui; + class GuiListener : public LogListener + { + public: + GuiListener(LogGui* pGui, uint32_t acceptedLogLevels = LogLevel::ANY, uint32_t acceptedLogCategories = LogLevel::ANY, const char* myName = "Gui Listener"); + virtual bool Log(LogMessage& message); + + private: + LogGui* mpGui; + }; + + class LogGui + { + public: + static LogGui& GetInstance(); + static void FreeInstance(); + + OpRes Initialize(); + void Show(); + void Clear(); + + void SetShow(bool show); + bool IsShown() const; + + private: + static LogGui* mpInstance; + bool mbShow; + std::vector mMsgHistory; + friend GuiListener; + GuiListener mListener; + + private: + LogGui(); + LogGui(const LogGui&) = delete; + const LogGui& operator=(const LogGui&) = delete; + }; +} + +#endif // LOG_GUI_H_ \ No newline at end of file