From 5915ae19389e2a7093134a15faa9f34283acd221 Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Mon, 19 Oct 2020 00:50:16 -0700 Subject: [PATCH] spritefont should use unordered_map --- public/blah/drawing/spritefont.cpp | 27 +++++++++++++++++---------- public/blah/drawing/spritefont.h | 16 +++------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/public/blah/drawing/spritefont.cpp b/public/blah/drawing/spritefont.cpp index 5062bc5..e8401e2 100644 --- a/public/blah/drawing/spritefont.cpp +++ b/public/blah/drawing/spritefont.cpp @@ -155,7 +155,7 @@ void SpriteFont::build(const Font& font, float size, const uint32_t* charset) packer.max_size = 8192; packer.power_of_two = true; - std::map glyphs; + std::unordered_map glyphs; List buffer; auto ranges = charset; @@ -207,14 +207,16 @@ void SpriteFont::build(const Font& font, float size, const uint32_t* charset) // add kerning for (auto a = glyphs.begin(); a != glyphs.end(); a++) 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 { - Tuple index; - index.first = codepoint0; - index.second = codepoint1; + uint64_t index = ((uint64_t)codepoint0 << 32) | codepoint1; auto it = m_kerning.find(index); 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) { - Tuple index; - index.first = codepoint0; - index.second = codepoint1; - m_kerning[index] = value; -} + uint64_t index = ((uint64_t)codepoint0 << 32) | codepoint1; + if (value == 0) + { + m_kerning.erase(index); + } + else + { + m_kerning[index] = value; + } +} const SpriteFont::Character& SpriteFont::get_character(uint32_t codepoint) const { diff --git a/public/blah/drawing/spritefont.h b/public/blah/drawing/spritefont.h index fa99fb8..9461771 100644 --- a/public/blah/drawing/spritefont.h +++ b/public/blah/drawing/spritefont.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include namespace Blah { @@ -20,19 +20,9 @@ namespace Blah Vec2 offset; }; 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 - std::map m_characters; - std::map m_kerning; + std::unordered_map m_characters; + std::unordered_map m_kerning; // built texture List m_atlas;