diff --git a/CMakeLists.txt b/CMakeLists.txt index 373242d..ca57cc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,12 +35,12 @@ set(LUNARIUM_SRC "src/graphics/opengl/glGraphics.cpp" "src/graphics/opengl/glText.cpp" "src/graphics/opengl/glShader.cpp" -"src/graphics/image.cpp" "src/graphics/internalFont.cpp" "src/input/keyboard.cpp" "src/input/inputManager.cpp" "src/graphics/gui/gui.cpp" "src/graphics/gui/logGui.cpp" +"src/assets/types/image.cpp" ) # add the executable diff --git a/docs/Tasks.todo b/docs/Tasks.todo index 6657a6b..4323312 100644 --- a/docs/Tasks.todo +++ b/docs/Tasks.todo @@ -14,6 +14,8 @@ 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) GUI: @@ -37,6 +39,10 @@ Core: Interface Class: ☐ 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 + Game: ☐ Implement Run Mode interface class ☐ Load game project data @@ -62,6 +68,8 @@ Game: ☐ Transform ☐ Image ☐ Animation Controller + ☐ Collider + ☐ Script Animations: ☐ Animated Sprite class @@ -70,10 +78,48 @@ Game: Editor: ☐ Implement Run Mode interface class ☐ Reference raw asset files in a "content" folder + ☐ Platform independant file browsing + + Raw Asset Loaders: + - 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 + + GUI Panels: + Project Overview (Tree view): + + Game Viewport: + + Scene View: + + Tile Map Editor: + ☐ Tile map canvas + ☐ Tile map pallete + ☐ Hideable grid + ☐ Stamp creater + + Asset Viewer: + + Properties: + + + + +Assets: + Types: + - Classes that represent each resource Types + ✔ Image class @done (9/16/2021, 2:46:34 PM) + ☐ Font class + ☐ Sound class + ☐ Script class + Loaders: + - Need class (or classes?) to load resources from the packed format that the pipeline generates -Asset Pipeline: - ☐ Read through the contents folder and generate asset files in a custom format (useable by the engine) + Asset Pipeline: + ☐ Read through the contents folder and generate asset files in a custom format (useable by the engine) Tester: - A special class that is used to unit-test features of the engine diff --git a/src/graphics/image.cpp b/src/assets/types/image.cpp similarity index 100% rename from src/graphics/image.cpp rename to src/assets/types/image.cpp diff --git a/src/graphics/image.h b/src/assets/types/image.h similarity index 94% rename from src/graphics/image.h rename to src/assets/types/image.h index 5ca62c3..e081bd3 100644 --- a/src/graphics/image.h +++ b/src/assets/types/image.h @@ -10,7 +10,7 @@ #ifndef IMAGE_H_ #define IMAGE_H_ -#include "definitions.h" +#include namespace lunarium { diff --git a/src/core/core.cpp b/src/core/core.cpp index 8e68704..4a3344b 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -14,7 +14,7 @@ // Sub Systems #include -#include +//#include #include #include #include @@ -123,7 +123,7 @@ namespace lunarium } // RUN MODE - const char* types[] = { "game", "editor", "test"}; + const char* types[] = { "game", "editor", "test" }; Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running in mode: %s", types[mState.Mode]); if (RunMode::MODE_TEST == mState.Mode) { @@ -135,7 +135,7 @@ namespace lunarium if (Failed(result)) { Logger::Log(LogCategory::CORE, LogLevel::FATAL_ERROR, - "Could not initialize the Run Mode: %s", result.Description); + "Could not initialize the Run Mode: %s", result.Description.c_str()); return; } @@ -268,7 +268,7 @@ namespace lunarium // mGUI.ShowDemoWindow(); LogGui::GetInstance().Show(); - mpRunMode->OnRender(); + mpRunMode->OnRender(mpGraphics); mGUI.EndFrame(); mpGraphics->EndDraw(); @@ -298,4 +298,24 @@ namespace lunarium mbMidTextureRender = false; return mpGraphics->EndDraw(); } + + //////////////////////////////////////////////////////////// + // STATIC INTERFACE + //////////////////////////////////////////////////////////// + + Window& Core::MainWindow() + { + return *mpInstance->mpWindow; + } + + IGraphics& Core::Graphics() + { + return *mpInstance->mpGraphics; + } + + InputManager& Core::Input() + { + return *mpInstance->mpInput; + } + } \ No newline at end of file diff --git a/src/core/core.h b/src/core/core.h index b6a384c..f3dcb1a 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -66,6 +66,12 @@ namespace lunarium InputManager::_KeyEvents mKeyEvents; + public: // SUBSYSTEM GETTERS + + static Window& MainWindow(); + static IGraphics& Graphics(); + static InputManager& Input(); + private: // HIDDEN METHODS Core(); @@ -75,6 +81,7 @@ namespace lunarium private: // RUN MODES Tester* mpTester; }; + } #endif // CORE_H_ \ No newline at end of file diff --git a/src/core/iRunMode.h b/src/core/iRunMode.h index c292813..e6d3c4b 100644 --- a/src/core/iRunMode.h +++ b/src/core/iRunMode.h @@ -15,13 +15,14 @@ namespace lunarium { + class IGraphics; class iRunMode { public: virtual OpRes Initialize() = 0; virtual void Shutdown() = 0; virtual void OnTick(double delta) = 0; - virtual void OnRender() = 0; + virtual void OnRender(IGraphics* g) = 0; public: // Optional Events virtual void OnKeyPress(InputManager::KeyPress kp); diff --git a/src/editor/asset_loaders/rawLoader.h b/src/editor/asset_loaders/rawLoader.h new file mode 100644 index 0000000..e69de29 diff --git a/src/graphics/igraphics.h b/src/graphics/igraphics.h index 4e67ec4..8720bfe 100644 --- a/src/graphics/igraphics.h +++ b/src/graphics/igraphics.h @@ -20,8 +20,6 @@ namespace lunarium class Window; class Image; - - enum RenderTarget { RT_WINDOW, @@ -69,7 +67,7 @@ namespace lunarium virtual int DefaultFont() const = 0; // For weight, 400 is normal and 700 is bold - virtual int CreateNewFont(const char* fontName, float size = 12.0f, int weight = 400) = 0; + virtual int CreateNewFont(const char* name, const unsigned char* fontData, int bufferSize, float size = 12.0f, int weight = 400) = 0; protected: diff --git a/src/graphics/opengl/glGraphics.cpp b/src/graphics/opengl/glGraphics.cpp index 65a3d6e..7c3df0d 100644 --- a/src/graphics/opengl/glGraphics.cpp +++ b/src/graphics/opengl/glGraphics.cpp @@ -9,8 +9,8 @@ #include "glGraphics.h" #include "defaultShaders.h" #include -#include "../image.h" -#include "../internalFont.h" +#include +#include "../internalFontData.h" #include #include #include @@ -84,8 +84,8 @@ namespace lunarium // Load the default internal font const char* font = "OpenSans-Regular.ttf"; - GenerateFontFileAt(font); - mDefaultFont = mText.LoadFont(font); + // GenerateFontFileAt(font); + mDefaultFont = mText.LoadFont(FontData, DataSize, font); if (mDefaultFont < 0) { Logger::Log(LogCategory::GRAPHICS, LogLevel::WARNING, "Unable to load the default font: %s", font); @@ -400,9 +400,9 @@ namespace lunarium } // For weight, 400 is normal and 700 is bold - int OglGraphics::CreateNewFont(const char* fontName, float size, int weight) + int OglGraphics::CreateNewFont(const char* name, const unsigned char* fontData, int bufferSize, float size, int weight) { - return mText.LoadFont(fontName, size, weight); + return mText.LoadFont(fontData, bufferSize, name, size, weight); } //////////////////////////////////////////////////////////// diff --git a/src/graphics/opengl/glGraphics.h b/src/graphics/opengl/glGraphics.h index 64e41a3..bb6de48 100644 --- a/src/graphics/opengl/glGraphics.h +++ b/src/graphics/opengl/glGraphics.h @@ -61,7 +61,7 @@ namespace lunarium int DefaultFont() const; // For weight, 400 is normal and 700 is bold - virtual int CreateNewFont(const char* fontName, float size = 12.0f, int weight = 400); + virtual int CreateNewFont(const char* name, const unsigned char* fontData, int bufferSize, float size = 12.0f, int weight = 400); private: // DATA Window* mpWindow; diff --git a/src/graphics/opengl/glText.cpp b/src/graphics/opengl/glText.cpp index f9115ea..873f83b 100644 --- a/src/graphics/opengl/glText.cpp +++ b/src/graphics/opengl/glText.cpp @@ -17,7 +17,7 @@ namespace lunarium { - int glText::LoadFont(const char* fontFile, float size, int weight) + int glText::LoadFont(const char* fontFile, const char* name, float size, int weight) { if (!mTextShader.IsBuilt()) { @@ -39,6 +39,36 @@ namespace lunarium return -1; } + return CreateFont(name, ft, face, size, weight); + } + + int glText::LoadFont(const unsigned char* fontData, int dataSize, const char* name, float size, int weight) + { + if (!mTextShader.IsBuilt()) + { + Logger::Log(LogCategory::GRAPHICS, LogLevel::ERROR, "Unable to load font because no text rendering shaders are loaded"); + return -1; + } + + FT_Library ft; + if (FT_Init_FreeType(&ft)) + { + Logger::Log(LogCategory::GRAPHICS, LogLevel::ERROR, "FREETYPE: Could not init FreeType Library"); + return -1; + } + + FT_Face face; + if (FT_New_Memory_Face(ft, fontData, dataSize, 0, &face)) + { + Logger::Log(LogCategory::GRAPHICS, LogLevel::ERROR, "FREETYPE: Failed to load font from buffer. Font name: %s", name); + return -1; + } + + return CreateFont(name, ft, face, size, weight); + } + + int glText::CreateFont(const char* name, FT_Library ft, FT_Face face, float size, int weight) + { FT_Set_Pixel_Sizes(face, 0, 48); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction @@ -48,14 +78,16 @@ namespace lunarium mFonts.push_back(Font()); int fontIdx = (int)(mFonts.size() - 1); + mFonts.back().Name = name; for (GLubyte c = 0; c < 128; c++) { // Load character glyph if (FT_Load_Char(face, c, FT_LOAD_RENDER)) { - Logger::Log(LogCategory::GRAPHICS, LogLevel::ERROR, "FREETYTPE: Failed to load Glyph %c from font file %s", c, fontFile); + Logger::Log(LogCategory::GRAPHICS, LogLevel::ERROR, "FREETYTPE: Failed to load Glyph %c from font: %s", c, name); continue; } + // Generate texture GLuint texture; glGenTextures(1, &texture); @@ -104,7 +136,7 @@ namespace lunarium FT_Done_FreeType(ft); return fontIdx; - } + } OpRes glText::Initialize() { diff --git a/src/graphics/opengl/glText.h b/src/graphics/opengl/glText.h index 7bd4e8a..f300265 100644 --- a/src/graphics/opengl/glText.h +++ b/src/graphics/opengl/glText.h @@ -12,12 +12,19 @@ #include #include +#include #include #include "glShader.h" #include #include #include +struct FT_LibraryRec_; +typedef struct FT_LibraryRec_ * FT_Library; + +struct FT_FaceRec_; +typedef struct FT_FaceRec_* FT_Face; + namespace lunarium { class glText @@ -25,7 +32,8 @@ namespace lunarium public: OpRes Initialize(); - int LoadFont(const char* fontFile, float size = 12.0f, int weight = 400); + int LoadFont(const char* fontFile, const char* name, float size = 12.0f, int weight = 400); + int LoadFont(const unsigned char* fontData, int dataSize, const char* name, float size = 12.0f, int weight = 400); void DrawString(int fontID, const char* text, glm::vec2 position, Color color, float scale, glm::mat4 projection); void DrawString(int fontID, const char* text, glm::vec2 topLeft, glm::vec2 botRight, Color color, float scale, glm::mat4 projection); @@ -42,6 +50,7 @@ namespace lunarium struct Font { + std::string Name; // The tallest character that does not go below the base line // This is also the distance from the string Y position to the baseline unsigned int MaxHeight; @@ -59,6 +68,9 @@ namespace lunarium GLuint mTextVBO; glShader mTextShader; + private: // HELPERS + int CreateFont(const char* name, FT_Library ft, FT_Face face, float size = 12.0f, int weight = 400); + private: // SHADER CODE const char* VertShader = "#version 450 core\n\ layout(location = 0) in vec4 vertex; \ diff --git a/src/tester/tester.cpp b/src/tester/tester.cpp index 49f8a5f..a60c664 100644 --- a/src/tester/tester.cpp +++ b/src/tester/tester.cpp @@ -7,6 +7,12 @@ ******************************************************************************/ #include "tester.h" +#include +#include +#include +#include +#include +#include namespace lunarium { @@ -17,7 +23,17 @@ namespace lunarium OpRes Tester::Initialize() { - return OpRes::Fail("Tester::Initialize not implemented"); + // return OpRes::Fail("Tester::Initialize not implemented"); + + mLogCat = Logger::RegisterCategory("TESTER"); + + mTextBoxWidth = 500; + + // Currently the full default window size + mImageSize.Width = 1280; + mImageSize.Height = 720; + + return OpRes::OK(); } void Tester::Shutdown() @@ -27,30 +43,60 @@ namespace lunarium void Tester::OnTick(double delta) { + // Textbox size adjustment + if (Core::Input().IsKeyDown(KeyCode::LEFT)) + { + mTextBoxWidth -= 10; + } + + if (Core::Input().IsKeyDown(KeyCode::RIGHT)) + { + mTextBoxWidth += 10; + } + + mTextBoxWidth = Math::ClampI(mTextBoxWidth, 20, 500); + + // Image Size adjustment + if (Core::Input().IsKeyDown(KeyCode::DOWN)) + { + mImageSize.Width -= 10; + mImageSize.Height -= 10; + } + if (Core::Input().IsKeyDown(KeyCode::UP)) + { + mImageSize.Width += 10; + mImageSize.Height += 10; + } + + mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280); + mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720); + + // Render to texture testing + OpRes result = Core::GetInstance().BeginRenderToTexture(); + if (Failed(result)) + { + Logger::Log(mLogCat, LogLevel::WARNING, "Unable to render to texture: %s", result.Description.c_str()); + return; + } + + IGraphics& g = Core::Graphics(); + + g.DrawFilledEllipse(glm::vec2(600, 300), glm::vec2(100, 150), Color(1.0f, 0.0f, 1.0f, 1.0f), 100); + g.DrawString("This is a test of the text renderer!", Rectangle(100, 200, mTextBoxWidth, 300), + Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f, g.DefaultFont()); + + mpRenderedImage = Core::GetInstance().EndRenderToTexture(); } - void Tester::OnRender() + void Tester::OnRender(IGraphics* g) { + g->DrawImage(*mpRenderedImage, Rectangle(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()), + Rectangle(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f)); + g->DrawBox(Rectangle(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(0.0f, 0.0f, 0.0f, 1.0f), 1.0f); } } - // DEBUG: Graphics testing - // static int width = 500; - // if (mpInput->IsKeyDown(KeyCode::LEFT)) - // { - // width -= 10; - // } - - // if (mpInput->IsKeyDown(KeyCode::RIGHT)) - // { - // width += 10; - // } - -// 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()); -// mpGraphics->DrawImage(*pImage, glm::vec2(0.0f, 0.0f), Color(1.0f, 1.0f, 1.0f, 1.0f)); \ No newline at end of file diff --git a/src/tester/tester.h b/src/tester/tester.h index 7a4b7c2..168a76a 100644 --- a/src/tester/tester.h +++ b/src/tester/tester.h @@ -10,9 +10,12 @@ #define TESTER_H_ #include +#include namespace lunarium { + class Image; + class Tester : public iRunMode { public: @@ -20,7 +23,7 @@ namespace lunarium OpRes Initialize(); void Shutdown(); void OnTick(double delta); - void OnRender(); + void OnRender(IGraphics* g); private: @@ -28,6 +31,11 @@ namespace lunarium const Tester& operator=(const Tester&) = delete; private: // Data + uint32_t mLogCat; + int mTextBoxWidth; + Sizei mImageSize; + Image* mpRenderedImage; + private: // Test methods }; diff --git a/src/utils/binaryFileBuffer.cpp b/src/utils/binaryFileBuffer.cpp index 3192815..9212041 100644 --- a/src/utils/binaryFileBuffer.cpp +++ b/src/utils/binaryFileBuffer.cpp @@ -83,6 +83,11 @@ namespace lunarium { return mFileSize; } + + const unsigned char* BinaryFileBuffer::GetBuffer() const + { + return mpData; + } bool BinaryFileBuffer::Read(char * buffer, int numBytes) { diff --git a/src/utils/binaryFileBuffer.h b/src/utils/binaryFileBuffer.h index 499049b..74b5b2a 100644 --- a/src/utils/binaryFileBuffer.h +++ b/src/utils/binaryFileBuffer.h @@ -26,6 +26,7 @@ namespace lunarium bool IsLoaded() const; const std::string& LoadedFileName() const; int GetFileSize() const; + const unsigned char* GetBuffer() const; bool Read(char* buffer, int numBytes); void SeekTo(int pos); diff --git a/src/utils/helpers.cpp b/src/utils/helpers.cpp index f6a81a2..7d56209 100644 --- a/src/utils/helpers.cpp +++ b/src/utils/helpers.cpp @@ -38,6 +38,39 @@ namespace lunarium return glsl_version; } + //////////////////////////////////////////////////////////// + // MATH FUNCTIONS + //////////////////////////////////////////////////////////// + int Math::ClampI(int value, int min, int max) + { + if (value < min) + { + return min; + } + + if (value > max) + { + return max; + } + + return value; + } + + float Math::ClampF(float value, float min, float max) + { + if (value < min) + { + return min; + } + + if (value > max) + { + return max; + } + + return value; + } + //////////////////////////////////////////////////////////// // STRING MANIPULATION //////////////////////////////////////////////////////////// diff --git a/src/utils/helpers.h b/src/utils/helpers.h index e8ac3e7..2aa8ee6 100644 --- a/src/utils/helpers.h +++ b/src/utils/helpers.h @@ -22,6 +22,13 @@ namespace lunarium static std::string GetGLSLVersionString(); }; + class Math + { + public: + static int ClampI(int value, int min, int max); + static float ClampF(float value, float min, float max); + }; + class String { public: diff --git a/src/utils/types.h b/src/utils/types.h index 1661b91..9cdbee8 100644 --- a/src/utils/types.h +++ b/src/utils/types.h @@ -9,6 +9,7 @@ #ifndef TYPES_H_ #define TYPES_H_ +#include #include namespace lunarium @@ -53,7 +54,7 @@ namespace lunarium ///////////////////////////////////////////////// - // SIZE + // RECTANGLE ///////////////////////////////////////////////// struct Rectangle {