Font loading crash fixed
This commit is contained in:
@@ -19,10 +19,11 @@ Font::Font(std::string path)
|
||||
FontSize = boost::lexical_cast<int>((*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<GLchar, Character>(c, character));
|
||||
}
|
||||
|
||||
|
||||
FT_Done_FreeType(library);
|
||||
GLERROR("Font Load");
|
||||
}
|
||||
|
||||
@@ -154,12 +154,17 @@ void RenderSystem::fillText(std::list<std::shared_ptr<RenderJob>>& jobs, World*
|
||||
continue;
|
||||
}
|
||||
|
||||
Font* font = ResourceManager::Load<Font>(textComponent["Resource"]);
|
||||
if (font == nullptr) {
|
||||
font = ResourceManager::Load<Font>("Fonts/DroidSans.ttf");
|
||||
Font* font;
|
||||
try {
|
||||
font = ResourceManager::Load<Font>(resource);
|
||||
} catch (const std::exception&) {
|
||||
try {
|
||||
font = ResourceManager::Load<Font>("Fonts/DroidSans.ttf,16");
|
||||
} catch (const std::exception&) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
glm::mat4 modelMatrix = Transform::ModelMatrix(textComponent.EntityID, world);
|
||||
std::shared_ptr<TextJob> modelJob = std::shared_ptr<TextJob>(new TextJob(modelMatrix, font, textComponent));
|
||||
jobs.push_back(modelJob);
|
||||
|
||||
@@ -34,12 +34,13 @@ void TextRenderer::Draw(RenderScene& scene)
|
||||
for (auto &job : scene.TextJobs) {
|
||||
auto textJob = std::dynamic_pointer_cast<TextJob>(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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user