Beginnings of the Tester Run Mode implemented

Changed font loading to use binary buffers
Gui_Panel_Refactor
Joeyrp 4 years ago
parent ffecb901d6
commit cc811b2a7e

@ -35,12 +35,12 @@ set(LUNARIUM_SRC
"src/graphics/opengl/glGraphics.cpp" "src/graphics/opengl/glGraphics.cpp"
"src/graphics/opengl/glText.cpp" "src/graphics/opengl/glText.cpp"
"src/graphics/opengl/glShader.cpp" "src/graphics/opengl/glShader.cpp"
"src/graphics/image.cpp"
"src/graphics/internalFont.cpp" "src/graphics/internalFont.cpp"
"src/input/keyboard.cpp" "src/input/keyboard.cpp"
"src/input/inputManager.cpp" "src/input/inputManager.cpp"
"src/graphics/gui/gui.cpp" "src/graphics/gui/gui.cpp"
"src/graphics/gui/logGui.cpp" "src/graphics/gui/logGui.cpp"
"src/assets/types/image.cpp"
) )
# add the executable # add the executable

@ -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) ✔ 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 the Image creation methods @done (9/9/2021, 2:50:20 PM)
✔ Implement Render to Texture @done (9/15/2021, 7:00:33 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: GUI:
@ -37,6 +39,10 @@ Core:
Interface Class: Interface Class:
☐ Provide Methods that give access to the C++ code ☐ 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: Game:
☐ Implement Run Mode interface class ☐ Implement Run Mode interface class
☐ Load game project data ☐ Load game project data
@ -62,6 +68,8 @@ Game:
☐ Transform ☐ Transform
☐ Image ☐ Image
☐ Animation Controller ☐ Animation Controller
☐ Collider
☐ Script
Animations: Animations:
☐ Animated Sprite class ☐ Animated Sprite class
@ -70,10 +78,48 @@ Game:
Editor: Editor:
☐ Implement Run Mode interface class ☐ Implement Run Mode interface class
☐ Reference raw asset files in a "content" folder ☐ 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: Asset Pipeline:
☐ Read through the contents folder and generate asset files in a custom format (useable by the engine) ☐ Read through the contents folder and generate asset files in a custom format (useable by the engine)
Tester: Tester:
- A special class that is used to unit-test features of the engine - A special class that is used to unit-test features of the engine

@ -10,7 +10,7 @@
#ifndef IMAGE_H_ #ifndef IMAGE_H_
#define IMAGE_H_ #define IMAGE_H_
#include "definitions.h" #include <graphics/definitions.h>
namespace lunarium namespace lunarium
{ {

@ -14,7 +14,7 @@
// Sub Systems // Sub Systems
#include <window/window.h> #include <window/window.h>
#include <graphics/image.h> //#include <assets/types/image.h>
#include <graphics/opengl/glGraphics.h> #include <graphics/opengl/glGraphics.h>
#include <graphics/gui/gui.h> #include <graphics/gui/gui.h>
#include <graphics/gui/logGui.h> #include <graphics/gui/logGui.h>
@ -123,7 +123,7 @@ namespace lunarium
} }
// RUN MODE // 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]); Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running in mode: %s", types[mState.Mode]);
if (RunMode::MODE_TEST == mState.Mode) if (RunMode::MODE_TEST == mState.Mode)
{ {
@ -135,7 +135,7 @@ namespace lunarium
if (Failed(result)) if (Failed(result))
{ {
Logger::Log(LogCategory::CORE, LogLevel::FATAL_ERROR, 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; return;
} }
@ -268,7 +268,7 @@ namespace lunarium
// mGUI.ShowDemoWindow(); // mGUI.ShowDemoWindow();
LogGui::GetInstance().Show(); LogGui::GetInstance().Show();
mpRunMode->OnRender(); mpRunMode->OnRender(mpGraphics);
mGUI.EndFrame(); mGUI.EndFrame();
mpGraphics->EndDraw(); mpGraphics->EndDraw();
@ -298,4 +298,24 @@ namespace lunarium
mbMidTextureRender = false; mbMidTextureRender = false;
return mpGraphics->EndDraw(); return mpGraphics->EndDraw();
} }
////////////////////////////////////////////////////////////
// STATIC INTERFACE
////////////////////////////////////////////////////////////
Window& Core::MainWindow()
{
return *mpInstance->mpWindow;
}
IGraphics& Core::Graphics()
{
return *mpInstance->mpGraphics;
}
InputManager& Core::Input()
{
return *mpInstance->mpInput;
}
} }

@ -66,6 +66,12 @@ namespace lunarium
InputManager::_KeyEvents mKeyEvents; InputManager::_KeyEvents mKeyEvents;
public: // SUBSYSTEM GETTERS
static Window& MainWindow();
static IGraphics& Graphics();
static InputManager& Input();
private: // HIDDEN METHODS private: // HIDDEN METHODS
Core(); Core();
@ -75,6 +81,7 @@ namespace lunarium
private: // RUN MODES private: // RUN MODES
Tester* mpTester; Tester* mpTester;
}; };
} }
#endif // CORE_H_ #endif // CORE_H_

@ -15,13 +15,14 @@
namespace lunarium namespace lunarium
{ {
class IGraphics;
class iRunMode class iRunMode
{ {
public: public:
virtual OpRes Initialize() = 0; virtual OpRes Initialize() = 0;
virtual void Shutdown() = 0; virtual void Shutdown() = 0;
virtual void OnTick(double delta) = 0; virtual void OnTick(double delta) = 0;
virtual void OnRender() = 0; virtual void OnRender(IGraphics* g) = 0;
public: // Optional Events public: // Optional Events
virtual void OnKeyPress(InputManager::KeyPress kp); virtual void OnKeyPress(InputManager::KeyPress kp);

@ -20,8 +20,6 @@ namespace lunarium
class Window; class Window;
class Image; class Image;
enum RenderTarget enum RenderTarget
{ {
RT_WINDOW, RT_WINDOW,
@ -69,7 +67,7 @@ namespace lunarium
virtual int DefaultFont() const = 0; virtual int DefaultFont() const = 0;
// For weight, 400 is normal and 700 is bold // 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: protected:

@ -9,8 +9,8 @@
#include "glGraphics.h" #include "glGraphics.h"
#include "defaultShaders.h" #include "defaultShaders.h"
#include <window/window.h> #include <window/window.h>
#include "../image.h" #include <assets/types/image.h>
#include "../internalFont.h" #include "../internalFontData.h"
#include <utils/logger.h> #include <utils/logger.h>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
@ -84,8 +84,8 @@ namespace lunarium
// Load the default internal font // Load the default internal font
const char* font = "OpenSans-Regular.ttf"; const char* font = "OpenSans-Regular.ttf";
GenerateFontFileAt(font); // GenerateFontFileAt(font);
mDefaultFont = mText.LoadFont(font); mDefaultFont = mText.LoadFont(FontData, DataSize, font);
if (mDefaultFont < 0) if (mDefaultFont < 0)
{ {
Logger::Log(LogCategory::GRAPHICS, LogLevel::WARNING, "Unable to load the default font: %s", font); 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 // 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);
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

@ -61,7 +61,7 @@ namespace lunarium
int DefaultFont() const; int DefaultFont() const;
// For weight, 400 is normal and 700 is bold // 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 private: // DATA
Window* mpWindow; Window* mpWindow;

@ -17,7 +17,7 @@
namespace lunarium 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()) if (!mTextShader.IsBuilt())
{ {
@ -39,6 +39,36 @@ namespace lunarium
return -1; 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); FT_Set_Pixel_Sizes(face, 0, 48);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction
@ -48,14 +78,16 @@ namespace lunarium
mFonts.push_back(Font()); mFonts.push_back(Font());
int fontIdx = (int)(mFonts.size() - 1); int fontIdx = (int)(mFonts.size() - 1);
mFonts.back().Name = name;
for (GLubyte c = 0; c < 128; c++) for (GLubyte c = 0; c < 128; c++)
{ {
// Load character glyph // Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) 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; continue;
} }
// Generate texture // Generate texture
GLuint texture; GLuint texture;
glGenTextures(1, &texture); glGenTextures(1, &texture);
@ -104,7 +136,7 @@ namespace lunarium
FT_Done_FreeType(ft); FT_Done_FreeType(ft);
return fontIdx; return fontIdx;
} }
OpRes glText::Initialize() OpRes glText::Initialize()
{ {

@ -12,12 +12,19 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include <string>
#include <glad/gl.h> #include <glad/gl.h>
#include "glShader.h" #include "glShader.h"
#include <utils/opRes.h> #include <utils/opRes.h>
#include <utils/types.h> #include <utils/types.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
struct FT_LibraryRec_;
typedef struct FT_LibraryRec_ * FT_Library;
struct FT_FaceRec_;
typedef struct FT_FaceRec_* FT_Face;
namespace lunarium namespace lunarium
{ {
class glText class glText
@ -25,7 +32,8 @@ namespace lunarium
public: public:
OpRes Initialize(); 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 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); 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 struct Font
{ {
std::string Name;
// The tallest character that does not go below the base line // The tallest character that does not go below the base line
// This is also the distance from the string Y position to the baseline // This is also the distance from the string Y position to the baseline
unsigned int MaxHeight; unsigned int MaxHeight;
@ -59,6 +68,9 @@ namespace lunarium
GLuint mTextVBO; GLuint mTextVBO;
glShader mTextShader; 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 private: // SHADER CODE
const char* VertShader = "#version 450 core\n\ const char* VertShader = "#version 450 core\n\
layout(location = 0) in vec4 vertex; \ layout(location = 0) in vec4 vertex; \

@ -7,6 +7,12 @@
******************************************************************************/ ******************************************************************************/
#include "tester.h" #include "tester.h"
#include <core/core.h>
#include <utils/helpers.h>
#include <utils/logger.h>
#include <graphics/igraphics.h>
#include <input/inputManager.h>
#include <assets/types/image.h>
namespace lunarium namespace lunarium
{ {
@ -17,7 +23,17 @@ namespace lunarium
OpRes Tester::Initialize() 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() void Tester::Shutdown()
@ -27,30 +43,60 @@ namespace lunarium
void Tester::OnTick(double delta) 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));

@ -10,9 +10,12 @@
#define TESTER_H_ #define TESTER_H_
#include <core/iRunMode.h> #include <core/iRunMode.h>
#include <utils/types.h>
namespace lunarium namespace lunarium
{ {
class Image;
class Tester : public iRunMode class Tester : public iRunMode
{ {
public: public:
@ -20,7 +23,7 @@ namespace lunarium
OpRes Initialize(); OpRes Initialize();
void Shutdown(); void Shutdown();
void OnTick(double delta); void OnTick(double delta);
void OnRender(); void OnRender(IGraphics* g);
private: private:
@ -28,6 +31,11 @@ namespace lunarium
const Tester& operator=(const Tester&) = delete; const Tester& operator=(const Tester&) = delete;
private: // Data private: // Data
uint32_t mLogCat;
int mTextBoxWidth;
Sizei mImageSize;
Image* mpRenderedImage;
private: // Test methods private: // Test methods
}; };

@ -83,6 +83,11 @@ namespace lunarium
{ {
return mFileSize; return mFileSize;
} }
const unsigned char* BinaryFileBuffer::GetBuffer() const
{
return mpData;
}
bool BinaryFileBuffer::Read(char * buffer, int numBytes) bool BinaryFileBuffer::Read(char * buffer, int numBytes)
{ {

@ -26,6 +26,7 @@ namespace lunarium
bool IsLoaded() const; bool IsLoaded() const;
const std::string& LoadedFileName() const; const std::string& LoadedFileName() const;
int GetFileSize() const; int GetFileSize() const;
const unsigned char* GetBuffer() const;
bool Read(char* buffer, int numBytes); bool Read(char* buffer, int numBytes);
void SeekTo(int pos); void SeekTo(int pos);

@ -38,6 +38,39 @@ namespace lunarium
return glsl_version; 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 // STRING MANIPULATION
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

@ -22,6 +22,13 @@ namespace lunarium
static std::string GetGLSLVersionString(); 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 class String
{ {
public: public:

@ -9,6 +9,7 @@
#ifndef TYPES_H_ #ifndef TYPES_H_
#define TYPES_H_ #define TYPES_H_
#include <cstdint>
#include <glm/glm.hpp> #include <glm/glm.hpp>
namespace lunarium namespace lunarium
@ -53,7 +54,7 @@ namespace lunarium
///////////////////////////////////////////////// /////////////////////////////////////////////////
// SIZE // RECTANGLE
///////////////////////////////////////////////// /////////////////////////////////////////////////
struct Rectangle struct Rectangle
{ {

Loading…
Cancel
Save