Font loading crash fixed
This commit is contained in:
@@ -20,13 +20,34 @@ struct TextJob : RenderJob
|
|||||||
Color = (glm::vec4)textComponent["Color"];
|
Color = (glm::vec4)textComponent["Color"];
|
||||||
Content = (std::string)textComponent["Content"];
|
Content = (std::string)textComponent["Content"];
|
||||||
Resource = font;
|
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::mat4 Matrix;
|
||||||
glm::vec4 Color;
|
glm::vec4 Color;
|
||||||
std::string Content;
|
std::string Content;
|
||||||
Font* Resource;
|
Font* Resource;
|
||||||
|
AlignmentEnum Alignment;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void CalculateHash() override
|
void CalculateHash() override
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ private:
|
|||||||
|
|
||||||
GLuint VAO, VBO;
|
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;
|
ShaderProgram* m_TextProgram;
|
||||||
|
|
||||||
|
|||||||
@@ -4,4 +4,5 @@
|
|||||||
<Resource></Resource>
|
<Resource></Resource>
|
||||||
<Color R="1" G="1" B="1" A="1"/>
|
<Color R="1" G="1" B="1" A="1"/>
|
||||||
<Visible>true</Visible>
|
<Visible>true</Visible>
|
||||||
|
<Alignment><Center/></Alignment>
|
||||||
</Text>
|
</Text>
|
||||||
@@ -3,6 +3,18 @@
|
|||||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||||
<xs:import schemaLocation="../Types.xsd" namespace="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:element name="Text">
|
||||||
<xs:annotation>
|
<xs:annotation>
|
||||||
<xs:documentation>A visible font loaded from disk</xs:documentation>
|
<xs:documentation>A visible font loaded from disk</xs:documentation>
|
||||||
@@ -21,6 +33,9 @@
|
|||||||
<xs:element name="Visible" type="t:bool" minOccurs="0">
|
<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:annotation><xs:documentation>Wether the text is visible or not</xs:documentation></xs:annotation>
|
||||||
</xs:element>
|
</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:all>
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
|
|||||||
@@ -6,59 +6,29 @@
|
|||||||
</Components>
|
</Components>
|
||||||
|
|
||||||
<Children>
|
<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>
|
<Entity>
|
||||||
<Components>
|
<Components>
|
||||||
<c:Camera>
|
<c:Camera>
|
||||||
<Name>ActionCamera</Name>
|
<Name>ActionCamera</Name>
|
||||||
</c:Camera>
|
</c:Camera>
|
||||||
<c:Listener/>
|
|
||||||
<c:Model>
|
<c:Model>
|
||||||
<Resource>Models/Camera.obj</Resource>
|
<Resource>Models/Camera.obj</Resource>
|
||||||
<Visible>false</Visible>
|
|
||||||
</c:Model>
|
</c:Model>
|
||||||
<c:Transform>
|
<c:Transform>
|
||||||
<Position X="7.20140934" Y="2.30552006" Z="8.67392826"/>
|
<Position X="-7.05254316" Y="2.96810508" Z="10.5229216"/>
|
||||||
<Orientation X="-0.296705663" Y="0.767948866" Z="2.27869563e-07"/>
|
<Orientation X="-0.24434644" Y="-0.680581629" Z="4.69731361e-07"/>
|
||||||
</c:Transform>
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
<Children>
|
<Children>
|
||||||
<Entity>
|
<Entity>
|
||||||
<Components>
|
<Components>
|
||||||
<c:Text>
|
<c:Text>
|
||||||
<Content>Camera 2</Content>
|
<Content>Camera #2</Content>
|
||||||
<Resource>Fonts/DroidSans.ttf,24</Resource>
|
<Resource>Fonts/DroidSans.ttf,64</Resource>
|
||||||
|
<Alignment>0</Alignment>
|
||||||
</c:Text>
|
</c:Text>
|
||||||
<c:Transform>
|
<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"/>
|
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
|
||||||
</c:Transform>
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
@@ -81,9 +51,11 @@
|
|||||||
<Entity>
|
<Entity>
|
||||||
<Components>
|
<Components>
|
||||||
<c:Model>
|
<c:Model>
|
||||||
<Resource>An error</Resource>
|
<Resource>Models/Assault.obj</Resource>
|
||||||
</c:Model>
|
</c:Model>
|
||||||
<c:Transform/>
|
<c:Transform>
|
||||||
|
<Position X="-2.5" Y="0" Z="1"/>
|
||||||
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
<Children/>
|
<Children/>
|
||||||
</Entity>
|
</Entity>
|
||||||
@@ -91,7 +63,7 @@
|
|||||||
<Components>
|
<Components>
|
||||||
<c:Text>
|
<c:Text>
|
||||||
<Content>Welcome!</Content>
|
<Content>Welcome!</Content>
|
||||||
<Resource>Fonts/DroidSans.ttf,64</Resource>
|
<Resource>Fonts/DroidSans.ttf,1280</Resource>
|
||||||
</c:Text>
|
</c:Text>
|
||||||
<c:Transform>
|
<c:Transform>
|
||||||
<Position X="0" Y="0" Z="0.700000048"/>
|
<Position X="0" Y="0" Z="0.700000048"/>
|
||||||
@@ -99,6 +71,122 @@
|
|||||||
</Components>
|
</Components>
|
||||||
<Children/>
|
<Children/>
|
||||||
</Entity>
|
</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>
|
</Children>
|
||||||
|
|
||||||
</Entity>
|
</Entity>
|
||||||
|
|||||||
@@ -19,10 +19,11 @@ Font::Font(std::string path)
|
|||||||
FontSize = boost::lexical_cast<int>((*it).c_str());
|
FontSize = boost::lexical_cast<int>((*it).c_str());
|
||||||
} catch (boost::bad_lexical_cast const&) {
|
} catch (boost::bad_lexical_cast const&) {
|
||||||
LOG_ERROR("input string did not have a valid font resolution");
|
LOG_ERROR("input string did not have a valid font resolution");
|
||||||
|
throw std::runtime_error("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return;
|
throw std::runtime_error("");;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -30,12 +31,12 @@ Font::Font(std::string path)
|
|||||||
|
|
||||||
if (FT_Init_FreeType(&library)) {
|
if (FT_Init_FreeType(&library)) {
|
||||||
LOG_ERROR("FreeType error: init failed");
|
LOG_ERROR("FreeType error: init failed");
|
||||||
return;
|
throw std::runtime_error("");;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FT_New_Face(library, filePath.c_str(), 0, &Face)) {
|
if (FT_New_Face(library, filePath.c_str(), 0, &Face)) {
|
||||||
LOG_ERROR("FreeType error: loading font");
|
LOG_ERROR("FreeType error: loading font");
|
||||||
return;
|
throw std::runtime_error("");;
|
||||||
}
|
}
|
||||||
|
|
||||||
FT_Set_Char_Size(Face, 0, FontSize*64, 300, 300); // temp
|
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)) {
|
if (FT_Load_Char(Face, 'X', FT_LOAD_RENDER)) {
|
||||||
LOG_ERROR("FreeType error: loading char");
|
LOG_ERROR("FreeType error: loading char");
|
||||||
return;
|
throw std::runtime_error("");;
|
||||||
}
|
}
|
||||||
|
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
@@ -87,6 +88,8 @@ Font::Font(std::string path)
|
|||||||
|
|
||||||
m_Characters.insert(std::pair<GLchar, Character>(c, character));
|
m_Characters.insert(std::pair<GLchar, Character>(c, character));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FT_Done_FreeType(library);
|
FT_Done_FreeType(library);
|
||||||
GLERROR("Font Load");
|
GLERROR("Font Load");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,12 +154,17 @@ void RenderSystem::fillText(std::list<std::shared_ptr<RenderJob>>& jobs, World*
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Font* font = ResourceManager::Load<Font>(textComponent["Resource"]);
|
Font* font;
|
||||||
if (font == nullptr) {
|
try {
|
||||||
font = ResourceManager::Load<Font>("Fonts/DroidSans.ttf");
|
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);
|
glm::mat4 modelMatrix = Transform::ModelMatrix(textComponent.EntityID, world);
|
||||||
std::shared_ptr<TextJob> modelJob = std::shared_ptr<TextJob>(new TextJob(modelMatrix, font, textComponent));
|
std::shared_ptr<TextJob> modelJob = std::shared_ptr<TextJob>(new TextJob(modelMatrix, font, textComponent));
|
||||||
jobs.push_back(modelJob);
|
jobs.push_back(modelJob);
|
||||||
|
|||||||
@@ -34,12 +34,13 @@ void TextRenderer::Draw(RenderScene& scene)
|
|||||||
for (auto &job : scene.TextJobs) {
|
for (auto &job : scene.TextJobs) {
|
||||||
auto textJob = std::dynamic_pointer_cast<TextJob>(job);
|
auto textJob = std::dynamic_pointer_cast<TextJob>(job);
|
||||||
if (textJob) {
|
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 penX = 0;
|
||||||
GLfloat penY = 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;
|
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
|
// Activate corresponding render state
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user