Skip to content

Commit e1b2b30

Browse files
committed
Update BitmapText to v.1.2
updates Bitmap Text to v1.2, bringing ability to trim both the character quad and texture quad to allow fixing slight rounding errors that can cause neighbour glyphs to spill as well as for special effects (especially since these can also be negative, allowing for expansion). also adds some fixes including: initialisation of missing class member, replacing sf::VertexArray with an std::vector<sf::Vertex>, and updates some code style including only calculating the "startVertexIndex" once per loop as well as removing const return values.
1 parent 743d538 commit e1b2b30

2 files changed

Lines changed: 84 additions & 39 deletions

File tree

src/SelbaWard/BitmapText.cpp

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ namespace selbaward
3737

3838
BitmapText::BitmapText()
3939
: m_pBitmapFont{ nullptr }
40-
, m_vertices(sf::PrimitiveType::Triangles)
41-
, m_string()
40+
, m_vertices{}
41+
, m_string{}
4242
, m_color{ sf::Color::White }
4343
, m_tracking{ 1 }
44+
, m_bounds{}
45+
, m_charTrim{}
46+
, m_texTrim{}
4447
{
4548
}
4649

@@ -62,7 +65,7 @@ void BitmapText::setString(const std::string& string)
6265
priv_updateVertices();
6366
}
6467

65-
const std::string BitmapText::getString() const
68+
std::string BitmapText::getString() const
6669
{
6770
return m_string;
6871
}
@@ -73,7 +76,7 @@ void BitmapText::setTracking(const int tracking)
7376
priv_updateVertices();
7477
}
7578

76-
const int BitmapText::getTracking() const
79+
int BitmapText::getTracking() const
7780
{
7881
return m_tracking;
7982
}
@@ -84,7 +87,7 @@ void BitmapText::setColor(const sf::Color& color)
8487
priv_updateColor();
8588
}
8689

87-
const sf::Color BitmapText::getColor() const
90+
sf::Color BitmapText::getColor() const
8891
{
8992
return m_color;
9093
}
@@ -114,18 +117,47 @@ sf::FloatRect BitmapText::getLocalBounds() const
114117
return m_bounds;
115118
}
116119

120+
void BitmapText::setCharacterAndTextureTrim(const sf::Vector2f characterAndTextureTrim)
121+
{
122+
m_charTrim = characterAndTextureTrim;
123+
m_texTrim = characterAndTextureTrim;
124+
priv_updateVertices();
125+
}
126+
127+
void BitmapText::setCharacterTrim(const sf::Vector2f characterTrim)
128+
{
129+
m_charTrim = characterTrim;
130+
priv_updateVertices();
131+
}
132+
133+
void BitmapText::setTextureTrim(const sf::Vector2f textureTrim)
134+
{
135+
m_texTrim = textureTrim;
136+
priv_updateVertices();
137+
}
138+
139+
sf::Vector2f BitmapText::getCharacterTrim() const
140+
{
141+
return m_charTrim;
142+
}
143+
144+
sf::Vector2f BitmapText::getTextureTrim() const
145+
{
146+
return m_texTrim;
147+
}
148+
117149

118150

119151
// PRIVATE
120152

121153
void BitmapText::draw(sf::RenderTarget& target, sf::RenderStates states) const
122154
{
123-
if (m_vertices.getVertexCount() == 0)
155+
if (m_vertices.empty())
124156
return;
125157

126158
states.transform *= getTransform();
127159
states.texture = m_pBitmapFont->getTexture();
128-
target.draw(m_vertices, states);
160+
target.draw(m_vertices.data(), m_vertices.size(), sf::PrimitiveType::Triangles, states);
129161
}
130162

131163
void BitmapText::priv_updateVertices()
@@ -134,55 +166,61 @@ void BitmapText::priv_updateVertices()
134166
{
135167
m_vertices.clear();
136168
m_bounds = { { 0.f, 0.f }, { 0.f, 0.f } };
169+
return;
137170
}
138171

139-
m_vertices.resize(m_string.length() * 6u);
140-
172+
constexpr std::size_t numOfVerticesPerQuad{ 6u };
173+
m_vertices.resize(m_string.length() * numOfVerticesPerQuad);
141174
sf::Vector2f penPosition{ 0.f, 0.f };
142-
143175
sf::Vector2f min{ 0.f, 0.f };
144176
sf::Vector2f max{ 0.f, 0.f };
145-
146177
for (std::size_t character{ 0u }; character < m_string.length(); ++character)
147178
{
148179
const std::size_t glyphNumber{ (m_string[character] >= 0u) ? static_cast<std::size_t>(m_string[character]) : static_cast<std::size_t>(m_string[character] + 256) }; // after 125, 126, 127 is -128, -127, -126. this moves them to 128, 129, 130
149-
150-
const int kerning{ (character < m_string.length() - 1u) ? m_pBitmapFont->getKerning(m_string.substr(character, 2u)) : 0 };
151-
152-
BitmapFont::Glyph glyph{ m_pBitmapFont->getGlyph(glyphNumber) };
153-
180+
const int kerning{ (character < (m_string.length() - 1u)) ? m_pBitmapFont->getKerning(m_string.substr(character, 2u)) : 0 };
181+
const BitmapFont::Glyph glyph{ m_pBitmapFont->getGlyph(glyphNumber) };
154182
const sf::Vector2f glyphOffset{ 0.f - glyph.startX, (glyph.baseline < 0) ? (0.f - glyph.baseline - glyph.textureRect.size.y) : (0.f - glyph.baseline) };
155183
const sf::Vector2f glyphPosition{ penPosition + glyphOffset };
184+
const std::size_t startVertexIndex{ character * numOfVerticesPerQuad };
156185

157-
m_vertices[(character * 6u) + 0u].position = glyphPosition;
158-
m_vertices[(character * 6u) + 1u].position = glyphPosition + sf::Vector2f(0, static_cast<float>(glyph.textureRect.size.y));
159-
m_vertices[(character * 6u) + 2u].position = glyphPosition + sf::Vector2f(static_cast<float>(glyph.textureRect.size.x), 0);
160-
m_vertices[(character * 6u) + 3u].position = glyphPosition + sf::Vector2f(static_cast<float>(glyph.textureRect.size.x), static_cast<float>(glyph.textureRect.size.y));
186+
m_vertices[startVertexIndex + 0u].position = glyphPosition;
187+
m_vertices[startVertexIndex + 1u].position = glyphPosition + sf::Vector2f{ 0.f, static_cast<float>(glyph.textureRect.size.y) };
188+
m_vertices[startVertexIndex + 2u].position = glyphPosition + sf::Vector2f{ static_cast<float>(glyph.textureRect.size.x), 0.f };
189+
m_vertices[startVertexIndex + 3u].position = glyphPosition + sf::Vector2f{ static_cast<float>(glyph.textureRect.size.x), static_cast<float>(glyph.textureRect.size.y) };
161190

162-
m_vertices[(character * 6u) + 0u].texCoords = sf::Vector2f(
191+
m_vertices[startVertexIndex + 0u].texCoords = sf::Vector2f(
163192
static_cast<float>(glyph.textureRect.position.x),
164193
static_cast<float>(glyph.textureRect.position.y));
165-
m_vertices[(character * 6u) + 1u].texCoords = sf::Vector2f(
194+
m_vertices[startVertexIndex + 1u].texCoords = sf::Vector2f(
166195
static_cast<float>(glyph.textureRect.position.x),
167196
static_cast<float>(glyph.textureRect.position.y + glyph.textureRect.size.y));
168-
m_vertices[(character * 6u) + 2u].texCoords = sf::Vector2f(
197+
m_vertices[startVertexIndex + 2u].texCoords = sf::Vector2f(
169198
static_cast<float>(glyph.textureRect.position.x + glyph.textureRect.size.x),
170199
static_cast<float>(glyph.textureRect.position.y));
171-
m_vertices[(character * 6u) + 3u].texCoords = sf::Vector2f(
200+
m_vertices[startVertexIndex + 3u].texCoords = sf::Vector2f(
172201
static_cast<float>(glyph.textureRect.position.x + glyph.textureRect.size.x),
173202
static_cast<float>(glyph.textureRect.position.y + glyph.textureRect.size.y));
174203

175-
m_vertices[(character * 6u) + 4u] = m_vertices[(character * 6u) + 2u];
176-
m_vertices[(character * 6u) + 5u] = m_vertices[(character * 6u) + 1u];
204+
min.x = std::min(min.x, m_vertices[startVertexIndex + 0u].position.x);
205+
max.x = std::max(max.x, m_vertices[startVertexIndex + 3u].position.x);
206+
min.y = std::min(min.y, m_vertices[startVertexIndex + 0u].position.y);
207+
max.y = std::max(max.y, m_vertices[startVertexIndex + 3u].position.y);
177208

178-
penPosition.x += (glyph.width > 0) ? (0.f + m_tracking + kerning + glyph.width) : (0.f + m_tracking + kerning + glyph.width + glyph.textureRect.size.x - glyph.startX);
209+
m_vertices[startVertexIndex + 0u].position += m_charTrim;
210+
m_vertices[startVertexIndex + 1u].position += { m_charTrim.x, -m_charTrim.y };
211+
m_vertices[startVertexIndex + 2u].position += { -m_charTrim.x, m_charTrim.y };
212+
m_vertices[startVertexIndex + 3u].position += -m_charTrim;
179213

180-
min.x = std::min(min.x, m_vertices[(character * 6u) + 0u].position.x);
181-
max.x = std::max(max.x, m_vertices[(character * 6u) + 3u].position.x);
182-
min.y = std::min(min.y, m_vertices[(character * 6u) + 0u].position.y);
183-
max.y = std::max(max.y, m_vertices[(character * 6u) + 3u].position.y);
184-
}
214+
m_vertices[startVertexIndex + 0u].texCoords += m_texTrim;
215+
m_vertices[startVertexIndex + 1u].texCoords += { m_texTrim.x, -m_texTrim.y };
216+
m_vertices[startVertexIndex + 2u].texCoords += { -m_texTrim.x, m_texTrim.y };
217+
m_vertices[startVertexIndex + 3u].texCoords += -m_texTrim;
185218

219+
m_vertices[startVertexIndex + 4u] = m_vertices[startVertexIndex + 2u];
220+
m_vertices[startVertexIndex + 5u] = m_vertices[startVertexIndex + 1u];
221+
222+
penPosition.x += (glyph.width > 0) ? (0.f + m_tracking + kerning + glyph.width) : (0.f + m_tracking + kerning + glyph.width + glyph.textureRect.size.x - glyph.startX);
223+
}
186224
priv_updateColor();
187225

188226
m_bounds.position.x = min.x;
@@ -193,8 +231,8 @@ void BitmapText::priv_updateVertices()
193231

194232
void BitmapText::priv_updateColor()
195233
{
196-
for (std::size_t v{ 0u }; v < m_vertices.getVertexCount(); ++v)
197-
m_vertices[v].color = m_color;
234+
for (auto& vertex : m_vertices)
235+
vertex.color = m_color;
198236
}
199237

200238
} // namespace selbaward

src/SelbaWard/BitmapText.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,39 @@
4040
namespace selbaward
4141
{
4242

43-
// SW Bitmap Text v1.1.5
43+
// SW Bitmap Text v1.2.0
4444
class BitmapText : public sf::Drawable, public sf::Transformable
4545
{
4646
public:
4747
BitmapText();
4848
void setBitmapFont(const BitmapFont& bitmapFont);
4949
void setBitmapFont();
5050
void setString(const std::string& string = "");
51-
const std::string getString() const;
51+
std::string getString() const;
5252
void setTracking(int tracking);
53-
const int getTracking() const;
53+
int getTracking() const;
5454
void setColor(const sf::Color& color);
55-
const sf::Color getColor() const;
55+
sf::Color getColor() const;
5656
void setScale(std::size_t scale);
5757
void setScale(std::size_t scaleX, std::size_t scaleY);
5858
void setScale(sf::Vector2<std::size_t> scale);
5959
sf::FloatRect getGlobalBounds() const;
6060
sf::FloatRect getLocalBounds() const;
61+
void setCharacterAndTextureTrim(sf::Vector2f characterAndTextureTrim);
62+
void setCharacterTrim(sf::Vector2f characterTrim);
63+
void setTextureTrim(sf::Vector2f textureTrim);
64+
sf::Vector2f getCharacterTrim() const;
65+
sf::Vector2f getTextureTrim() const;
6166

6267
private:
6368
const BitmapFont* m_pBitmapFont;
64-
sf::VertexArray m_vertices;
69+
std::vector<sf::Vertex> m_vertices;
6570
std::string m_string;
6671
sf::Color m_color;
6772
int m_tracking;
6873
sf::FloatRect m_bounds;
74+
sf::Vector2f m_charTrim;
75+
sf::Vector2f m_texTrim;
6976

7077
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
7178
void priv_updateVertices();

0 commit comments

Comments
 (0)