From 7ca93ababf1b0de970e6e91630387f428c0c2b92 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Wed, 27 Oct 2021 14:42:58 -0400 Subject: [PATCH] Separated out assets and utils into their own libraries --- CMakeLists.txt | 25 +++++------ docs/Tasks.todo | 41 ++++++++++--------- src/assets/CMakeLists.txt | 11 +++++ .../rawLoader.h => assets/assetManager.cpp} | 0 src/{graphics => assets}/definitions.h | 0 src/assets/types/image.cpp | 28 ++++++++++++- src/assets/types/image.h | 15 ++++--- src/core/core.cpp | 1 + src/core/core.h | 1 + src/graphics/igraphics.h | 6 +-- src/graphics/opengl/glGraphics.cpp | 34 ++++++--------- src/graphics/opengl/glGraphics.h | 5 +-- src/utils/CMakeLists.txt | 9 ++++ src/utils/helpers.cpp | 2 +- src/utils/helpers.h | 1 + 15 files changed, 106 insertions(+), 73 deletions(-) create mode 100644 src/assets/CMakeLists.txt rename src/{editor/asset_loaders/rawLoader.h => assets/assetManager.cpp} (100%) rename src/{graphics => assets}/definitions.h (100%) create mode 100644 src/utils/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index e3ee2ee..153781d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,16 +30,6 @@ set(LUNARIUM_SRC "src/core/iRunMode.cpp" "src/window/window.cpp" "src/tester/tester.cpp" -"src/utils/types.cpp" -"src/utils/logger.cpp" -"src/utils/highResTimer.cpp" -"src/utils/helpers.cpp" -"src/utils/opRes.cpp" -"src/utils/args.cpp" -"src/utils/binaryFileBuffer.cpp" -"src/utils/frameCounter.cpp" -"src/utils/stb/stb_image.cpp" -"src/utils/stb/stb_image_write.cpp" "src/graphics/opengl/glGraphics.cpp" "src/graphics/opengl/glText.cpp" "src/graphics/opengl/glShader.cpp" @@ -51,9 +41,6 @@ set(LUNARIUM_SRC "src/graphics/gui/luaConsole.cpp" "src/scripting/scriptManager.cpp" "src/scripting/coreAPI.cpp" -"src/assets/types/image.cpp" -"src/assets/types/asset.cpp" -"src/assets/loaders/binary/assetIndex.cpp" ) # add the executable @@ -108,6 +95,12 @@ add_subdirectory(external/glm) # add dearimgui add_subdirectory(src/graphics/dearimgui) +# add utils +add_subdirectory(src/utils) + +# add assets +add_subdirectory(src/assets) + # add lua -- https://github.com/walterschell/Lua add_subdirectory(external/lua) @@ -134,12 +127,14 @@ target_link_directories(${PROJECT_NAME} PRIVATE external/glfw/src PRIVATE external/glm PRIVATE src/graphics/dearimgui + PRIVATE src/utils + PRIVATE src/assets PRIVATE external/glad/src PRIVATE external/freetype/src ) if(WIN32) - target_link_libraries(${PROJECT_NAME} opengl32.lib glfw glad glm dearimgui lua_static pugixml freetype) + target_link_libraries(${PROJECT_NAME} opengl32.lib glfw glad glm dearimgui utils assets lua_static pugixml freetype) elseif(UNIX) - target_link_libraries(${PROJECT_NAME} X11 GL glfw glad glm dearimgui lua_static pugixml freetype) + target_link_libraries(${PROJECT_NAME} X11 GL glfw glad glm dearimgui utils assets lua_static pugixml freetype) endif() diff --git a/docs/Tasks.todo b/docs/Tasks.todo index aefad8a..0fe74e9 100644 --- a/docs/Tasks.todo +++ b/docs/Tasks.todo @@ -24,7 +24,7 @@ Core: ✔ Debug log window @done (9/10/2021, 4:44:48 PM) ✔ Add key to show debug log window @done (9/13/2021, 6:47:44 PM) ☐ Add checkboxes to disable log categories and levels - ☐ Add LUA Console window + ✔ Add LUA Console window @done (10/26/2021, 4:43:41 PM) Input: ✔ Port over the Element2D input system and adjust it to use glfw @done (9/8/2021, 8:20:07 PM) @@ -41,8 +41,8 @@ Core: ☐ Provide Methods that give access to the C++ code Utils: - ☐ Make Logger fully static (no need to ever GetInstance) - ☐ Need to add a static initialize method + ✔ Make Logger fully static (no need to ever GetInstance) @done (10/26/2021, 4:43:55 PM) + ✔ Need to add a static initialize method @done (10/26/2021, 4:43:57 PM) Game: ☐ Implement Run Mode interface class @@ -76,19 +76,20 @@ Game: ☐ Animated Sprite class -Editor: +Editor: @high ✔ Come up with project directory structure @done (9/17/2021, 6:46:44 PM) - ✔ Implement Run Mode interface class @done (10/25/2021, 6:54:36 PM) - ☐ Reference raw asset files in a "content" folder + ☐ Make the editor a separate module @high + ☐ Implement Run Mode interface class @high + ☐ Reference raw asset files in a "content" folder@high ☐ Platform independant file browsing ☐ Scan script files to make sure they don't overwrite globals Raw Asset Importers: - - Need classes to load raw resource files for the editor - ☐ Raw Resource loader interface class - ☐ Raw Image loader class - ☐ Raw Sound loader class - ☐ Raw font file loader class + - Need classes to import raw resource files for the editor + ☐ Raw Resource importer interface class + ☐ Raw Image importer class + ☐ Raw Sound importer class + ☐ Raw font file importer class GUI Panels: Project Overview (Tree view): @@ -108,12 +109,12 @@ Editor: Properties: - - Assets: Types: - Classes that represent each resource Types ✔ Image class @done (9/16/2021, 2:46:34 PM) + ☐ Decouple Image class from OGLRenderer @high + - Give the renderer a register image method that will take an Image and finish setting it up ☐ Font class ☐ Sound class ☐ Script class @@ -132,11 +133,11 @@ Assets: Tester: - A special class that is used to unit-test features of the engine ✔ Implement Run Mode interface class @done (10/25/2021, 7:37:00 PM) - ☐ Needs a timer to keep track of how long a test has run - ☐ Main Tick method should use the timer to determine when to switch to the next test - ☐ Add function for testing render to Texture - ☐ Add function for testing input - ☐ Add function for testing text rendering - ☐ Add function for testing shape drawing - ☐ Add function for testing image drawing + ✘ Needs a timer to keep track of how long a test has run @cancelled (10/26/2021, 4:36:45 PM) + ✘ Main Tick method should use the timer to determine when to switch to the next test @cancelled (10/26/2021, 4:36:16 PM) + ✔ Add function for testing render to Texture @done (10/26/2021, 4:35:52 PM) + ✔ Add function for testing input @done (10/26/2021, 4:36:05 PM) + ✔ Add function for testing text rendering @done (10/26/2021, 4:35:54 PM) + ✔ Add function for testing shape drawing @done (10/26/2021, 4:35:58 PM) + ✔ Add function for testing image drawing @done (10/26/2021, 4:36:00 PM) \ No newline at end of file diff --git a/src/assets/CMakeLists.txt b/src/assets/CMakeLists.txt new file mode 100644 index 0000000..12904f1 --- /dev/null +++ b/src/assets/CMakeLists.txt @@ -0,0 +1,11 @@ +add_library(assets assetManager.cpp types/asset.cpp types/image.cpp loaders/assetIndex.cpp) + +# add utils +target_link_directories(assets + PRIVATE ../utils +) + +target_include_directories(assets + PUBLIC ../ + PUBLIC ../../external/glm +) \ No newline at end of file diff --git a/src/editor/asset_loaders/rawLoader.h b/src/assets/assetManager.cpp similarity index 100% rename from src/editor/asset_loaders/rawLoader.h rename to src/assets/assetManager.cpp diff --git a/src/graphics/definitions.h b/src/assets/definitions.h similarity index 100% rename from src/graphics/definitions.h rename to src/assets/definitions.h diff --git a/src/assets/types/image.cpp b/src/assets/types/image.cpp index 5186063..189520a 100644 --- a/src/assets/types/image.cpp +++ b/src/assets/types/image.cpp @@ -14,7 +14,7 @@ namespace lunarium { Image::Image() - : Asset(AssetType::ASSET_TYPE_IMAGE), mRawData(nullptr), mRawDataSize(0), mWidth(0), mHeight(0) + : Asset(AssetType::ASSET_TYPE_IMAGE), mRawData(nullptr), mRawDataSize(0), mWidth(0), mHeight(0), mGLTextureID((unsigned)-1) { } @@ -71,6 +71,32 @@ namespace lunarium return mFormat; } + const unsigned char* Image::GetData() const + { + return mRawData; + } + + + void Image::SetWidth(int width) + { + mWidth = width; + } + + void Image::SetHeight(int height) + { + mHeight = height; + } + + void Image::SetFormat(ImageFormat format) + { + mFormat = format; + } + + void Image::SetGLTextureID(unsigned int id) + { + mGLTextureID = id; + } + void Image::FreeRawData() { delete[] mRawData; diff --git a/src/assets/types/image.h b/src/assets/types/image.h index 96127ce..53ef031 100644 --- a/src/assets/types/image.h +++ b/src/assets/types/image.h @@ -10,29 +10,32 @@ #ifndef IMAGE_H_ #define IMAGE_H_ -#include +#include #include "asset.h" namespace lunarium { - class OglGraphics; class Image : public Asset { - friend OglGraphics; - public: + Image(); + ~Image(); unsigned int GetGLTextureID() const; int GetWidth() const; int GetHeight() const; ImageFormat GetFormat() const; + const unsigned char* GetData() const; + + void SetGLTextureID(unsigned int id); + void SetWidth(int width); + void SetHeight(int height); + void SetFormat(ImageFormat format); void FreeRawData(); private: - Image(); - ~Image(); Image(const Image& rhs); const Image& operator=(const Image& rhs); diff --git a/src/core/core.cpp b/src/core/core.cpp index b63e62f..0064a37 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -10,6 +10,7 @@ #include "version.h" #include +#include // Run modes #include diff --git a/src/core/core.h b/src/core/core.h index 412af70..585d1e2 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -44,6 +44,7 @@ namespace lunarium OpRes BeginRenderToTexture(); Image* EndRenderToTexture(); + private: // DATA static Core* mpInstance; bool mbIsInit; diff --git a/src/graphics/igraphics.h b/src/graphics/igraphics.h index 8720bfe..4ba61e5 100644 --- a/src/graphics/igraphics.h +++ b/src/graphics/igraphics.h @@ -10,7 +10,6 @@ #ifndef IGRAPHICS_H_ #define IGRAPHICS_H_ -#include "definitions.h" #include #include @@ -58,10 +57,7 @@ namespace lunarium virtual void DrawImage(Image& image, Rectangle source, Rectangle destination, Color color) = 0; virtual void DrawString(const char* string, Rectangle boundingArea, Color color, float scale = 1.0f, int font = 0) = 0; - // 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; + virtual void RegisterImage(Image& image) = 0; // Fonts virtual int DefaultFont() const = 0; diff --git a/src/graphics/opengl/glGraphics.cpp b/src/graphics/opengl/glGraphics.cpp index 920edf7..0a62fc7 100644 --- a/src/graphics/opengl/glGraphics.cpp +++ b/src/graphics/opengl/glGraphics.cpp @@ -145,7 +145,7 @@ namespace lunarium if (mRT == RenderTarget::RT_IMAGE) { glBindFramebuffer(GL_FRAMEBUFFER, 0); - glBindTexture(GL_TEXTURE_2D, mpFBTexture->mGLTextureID); + glBindTexture(GL_TEXTURE_2D, mpFBTexture->GetGLTextureID()); // Buffer width and height (in pixels) is the same as the screen size // Need to multiply these by the number bytes per pixel @@ -158,9 +158,9 @@ namespace lunarium //glSetTexImage() mpFBTexture->FreeRawData(); - mpFBTexture->mWidth = mFBWidth; - mpFBTexture->mHeight = mFBHeight; - mpFBTexture->mFormat = ImageFormat::RGBA; + mpFBTexture->SetWidth(mFBWidth); + mpFBTexture->SetHeight(mFBHeight); + mpFBTexture->SetFormat(ImageFormat::RGBA); // return a copy of the image return mpFBTexture; @@ -369,14 +369,15 @@ 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. - Image* OglGraphics::CreateImage(const unsigned char* pData, int width, int height, ImageFormat format) + void OglGraphics::RegisterImage(Image& image) { 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); + glTexImage2D(GL_TEXTURE_2D, 0, glFormat[image.GetFormat()], image.GetWidth(), + image.GetHeight(), 0, glFormat[image.GetFormat()], GL_UNSIGNED_BYTE, image.GetData()); glGenerateMipmap(GL_TEXTURE_2D); @@ -387,20 +388,9 @@ namespace lunarium 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; + image.SetGLTextureID(textureID); } - void OglGraphics::DestroyImage(Image* i) - { - i->FreeRawData(); - delete i; - } - // Fonts int OglGraphics::DefaultFont() const { @@ -537,15 +527,17 @@ namespace lunarium glBindFramebuffer(GL_FRAMEBUFFER, mFBO); // Texture - glGenTextures(1, &mpFBTexture->mGLTextureID); - glBindTexture(GL_TEXTURE_2D, mpFBTexture->mGLTextureID); + unsigned int id = -1; + glGenTextures(1, &id); + mpFBTexture->SetGLTextureID(id); + glBindTexture(GL_TEXTURE_2D, mpFBTexture->GetGLTextureID()); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mFBWidth, mFBHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_2D, 0); // Attach texture - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mpFBTexture->mGLTextureID, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mpFBTexture->GetGLTextureID(), 0); // Render Buffer for depth/stencil testing glGenRenderbuffers(1, &mRBO); diff --git a/src/graphics/opengl/glGraphics.h b/src/graphics/opengl/glGraphics.h index bb6de48..b237e6c 100644 --- a/src/graphics/opengl/glGraphics.h +++ b/src/graphics/opengl/glGraphics.h @@ -52,10 +52,7 @@ namespace lunarium virtual void DrawImage(Image& image, Rectangle source, Rectangle destination, Color color); virtual void DrawString(const char* string, Rectangle boundingArea, Color color, float scale = 1.0f, int font = 0); - // 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); + virtual void RegisterImage(Image& image); // Fonts int DefaultFont() const; diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 0000000..9fdafd4 --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1,9 @@ +add_library(utils stb/stb_image_write.cpp stb/stb_image.cpp args.cpp binaryFileBuffer.cpp frameCounter.cpp + helpers.cpp highResTimer.cpp logger.cpp opRes.cpp types.cpp) + +target_include_directories(utils + PUBLIC "${PROJECT_BINARY_DIR}" + PUBLIC ../../external/glm +) + +message( " current source dir: ${CMAKE_CURRENT_SOURCE_DIR}" ) \ No newline at end of file diff --git a/src/utils/helpers.cpp b/src/utils/helpers.cpp index 7d56209..eff3e16 100644 --- a/src/utils/helpers.cpp +++ b/src/utils/helpers.cpp @@ -7,7 +7,6 @@ ******************************************************************************/ #include "helpers.h" -#include #include #ifdef WIN32 @@ -29,6 +28,7 @@ namespace lunarium #endif // WIN32 } + std::string System::GetGLSLVersionString() { std::string glsl_version = "#version "; diff --git a/src/utils/helpers.h b/src/utils/helpers.h index 2aa8ee6..ec3785b 100644 --- a/src/utils/helpers.h +++ b/src/utils/helpers.h @@ -10,6 +10,7 @@ #define HELPERS_H_ #include "types.h" +#include #include #include