#include using namespace JGL; char CachedGlyph::getCharacter() { return character; } const std::array CachedGlyph::getTexCoords() const { return texcoords; } CachedGlyph::CachedGlyph(char c, std::array texcoords, float x2offset, float y2offset, float w, float h, float advanceX, float advanceY) { character = c; this->x2offset = x2offset; this->y2offset = y2offset; this->w = w; this->h = h; this->advanceX = advanceX; this->advanceY = advanceY; this->texcoords = texcoords; } //TODO //Because most things shown would be english characters. We can cut down on the iteration time significantly //by putting each english character at the beginning of the list in order of how often they usually occur in text. void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) { glyphs.emplace(glyph->getCharacter(), glyph); } unsigned int JGL::CachedFont::getFontSize() { return font_size; } unsigned int JGL::CachedFont::getFontIndex() { return font_index; } //TODO make this code go faster. CachedGlyph* JGL::CachedFont::getGlyph(char c) { auto it = glyphs.find(c); if (it != glyphs.end()) return it->second; return nullptr; } CachedFont::CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index) { this->texture = texture_id; this->texture_width = texture_width; this->texture_height = texture_height; this->font_size = font_size; this->font_index = font_index; } std::map CachedFont::getGlyphs() { return glyphs; } const GLuint* CachedFont::getTexture() { return &texture; } GLsizei CachedFont::getTextureWidth() const { return texture_width; } GLsizei CachedFont::getTextureHeight() const { return texture_height; } void FontCache::appendFont(CachedFont* font) { cachedFonts.push_back(font); } void FontCache::newFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index) { auto* font = new CachedFont(texture_id, texture_width, texture_height, font_size, font_index); cachedFonts.push_back(font); } void FontCache::eraseFont(CachedFont* font) { for (int i = 0; i < cachedFonts.size(); i++) { if (cachedFonts[i] == font) { delete cachedFonts[i]; cachedFonts.erase(cachedFonts.begin() + i); } } } void FontCache::purgeCache() { //Remove every font from the cache. for (const auto& font : cachedFonts) eraseFont(font); } std::vector FontCache::getFonts() { return cachedFonts; } CachedFont* FontCache::getFont(unsigned int font_size, unsigned int font_index) { if (cachedFonts.empty()) return nullptr; for (auto* f : cachedFonts) if (f->getFontIndex() == font_index && f->getFontSize() == font_size) return f; return nullptr; }