spritefont should use unordered_map

This commit is contained in:
Noel Berry 2020-10-19 00:50:16 -07:00
parent 28c142e8de
commit 5915ae1938
2 changed files with 20 additions and 23 deletions

View File

@ -155,7 +155,7 @@ void SpriteFont::build(const Font& font, float size, const uint32_t* charset)
packer.max_size = 8192; packer.max_size = 8192;
packer.power_of_two = true; packer.power_of_two = true;
std::map<uint32_t, int> glyphs; std::unordered_map<uint32_t, int> glyphs;
List<Color> buffer; List<Color> buffer;
auto ranges = charset; auto ranges = charset;
@ -207,14 +207,16 @@ void SpriteFont::build(const Font& font, float size, const uint32_t* charset)
// add kerning // add kerning
for (auto a = glyphs.begin(); a != glyphs.end(); a++) for (auto a = glyphs.begin(); a != glyphs.end(); a++)
for (auto b = glyphs.begin(); b != glyphs.end(); b++) for (auto b = glyphs.begin(); b != glyphs.end(); b++)
set_kerning(a->first, b->first, font.GetKerning(a->second, b->second, scale)); {
auto kern = font.GetKerning(a->second, b->second, scale);
if (kern != 0)
set_kerning(a->first, b->first, kern);
}
} }
float SpriteFont::get_kerning(uint32_t codepoint0, uint32_t codepoint1) const float SpriteFont::get_kerning(uint32_t codepoint0, uint32_t codepoint1) const
{ {
Tuple index; uint64_t index = ((uint64_t)codepoint0 << 32) | codepoint1;
index.first = codepoint0;
index.second = codepoint1;
auto it = m_kerning.find(index); auto it = m_kerning.find(index);
if (it != m_kerning.end()) if (it != m_kerning.end())
@ -224,12 +226,17 @@ float SpriteFont::get_kerning(uint32_t codepoint0, uint32_t codepoint1) const
void SpriteFont::set_kerning(uint32_t codepoint0, uint32_t codepoint1, float value) void SpriteFont::set_kerning(uint32_t codepoint0, uint32_t codepoint1, float value)
{ {
Tuple index; uint64_t index = ((uint64_t)codepoint0 << 32) | codepoint1;
index.first = codepoint0;
index.second = codepoint1;
m_kerning[index] = value;
}
if (value == 0)
{
m_kerning.erase(index);
}
else
{
m_kerning[index] = value;
}
}
const SpriteFont::Character& SpriteFont::get_character(uint32_t codepoint) const const SpriteFont::Character& SpriteFont::get_character(uint32_t codepoint) const
{ {

View File

@ -4,7 +4,7 @@
#include <blah/containers/str.h> #include <blah/containers/str.h>
#include <blah/drawing/subtexture.h> #include <blah/drawing/subtexture.h>
#include <blah/math/vec2.h> #include <blah/math/vec2.h>
#include <map> #include <unordered_map>
namespace Blah namespace Blah
{ {
@ -20,19 +20,9 @@ namespace Blah
Vec2 offset; Vec2 offset;
}; };
private: private:
// tuple between two characters
struct Tuple { uint32_t first; uint32_t second; };
struct TupleCompare
{
bool operator() (const Tuple& lhs, const Tuple& rhs) const
{
return ((uint64_t)(lhs.first) | ((uint64_t)lhs.second << 32)) < ((uint64_t)(rhs.first) | ((uint64_t)rhs.second << 32));
}
};
// charset & kerning maps // charset & kerning maps
std::map<uint32_t, Character> m_characters; std::unordered_map<uint32_t, Character> m_characters;
std::map<Tuple, float, TupleCompare> m_kerning; std::unordered_map<uint64_t, float> m_kerning;
// built texture // built texture
List<TextureRef> m_atlas; List<TextureRef> m_atlas;