From 4486eb88906d3119fc5494f9502f4d97f32aedc5 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Mon, 20 Sep 2021 18:04:59 -0400 Subject: [PATCH] Debug log and LUA console gui windows implemented --- CMakeLists.txt | 9 +++ LunariumConfig.h.in | 4 +- docs/Tasks.todo | 14 +++- docs/project_structure.txt | 27 +++++++ scripts/cmconfig.bat | 6 +- src/core/core.cpp | 72 +++++++++++++++--- src/core/core.h | 6 +- src/graphics/gui/gui.cpp | 5 ++ src/graphics/gui/gui.h | 2 + src/graphics/gui/logGui.cpp | 17 ++++- src/graphics/gui/luaConsole.cpp | 125 ++++++++++++++++++++++++++++++++ src/graphics/gui/luaConsole.h | 50 +++++++++++++ src/input/inputManager.cpp | 11 ++- src/input/inputManager.h | 4 +- src/input/keyboard.cpp | 1 + src/tester/tester.cpp | 13 ++++ src/window/window.cpp | 4 + src/window/window.h | 1 + 18 files changed, 344 insertions(+), 27 deletions(-) create mode 100644 docs/project_structure.txt create mode 100644 src/graphics/gui/luaConsole.cpp create mode 100644 src/graphics/gui/luaConsole.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ca57cc9..40925c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) set(OpenGL_MAJOR_VERSION 4) set(OpenGL_MINOR_VERSION 5) +# Option to build without the editor +set(BUILD_NO_EDITOR 0) +option(NO_EDITOR "Build with no editor" OFF) +if (NO_EDITOR) + message(STATUS "Building without the editor") + set(BUILD_NO_EDITOR 1) +endif () + configure_file(LunariumConfig.h.in LunariumConfig.h) # Source Files @@ -40,6 +48,7 @@ set(LUNARIUM_SRC "src/input/inputManager.cpp" "src/graphics/gui/gui.cpp" "src/graphics/gui/logGui.cpp" +"src/graphics/gui/luaConsole.cpp" "src/assets/types/image.cpp" ) diff --git a/LunariumConfig.h.in b/LunariumConfig.h.in index 2bb0a80..0a1a00c 100644 --- a/LunariumConfig.h.in +++ b/LunariumConfig.h.in @@ -4,4 +4,6 @@ #define Lunarium_VERSION_PATCH @Lunarium_VERSION_PATCH@ #define OPENGL_MAJOR_VERSION @OpenGL_MAJOR_VERSION@ -#define OPENGL_MINOR_VERSION @OpenGL_MINOR_VERSION@ \ No newline at end of file +#define OPENGL_MINOR_VERSION @OpenGL_MINOR_VERSION@ + +#define BUILD_NO_EDITOR @BUILD_NO_EDITOR@ \ No newline at end of file diff --git a/docs/Tasks.todo b/docs/Tasks.todo index 4323312..284e991 100644 --- a/docs/Tasks.todo +++ b/docs/Tasks.todo @@ -1,11 +1,13 @@ Build System: - ☐ Add a build option to do a build without the editor + ✔ Add a build option to do a build without the editor @done (9/17/2021, 7:25:08 PM) + ☐ Modify .sh scripts to recognize the noeditor flag Core: ☐ Add log settings to the state file ✔ Add run modes (Editor, Game, Test) to state file @done (9/15/2021, 7:27:03 PM) ✔ Add run mode interface class @done (9/15/2021, 8:22:35 PM) + ☐ Read the window size and position on shutdown and write these to the state file Graphics: ✔ Decide on a font/text rendering system @done (9/7/2021, 1:39:53 PM) @@ -14,8 +16,7 @@ Core: ✔ Make the text renderer smarter about breaking up words on multiple lines @low @done (9/8/2021, 2:23:03 PM) ✔ Implement the Image creation methods @done (9/9/2021, 2:50:20 PM) ✔ Implement Render to Texture @done (9/15/2021, 7:00:33 PM) - ☐ Adjust the font loading code to use the binary file buffer instead of ifstream - ("b. From Memory": https://www.freetype.org/freetype2/docs/tutorial/step1.html) + ✔ Adjust the font loading code to use the binary file buffer instead of ifstream @done (9/17/2021, 6:11:06 PM) GUI: @@ -76,9 +77,11 @@ Game: Editor: + ✔ Come up with project directory structure @done (9/17/2021, 6:46:44 PM) ☐ Implement Run Mode interface class ☐ Reference raw asset files in a "content" folder ☐ Platform independant file browsing + ☐ Scan script files to make sure they don't overwrite globals Raw Asset Loaders: - Need classes to load raw resource files for the editor @@ -117,6 +120,11 @@ Assets: Loaders: - Need class (or classes?) to load resources from the packed format that the pipeline generates + Come up with binary file formats for each type: + ☐ .xml (This will probably be multiple different formats depending on what the .xml file is describing) + ☐ Image + ☐ Script + ☐ Audio Asset Pipeline: ☐ Read through the contents folder and generate asset files in a custom format (useable by the engine) diff --git a/docs/project_structure.txt b/docs/project_structure.txt new file mode 100644 index 0000000..aac0225 --- /dev/null +++ b/docs/project_structure.txt @@ -0,0 +1,27 @@ + +The root project folder needs to contain a project.xml file that describes the project (See ??? for details) + +Project directory structure (this should be auto-generated by the editor when creating a new project): + + Project root + ├── project.xml + ├── engine/ + │ └── Lunarium_NE.exe (non-editor version of the engine. Used for creating a release build of the project) + │ + ├── contents/ + │ ├── content_meta.xml - (Associates each asset with a unique id, and whatever else is needed to compile the assets) + │ └── assets/ + │ └── (Can be organized however the user wants) + │ + └─ release/ + ├── (This directory is generated by the editor when building the project) + ├── game_name.exe - (The non-editor version of the engine renamed to the game name) + └── data/ - (The data folder generated by the asset compiler, contains all the assets for the game in binary formats) + + + + + +├── +│ +└─ \ No newline at end of file diff --git a/scripts/cmconfig.bat b/scripts/cmconfig.bat index e466c5c..0313ed3 100644 --- a/scripts/cmconfig.bat +++ b/scripts/cmconfig.bat @@ -3,4 +3,8 @@ REM This script expects to be run from the parent directory REM ex. scripts/cmconfig.bat @echo on -cmake -Wno-dev -DGLFW_BUILD_DOCS=OFF -B build/ -S . -G "Visual Studio 16 2019" -A x64 \ No newline at end of file +IF "%~1" == "noedit" ( +cmake -Wno-dev -DNO_EDITOR=ON -DGLFW_BUILD_DOCS=OFF -B build/ -S . -G "Visual Studio 16 2019" -A x64 +) ELSE ( +cmake -Wno-dev -DGLFW_BUILD_DOCS=OFF -B build/ -S . -G "Visual Studio 16 2019" -A x64 +) \ No newline at end of file diff --git a/src/core/core.cpp b/src/core/core.cpp index 4a3344b..b2fb7d4 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -9,22 +9,25 @@ #include "core.h" #include "version.h" +#include + // Run modes #include // Sub Systems #include -//#include +#include #include #include #include +#include namespace lunarium { Core* Core::mpInstance = nullptr; Core::Core() : mbIsInit(false), mpArgs(nullptr), mpWindow(nullptr), mpGraphics(nullptr), mpInput(nullptr), - mGUI(GUI::GetInstance()), mbMidRender(false), mbMidTextureRender(false), mpRunMode(nullptr) + mGUI(GUI::GetInstance()), mbMidRender(false), mbMidTextureRender(false), mpRunMode(nullptr), mbShowGuiDemo(false) { } @@ -49,8 +52,16 @@ namespace lunarium Logger::Log(LogCategory::CORE, LogLevel::INFO, "Lunarium is shutting down!"); + mpInstance->mpWindow->GetFramebufferSize(&mpInstance->mState.Display.WindowedSize.Width, + &mpInstance->mState.Display.WindowedSize.Height); + + mpInstance->mpWindow->GetPosition(&mpInstance->mState.Display.WindowStartPosition.X, + &mpInstance->mState.Display.WindowStartPosition.Y); + + mpInstance->mState.SaveToFile("lunarium_state.xml"); // Shutdown subsystems + // LuaConsole::FreeInstance(); LogGui::FreeInstance(); GUI::GetInstance().Shutdown(); GUI::FreeInstance(); @@ -212,6 +223,11 @@ namespace lunarium return mState; } + void Core::SignalShutdown() + { + mpWindow->SetShouldCloseFlag(true); + } + //////////////////////////////////////////////////////////// // GAME LOOP //////////////////////////////////////////////////////////// @@ -231,29 +247,54 @@ namespace lunarium // Poll input Window::PollEvents(); - mKeyEvents = mpInput->PollKeys(); + auto keyEvents = mpInput->PollKeys(); - // HACK: Temporary solution to close the program - if (mpInput->IsKeyDown(KeyCode::ESCAPE)) + // HIDE/SHOW THE DEBUG WINDOWS + if (mpInput->IsKeyPressed(KeyCode::F2, true)) { - mpWindow->SetShouldCloseFlag(true); + mbShowGuiDemo = !mbShowGuiDemo; } - if (mpInput->IsKeyPressed(KeyCode::F3)) + if (mpInput->IsKeyPressed(KeyCode::F3, true)) { //Logger::Log(LogCategory::CORE, LogLevel::INFO, "Toggling the Debug Log Window"); LogGui::GetInstance().SetShow(!LogGui::GetInstance().IsShown()); } + if (mpInput->IsKeyPressed(KeyCode::F4, true)) + { + LuaConsole::GetInstance().SetShow(!LuaConsole::GetInstance().IsShown()); + } - // TODO: Send input events + if (!ImGui::GetIO().WantCaptureKeyboard) + { + // Send key events + for (int i = 0; i < keyEvents.KeysPressed.size(); i++) + { + mpRunMode->OnKeyPress(keyEvents.KeysPressed[i]); + } + + for (int i = 0; i < keyEvents.KeysReleased.size(); i++) + { + mpRunMode->OnKeyRelease(keyEvents.KeysReleased[i]); + } + } + else + { + // Check if there is a new LUA command + std::string command; + if (LuaConsole::GetInstance().GetNewCommand(command)) + { + Logger::Log(LogCategory::CORE, LogLevel::INFO, "New LUA command: %s", command.c_str()); + } + } - // Update game state + // UPDATE game state mpRunMode->OnTick(mFrameCounter.GetFrameData().LastFrameTime); - // Render + // REDNER if (mbMidTextureRender) { Logger::Log(LogCategory::CORE, LogLevel::WARNING, "Render to texture was not ended!"); @@ -264,12 +305,19 @@ namespace lunarium mpGraphics->BeginDraw(); mbMidRender = true; - // DEBUG: Render test ImGUI window - // mGUI.ShowDemoWindow(); + // Gui windows + if (mbShowGuiDemo) + { + mGUI.ShowDemoWindow(); + } + LogGui::GetInstance().Show(); + LuaConsole::GetInstance().Show(); + // Run mode mpRunMode->OnRender(mpGraphics); + // END RENDER mGUI.EndFrame(); mpGraphics->EndDraw(); mbMidRender = false; diff --git a/src/core/core.h b/src/core/core.h index f3dcb1a..412af70 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -11,7 +11,6 @@ #include "state.h" #include "iRunMode.h" -#include #include #include #include @@ -24,6 +23,7 @@ namespace lunarium class GUI; class IGraphics; class Window; + class InputManager; class Core { @@ -32,6 +32,7 @@ namespace lunarium static void Shutdown(); void Initialize(int argc, char** argv); + void SignalShutdown(); bool IsInit() const; const State& GetState() const; @@ -52,6 +53,7 @@ namespace lunarium iRunMode* mpRunMode; bool mbMidRender; bool mbMidTextureRender; + bool mbShowGuiDemo; // Log Files std::ofstream mMasterLogFile; @@ -64,8 +66,6 @@ namespace lunarium InputManager* mpInput; GUI& mGUI; - InputManager::_KeyEvents mKeyEvents; - public: // SUBSYSTEM GETTERS static Window& MainWindow(); diff --git a/src/graphics/gui/gui.cpp b/src/graphics/gui/gui.cpp index 6f4b0a2..41a0397 100644 --- a/src/graphics/gui/gui.cpp +++ b/src/graphics/gui/gui.cpp @@ -88,6 +88,11 @@ namespace lunarium mbIsInit = false; } + bool GUI::WantCaptureKeyboard() const + { + return ImGui::GetIO().WantCaptureKeyboard; + } + void GUI::NewFrame() { if (!mbIsInit) diff --git a/src/graphics/gui/gui.h b/src/graphics/gui/gui.h index 1c8707b..0697ac0 100644 --- a/src/graphics/gui/gui.h +++ b/src/graphics/gui/gui.h @@ -26,6 +26,8 @@ namespace lunarium void EndFrame(); void ShowDemoWindow(); + bool WantCaptureKeyboard() const; + private: GUI(); GUI(const GUI&) = delete; diff --git a/src/graphics/gui/logGui.cpp b/src/graphics/gui/logGui.cpp index 192d102..6bbdc58 100644 --- a/src/graphics/gui/logGui.cpp +++ b/src/graphics/gui/logGui.cpp @@ -7,6 +7,8 @@ ******************************************************************************/ #include "logGui.h" +#include +#include #include #include #include @@ -43,7 +45,7 @@ namespace lunarium //////////////////////////////////////////////////////////// LogGui* LogGui::mpInstance = nullptr; LogGui::LogGui() - : mbShow(true), mListener(this), mbOglDebug(false), mbInfoVerbose(false) + : mbShow(false), mListener(this), mbOglDebug(false), mbInfoVerbose(false) { } @@ -77,8 +79,17 @@ namespace lunarium if (!mbShow) return; - ImGui::SetNextWindowSize(ImVec2(300, 400), ImGuiCond_FirstUseEver); - if (!ImGui::Begin("Debug Log", &mbShow)) + int x, y; + Core::MainWindow().GetPosition(&x, &y); + int width, height; + Core::MainWindow().GetFramebufferSize(&width, &height); + + int logHeight = height / 2.0f; + + ImGui::SetNextWindowPos(ImVec2(x, y + logHeight), ImGuiCond_Always); + ImGui::SetNextWindowSize(ImVec2((width / 3.0f) * 2.0f, logHeight), ImGuiCond_Always); + if (!ImGui::Begin("Debug Log", &mbShow, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove + | ImGuiWindowFlags_NoSavedSettings)) { ImGui::End(); return; diff --git a/src/graphics/gui/luaConsole.cpp b/src/graphics/gui/luaConsole.cpp new file mode 100644 index 0000000..60d67a5 --- /dev/null +++ b/src/graphics/gui/luaConsole.cpp @@ -0,0 +1,125 @@ +/****************************************************************************** +* File - luaConsole.cpp +* Author - Joey Pollack +* Date - 2021/09/17 (y/m/d) +* Mod Date - 2021/09/17 (y/m/d) +* Description - GUI window for the LUA console +******************************************************************************/ + +#include "luaConsole.h" +#include +#include +#include +#include +#include +#include +#include + +namespace lunarium +{ + LuaConsole* LuaConsole::mpInstance = nullptr; + LuaConsole::LuaConsole() + : mbShow(false), mbNewCommand(false) + { + memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); + } + + LuaConsole& LuaConsole::GetInstance() + { + if (!mpInstance) + { + mpInstance = new LuaConsole; + } + + return *mpInstance; + } + + void LuaConsole::FreeInstance() + { + delete mpInstance; + mpInstance = nullptr; + } + + OpRes LuaConsole::Initialize() + { + return OpRes::OK(); + } + + void LuaConsole::Show() + { + // TODO: LuaConsole::Show + if (!mbShow) + return; + + int x, y; + Core::MainWindow().GetPosition(&x, &y); + int width, height; + Core::MainWindow().GetFramebufferSize(&width, &height); + + ImGui::SetNextWindowPos(ImVec2(x, y), ImGuiCond_Always); + ImGui::SetNextWindowSize(ImVec2(width / 3.0f, height / 3.0f), ImGuiCond_Always); + if (!ImGui::Begin("LUA Console", &mbShow, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove + | ImGuiWindowFlags_NoSavedSettings)) + { + ImGui::End(); + return; + } + + ImGui::BeginChild("history", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); + for (int i = 0; i < mCommandHistory.size(); i++) + { + const char* msg = mCommandHistory[i].c_str(); + int len = strlen(msg); + + ImGui::TextUnformatted(msg); + } + + + ImGui::EndChild(); + ImGui::Separator(); + ImGui::InputText("command", mBuffer, LUA_CON_BUFFER_SIZE); + + if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) + ImGui::SetScrollHereY(1.0f); + ImGui::End(); + + if (Core::Input().IsKeyPressed(KeyCode::RETURN) && strlen(mBuffer) > 1) + { + mCommandHistory.push_back(mBuffer); + memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); + mbNewCommand = true; + } + } + + void LuaConsole::SetShow(bool show) + { + mbShow = show; + } + + bool LuaConsole::IsShown() const + { + return mbShow; + } + + const std::vector* LuaConsole::GetCommandHistory() const + { + return &mCommandHistory; + } + + std::string LuaConsole::GetLastCommand() + { + return mCommandHistory.back(); + } + + bool LuaConsole::GetNewCommand(std::string& command) + { + if (mbNewCommand) + { + command = mCommandHistory.back(); + mbNewCommand = false; + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/src/graphics/gui/luaConsole.h b/src/graphics/gui/luaConsole.h new file mode 100644 index 0000000..b2ee532 --- /dev/null +++ b/src/graphics/gui/luaConsole.h @@ -0,0 +1,50 @@ +/****************************************************************************** +* File - luaConsole.h +* Author - Joey Pollack +* Date - 2021/09/17 (y/m/d) +* Mod Date - 2021/09/17 (y/m/d) +* Description - GUI window for the LUA console +******************************************************************************/ + +#ifndef LUA_CONSOLE_H_ +#define LUA_CONSOLE_H_ + +#include +#include +#include + +namespace lunarium +{ + const int LUA_CON_BUFFER_SIZE = 64; + class LuaConsole + { + public: + static LuaConsole& GetInstance(); + static void FreeInstance(); + + OpRes Initialize(); + void Show(); + + void SetShow(bool show); + bool IsShown() const; + + const std::vector* GetCommandHistory() const; + std::string GetLastCommand(); + bool GetNewCommand(std::string& command); // returns true if there is a new and false if not + + + private: + static LuaConsole* mpInstance; + bool mbShow; + char mBuffer[LUA_CON_BUFFER_SIZE]; + std::vector mCommandHistory; + bool mbNewCommand; + + private: + LuaConsole(); + LuaConsole(const LuaConsole&) = delete; + const LuaConsole& operator=(const LuaConsole&) = delete; + }; +} + +#endif // LUA_CONSOLE_H_ \ No newline at end of file diff --git a/src/input/inputManager.cpp b/src/input/inputManager.cpp index 283f163..1cc518a 100644 --- a/src/input/inputManager.cpp +++ b/src/input/inputManager.cpp @@ -10,6 +10,7 @@ #include #include "inputManager.h" +#include #include #include @@ -120,13 +121,19 @@ namespace lunarium keyCode == KeyCode::MOUSE_X2_BUTTON ); } - bool InputManager::IsKeyDown(KeyCode key) + bool InputManager::IsKeyDown(KeyCode key, bool ignoreWantCapture) { + if (GUI::GetInstance().WantCaptureKeyboard() && !ignoreWantCapture) + return false; + return (mKeyboardState[key] == KEY_DOWN); } - bool InputManager::IsKeyPressed(KeyCode key) + bool InputManager::IsKeyPressed(KeyCode key, bool ignoreWantCapture) { + if (GUI::GetInstance().WantCaptureKeyboard() && !ignoreWantCapture) + return false; + for (int i = 0; i < mKeyEvents.KeysPressed.size(); i++) { if (mKeyEvents.KeysPressed[i].Key.Code == key) diff --git a/src/input/inputManager.h b/src/input/inputManager.h index 429e1a6..e3ad039 100644 --- a/src/input/inputManager.h +++ b/src/input/inputManager.h @@ -72,10 +72,10 @@ namespace lunarium public: // Returns true if a key is down and was NOT down last frame - bool IsKeyPressed(KeyCode key); + bool IsKeyPressed(KeyCode key, bool ignoreWantCapture = false); // Returns true if a key is down - bool IsKeyDown(KeyCode key); + bool IsKeyDown(KeyCode key, bool ignoreWantCapture = false); // Returns the mouse position in screen coordinates glm::vec2 GetMousePosition(); diff --git a/src/input/keyboard.cpp b/src/input/keyboard.cpp index 52399dc..0511bd1 100644 --- a/src/input/keyboard.cpp +++ b/src/input/keyboard.cpp @@ -117,6 +117,7 @@ namespace lunarium mKeyboard.insert(std::pair(KeyCode::TAB, { KeyCode::TAB, "tab", "tab", (char)(9), (char)(9) })); mKeyboard.insert(std::pair(KeyCode::RETURN, { KeyCode::RETURN, "return", "return", (char)(10), (char)(10) })); mKeyboard.insert(std::pair(KeyCode::ESCAPE, { KeyCode::ESCAPE, "escape", "escape", (char)(27), (char)(27) })); + mKeyboard.insert(std::pair(KeyCode::SPACE, { KeyCode::SPACE, "space", "space", (char)(32), (char)(32) })); // Arrow Keys mKeyboard.insert(std::pair(KeyCode::LEFT, { KeyCode::LEFT, "Left Arrow", "Left Arrow", (char)(0), (char)(0) })); diff --git a/src/tester/tester.cpp b/src/tester/tester.cpp index a60c664..ce21d76 100644 --- a/src/tester/tester.cpp +++ b/src/tester/tester.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace lunarium { @@ -27,6 +28,13 @@ namespace lunarium mLogCat = Logger::RegisterCategory("TESTER"); +#if BUILD_NO_EDITOR + Logger::Log(mLogCat, LogLevel::INFO, "BUILDING NO EDITOR!"); +#else + Logger::Log(mLogCat, LogLevel::INFO, "BUILDING WITH THE EDITOR!"); +#endif + + mTextBoxWidth = 500; // Currently the full default window size @@ -43,6 +51,11 @@ namespace lunarium void Tester::OnTick(double delta) { + if (Core::Input().IsKeyDown(KeyCode::ESCAPE)) + { + Core::GetInstance().SignalShutdown(); + } + // Textbox size adjustment if (Core::Input().IsKeyDown(KeyCode::LEFT)) { diff --git a/src/window/window.cpp b/src/window/window.cpp index 40c6b03..03cbaeb 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -177,6 +177,10 @@ namespace lunarium glfwGetFramebufferSize(mpWindow, width, height); } + void Window::GetPosition(int* x, int* y) const + { + glfwGetWindowPos(mpWindow, x, y); + } bool Window::ShouldWindowClose() const { diff --git a/src/window/window.h b/src/window/window.h index 00ca793..afa3bf3 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -38,6 +38,7 @@ namespace lunarium void ChangeDisplayMode(bool fullscreen, int xPos, int yPos, int width, int height); void GetFramebufferSize(int* width, int* height) const; + void GetPosition(int* x, int* y) const; void SetShouldCloseFlag(bool should_close); bool ShouldWindowClose() const;