diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ae5413..fa19780 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ set(LUNARIUM_SRC "src/graphics/internalFont.cpp" "src/input/keyboard.cpp" "src/input/inputManager.cpp" +"src/graphics/gui/gui.cpp" ) # add the executable diff --git a/src/core/core.cpp b/src/core/core.cpp index db86bc4..4ce5495 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -12,12 +12,14 @@ // Sub Systems #include #include +#include namespace lunarium { Core* Core::mpInstance = nullptr; Core::Core() - : mbIsInit(false), mpArgs(nullptr), mpWindow(nullptr), mpGraphics(nullptr), mpInput(nullptr) + : mbIsInit(false), mpArgs(nullptr), mpWindow(nullptr), mpGraphics(nullptr), mpInput(nullptr), + mGUI(GUI::GetInstance()) { } @@ -40,6 +42,9 @@ namespace lunarium Logger::Log(LogCategory::CORE, LogLevel::INFO, "Lunarium is shutting down!"); // Shutdown subsystems + GUI::GetInstance().Shutdown(); + GUI::FreeInstance(); + mpInstance->mpInput->Shutdown(); delete mpInstance->mpInput; mpInstance->mpInput = nullptr; @@ -144,6 +149,9 @@ namespace lunarium mpInput = new InputManager; mpInput->Initialize(mpWindow); + // GUI + mGUI.Initialize(mpWindow); + mbIsInit = true; } @@ -196,8 +204,12 @@ namespace lunarium // Update game state // Render + mGUI.NewFrame(); mpGraphics->BeginDraw(); + // DEBUG: Render test ImGUI window + mGUI.ShowDemoWindow(); + // 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), diff --git a/src/core/core.h b/src/core/core.h index 92c298f..f8d480f 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -17,6 +17,7 @@ namespace lunarium { + class GUI; class IGraphics; class Window; @@ -51,6 +52,7 @@ namespace lunarium Window* mpWindow; IGraphics* mpGraphics; InputManager* mpInput; + GUI& mGUI; InputManager::_KeyEvents mKeyEvents; diff --git a/src/graphics/gui/gui.cpp b/src/graphics/gui/gui.cpp new file mode 100644 index 0000000..38ad4f3 --- /dev/null +++ b/src/graphics/gui/gui.cpp @@ -0,0 +1,117 @@ +/****************************************************************************** +* File - gui.cpp +* Author - Joey Pollack +* Date - 2021/09/09 (y/m/d) +* Mod Date - 2021/09/09 (y/m/d) +* Description - The main class that manages Dear ImGui +******************************************************************************/ + +#include "gui.h" + +#include +#include +#include +#include +#include +#include + +namespace lunarium +{ + GUI* GUI::mpInstance = nullptr; + GUI::GUI() + : mbIsInit(false), mbShowDemo(false) + { + + } + + GUI& GUI::GetInstance() + { + if (!mpInstance) + { + mpInstance = new GUI; + } + + return *mpInstance; + } + + void GUI::FreeInstance() + { + delete mpInstance; + mpInstance = nullptr; + } + + OpRes GUI::Initialize(Window* pWindow) + { + if (mbIsInit) + { + return OpRes::Fail("GUI is already initialized!"); + } + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows + + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + + // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones. + ImGuiStyle& style = ImGui::GetStyle(); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + style.WindowRounding = 0.0f; + style.Colors[ImGuiCol_WindowBg].w = 1.0f; + } + + // Setup Platform/Renderer backends + ImGui_ImplGlfw_InitForOpenGL(pWindow->GetWindow(), true); + ImGui_ImplOpenGL3_Init(System::GetGLSLVersionString().c_str()); + + // Our state + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + mbShowDemo = true; + + Logger::Log(LogCategory::GRAPHICS, LogLevel::INFO, "ImGui setup"); + return OpRes::OK(); + } + + void GUI::Shutdown() + { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + + mbIsInit = false; + } + + void GUI::NewFrame() + { + if (!mbIsInit) + return; + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + } + + void GUI::ShowDemoWindow() + { + if (!mbIsInit) + return; + + ImGui::ShowDemoWindow(&mbShowDemo); + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + GLFWwindow* backup_current_context = glfwGetCurrentContext(); + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + glfwMakeContextCurrent(backup_current_context); + } + } +} \ No newline at end of file diff --git a/src/graphics/gui/gui.h b/src/graphics/gui/gui.h new file mode 100644 index 0000000..235fdcf --- /dev/null +++ b/src/graphics/gui/gui.h @@ -0,0 +1,40 @@ +/****************************************************************************** +* File - gui.h +* Author - Joey Pollack +* Date - 2021/09/09 (y/m/d) +* Mod Date - 2021/09/09 (y/m/d) +* Description - The main class that manages Dear ImGui +******************************************************************************/ + +#ifndef GUI_H_ +#define GUI_H_ + +#include + +namespace lunarium +{ + class Window; + class GUI + { + public: + static GUI& GetInstance(); + static void FreeInstance(); + OpRes Initialize(Window* pWindow); + void Shutdown(); + + void NewFrame(); + void ShowDemoWindow(); + + private: + GUI(); + GUI(const GUI&) = delete; + const GUI operator=(const GUI&) = delete; + + private: + static GUI* mpInstance; + bool mbIsInit; + bool mbShowDemo; + }; +} + +#endif // GIU_H_ \ No newline at end of file diff --git a/src/test_main.cpp b/src/test_main.cpp index 686bc06..e774d6a 100644 --- a/src/test_main.cpp +++ b/src/test_main.cpp @@ -7,7 +7,7 @@ #include #include #include "utils/logger.h" -#include "utilsoOpRes.h" +#include "utils/OpRes.h" #include "core/state.h" #include "core/version.h" #include "utils/args.h"