diff --git a/include/Spectre/Graphics/Font.h b/include/Spectre/Graphics/Font.h index ccb8c1d..aa03649 100644 --- a/include/Spectre/Graphics/Font.h +++ b/include/Spectre/Graphics/Font.h @@ -57,16 +57,20 @@ protected : protected : + struct CacheTexture { + vec2u texpos; + unsigned shelf; + Texture texture; + }; + FontDriver *m_driver; mutable std::map m_charset; unsigned int m_size; // font size, in pixels. not points. - // Texture atlas used by the font. - mutable Texture m_texture; - mutable vec2u m_texpos; - mutable unsigned m_shelf; + // Alpha Cache texture. + mutable CacheTexture m_cacheTextureA; std::vector m_rawData; }; diff --git a/source/Graphics/Font.cpp b/source/Graphics/Font.cpp index 13568f1..71ffe64 100644 --- a/source/Graphics/Font.cpp +++ b/source/Graphics/Font.cpp @@ -29,8 +29,8 @@ bool Font::loadFromFile(const std::string& filename, unsigned int size) } m_size = size; - m_shelf = 0; - m_texpos = vec2u(0, 0); + m_cacheTextureA.shelf = 0; + m_cacheTextureA.texpos = vec2u(0, 0); createTexture(); @@ -66,10 +66,10 @@ void Font::loadChar(unsigned char code) const Vector2u pos = findTextureRegion(img); - m_texture.enable(); - m_texture.update(pos, img); + m_cacheTextureA.texture.enable(); + m_cacheTextureA.texture.update(pos, img); - glyph.texture = &m_texture; + glyph.texture = &m_cacheTextureA.texture; glyph.texture_origin = vec2u(pos.x, pos.y); } else { glyph.texture = NULL; @@ -80,48 +80,49 @@ void Font::loadChar(unsigned char code) const void Font::createTexture() { - m_texture.create(128, 32, PixelFormat::PF_Alpha); - m_texture.enable(); - m_texture.setSmooth(true); - m_texture.disable(); + m_cacheTextureA.texture.create(128, 32, PixelFormat::PF_Alpha); + m_cacheTextureA.texture.enable(); + m_cacheTextureA.texture.setSmooth(true); + m_cacheTextureA.texture.disable(); } 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. - m_texture.create(m_texture.getSize().x, nextSize, tmpImg.getFormat()); - m_texture.update(vec2u(0, 0), tmpImg); + tex.create(tex.getSize().x, nextSize, tmpImg.getFormat()); + tex.update(vec2u(0, 0), tmpImg); } Vector2u Font::findTextureRegion(const Image& img) const { Vector2u pos; - if (m_texpos.x + img.getWidth() + 1 > m_texture.getSize().x) { - m_texpos.y += m_shelf; - m_texpos.x = 0; - m_shelf = 0; + if (m_cacheTextureA.texpos.x + img.getWidth() + 1 > m_cacheTextureA.texture.getSize().x) { + m_cacheTextureA.texpos.y += m_cacheTextureA.shelf; + m_cacheTextureA.texpos.x = 0; + m_cacheTextureA.shelf = 0; } - if (img.getHeight() + 1 > m_shelf) { - m_shelf = img.getHeight() + 1; - if (m_texpos.y + m_shelf > m_texture.getSize().y) { + if (img.getHeight() + 1 > m_cacheTextureA.shelf) { + m_cacheTextureA.shelf = img.getHeight() + 1; + if (m_cacheTextureA.texpos.y + m_cacheTextureA.shelf > m_cacheTextureA.texture.getSize().y) { resizeTexture(); } } - pos = m_texpos; + pos = m_cacheTextureA.texpos; - m_texpos.x += img.getWidth() + 1; + m_cacheTextureA.texpos.x += img.getWidth() + 1; return pos; } const Texture* Font::getTexture() const { - return &m_texture; + return &m_cacheTextureA.texture; }