// can't unload TTFs, so we'll need to make sure we don't keep reloading them, sadly void* TTF_Load(Asset &asset) { TTFFont_t *fnt = new TTFFont_t(); if (ctx == nullptr) { ctx = glfonsCreate(512, 512, FONS_ZERO_TOPLEFT); } int found = fonsGetFontByName(ctx, asset.name); if (found != FONS_INVALID) { fnt->valid = true; fnt->hnd = found; found++; return fnt; } unsigned char *font; auto sz = FS_ReadFile(asset.path, (void **)&font); if (sz == -1) { Con_Errorf(ERR_GAME, "couldn't load file %s", asset.path); return nullptr; } int hnd = fonsAddFontMem(ctx, asset.name, font, sz, 1); if (hnd < 0) { return nullptr; } fnt->valid = true; fnt->hnd = hnd; return (void*)fnt; }
/** * Add text into the draw list */ unsigned int FontStashAdapter::addText( std::string text, int x, int y, std::string font_name, Color color, float font_size, float spacing, float blur ) { /* Remember where the index of this text in the vector */ unsigned int text_entry_id = static_cast<unsigned int>(texts.size()); int font_ID = fonsGetFontByName(context, font_name.c_str() ); texts.push_back( TextInfo( text, x, y, font_ID, color, font_size, spacing, blur ) ); return text_entry_id; }
/** * Update the existing text, coordinate of the text, and also other parameters */ void FontStashAdapter::setText( unsigned int text_entry_ID, std::string text, int x, int y, std::string font_name, Color color, float font_size, float spacing, float blur ) { texts[text_entry_ID].text = text; texts[text_entry_ID].x = x; texts[text_entry_ID].y = y; texts[text_entry_ID].fontID = fonsGetFontByName(context, font_name.c_str() ); texts[text_entry_ID].color = Utils::rgbaTo32Bit(color); texts[text_entry_ID].fontSize = font_size; texts[text_entry_ID].spacing = spacing; texts[text_entry_ID].blur = blur; }