Skip to content

Commit

Permalink
Fix: Don't rely on static initialization to set up sprite font caches.
Browse files Browse the repository at this point in the history
The order of static initialization is undefined, so this can cause initalization
before relevant caches are initializations.
  • Loading branch information
PeterN committed May 18, 2023
1 parent 418888a commit f454ec8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/fontcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ int GetCharacterHeight(FontSize size)
}


/* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };

/* static */ FontCache *FontCache::caches[FS_END];

/* static */ void FontCache::InitializeFontCaches()
{
for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) {
if (FontCache::caches[fs] == nullptr) new SpriteFontCache(fs); /* FontCache inserts itself into to the cache. */
}
}

/* Check if a glyph should be rendered with anti-aliasing. */
bool GetFontAAState(FontSize size, bool check_blitter)
Expand Down Expand Up @@ -136,6 +141,8 @@ extern void LoadCoreTextFont(FontSize fs);
*/
void InitFontCache(bool monospace)
{
FontCache::InitializeFontCaches();

for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
if (monospace != (fs == FS_MONO)) continue;

Expand Down
2 changes: 2 additions & 0 deletions src/fontcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class FontCache {
FontCache(FontSize fs);
virtual ~FontCache();

static void InitializeFontCaches();

static int GetDefaultFontHeight(FontSize fs);

/**
Expand Down

0 comments on commit f454ec8

Please sign in to comment.