From 8f3524ae4edd580c667877c7463416958e43eec7 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Thu, 9 Sep 2021 16:00:36 -0400 Subject: [PATCH] Reorganized some files to avoid directly including glad/gl and glfw CreateImage method implemented --- CMakeLists.txt | 2 +- docs/Tasks.todo | 9 ++++++--- src/core/core.cpp | 22 ++++++++++++++------- src/core/core.h | 8 ++++++-- src/graphics/igraphics.h | 1 + src/graphics/opengl/glGraphics.cpp | 31 ++++++++++++++++++++++++++++-- src/graphics/opengl/glGraphics.h | 1 + src/input/inputManager.cpp | 26 ++++++++++++++++++++++++- src/input/inputManager.h | 5 +++++ src/input/keyboard.h | 2 +- src/{input => window}/keyCodes.h | 4 ++-- src/{core => window}/window.cpp | 0 src/{core => window}/window.h | 0 13 files changed, 92 insertions(+), 19 deletions(-) rename src/{input => window}/keyCodes.h (98%) rename src/{core => window}/window.cpp (100%) rename src/{core => window}/window.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ac09218..2ae5413 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ set(LUNARIUM_SRC "src/core/core.cpp" "src/core/state.cpp" "src/core/version.cpp" -"src/core/window.cpp" +"src/window/window.cpp" "src/utils/types.cpp" "src/utils/logger.cpp" "src/utils/highResTimer.cpp" diff --git a/docs/Tasks.todo b/docs/Tasks.todo index 825cb56..eb0059f 100644 --- a/docs/Tasks.todo +++ b/docs/Tasks.todo @@ -6,16 +6,19 @@ Core: ☐ Add log settings to the state file Graphics: - ☐ Dear ImGui class with basic initialization ✔ Decide on a font/text rendering system @done (9/7/2021, 1:39:53 PM) ✔ Add FreeType to the project @done (9/7/2021, 2:23:13 PM) ✔ Add a new class for font loading/management and text rendering @done (9/7/2021, 3:57:08 PM) ✔ 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 + ✔ Implement the Image creation methods @done (9/9/2021, 2:50:20 PM) + + + Dear ImGui: + ☐ Dear ImGui class with basic initialization Input: ✔ Port over the Element2D input system and adjust it to use glfw @done (9/8/2021, 8:20:07 PM) - ☐ Add the InputManager to the core + ✔ Add the InputManager to the core @done (9/9/2021, 2:57:06 PM) Audio: diff --git a/src/core/core.cpp b/src/core/core.cpp index f9ddc3f..db86bc4 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -10,14 +10,14 @@ #include "version.h" // Sub Systems -#include "window.h" +#include #include namespace lunarium { Core* Core::mpInstance = nullptr; Core::Core() - : mbIsInit(false), mpArgs(nullptr), mpWindow(nullptr), mpGraphics(nullptr) + : mbIsInit(false), mpArgs(nullptr), mpWindow(nullptr), mpGraphics(nullptr), mpInput(nullptr) { } @@ -40,6 +40,10 @@ namespace lunarium Logger::Log(LogCategory::CORE, LogLevel::INFO, "Lunarium is shutting down!"); // Shutdown subsystems + mpInstance->mpInput->Shutdown(); + delete mpInstance->mpInput; + mpInstance->mpInput = nullptr; + mpInstance->mpGraphics->Shutdown(); delete mpInstance->mpGraphics; mpInstance->mpGraphics = nullptr; @@ -134,8 +138,11 @@ namespace lunarium return; } - // DEBUG - mpGraphics->SetClearColor(Color(0.0f, 0.0f, 0.0f, 1.0f)); + mpGraphics->SetClearColor(Color(0.5f, 0.5f, 0.75f, 1.0f)); + + // INPUT + mpInput = new InputManager; + mpInput->Initialize(mpWindow); mbIsInit = true; } @@ -166,21 +173,22 @@ namespace lunarium // Poll input Window::PollEvents(); + mKeyEvents = mpInput->PollKeys(); // HACK: Temporary solution to close the program - if (glfwGetKey(mpWindow->GetWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS) + if (mpInput->IsKeyDown(KeyCode::ESCAPE)) { mpWindow->SetShouldCloseFlag(true); } // DEBUG: Graphics testing static int width = 500; - if (glfwGetKey(mpWindow->GetWindow(), GLFW_KEY_LEFT) == GLFW_PRESS) + if (mpInput->IsKeyDown(KeyCode::LEFT)) { width -= 10; } - if (glfwGetKey(mpWindow->GetWindow(), GLFW_KEY_RIGHT) == GLFW_PRESS) + if (mpInput->IsKeyDown(KeyCode::RIGHT)) { width += 10; } diff --git a/src/core/core.h b/src/core/core.h index df12fcc..92c298f 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -10,6 +10,7 @@ #define CORE_H_ #include "state.h" +#include #include #include #include @@ -47,8 +48,11 @@ namespace lunarium private: // SUBSYSTEMS - Window* mpWindow; - IGraphics* mpGraphics; + Window* mpWindow; + IGraphics* mpGraphics; + InputManager* mpInput; + + InputManager::_KeyEvents mKeyEvents; private: // HIDDEN METHODS diff --git a/src/graphics/igraphics.h b/src/graphics/igraphics.h index 72c74b0..c2dfee6 100644 --- a/src/graphics/igraphics.h +++ b/src/graphics/igraphics.h @@ -60,6 +60,7 @@ namespace lunarium // Takes raw image data and creates and Image class instance out of it. // The raw data must be in one of the ImageFormats and it must be 1 byte per channel. virtual Image* CreateImage(const unsigned char* pData, int width, int height, ImageFormat format) = 0; + virtual void DestroyImage(Image* i) = 0; // Fonts virtual int DefaultFont() const = 0; diff --git a/src/graphics/opengl/glGraphics.cpp b/src/graphics/opengl/glGraphics.cpp index c4ac9d9..08caf6e 100644 --- a/src/graphics/opengl/glGraphics.cpp +++ b/src/graphics/opengl/glGraphics.cpp @@ -8,7 +8,7 @@ #include "glGraphics.h" #include "defaultShaders.h" -#include +#include #include "../image.h" #include "../internalFont.h" #include @@ -318,9 +318,36 @@ namespace lunarium // The raw data must be in one of the ImageFormats and it must be 1 byte per channel. Image* OglGraphics::CreateImage(const unsigned char* pData, int width, int height, ImageFormat format) { - return nullptr; + unsigned int glFormat[4] = { GL_RGB, GL_RGBA, GL_BGR, GL_BGRA }; + unsigned int textureID = 0; + glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_2D, textureID); + + glTexImage2D(GL_TEXTURE_2D, 0, glFormat[format], width, height, 0, glFormat[format], GL_UNSIGNED_BYTE, pData); + glGenerateMipmap(GL_TEXTURE_2D); + + + // TODO: Move this to a different function to allow for more user options + // Or make a version of the method that takes a struct for these options + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + Image* image = new Image; + image->mWidth = width; + image->mHeight = height; + image->mGLTextureID = textureID; + + return image; } + void OglGraphics::DestroyImage(Image* i) + { + i->FreeRawData(); + delete i; + } + // Fonts int OglGraphics::DefaultFont() const { diff --git a/src/graphics/opengl/glGraphics.h b/src/graphics/opengl/glGraphics.h index 37ead13..68068f1 100644 --- a/src/graphics/opengl/glGraphics.h +++ b/src/graphics/opengl/glGraphics.h @@ -52,6 +52,7 @@ namespace lunarium // Takes raw image data and creates and Image class instance out of it. // The raw data must be in one of the ImageFormats and it must be 1 byte per channel. virtual Image* CreateImage(const unsigned char* pData, int width, int height, ImageFormat format); + virtual void DestroyImage(Image* i); // Fonts int DefaultFont() const; diff --git a/src/input/inputManager.cpp b/src/input/inputManager.cpp index 5cf24ee..f0c9d64 100644 --- a/src/input/inputManager.cpp +++ b/src/input/inputManager.cpp @@ -8,7 +8,7 @@ * ******************************************************************************/ -#include +#include #include "inputManager.h" #include @@ -25,8 +25,19 @@ namespace lunarium mpWindow = pWindow; } + void InputManager::Shutdown() + { + mpWindow = nullptr; + } + const InputManager::_KeyEvents& InputManager::PollKeys() { + if (!mpWindow) + { + Logger::Log(LogCategory::CORE, LogLevel::WARNING, "InputManager::PollKeys called but InputManager is not initialized!"); + return mKeyEvents; + } + mKeyEvents.KeysPressed.clear(); mKeyEvents.KeysReleased.clear(); @@ -112,6 +123,19 @@ namespace lunarium return (mKeyboardState[key] == KEY_DOWN); } + bool InputManager::IsKeyPressed(KeyCode key) + { + for (int i = 0; i < mKeyEvents.KeysPressed.size(); i++) + { + if (mKeyEvents.KeysPressed[i].Key.Code == key) + { + return true; + } + } + + return false; + } + // TODO: FIX WINDOWS SPECIFIC CODE: glm::vec2 InputManager::GetMousePosition() { diff --git a/src/input/inputManager.h b/src/input/inputManager.h index fe0a369..204c328 100644 --- a/src/input/inputManager.h +++ b/src/input/inputManager.h @@ -58,6 +58,7 @@ namespace lunarium private: // ENGINE ONLY METHODS InputManager(); void Initialize(Window* pWindow); + void Shutdown(); // Returns an array of keys that were pressed // Keys are only pressed if they were not held on the previous frame @@ -70,6 +71,10 @@ namespace lunarium public: + // Returns true if a key is down and was NOT down last frame + bool IsKeyPressed(KeyCode key); + + // Returns true if a key is down bool IsKeyDown(KeyCode key); // Returns the mouse position in screen coordinates diff --git a/src/input/keyboard.h b/src/input/keyboard.h index 6bdacd2..0a9ae29 100644 --- a/src/input/keyboard.h +++ b/src/input/keyboard.h @@ -14,7 +14,7 @@ #include #include -#include "keyCodes.h" +#include namespace lunarium { diff --git a/src/input/keyCodes.h b/src/window/keyCodes.h similarity index 98% rename from src/input/keyCodes.h rename to src/window/keyCodes.h index a08bb20..961e1f1 100644 --- a/src/input/keyCodes.h +++ b/src/window/keyCodes.h @@ -10,7 +10,7 @@ #ifndef KEY_CODES_H_ #define KEY_CODES_H_ -#include +#include "window.h" namespace lunarium { @@ -129,7 +129,7 @@ namespace lunarium CONTROL = GLFW_KEY_LEFT_CONTROL, ALT = GLFW_KEY_LEFT_ALT, - KEY_UNKNOWN = 0xFF + KEY_UNKNOWN = 0xFFFF }; static int KeyCodeList[] = { diff --git a/src/core/window.cpp b/src/window/window.cpp similarity index 100% rename from src/core/window.cpp rename to src/window/window.cpp diff --git a/src/core/window.h b/src/window/window.h similarity index 100% rename from src/core/window.h rename to src/window/window.h