Font loading crash fixed

This commit is contained in:
viktorljung
2016-01-19 17:18:18 +01:00
parent dbe56280a6
commit f0763bc2a3
8 changed files with 192 additions and 51 deletions
+21
View File
@@ -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
{
+1 -1
View File
@@ -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;
+1
View File
@@ -4,4 +4,5 @@
<Resource></Resource>
<Color R="1" G="1" B="1" A="1"/>
<Visible>true</Visible>
<Alignment><Center/></Alignment>
</Text>
+15
View File
@@ -3,6 +3,18 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
<xs:complexType name="AlignmentEnum" mixed="true">
<xs:complexContent>
<xs:extension base="t:enum">
<xs:choice>
<xs:element name="Right" type="xs:integer" fixed="0" minOccurs="0"/>
<xs:element name="Left" type="xs:integer" fixed="1" minOccurs="0"/>
<xs:element name="Center" type="xs:integer" fixed="2" minOccurs="0"/>
</xs:choice>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Text">
<xs:annotation>
<xs:documentation>A visible font loaded from disk</xs:documentation>
@@ -21,6 +33,9 @@
<xs:element name="Visible" type="t:bool" minOccurs="0">
<xs:annotation><xs:documentation>Wether the text is visible or not</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="Alignment" type="AlignmentEnum" minOccurs="0">
<xs:annotation><xs:documentation>Text alignment</xs:documentation></xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
+127 -39
View File
@@ -6,59 +6,29 @@
</Components>
<Children>
<Entity>
<Components>
<c:Camera>
<Name>MainCamera</Name>
</c:Camera>
<c:Model>
<Resource>Models/Camera.obj</Resource>
</c:Model>
<c:Transform>
<Position X="3.57542324" Y="3.61114573" Z="9.16725922"/>
<Orientation X="-0.488691777" Y="0.575939536" Z="-3.55363916e-08"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:Text>
<Content>Camera 1</Content>
<Resource>Fonts/DroidSans.ttf,24</Resource>
</c:Text>
<c:Transform>
<Position X="0.298732847" Y="0.296712071" Z="-1"/>
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:Camera>
<Name>ActionCamera</Name>
</c:Camera>
<c:Listener/>
<c:Model>
<Resource>Models/Camera.obj</Resource>
<Visible>false</Visible>
</c:Model>
<c:Transform>
<Position X="7.20140934" Y="2.30552006" Z="8.67392826"/>
<Orientation X="-0.296705663" Y="0.767948866" Z="2.27869563e-07"/>
<Position X="-7.05254316" Y="2.96810508" Z="10.5229216"/>
<Orientation X="-0.24434644" Y="-0.680581629" Z="4.69731361e-07"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:Text>
<Content>Camera 2</Content>
<Resource>Fonts/DroidSans.ttf,24</Resource>
<Content>Camera #2</Content>
<Resource>Fonts/DroidSans.ttf,64</Resource>
<Alignment>0</Alignment>
</c:Text>
<c:Transform>
<Position X="0.286911726" Y="0.310270816" Z="-1"/>
<Position X="0.700063765" Y="0.310270816" Z="-1"/>
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
</c:Transform>
</Components>
@@ -81,9 +51,11 @@
<Entity>
<Components>
<c:Model>
<Resource>An error</Resource>
<Resource>Models/Assault.obj</Resource>
</c:Model>
<c:Transform/>
<c:Transform>
<Position X="-2.5" Y="0" Z="1"/>
</c:Transform>
</Components>
<Children/>
</Entity>
@@ -91,7 +63,7 @@
<Components>
<c:Text>
<Content>Welcome!</Content>
<Resource>Fonts/DroidSans.ttf,64</Resource>
<Resource>Fonts/DroidSans.ttf,1280</Resource>
</c:Text>
<c:Transform>
<Position X="0" Y="0" Z="0.700000048"/>
@@ -99,6 +71,122 @@
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:RaptorCopter>
<Speed>2</Speed>
<Axis X="0" Y="1" Z="0"/>
</c:RaptorCopter>
<c:Transform>
<Position X="0" Y="2.37320328" Z="0"/>
<Orientation X="0" Y="6311.2666" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="1" G="0" R="1"/>
<Radius>6</Radius>
</c:PointLight>
<c:Transform>
<Position X="-2.5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="0" G="1" R="1"/>
<Radius>6</Radius>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="-2.5"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="1" G="1" R="0"/>
<Radius>6</Radius>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="2.5"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:PointLight>
<Radius>6</Radius>
</c:PointLight>
<c:Transform>
<Position X="2.5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:PointLight>
<Radius>51</Radius>
<Intensity>0.4999997615814209</Intensity>
</c:PointLight>
<c:Transform>
<Position X="0" Y="10" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Model>
<Resource>Models/Assault.obj</Resource>
</c:Model>
<c:Transform>
<Position X="2.5" Y="0" Z="1"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Camera>
<Name>MainCamera</Name>
</c:Camera>
<c:Listener/>
<c:Model>
<Resource>Models/Camera.obj</Resource>
<Visible>false</Visible>
</c:Model>
<c:Transform>
<Position X="-0.144499183" Y="2.85344434" Z="8.00381374"/>
<Orientation X="-0.418875456" Y="-8.38464803e-06" Z="4.82539235e-08"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:Text>
<Content>Taiwan #1</Content>
<Resource>Fonts/DroidSans.ttf,64</Resource>
<Alignment>0</Alignment>
</c:Text>
<c:Transform>
<Position X="0.716896772" Y="0.296712071" Z="-1"/>
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
</Children>
</Entity>
+7 -4
View File
@@ -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");
}
+9 -4
View File
@@ -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);
+11 -3
View File
@@ -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