1
0
Fork 0

include/Spectre/Graphics/Font.h: move texture variable into it's own struct.

When implementing rendering of glyphs with different properties: size, stroke width, bold etc.
We will need to store them in different textures.
This commit is contained in:
Henrik Hautakoski 2016-04-23 18:36:39 +02:00
parent 1e5d448fa1
commit c74dda1200
2 changed files with 32 additions and 27 deletions

View file

@ -57,16 +57,20 @@ protected :
protected : protected :
struct CacheTexture {
vec2u texpos;
unsigned shelf;
Texture texture;
};
FontDriver *m_driver; FontDriver *m_driver;
mutable std::map<unsigned char, Glyph> m_charset; mutable std::map<unsigned char, Glyph> m_charset;
unsigned int m_size; // font size, in pixels. not points. unsigned int m_size; // font size, in pixels. not points.
// Texture atlas used by the font. // Alpha Cache texture.
mutable Texture m_texture; mutable CacheTexture m_cacheTextureA;
mutable vec2u m_texpos;
mutable unsigned m_shelf;
std::vector<unsigned char> m_rawData; std::vector<unsigned char> m_rawData;
}; };

View file

@ -29,8 +29,8 @@ bool Font::loadFromFile(const std::string& filename, unsigned int size)
} }
m_size = size; m_size = size;
m_shelf = 0; m_cacheTextureA.shelf = 0;
m_texpos = vec2u(0, 0); m_cacheTextureA.texpos = vec2u(0, 0);
createTexture(); createTexture();
@ -66,10 +66,10 @@ void Font::loadChar(unsigned char code) const
Vector2u pos = findTextureRegion(img); Vector2u pos = findTextureRegion(img);
m_texture.enable(); m_cacheTextureA.texture.enable();
m_texture.update(pos, img); m_cacheTextureA.texture.update(pos, img);
glyph.texture = &m_texture; glyph.texture = &m_cacheTextureA.texture;
glyph.texture_origin = vec2u(pos.x, pos.y); glyph.texture_origin = vec2u(pos.x, pos.y);
} else { } else {
glyph.texture = NULL; glyph.texture = NULL;
@ -80,48 +80,49 @@ void Font::loadChar(unsigned char code) const
void Font::createTexture() void Font::createTexture()
{ {
m_texture.create(128, 32, PixelFormat::PF_Alpha); m_cacheTextureA.texture.create(128, 32, PixelFormat::PF_Alpha);
m_texture.enable(); m_cacheTextureA.texture.enable();
m_texture.setSmooth(true); m_cacheTextureA.texture.setSmooth(true);
m_texture.disable(); m_cacheTextureA.texture.disable();
} }
void Font::resizeTexture() const void Font::resizeTexture() const
{ {
unsigned int nextSize = math::nextPowerOfTwo(m_texture.getSize().y + 1); Texture& tex = m_cacheTextureA.texture;
unsigned int nextSize = math::nextPowerOfTwo(tex.getSize().y + 1);
Image tmpImg = m_texture.copyToImage(); Image tmpImg = tex.copyToImage();
// recreate texture. // recreate texture.
m_texture.create(m_texture.getSize().x, nextSize, tmpImg.getFormat()); tex.create(tex.getSize().x, nextSize, tmpImg.getFormat());
m_texture.update(vec2u(0, 0), tmpImg); tex.update(vec2u(0, 0), tmpImg);
} }
Vector2u Font::findTextureRegion(const Image& img) const Vector2u Font::findTextureRegion(const Image& img) const
{ {
Vector2u pos; Vector2u pos;
if (m_texpos.x + img.getWidth() + 1 > m_texture.getSize().x) { if (m_cacheTextureA.texpos.x + img.getWidth() + 1 > m_cacheTextureA.texture.getSize().x) {
m_texpos.y += m_shelf; m_cacheTextureA.texpos.y += m_cacheTextureA.shelf;
m_texpos.x = 0; m_cacheTextureA.texpos.x = 0;
m_shelf = 0; m_cacheTextureA.shelf = 0;
} }
if (img.getHeight() + 1 > m_shelf) { if (img.getHeight() + 1 > m_cacheTextureA.shelf) {
m_shelf = img.getHeight() + 1; m_cacheTextureA.shelf = img.getHeight() + 1;
if (m_texpos.y + m_shelf > m_texture.getSize().y) { if (m_cacheTextureA.texpos.y + m_cacheTextureA.shelf > m_cacheTextureA.texture.getSize().y) {
resizeTexture(); resizeTexture();
} }
} }
pos = m_texpos; pos = m_cacheTextureA.texpos;
m_texpos.x += img.getWidth() + 1; m_cacheTextureA.texpos.x += img.getWidth() + 1;
return pos; return pos;
} }
const Texture* Font::getTexture() const const Texture* Font::getTexture() const
{ {
return &m_texture; return &m_cacheTextureA.texture;
} }