Initial commit
This commit is contained in:
commit
edfc5298e1
252 changed files with 93965 additions and 0 deletions
131
source/Graphics/Text.cpp
Normal file
131
source/Graphics/Text.cpp
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
|
||||
#include <Spectre/Graphics/Text.h>
|
||||
|
||||
Text::Text() :
|
||||
m_font (NULL),
|
||||
m_color (255, 255, 255, 255)
|
||||
{
|
||||
}
|
||||
|
||||
Text::Text(const std::string text, const Font& font) :
|
||||
m_font (&font),
|
||||
m_color (255, 255, 255, 255),
|
||||
m_geometryNeedsUpdate (true)
|
||||
{
|
||||
}
|
||||
|
||||
void Text::setString(const std::string& string)
|
||||
{
|
||||
m_string = string;
|
||||
m_geometryNeedsUpdate = true;
|
||||
}
|
||||
|
||||
const std::string& Text::getString() const
|
||||
{
|
||||
return m_string;
|
||||
}
|
||||
|
||||
void Text::setFont(const Font& font)
|
||||
{
|
||||
m_font = &font;
|
||||
m_geometryNeedsUpdate = true;
|
||||
}
|
||||
const Font* Text::getFont() const
|
||||
{
|
||||
return m_font;
|
||||
}
|
||||
|
||||
void Text::setColor(const Color& color)
|
||||
{
|
||||
m_color = color;
|
||||
m_geometryNeedsUpdate = true;
|
||||
}
|
||||
|
||||
const Color& Text::getColor() const
|
||||
{
|
||||
return m_color;
|
||||
}
|
||||
|
||||
Vector2f Text::getSize() const
|
||||
{
|
||||
Vector2f size;
|
||||
|
||||
for(int i = 0; i < m_string.size(); i++) {
|
||||
|
||||
Font::Glyph glyph = m_font->getGlyph(m_string[i]);
|
||||
|
||||
float h = glyph.size.y;
|
||||
float w = glyph.advance;
|
||||
|
||||
if (h > size.y) {
|
||||
size.y = h;
|
||||
}
|
||||
size.x += w;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
const Texture* Text::getTexture() const
|
||||
{
|
||||
if (m_geometryNeedsUpdate) {
|
||||
updateGeometry();
|
||||
m_geometryNeedsUpdate = false;
|
||||
}
|
||||
return m_font->getTexture();
|
||||
}
|
||||
|
||||
const std::vector<unsigned short>& Text::getIndices() const
|
||||
{
|
||||
return m_indicies;
|
||||
}
|
||||
|
||||
const std::vector<Vertex2D>& Text::getVertices() const
|
||||
{
|
||||
if (m_geometryNeedsUpdate) {
|
||||
updateGeometry();
|
||||
m_geometryNeedsUpdate = false;
|
||||
}
|
||||
return m_vertices;
|
||||
}
|
||||
|
||||
void Text::updateGeometry() const
|
||||
{
|
||||
Vector2f origin;
|
||||
Vector2f size = getSize();
|
||||
Vector3f color = getColor().toRGBf();
|
||||
|
||||
m_vertices.clear();
|
||||
|
||||
for(int i = 0; i < m_string.length(); i++) {
|
||||
|
||||
Vertex2D v1, v2, v3, v4;
|
||||
|
||||
Font::Glyph glyph = m_font->getGlyph(m_string[i]);
|
||||
|
||||
if (glyph.texture) {
|
||||
|
||||
Vector2f vpos = origin + vec2f(glyph.offset.x, size.y - glyph.offset.y);
|
||||
|
||||
Vertex2D v1 = Vertex2D(vpos + vec2f( 0.0f, 0.0f), vec2f(glyph.texture_origin.x, glyph.texture_origin.y));
|
||||
Vertex2D v2 = Vertex2D(vpos + vec2f( glyph.size.x, 0.0f), vec2f(glyph.texture_origin.x + glyph.size.x, glyph.texture_origin.y));
|
||||
Vertex2D v3 = Vertex2D(vpos + vec2f( glyph.size.x, glyph.size.y), vec2f(glyph.texture_origin.x + glyph.size.x, glyph.texture_origin.y + glyph.size.y));
|
||||
Vertex2D v4 = Vertex2D(vpos + vec2f( 0.0f, glyph.size.y), vec2f(glyph.texture_origin.x, glyph.texture_origin.y + glyph.size.y));
|
||||
|
||||
v1.color = color;
|
||||
v2.color = color;
|
||||
v3.color = color;
|
||||
v4.color = color;
|
||||
|
||||
m_vertices.push_back(v1);
|
||||
m_vertices.push_back(v2);
|
||||
m_vertices.push_back(v3);
|
||||
m_vertices.push_back(v4);
|
||||
}
|
||||
|
||||
origin.x += glyph.advance;
|
||||
}
|
||||
|
||||
int x = 0;
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue