PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
FontCache.h
Go to the documentation of this file.
1#ifndef __FONTCACHE_H__
2#define __FONTCACHE_H__
3
4#include <string>
5#include <map>
6#include "SDL.h"
7#include "SDL_ttf.h"
8
9namespace FontCache {
10
15 std::string filename; // Font filename (e.g., "DejaVuSans.ttf")
16 int style; // Font style (PFS_Normal, PFS_Bold, PFS_Italic, etc.)
17 int size; // Point size (e.g., 24)
18
20 bool operator<(const FontCacheKey& other) const;
21
23 bool operator==(const FontCacheKey& other) const;
24};
25
29 TTF_Font* ttf_font; // Shared SDL_TTF font object
30 int ref_count; // Number of PlatformFont objects using this font
31
32 CachedTTFFont(TTF_Font* f) : ttf_font(f), ref_count(1) {}
33};
34
47public:
50
58 TTF_Font* GetFont(const FontCacheKey& key, const std::string& full_path);
59
65 void ReleaseFont(const FontCacheKey& key);
66
72 void GetStats(int& unique_fonts, int& total_refs, int& cache_hits, int& cache_misses);
73
75 void PrintCache();
76
77private:
79 FontCacheManager() : mCacheHits(0), mCacheMisses(0) {}
80
83
85 FontCacheManager(const FontCacheManager&) = delete;
86 FontCacheManager& operator=(const FontCacheManager&) = delete;
87
89 std::map<FontCacheKey, CachedTTFFont> mCache;
90
92 int mCacheHits; // Number of successful cache lookups
93 int mCacheMisses; // Number of new font loads
94};
95
96} // namespace FontCache
97
98#endif // __FONTCACHE_H__
static FontCacheManager & GetInstance()
Get singleton instance.
Definition FontCache.cpp:33
void ReleaseFont(const FontCacheKey &key)
Definition FontCache.cpp:79
TTF_Font * GetFont(const FontCacheKey &key, const std::string &full_path)
Definition FontCache.cpp:42
void PrintCache()
Print cache contents to stdout (for debugging)
void GetStats(int &unique_fonts, int &total_refs, int &cache_hits, int &cache_misses)
CachedTTFFont(TTF_Font *f)
Definition FontCache.h:32
bool operator<(const FontCacheKey &other) const
Comparison operator for std::map ordering.
Definition FontCache.cpp:10
bool operator==(const FontCacheKey &other) const
Equality operator for debugging/testing.
Definition FontCache.cpp:23