diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a23d59..2726c90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,7 @@ set(LUNARIUM_SRC "src/platform/terminal.cpp" "src/renderer/render_context.cpp" "src/renderer/renderer2D.cpp" +"src/renderer/text_renderer.cpp" "src/renderer/vertex_buffer.cpp" "src/renderer/index_buffer.cpp" "src/renderer/orthographic_camera.cpp" diff --git a/src/renderer/renderer2D.cpp b/src/renderer/renderer2D.cpp index dcbcf3c..b08e9d7 100644 --- a/src/renderer/renderer2D.cpp +++ b/src/renderer/renderer2D.cpp @@ -9,6 +9,8 @@ #include "renderer2D.h" #include "orthographic_camera.h" #include "shaders/defaultShaders.h" +#include +#include #include #include @@ -37,6 +39,13 @@ namespace lunarium OpRes Renderer2D::Initialize() { + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction for texture loading + glPixelStorei(GL_PACK_ALIGNMENT, 1); // Disable byte-alignment restriction for texture storing + + float max_tex_size = 0.0f; + glGetFloatv(GL_MAX_TEXTURE_SIZE, &max_tex_size); + Logger::Info(LogCategory::GRAPHICS, "OpenGL max texture size is: %f", max_tex_size); + mpCamera = nullptr; u32* indices = new u32[mQuadData.MaxIndices]; @@ -60,6 +69,7 @@ namespace lunarium mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // translation mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // angle mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // Scale + mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // tex_is_grey_scale mQuadData.mVertexBuffer = VertexBuffer::Create(mQuadData.mBufferLayout, mQuadData.MaxVertices, nullptr, mQuadData.MaxIndices, indices); // RAW VERTEX BUFFER @@ -87,11 +97,17 @@ namespace lunarium // TODO: INIT SPRITE DATA - // TODO: INIT DEBUG TEXTURE + // TODO: INIT WHITE TEXTURE u8 data[4] = {255, 255, 255, 255}; mpWhiteTexture = Texture::Create(data, 1, 1); mLoadedTextures.push_back(mpWhiteTexture); + // TODO: INIT TEXT SYSTEM + mTextAPI.Initialize().LogIfFailed(LogCategory::GRAPHICS); + + mDefaultFont = mTextAPI.LoadFont(lunarium::OpenFontData, lunarium::OpenDataSize, "Open Font"); + //mDefaultFont = mTextAPI.LoadFont(lunarium::RobotoFontData, lunarium::RobotoDataSize, "Roboto Font"); + // DEBUG: INIT TEST DATA // mTestData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 2}); // Position @@ -220,6 +236,8 @@ namespace lunarium Flush(); } + float tex_is_grey_scale = (texture && texture->GetFormat() == TextureFormat::RED) ? 1.0f : 0.0f; + // FIRST QuadData::Vertex v1; int vert_size = sizeof(QuadData::Vertex); @@ -230,6 +248,7 @@ namespace lunarium v1.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v1.angle = angle; v1.scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); + v1.tex_is_grey_scale = tex_is_grey_scale; memcpy(&mQuadData.mRawVertexBuffer[mQuadData.mRawBufferIndex], &v1, vert_size); mQuadData.mRawBufferIndex += 1; @@ -242,6 +261,7 @@ namespace lunarium v2.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v2.angle = angle; v2.scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); + v2.tex_is_grey_scale = tex_is_grey_scale; memcpy(&mQuadData.mRawVertexBuffer[mQuadData.mRawBufferIndex], &v2, vert_size); mQuadData.mRawBufferIndex += 1; @@ -254,6 +274,7 @@ namespace lunarium v3.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v3.angle = angle; v3.scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); + v3.tex_is_grey_scale = tex_is_grey_scale; memcpy(&mQuadData.mRawVertexBuffer[mQuadData.mRawBufferIndex], &v3, vert_size); mQuadData.mRawBufferIndex += 1; @@ -266,6 +287,7 @@ namespace lunarium v4.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); v4.angle = angle; v4.scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); + v4.tex_is_grey_scale = tex_is_grey_scale; memcpy(&mQuadData.mRawVertexBuffer[mQuadData.mRawBufferIndex], &v4, vert_size); mQuadData.mRawBufferIndex += 1; @@ -382,8 +404,13 @@ namespace lunarium //mQuadData.mQuadShader->SetUniform(uni, Color::Red().arr).LogIfFailed(LogCategory::GRAPHICS, "Failed to set uniform for the test shader!"); mQuadData.mVertexBuffer->Bind(); + glDrawElements(GL_TRIANGLES, mQuadData.mNumQuads * 6, GL_UNSIGNED_INT, nullptr); - //glDrawArrays(GL_TRIANGLES, 0, mQuadData.mNumQuads * 2); + // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); + // glLineWidth(20.0f); + // glDrawElements(GL_TRIANGLES, mQuadData.mNumQuads * 6, GL_UNSIGNED_INT, nullptr); + + // glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); mFrameStats.DrawCalls++; } // Reset drawing data @@ -400,4 +427,10 @@ namespace lunarium // TODO: Add the debug texture back to the map mLoadedTextures.push_back(mpWhiteTexture); } + + + Texture* Renderer2D::GetTextDebugTexture() + { + return mTextAPI.GetDebugTexture(); + } } \ No newline at end of file diff --git a/src/renderer/renderer2D.h b/src/renderer/renderer2D.h index 53448e8..29481b9 100644 --- a/src/renderer/renderer2D.h +++ b/src/renderer/renderer2D.h @@ -13,12 +13,15 @@ #include "render_common.h" #include +#include "text_renderer.h" + #include #include namespace lunarium { class OrthographicCamera; + class Texture; class Renderer2D @@ -46,6 +49,9 @@ namespace lunarium void DrawSprite(Texture& image, Rectangle source, Rectangle destination, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0); void DrawString(const char* string, Rectangle boundingArea, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float scale = 1.0f, int font = 0); + // DEBUG: + Texture* GetTextDebugTexture(); + private: friend class RenderContext; OpRes Initialize(); @@ -83,10 +89,10 @@ namespace lunarium }; const glm::vec2 vert_tex[4] = { - { 0.0f, 1.0f }, - { 1.0f, 1.0f }, - { 1.0f, 0.0f }, { 0.0f, 0.0f }, + { 1.0f, 0.0f }, + { 1.0f, 1.0f }, + { 0.0f, 1.0f }, }; struct Vertex @@ -98,6 +104,7 @@ namespace lunarium glm::vec3 translation; float angle; glm::vec3 scale; + float tex_is_grey_scale; // 0 or 1, this is mostly used for text rendering }; //const u32 indices[6] = { 0, 1, 2, 0, 2, 3 }; @@ -112,12 +119,16 @@ namespace lunarium Shader* mQuadShader; } mQuadData; + TextRenderer mTextAPI; + int mDefaultFont; + FrameStats mFrameStats; OrthographicCamera* mpCamera; std::vector mLoadedTextures; Texture* mpWhiteTexture; Color mClearColor; + /// DEBUG STUFF u32 mTVAO; u32 mTVBO; diff --git a/src/renderer/shaders/quad.frag b/src/renderer/shaders/quad.frag index 716d99c..cc225f0 100644 --- a/src/renderer/shaders/quad.frag +++ b/src/renderer/shaders/quad.frag @@ -2,6 +2,7 @@ layout (location = 0) in vec2 f_tex_coords; layout (location = 1) in vec4 f_vert_color; layout (location = 2) flat in int f_tex_index; +layout (location = 3) flat in int f_tex_is_grey; out vec4 color; layout (binding = 0) uniform sampler2D textures[32]; @@ -10,7 +11,16 @@ void main() { //color = f_vert_color * texture(textures[int(f_tex_index)], f_tex_coords); //color = vec4(float(f_tex_index) * 0.01, 0.0 , 0.0, 1.0); - color = f_vert_color * texture(textures[f_tex_index], f_tex_coords); + if (f_tex_is_grey == 1) + { + float tex_alpha = texture(textures[f_tex_index], f_tex_coords).x; + color = f_vert_color * vec4(1.0, 1.0, 1.0, tex_alpha); + } + else + { + color = f_vert_color * texture(textures[f_tex_index], f_tex_coords); + } + // switch(f_tex_index) // { // case 0: color = vec4(1.0, 0.0, 0.0, 1.0); break; //texture(textures[ 0], f_tex_coords); break; diff --git a/src/renderer/shaders/quad.vert b/src/renderer/shaders/quad.vert index 7ca71a2..431eb9b 100644 --- a/src/renderer/shaders/quad.vert +++ b/src/renderer/shaders/quad.vert @@ -7,10 +7,12 @@ layout(location = 3) in float tex_slot; layout(location = 4) in vec3 translation; layout(location = 5) in float angle; layout(location = 6) in vec3 scale; +layout(location = 7) in vec3 tex_is_grey_scale; layout (location = 0) out vec2 f_tex_coords; layout (location = 1) out vec4 f_vert_color; layout (location = 2) flat out int f_tex_index; +layout (location = 3) flat out int f_tex_is_grey; uniform mat4 projview; @@ -19,6 +21,7 @@ void main() f_tex_index = int(tex_slot); f_tex_coords = tex_coords; f_vert_color = color; + f_tex_is_grey = int(tex_is_grey_scale); // mat4 ModelTrans = mat4( // vec4( scale.x * cos(angle), scale.x * -sin(angle), 0.0, 0.0), // vec4( scale.y * sin(angle), scale.y * cos(angle), 0.0, 0.0), diff --git a/src/renderer/text_renderer.cpp b/src/renderer/text_renderer.cpp new file mode 100644 index 0000000..0a8587b --- /dev/null +++ b/src/renderer/text_renderer.cpp @@ -0,0 +1,230 @@ +/****************************************************************************** +* File - text_renderer.h +* Author - Joey Pollack +* Date - 2022/08/15 (y/m/d) +* Mod Date - 2022/08/15 (y/m/d) +* Description - manage fonts and render text +******************************************************************************/ + +#include "text_renderer.h" +#include + +#include "texture.h" + +#include +#include FT_FREETYPE_H + +namespace lunarium +{ + OpRes TextRenderer::Initialize() + { + if (FT_Init_FreeType(&mFTHandle)) + { + return OpRes::Fail("FREETYPE: Could not init FreeType Library"); + } + // glGenVertexArrays(1, &mTextVAO); + // glGenBuffers(1, &mTextVBO); + // glBindVertexArray(mTextVAO); + // glBindBuffer(GL_ARRAY_BUFFER, mTextVBO); + // glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW); + // glEnableVertexAttribArray(0); + // glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0); + // glBindBuffer(GL_ARRAY_BUFFER, 0); + // glBindVertexArray(0); + + + // // Load text shaders + // mTextShader.SetSource(glShader::TYPE_VERTEX, VertShader, false); + // mTextShader.SetSource(glShader::TYPE_FRAGMENT, FragShader, false); + // if (!mTextShader.BuildProgram()) + // { + // return OpRes::Fail("Unable to build text shader program. Fonts will not load and text will not render."); + // } + + return OpRes::OK(); + } + + int TextRenderer::LoadFont(const char* fontFile, const char* name, float size, int weight) + { + + if (!mFTHandle) + { + Logger::Error(LogCategory::GRAPHICS, "Cannot load font: Freetype handle is invalid"); + return -1; + } + + FT_Face face; + if (FT_New_Face(mFTHandle, fontFile, 0, &face)) + { + Logger::Error(LogCategory::GRAPHICS, "FREETYPE: Failed to load font from file: %s", fontFile); + return -1; + } + + return CreateFont(name, face, size, weight); + } + + int TextRenderer::LoadFont(const unsigned char* fontData, int dataSize, const char* name, float size, int weight) + { + + if (!mFTHandle) + { + Logger::Error(LogCategory::GRAPHICS, "Cannot load font: Freetype handle is invalid"); + return -1; + } + + FT_Face face; + if (FT_New_Memory_Face(mFTHandle, fontData, dataSize, 0, &face)) + { + Logger::Error(LogCategory::GRAPHICS, "FREETYPE: Failed to load font from buffer. Font name: %s", name); + return -1; + } + + return CreateFont(name, face, size, weight); + } + + int TextRenderer::CreateFont(const char* name, FT_Face face, float size, int weight) + { + FT_Set_Pixel_Sizes(face, 0, 48); + + //glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction + + // NOTE: Testing code + float sizes[128] = { 0.0f }; + + mFonts.push_back(Font()); + int fontIdx = (int)(mFonts.size() - 1); + Font& f = mFonts.back(); + mFonts.back().Name = name; + + u32 max_width = 0; + u32 max_height = 0; + + for (u8 c = 0; c < 128; c++) + { + // Load character glyph + if (FT_Load_Char(face, c, FT_LOAD_RENDER)) + { + Logger::Error(LogCategory::GRAPHICS, "FREETYTPE: Failed to load Glyph %c from font: %s", c, name); + continue; + } + + u32 width = face->glyph->bitmap.width; + u32 height = face->glyph->bitmap.rows; + u8* pixel_data = new u8[width * height]; + memcpy(pixel_data, face->glyph->bitmap.buffer, width * height); + + Character character = Character + { + c, pixel_data, 0, 0, Sizeu { width, height }, + glm::vec2((float)face->glyph->bitmap_left, (float)face->glyph->bitmap_top), // bearing + (int)face->glyph->bitmap.rows - face->glyph->bitmap_top, // downshift + (unsigned int)face->glyph->advance.x // advance + }; + + + mFonts.back().CharSet.insert(std::pair(c, character)); + + if (width > max_width) + { + max_width = width; + } + + if (height > max_height) + { + max_height = height; + } + + // Find Max (positive) Down Shift + if (character.DownShift > 0 && ((int)mFonts.back().MaxDownShift) < character.DownShift) + { + mFonts.back().MaxDownShift = character.DownShift; + } + + // Determines the distance from the top down to the baseline + if (character.Size.Height == character.Bearing.y && mFonts.back().MaxHeight < character.Size.Height) + { + mFonts.back().MaxHeight = (unsigned int)character.Size.Height; + } + } + + // Put the font texture together + u32 single_row_width = f.CharSet.size() * max_width; + if (single_row_width > Texture::GetMaxSize()) + { + FreeTempPixelData(f.CharSet); + mFonts.pop_back(); + Logger::Error(LogCategory::GRAPHICS, "Failed to create font texture: texture width (%d) is larger than max texture size: (%d)", single_row_width, Texture::GetMaxSize()); + return -1; + } + + // Create buffer of the correct size (single row texture for now - this may be improved later) + // u32 structured_width = max_width * f.CharSet.size(); + // u32 structured_height = max_height; + // u8** structured_buffer = new u8*[structured_height]; + + // for (int i = 0; i < structured_height; i++) + // { + // structured_buffer[i] = new u8[structured_width]; + // memset(structured_buffer[i], 0, structured_width); + + // // Copy the current row for each char into the buffer + // u8* index = structured_buffer[i]; + // for (auto iter = f.CharSet.begin(); iter != f.CharSet.end(); iter++) + // { + // memcpy(index, &iter->second.PixelDataBuffer[iter->second.Size.Width * i], iter->second.Size.Width); + // index += max_width; + // } + // } + + u8* texture_buffer = new u8[single_row_width * max_height]; + memset(texture_buffer, 0, single_row_width * max_height); + + // Copy the pixel data into the proper spot in the buffer + u8* buffer_index = texture_buffer; + for (int i = 0; i < max_height; i++) + { + for (auto iter = f.CharSet.begin(); iter != f.CharSet.end(); iter++) + { + if (i < iter->second.Size.Height) + { + memcpy(buffer_index, &iter->second.PixelDataBuffer[iter->second.Size.Width * i], iter->second.Size.Width); + } + buffer_index += max_width; + } + } + + FreeTempPixelData(f.CharSet); + + f.FontTexture = Texture::Create(texture_buffer, single_row_width, max_height, TextureFormat::RED, true); + mDebugTexture = f.FontTexture; + + delete[] texture_buffer; + + // DEBUG STUFF: + // Character* c = &mFonts.back().CharSet['a']; + // mDebugTexture = Texture::Create(c->PixelDataBuffer, c->Size.Width, c->Size.Height, TextureFormat::RED, true); + + FT_Done_Face(face); + + return fontIdx; + } + + + void TextRenderer::FreeTempPixelData(std::map& char_set) + { + for (auto iter = char_set.begin(); iter != char_set.end(); iter++) + { + delete[] iter->second.PixelDataBuffer; + iter->second.PixelDataBuffer = nullptr; + } + } + + ///////////////////////////////////////////////////////////////////// + // DEBUG DEBUG DEBUG + ///////////////////////////////////////////////////////////////////// + + Texture* TextRenderer::GetDebugTexture() + { + return mDebugTexture; + } +} \ No newline at end of file diff --git a/src/renderer/text_renderer.h b/src/renderer/text_renderer.h new file mode 100644 index 0000000..50598df --- /dev/null +++ b/src/renderer/text_renderer.h @@ -0,0 +1,84 @@ +/****************************************************************************** +* File - text_renderer.h +* Author - Joey Pollack +* Date - 2022/08/15 (y/m/d) +* Mod Date - 2022/08/15 (y/m/d) +* Description - manage fonts and render text +******************************************************************************/ + +#ifndef LUNARIUM_TEXT_RENDERER_H_ +#define LUNARIUM_TEXT_RENDERER_H_ + +#include +#include + +#include +#include +#include + +struct FT_LibraryRec_; +typedef struct FT_LibraryRec_ * FT_Library; + +struct FT_FaceRec_; +typedef struct FT_FaceRec_* FT_Face; + +namespace lunarium +{ + class Texture; + class TextRenderer + { + private: + friend class Renderer2D; + OpRes Initialize(); + + // DEBUG + Texture* GetDebugTexture(); + + public: + + 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); + + private: // HELPERS + int CreateFont(const char* name, FT_Face face, float size = 12.0f, int weight = 400); + + private: + Texture* mDebugTexture; + + struct Character + { + u8 glyph; + u8* PixelDataBuffer; // Temporary storage for the character's pixel data - this should be cleanup after the font texture is created + u32 TextureDataWidthOffset; // how far into the row does this chars texture data begin + u32 TextureDataHeightOffset; // how far into the col does this chars texture data begin + Sizeu Size; // Size of glyph + glm::vec2 Bearing; // Offset from baseline to left/top of glyph + int DownShift; // Size/Amount of glyph below the baseline + unsigned int Advance; // Offset to advance to next glyph + }; + + struct Font + { + Texture* FontTexture; + 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 + u32 MaxHeight; + + // This is the max distance a char will go below the baseline + u32 MaxDownShift; + std::map CharSet; + Font() : MaxHeight(0), MaxDownShift(0) + {} + }; + + + FT_Library mFTHandle; + std::vector mFonts; + + private: + void FreeTempPixelData(std::map& char_set); + }; +} + +#endif // LUNARIUM_TEXT_RENDERER_H_ \ No newline at end of file diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp index 99dcb7d..ba8620d 100644 --- a/src/renderer/texture.cpp +++ b/src/renderer/texture.cpp @@ -18,7 +18,15 @@ namespace lunarium } - Texture* Texture::Create(u8* data, u32 width, u32 height, TextureFormat format) + + i32 Texture::GetMaxSize() + { + i32 max_size = 0; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_size); + return max_size; + } + + Texture* Texture::Create(u8* data, u32 width, u32 height, TextureFormat format, bool clamp) { if (((i32)width) < 1 || ((i32)height) < 1) { @@ -26,7 +34,7 @@ namespace lunarium return nullptr; } - u32 formats[2] = { GL_RGB, GL_RGBA }; + u32 formats[3] = { GL_RED, GL_RGB, GL_RGBA }; Texture* t = new Texture(); t->mFormat = format; @@ -43,10 +51,11 @@ namespace lunarium Logger::Error(LogCategory::GRAPHICS, "Error setting texture data"); } + GLint param = clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT; glGenerateMipmap(GL_TEXTURE_2D); - 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_WRAP_S, param); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -103,8 +112,8 @@ namespace lunarium u8* Texture::GetPixels() const { - u32 format_size[2] = { 3, 4 }; - u32 formats[2] = { GL_RGB, GL_RGBA }; + u32 format_size[3] = { 1, 3, 4 }; + u32 formats[3] = { GL_RED, GL_RGB, GL_RGBA }; u8* buffer = new u8[mWidth * mHeight * format_size[(int)mFormat]]; Bind(); @@ -114,4 +123,5 @@ namespace lunarium return buffer; } + } \ No newline at end of file diff --git a/src/renderer/texture.h b/src/renderer/texture.h index b061109..1009bb9 100644 --- a/src/renderer/texture.h +++ b/src/renderer/texture.h @@ -15,6 +15,7 @@ namespace lunarium { enum class TextureFormat { + RED, RGB, RGBA, }; @@ -22,8 +23,9 @@ namespace lunarium class Texture { public: - static Texture* Create(u8* data = nullptr, u32 width = 1, u32 height = 1, TextureFormat format = TextureFormat::RGBA); + static Texture* Create(u8* data = nullptr, u32 width = 1, u32 height = 1, TextureFormat format = TextureFormat::RGBA, bool clamp = false); static void Destroy(Texture** ppTex); + static i32 GetMaxSize(); void Bind(u32 slot = 0) const; void Unbind() const; diff --git a/src/run_modes/testbed/scenes/simple_render_scene.cpp b/src/run_modes/testbed/scenes/simple_render_scene.cpp index 63f6915..91f4f81 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.cpp +++ b/src/run_modes/testbed/scenes/simple_render_scene.cpp @@ -27,7 +27,7 @@ namespace lunarium { SimpleRenderScene::SimpleRenderScene(uint32_t logCat) - : BaseScene(logCat), mStressTest(true), mFrameTime(0.0), mNumFrames(0) + : BaseScene(logCat), mTestMode(TestMode::String), mFrameTime(0.0), mNumFrames(0) { srand((u32)time(0)); } @@ -66,13 +66,13 @@ namespace lunarium mSrcHeight = h; //delete[] buffer; - stbi_set_flip_vertically_on_load(1); - buffer = stbi_load("lunarium_test_image.png", &w, &h, &n, 0); + stbi_set_flip_vertically_on_load(0); + buffer = stbi_load("lunarium_text_test.png", &w, &h, &n, 0); format = TextureFormat::RGBA; - if (n == 3) + if (n == 1) { - format = TextureFormat::RGB; + format = TextureFormat::RED; } mpTestImageLoad2 = Texture::Create(buffer, w, h, format); //delete[] buffer; @@ -102,6 +102,7 @@ namespace lunarium angle = 0.0f; box_angle = 0.0f; + mTextDebugPosX = 200; } void SimpleRenderScene::OnTick(double delta) @@ -114,12 +115,21 @@ namespace lunarium // Textbox size adjustment if (Core::Input().IsKeyDown(KeyCode::LEFT)) { - mTextBoxWidth -= 10; + if (mTestMode == TestMode::Basic) + mTextBoxWidth -= 10; + + if (mTestMode == TestMode::String) + mTextDebugPosX -= 10; } if (Core::Input().IsKeyDown(KeyCode::RIGHT)) { mTextBoxWidth += 10; + if (mTestMode == TestMode::Basic) + mTextBoxWidth += 10; + + if (mTestMode == TestMode::String) + mTextDebugPosX += 10; } if (Core::Input().IsKeyDown(KeyCode::R)) @@ -161,7 +171,14 @@ namespace lunarium if (Core::Input().IsKeyPressed(KeyCode::TAB)) { - mStressTest = !mStressTest; + if (mTestMode == TestMode::Basic) + mTestMode = TestMode::Stress; + + if (mTestMode == TestMode::Stress) + mTestMode = TestMode::String; + + if (mTestMode == TestMode::String) + mTestMode = TestMode::Basic; } if (Core::Input().IsKeyPressed(KeyCode::Q)) @@ -198,6 +215,16 @@ namespace lunarium //Logger::Info(mLogCat, "") } + if (Core::Input().IsKeyPressed(KeyCode::T)) + { + Texture* dt = Core::Graphics().GetTextDebugTexture(); + stbi_flip_vertically_on_write(0); + stbi_write_png("lunarium_text_test.png", dt->GetWidth(), dt->GetHeight(), 1, + dt->GetPixels(), dt->GetWidth() * 1); + Logger::Info(mLogCat, "Saved Text test image"); + + } + mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280); mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720); @@ -232,14 +259,13 @@ namespace lunarium mTimer.Reset(); Core::Graphics().BeginDraw(&main_cam); - if (mStressTest) - { - RenderBatchStressTest(Core::Graphics()); - } - else + switch (mTestMode) { - RenderScene(Core::Graphics()); + case TestMode::Basic: RenderScene(Core::Graphics()); break; + case TestMode::Stress: RenderBatchStressTest(Core::Graphics()); break; + case TestMode::String: RenderStringTest(Core::Graphics()); break; } + Core::Graphics().EndDraw(); // Graphics end draw must happen before GUI end frame @@ -255,6 +281,8 @@ namespace lunarium void SimpleRenderScene::RenderScene(Renderer2D& g) { + + // g.DrawImage(*mpRenderedImage, Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()), // Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f), angle); @@ -292,6 +320,13 @@ namespace lunarium } + void SimpleRenderScene::RenderStringTest(Renderer2D& g) + { + Texture* dt = g.GetTextDebugTexture(); + g.DrawQuad(Rectangle(mTextDebugPosX, 400, dt->GetWidth() , dt->GetHeight() ), Color::Blue(), dt); + } + + void SimpleRenderScene::DrawStatsGUI() { ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver); diff --git a/src/run_modes/testbed/scenes/simple_render_scene.h b/src/run_modes/testbed/scenes/simple_render_scene.h index bac9692..69377e5 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.h +++ b/src/run_modes/testbed/scenes/simple_render_scene.h @@ -21,6 +21,12 @@ namespace lunarium class FrameBuffer; class SimpleRenderScene : public BaseScene { + enum TestMode + { + Basic, + Stress, + String, + }; public: SimpleRenderScene(uint32_t logCat); ~SimpleRenderScene(); @@ -28,6 +34,7 @@ namespace lunarium virtual void OnTick(double delta); void RenderScene(Renderer2D& g); void RenderBatchStressTest(Renderer2D& g); + void RenderStringTest(Renderer2D& g); private: @@ -43,13 +50,14 @@ namespace lunarium float mSrcWidth; float mSrcHeight; + i32 mTextDebugPosX; HighResTimer mTimer; double mFrameTime; i64 mNumFrames; Renderer2D::FrameStats mRenderStats; - bool mStressTest; + TestMode mTestMode; int mNumQuadsToRender = 500; bool mUseTextures = false;