diff --git a/include/Engine/Rendering/TextJob.h b/include/Engine/Rendering/TextJob.h index d6374d84..a11761d2 100644 --- a/include/Engine/Rendering/TextJob.h +++ b/include/Engine/Rendering/TextJob.h @@ -20,13 +20,34 @@ struct TextJob : RenderJob Color = (glm::vec4)textComponent["Color"]; Content = (std::string)textComponent["Content"]; Resource = font; + + if ((int)textComponent["Alignment"] == textComponent["Alignment"].Enum("Left")) { + Alignment = AlignmentEnum::Left; + } else if ((int)textComponent["Alignment"] == textComponent["Alignment"].Enum("Right")) { + Alignment = AlignmentEnum::Right; + } else if ((int)textComponent["Alignment"] == textComponent["Alignment"].Enum("Center")) { + Alignment = AlignmentEnum::Center; + } else { + LOG_ERROR("Text alignment invalid"); + Alignment = AlignmentEnum::Left; + } + + }; + + enum class AlignmentEnum + { + Left, + Right, + Center }; glm::mat4 Matrix; glm::vec4 Color; std::string Content; Font* Resource; + AlignmentEnum Alignment; + void CalculateHash() override { diff --git a/include/Engine/Rendering/TextRenderer.h b/include/Engine/Rendering/TextRenderer.h index e7323deb..8094dda0 100644 --- a/include/Engine/Rendering/TextRenderer.h +++ b/include/Engine/Rendering/TextRenderer.h @@ -25,7 +25,7 @@ private: GLuint VAO, VBO; - void RenderText(std::string text, Font* font, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix); + void RenderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix); ShaderProgram* m_TextProgram; diff --git a/resources/Schema/Components/Text.xml b/resources/Schema/Components/Text.xml index ecf9bac8..9ca629d1 100644 --- a/resources/Schema/Components/Text.xml +++ b/resources/Schema/Components/Text.xml @@ -4,4 +4,5 @@ true +
\ No newline at end of file diff --git a/resources/Schema/Components/Text.xsd b/resources/Schema/Components/Text.xsd index c66acacd..14e91a35 100644 --- a/resources/Schema/Components/Text.xsd +++ b/resources/Schema/Components/Text.xsd @@ -3,6 +3,18 @@ + + + + + + + + + + + + A visible font loaded from disk @@ -21,6 +33,9 @@ Wether the text is visible or not + + Text alignment + diff --git a/resources/Schema/Entities/RenderingWorld.xml b/resources/Schema/Entities/RenderingWorld.xml index f35747c4..6e4fbece 100644 --- a/resources/Schema/Entities/RenderingWorld.xml +++ b/resources/Schema/Entities/RenderingWorld.xml @@ -6,59 +6,29 @@ - - - - MainCamera - - - Models/Camera.obj - - - - - - - - - - - Camera 1 - Fonts/DroidSans.ttf,24 - - - - - - - - - - ActionCamera - Models/Camera.obj - false - - + + - Camera 2 - Fonts/DroidSans.ttf,24 + Camera #2 + Fonts/DroidSans.ttf,64 + 0 - + @@ -81,9 +51,11 @@ - An error + Models/Assault.obj - + + + @@ -91,7 +63,7 @@ Welcome! - Fonts/DroidSans.ttf,64 + Fonts/DroidSans.ttf,1280 @@ -99,6 +71,122 @@ + + + + 2 + + + + + + + + + + + + + 6 + + + + + + + + + + + + 6 + + + + + + + + + + + + 6 + + + + + + + + + + + 6 + + + + + + + + + + + + + 51 + 0.4999997615814209 + + + + + + + + + + + Models/Assault.obj + + + + + + + + + + + MainCamera + + + + Models/Camera.obj + false + + + + + + + + + + + Taiwan #1 + Fonts/DroidSans.ttf,64 + 0 + + + + + + + + + + diff --git a/src/Engine/Rendering/Font.cpp b/src/Engine/Rendering/Font.cpp index ba99ee31..8af013e8 100644 --- a/src/Engine/Rendering/Font.cpp +++ b/src/Engine/Rendering/Font.cpp @@ -19,10 +19,11 @@ Font::Font(std::string path) FontSize = boost::lexical_cast((*it).c_str()); } catch (boost::bad_lexical_cast const&) { LOG_ERROR("input string did not have a valid font resolution"); + throw std::runtime_error(""); } } } else { - return; + throw std::runtime_error("");; } @@ -30,12 +31,12 @@ Font::Font(std::string path) if (FT_Init_FreeType(&library)) { LOG_ERROR("FreeType error: init failed"); - return; + throw std::runtime_error("");; } if (FT_New_Face(library, filePath.c_str(), 0, &Face)) { LOG_ERROR("FreeType error: loading font"); - return; + throw std::runtime_error("");; } FT_Set_Char_Size(Face, 0, FontSize*64, 300, 300); // temp @@ -43,7 +44,7 @@ Font::Font(std::string path) if (FT_Load_Char(Face, 'X', FT_LOAD_RENDER)) { LOG_ERROR("FreeType error: loading char"); - return; + throw std::runtime_error("");; } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); @@ -87,6 +88,8 @@ Font::Font(std::string path) m_Characters.insert(std::pair(c, character)); } + + FT_Done_FreeType(library); GLERROR("Font Load"); } diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 20614781..0e411bd9 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -154,12 +154,17 @@ void RenderSystem::fillText(std::list>& jobs, World* continue; } - Font* font = ResourceManager::Load(textComponent["Resource"]); - if (font == nullptr) { - font = ResourceManager::Load("Fonts/DroidSans.ttf"); + Font* font; + try { + font = ResourceManager::Load(resource); + } catch (const std::exception&) { + try { + font = ResourceManager::Load("Fonts/DroidSans.ttf,16"); + } catch (const std::exception&) { + continue; + } } - glm::mat4 modelMatrix = Transform::ModelMatrix(textComponent.EntityID, world); std::shared_ptr modelJob = std::shared_ptr(new TextJob(modelMatrix, font, textComponent)); jobs.push_back(modelJob); diff --git a/src/Engine/Rendering/TextRenderer.cpp b/src/Engine/Rendering/TextRenderer.cpp index 5b51c063..0bd58465 100644 --- a/src/Engine/Rendering/TextRenderer.cpp +++ b/src/Engine/Rendering/TextRenderer.cpp @@ -34,12 +34,13 @@ void TextRenderer::Draw(RenderScene& scene) for (auto &job : scene.TextJobs) { auto textJob = std::dynamic_pointer_cast(job); if (textJob) { - RenderText(textJob->Content, textJob->Resource, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix()); + + RenderText(textJob->Content, textJob->Resource, textJob->Alignment, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix()); } } } -void TextRenderer::RenderText(std::string text, Font* font, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix) +void TextRenderer::RenderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix) { GLfloat penX = 0; GLfloat penY = 0; @@ -60,7 +61,14 @@ void TextRenderer::RenderText(std::string text, Font* font, glm::vec4 color, glm stringWidth += (ch.Advance >> 6) * scale; } - penX = -stringWidth/2.f; + if(alignment == TextJob::AlignmentEnum::Center) { + penX = -stringWidth/2.f; + } else if (alignment == TextJob::AlignmentEnum::Right) { + penX = -stringWidth; + } else { + penX = 0; + } + // Activate corresponding render state