diff --git a/src/renderer/renderer2D.cpp b/src/renderer/renderer2D.cpp index b08e9d7..5d160b9 100644 --- a/src/renderer/renderer2D.cpp +++ b/src/renderer/renderer2D.cpp @@ -194,7 +194,7 @@ namespace lunarium ///////////////////////////////////////////////////////////////////// // DRAW METHODS ///////////////////////////////////////////////////////////////////// - void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle) + void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle, Rectangle sub_texture_area) { int texture_slot = -1; if (texture) @@ -237,12 +237,27 @@ namespace lunarium } float tex_is_grey_scale = (texture && texture->GetFormat() == TextureFormat::RED) ? 1.0f : 0.0f; + float left = sub_texture_area.X; + if (left < 0) + { + sub_texture_area = Rectangle::MakeFromTopLeft(0, 0, mLoadedTextures[texture_slot]->GetWidth(), mLoadedTextures[texture_slot]->GetHeight()); + } + + float xScale = (sub_texture_area.HalfWidth * 2) / mLoadedTextures[texture_slot]->GetWidth(); + float xOffset = sub_texture_area.left() / mLoadedTextures[texture_slot]->GetWidth(); + float yScale = (sub_texture_area.HalfHeight * 2) / mLoadedTextures[texture_slot]->GetHeight(); + float yOffset = sub_texture_area.top() / mLoadedTextures[texture_slot]->GetHeight(); + + yOffset *= -1.0f; + + // mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale * -1.0f, yOffset}); + // vec2(vertex.z * uvManip.x + uvManip.y, vertex.w * uvManip.z - uvManip.w); // FIRST QuadData::Vertex v1; int vert_size = sizeof(QuadData::Vertex); v1.pos = mQuadData.vert_pos[0]; - v1.tex_coord = mQuadData.vert_tex[0]; + v1.tex_coord = glm::vec2(mQuadData.vert_tex[0].x * xScale + xOffset, mQuadData.vert_tex[0].y * yScale - yOffset); v1.color = color; v1.tex_slot = texture_slot; v1.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); @@ -255,7 +270,7 @@ namespace lunarium // SECOND QuadData::Vertex v2; v2.pos = mQuadData.vert_pos[1]; - v2.tex_coord = mQuadData.vert_tex[1]; + v2.tex_coord = glm::vec2(mQuadData.vert_tex[1].x * xScale + xOffset, mQuadData.vert_tex[1].y * yScale - yOffset); v2.color = color; v2.tex_slot = texture_slot; v2.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); @@ -268,7 +283,7 @@ namespace lunarium // THIRD QuadData::Vertex v3; v3.pos = mQuadData.vert_pos[2]; - v3.tex_coord = mQuadData.vert_tex[2]; + v3.tex_coord = glm::vec2(mQuadData.vert_tex[2].x * xScale + xOffset, mQuadData.vert_tex[2].y * yScale - yOffset); v3.color = color; v3.tex_slot = texture_slot; v3.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); @@ -281,7 +296,7 @@ namespace lunarium // FOURTH QuadData::Vertex v4; v4.pos = mQuadData.vert_pos[3]; - v4.tex_coord = mQuadData.vert_tex[3]; + v4.tex_coord = glm::vec2(mQuadData.vert_tex[3].x * xScale + xOffset, mQuadData.vert_tex[3].y * yScale - yOffset); v4.color = color; v4.tex_slot = texture_slot; v4.translation = glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f); @@ -358,11 +373,13 @@ namespace lunarium // glBindVertexArray(0); } - void Renderer2D::DrawString(const char* string, Rectangle boundingArea, Color color, float scale, int font) + void Renderer2D::DrawString(const char* string, Rectangle bounding_box, Color color, float scale, int font) { - Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawString is not yet implemented"); + //Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawString is not yet implemented"); // mText.DrawString(font, string, glm::vec2(boundingArea.left(), boundingArea.top()), // glm::vec2(boundingArea.right(), boundingArea.bottom()), color, scale, mProjection); + u32 f = font < 0 ? mDefaultFont : font; + mTextAPI.DrawString(*this, f, string, bounding_box, color, scale); } diff --git a/src/renderer/renderer2D.h b/src/renderer/renderer2D.h index 29481b9..ff598d8 100644 --- a/src/renderer/renderer2D.h +++ b/src/renderer/renderer2D.h @@ -43,11 +43,13 @@ namespace lunarium void EndDraw(); // Draw methods - void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f); + + /// sub_texture_area is the area of the texture to draw + void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f, Rectangle sub_texture_area = Rectangle(-1, -1, -1, -1)); void DrawQuads(Rectangle* quads, u32 num_quads, Color* pColors); void DrawSprite(Texture& image, glm::vec2 position, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float angle = 0); 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); + void DrawString(const char* string, Rectangle bounding_box, Color color = {1.0f, 1.0f, 1.0f, 1.0f}, float scale = 1.0f, int font = -1); // DEBUG: Texture* GetTextDebugTexture(); diff --git a/src/renderer/text_renderer.cpp b/src/renderer/text_renderer.cpp index 0a8587b..a53cb34 100644 --- a/src/renderer/text_renderer.cpp +++ b/src/renderer/text_renderer.cpp @@ -157,25 +157,6 @@ namespace lunarium 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); @@ -208,6 +189,11 @@ namespace lunarium return fontIdx; } + + void TextRenderer::DrawString(Renderer2D& r, u32 font_id, const char* str, Rectangle bounding_box, Color c, float scale) + { + + } void TextRenderer::FreeTempPixelData(std::map& char_set) diff --git a/src/renderer/text_renderer.h b/src/renderer/text_renderer.h index 50598df..25aa61e 100644 --- a/src/renderer/text_renderer.h +++ b/src/renderer/text_renderer.h @@ -24,12 +24,14 @@ typedef struct FT_FaceRec_* FT_Face; namespace lunarium { + class Renderer2D; class Texture; class TextRenderer { private: friend class Renderer2D; OpRes Initialize(); + void DrawString(Renderer2D& r, u32 font_id, const char* str, Rectangle bounding_box, Color c, float scale); // DEBUG Texture* GetDebugTexture(); diff --git a/src/run_modes/testbed/scenes/simple_render_scene.cpp b/src/run_modes/testbed/scenes/simple_render_scene.cpp index 91f4f81..a4e1114 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.cpp +++ b/src/run_modes/testbed/scenes/simple_render_scene.cpp @@ -67,13 +67,18 @@ namespace lunarium //delete[] buffer; stbi_set_flip_vertically_on_load(0); - buffer = stbi_load("lunarium_text_test.png", &w, &h, &n, 0); + buffer = stbi_load("debug_texture.jpeg", &w, &h, &n, 0); format = TextureFormat::RGBA; - if (n == 1) + if (n == 3) + { + format = TextureFormat::RGB; + } + else if (n == 1) { format = TextureFormat::RED; } + mpTestImageLoad2 = Texture::Create(buffer, w, h, format); //delete[] buffer; @@ -103,6 +108,7 @@ namespace lunarium angle = 0.0f; box_angle = 0.0f; mTextDebugPosX = 200; + mSubTex = Rectangle::MakeFromTopLeft(0, 0, 256, 256); } void SimpleRenderScene::OnTick(double delta) @@ -174,10 +180,10 @@ namespace lunarium if (mTestMode == TestMode::Basic) mTestMode = TestMode::Stress; - if (mTestMode == TestMode::Stress) + else if (mTestMode == TestMode::Stress) mTestMode = TestMode::String; - if (mTestMode == TestMode::String) + else if (mTestMode == TestMode::String) mTestMode = TestMode::Basic; } @@ -288,6 +294,8 @@ namespace lunarium //g.DrawQuad(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 0.0f, 1.0f), nullptr, angle); + g.DrawQuad(Rectangle(100, 100, 16, 16), Color::White(), mpTestImageLoad2, 0.0f, mSubTex); + // g.DrawQuad(Rectangle(200, 200, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f));//, nullptr, box_angle); // g.DrawQuad(Rectangle(400, 300, 64.0f, 64.0f), Color(0.0f, 1.0f, 1.0f, 1.0f), nullptr, box_angle); // g.DrawQuad(Rectangle(600, 300, 100.0f, 100.0f), Color::Blue(), nullptr, 45.0f); @@ -324,14 +332,22 @@ namespace lunarium { Texture* dt = g.GetTextDebugTexture(); g.DrawQuad(Rectangle(mTextDebugPosX, 400, dt->GetWidth() , dt->GetHeight() ), Color::Blue(), dt); + + //g.DrawString("This is a test string!", Rectangle(100, 400, 200, 50), Color::Green()); } void SimpleRenderScene::DrawStatsGUI() { + std::string mode_names[3] = { "Basic", "Stress", "String" }; ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver); - ImGui::SetNextWindowSize(ImVec2(400, 600)); - ImGui::Begin("RENDER INFO"); + ImGui::SetNextWindowSize(ImVec2(400, 800)); + ImGui::Begin("RENDER INFO"); + ImGui::BeginChild("Scene Info", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); + ImGui::Text("Scene Info"); + ImGui::Separator(); + ImGui::Text("Scene Mode: %s", mode_names[mTestMode].c_str()); + ImGui::EndChild(); ImGui::BeginChild("Per Frame", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); ImGui::Text("Per Frame"); ImGui::Separator(); @@ -345,11 +361,16 @@ namespace lunarium ImGui::Text("FPS: %d", Core::GetInstance().GetFrameData().CurrentFPS); ImGui::Text("Frame Number: %d", mNumFrames); ImGui::EndChild(); - ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); + ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 4.5f), true); ImGui::Text("Settings"); ImGui::Separator(); ImGui::InputInt("Number of Quads to draw", &mNumQuadsToRender); ImGui::Checkbox("Draw Textures", &mUseTextures); + float vals[4] = {mSubTex.left(), mSubTex.top(), mSubTex.HalfWidth * 2, mSubTex.HalfHeight * 2 }; + if (ImGui::DragFloat4("Sub Texture Rect", vals)) + { + mSubTex = Rectangle::MakeFromTopLeft(vals[0], vals[1], vals[2], vals[3]); + } ImGui::EndChild(); ImGui::End(); diff --git a/src/run_modes/testbed/scenes/simple_render_scene.h b/src/run_modes/testbed/scenes/simple_render_scene.h index 69377e5..fe6d0c7 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.h +++ b/src/run_modes/testbed/scenes/simple_render_scene.h @@ -51,6 +51,7 @@ namespace lunarium float mSrcWidth; float mSrcHeight; i32 mTextDebugPosX; + Rectangle mSubTex; HighResTimer mTimer; double mFrameTime;