- access via a singleton instead.
glColor4fv(_color);
transform();
- FGFontCache *fc = globals->get_fontcache();
- fntFont* font = fc->getTexFont(_font_name.c_str());
+ fntFont* font = FGFontCache::instance()->getTexFont(_font_name);
if (!font) {
return; // don't crash on missing fonts
}
FGTextLayer::setFontName(const std::string &name)
{
_font_name = name + ".txf";
- FGFontCache *fc = globals->get_fontcache();
- fntFont* font = fc->getTexFont(_font_name.c_str());
+ fntFont* font = FGFontCache::instance()->getTexFont(_font_name);
if (!font) {
SG_LOG(SG_COCKPIT, SG_WARN, "unable to find font:" << name);
}
// FGFontCache class.
////////////////////////////////////////////////////////////////////////
+static std::auto_ptr<FGFontCache> global_fontCacheInstance;
+
+
extern puFont FONT_HELVETICA_14;
extern puFont FONT_SANS_12B;
extern puFont FONT_HELVETICA_12;
{
struct GuiFont
{
- const char *name;
+ const char* name;
puFont *font;
struct Predicate
: public std::unary_function<const GuiFont, bool>
{
- Predicate(const char* name_) : name(name_) {}
+ Predicate(const std::string& name_) : name(name_) {}
bool operator() (const GuiFont& f1) const
{
- return ::strcmp(f1.name, name) == 0;
+ return (name == f1.name);
}
- const char* name;
+ const std::string name;
};
};
const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])-1];
}
+FGFontCache* FGFontCache::instance()
+{
+ if (!global_fontCacheInstance.get()) {
+ global_fontCacheInstance.reset(new FGFontCache);
+ }
+
+ return global_fontCacheInstance.get();
+}
+
FGFontCache::FGFontCache() :
_initialized(false)
{
}
struct FGFontCache::fnt *
-FGFontCache::getfnt(const char *name, float size, float slant)
+FGFontCache::getfnt(const std::string& fontName, float size, float slant)
{
- std::string fontName(name);
FntParams fntParams(fontName, size, slant);
PuFontMap::iterator i = _puFonts.find(fntParams);
if (i != _puFonts.end())
texfont = texi->second;
} else {
const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
- GuiFont::Predicate(name));
+ GuiFont::Predicate(fontName));
if (guifont != guifontsEnd) {
pufont = guifont->font;
}
}
puFont *
-FGFontCache::get(const char *name, float size, float slant)
+FGFontCache::get(const std::string& name, float size, float slant)
{
return getfnt(name, size, slant)->pufont;
}
fntTexFont *
-FGFontCache::getTexFont(const char *name, float size, float slant)
+FGFontCache::getTexFont(const std::string& name, float size, float slant)
{
init();
return getfnt(name, size, slant)->texfont;
}
SGPath
-FGFontCache::getfntpath(const char *name)
+FGFontCache::getfntpath(const std::string& name)
{
init();
SGPath path(_path);
- if (name && std::string(name) != "") {
+ if (name != "") {
path.append(name);
if (path.exists())
return path;
*/
class FGFontCache {
private:
+ FGFontCache(); // private constructor, use singleton instance() accessor
+
// The parameters of a request to the cache.
struct FntParams
{
PuFontMap _puFonts;
bool _initialized;
- struct fnt *getfnt(const char *name, float size, float slant);
+ struct fnt *getfnt(const std::string& name, float size, float slant);
void init();
public:
- FGFontCache();
+ // note this accesor is NOT thread-safe
+ static FGFontCache* instance();
+
~FGFontCache();
- puFont *get(const char *name, float size=15.0, float slant=0.0);
+ puFont *get(const std::string& name, float size=15.0, float slant=0.0);
puFont *get(SGPropertyNode *node);
- fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
+ fntTexFont *getTexFont(const std::string& name, float size=15.0, float slant=0.0);
- SGPath getfntpath(const char *name);
+ SGPath getfntpath(const std::string& name);
/**
* Preload all the fonts in the FlightGear font directory. It is
* important to load the font textures early, with the proper
SGPropertyNode *fontnode = props->getNode("font");
if (fontnode) {
- FGFontCache *fc = globals->get_fontcache();
- _font = fc->get(fontnode);
+ _font = FGFontCache::instance()->get(fontnode);
} else {
_font = _gui->getDefaultFont();
}
object->setBorderThickness( props->getIntValue("border", 2) );
if (SGPropertyNode *nft = props->getNode("font", false)) {
- FGFontCache *fc = globals->get_fontcache();
- puFont *lfnt = fc->get(nft);
+ puFont *lfnt = FGFontCache::instance()->get(nft);
object->setLabelFont(*lfnt);
object->setLegendFont(*lfnt);
} else {
puSetDefaultStyle ( PUSTYLE_SMALL_SHADED ); //PUSTYLE_DEFAULT
puSetDefaultColourScheme (0.8, 0.8, 0.9, 1);
- FGFontCache *fc = globals->get_fontcache();
+ FGFontCache *fc = FGFontCache::instance();
fc->initializeFonts();
puFont *GuiFont
= fc->get(globals->get_locale()->getDefaultFont("typewriter.txf"),
void
NewGUI::setupFont (SGPropertyNode *node)
{
- _font = globals->get_fontcache()->get(node);
+ _font = FGFontCache::instance()->get(node);
puSetDefaultFonts(*_font, *_font);
return;
}
void HUD::init()
{
- const char* fontName = 0;
- _font_cache = globals->get_fontcache();
+ std::string fontName;
if (!_font) {
fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
- _font = _font_cache->getTexFont(fontName);
+ _font = FGFontCache::instance()->getTexFont(fontName);
}
if (!_font)
throw sg_io_exception("/sim/hud/font/name is not a texture font",
#include <Aircraft/controls.hxx>
#include <Airports/runways.hxx>
#include <Autopilot/route_mgr.hxx>
-#include <GUI/FGFontCache.hxx>
#include <GUI/gui.h>
#include <Scenery/scenery.hxx>
#include <Scenery/tilemgr.hxx>
commands( SGCommandMgr::instance() ),
channel_options_list( NULL ),
initial_waypoints( NULL ),
- fontcache ( new FGFontCache ),
channellist( NULL ),
haveUserSettings(false),
_chatter_queue(NULL)
delete channel_options_list;
delete initial_waypoints;
- delete fontcache;
delete channellist;
simgear::PropertyObjectBase::setDefaultRoot(NULL);
class FGViewMgr;
class FGViewer;
class FGRenderer;
-class FGFontCache;
class FGSampleQueue;
namespace simgear { namespace pkg {
// and or flight-plan file during initialization
string_list *initial_waypoints;
- FGFontCache *fontcache;
-
// Navigational Aids
FGTACANList *channellist;
FGScenery * get_scenery () const;
FGTileMgr * get_tile_mgr () const;
-
- inline FGFontCache *get_fontcache() const { return fontcache; }
inline FGTACANList *get_channellist() const { return channellist; }
inline void set_channellist( FGTACANList *c ) { channellist = c; }
stateSet->setTextureAttribute(0, splashTexture);
geode->addDrawable(geometry);
-
+ FGFontCache* fontCache = FGFontCache::instance();
osgText::Text* text = new osgText::Text;
std::string fn = style->getStringValue("fonts/splash", "");
- text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
+ text->setFont(fontCache->getfntpath(fn).str());
text->setCharacterSize(0.06);
text->setColor(osg::Vec4(1, 1, 1, 1));
text->setPosition(osg::Vec3(0, -0.92, 0));
geode->addDrawable(text);
osgText::Text* spinnertext = new osgText::Text;
- spinnertext->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
+ spinnertext->setFont(fontCache->getfntpath(fn).str());
spinnertext->setCharacterSize(0.06);
spinnertext->setColor(osg::Vec4(1, 1, 1, 1));
spinnertext->setPosition(osg::Vec3(0, -0.97, 0));
geode->addDrawable(spinnertext);
text = new osgText::Text;
- text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
+ text->setFont(fontCache->getfntpath(fn).str());
text->setCharacterSize(0.08);
text->setColor(osg::Vec4(1, 1, 1, 1));
text->setPosition(osg::Vec3(0, 0.92, 0));
text = new osgText::Text;
- text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
+ text->setFont(fontCache->getfntpath(fn).str());
text->setCharacterSize(0.06);
text->setColor(osg::Vec4(1, 1, 1, 1));
text->setPosition(osg::Vec3(0, 0.82, 0));