Cornell Cocos
Cornell Extensions to Cocos2d
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
FontLoader Class Reference

#include <CUFontLoader.h>

Inheritance diagram for FontLoader:
Loader< TTFont > BaseLoader

Classes

class  Coordinator
 

Public Member Functions

void start () override
 
void stop () override
 
size_t waitCount () const override
 
TTFontload (std::string key, std::string source) override
 
TTFontload (std::string key, std::string source, float size)
 
void loadAsync (std::string key, std::string source) override
 
void loadAsync (std::string key, std::string source, float size)
 
void unload (std::string key) override
 
void unloadAll () override
 
float getDefaultSize () const
 
void setDefaultSize (float size)
 
CC_CONSTRUCTOR_ACCESS _default (DEFAULT_SIZE)
 
virtual ~FontLoader ()
 
- Public Member Functions inherited from Loader< TTFont >
virtual bool contains (std::string key) const
 
virtual TTFontget (std::string key) const
 
TTFontoperator[] (std::string key) const
 
virtual size_t loadCount () const override
 
- Public Member Functions inherited from BaseLoader
bool isActive () const
 
bool isComplete () const
 
float progress () const
 
CC_CONSTRUCTOR_ACCESS _active (false)
 
virtual ~BaseLoader ()
 

Static Public Member Functions

static FontLoadercreate ()
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: FontLoader() : Loader<TTFont>()
 
- Public Attributes inherited from BaseLoader
CC_CONSTRUCTOR_ACCESS __pad0__: BaseLoader() : Ref()
 

Protected Member Functions

void allocate (std::string key, TTFont *font)
 

Protected Attributes

float _default
 
std::unordered_set< std::string > _fqueue
 
- Protected Attributes inherited from Loader< TTFont >
std::unordered_map< std::string, TTFont * > _assets
 
- Protected Attributes inherited from BaseLoader
bool _active
 

Static Protected Attributes

static Coordinator_gCoordinator = nullptr
 

Detailed Description

Class is a implementation of Loader<TTFont>

This asset loader allows us to allocate font objects from the associated source files. Note that a True Type font asset is both the source file AND the font size. Because the font is converted to a texture for usage, different sizes are different fonts. This loader can be given a default font size, so that all fonts loaded have this size.

As with all of our loaders, this loader is designed to be attached to a scene. This is the natural was to do things, as Cocos2d is scene based. However, its asset loading is typically done through the director, which is global. This makes is hard to determine when it is safe to unload an asset. Even though the current scene many not need it, it may be used by another active scene. Unloading the asset would corrupt that scene.

This loader solves this problem by have a static coordinator behind the scenes. This coordinate is shared across all loader instances. It decides When an asset is truly ready to be unloaded. We assume that all instances are run only in the Director thread.

Font objects are uniquely identified by their source file and size. Attempt to load a font object a second time, even under a new key, will return a reference to the same font object.

Constructor & Destructor Documentation

virtual FontLoader::~FontLoader ( )
inlinevirtual

Disposes of the font loader.

This destructor will stop the font loader if not done so already.

Member Function Documentation

void FontLoader::allocate ( std::string  key,
TTFont font 
)
protected

A function to create a new font from a filename.

This method should be part of a C++11 closure so that it can be used by the thread pool for asynchronous loading. When done, it will safely update the data structures of this loader.

Parameters
keyThe key for the font information
fontThe font to associate with the key
FontLoader * FontLoader::create ( )
static

Creates a new FontLoader.

This constructor does not start the font loader. It simply creates an object for the font loader so that it can be attached to the asset manager. Call start() when you are ready to start using it.

Returns
an autoreleased FontLoader object
float FontLoader::getDefaultSize ( ) const
inline

Returns the default font size

Any font processed by this loader will have this size unless otherwise specified.

Returns
the default font size
TTFont* FontLoader::load ( std::string  key,
std::string  source 
)
inlineoverridevirtual

Loads a font and assigns it to the given key.

The font will be loaded synchronously. It will be available immediately. This method should be limited to those times in which a font is really necessary immediately, such as for a loading screen.

The loaded font will use the default size of this loader.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font file

the loaded font

Returns
the loaded font

Reimplemented from Loader< TTFont >.

TTFont * FontLoader::load ( std::string  key,
std::string  source,
float  size 
)

Loads a font and assigns it to the given key.

The font will be loaded synchronously. It will be available immediately. This method should be limited to those times in which a font is really necessary immediately, such as for a loading screen.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font file
sizeThe font size for this asset

the loaded font

Returns
the loaded font

Loads a font and assigns it to the given key.

The font will be loaded synchronously. It will be available immediately. This method should be limited to those times in which a font is really necessary immediately, such as for a loading screen.

The loaded font will use the default size of this loader.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font file

the loaded font

Returns
the loaded font
void FontLoader::loadAsync ( std::string  key,
std::string  source 
)
inlineoverridevirtual

Adds a new font to the loading queue.

The font will be loaded asynchronously. When it is finished loading, it will be added to this loader, and accessible under the given key. This method will mark the loading process as not complete, even if it was completed previously. It is not safe to access the loaded font until it is complete again.

The loaded font will use the default size of this loader.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font file

the font upon loading

Reimplemented from BaseLoader.

void FontLoader::loadAsync ( std::string  key,
std::string  source,
float  size 
)

Adds a new font to the loading queue.

The font will be loaded asynchronously. When it is finished loading, it will be added to this loader, and accessible under the given key. This method will mark the loading process as not complete, even if it was completed previously. It is not safe to access the loaded font until it is complete again.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font file
sizeThe font size for this asset

the font upon loading

Loads a font and assigns it to the given key.

The font will be loaded synchronously. It will be available immediately. This method should be limited to those times in which a font is really necessary immediately, such as for a loading screen.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font file
sizeThe font size for this asset

the font asset upon loading

void FontLoader::setDefaultSize ( float  size)
inline

Sets the default font size

Any font processed by this loader will have this size unless otherwise specified.

Parameters
sizethe default font size
void FontLoader::start ( )
overridevirtual

Starts this font loader.

Any assets loaded by this loader will be immediately released by the loader. However, an asset may still be available if it is attached to another loader. The asset manager is backed by a central coordinator that allows the sharing of assets.

Once the loader is stopped, any attempts to load a new asset will fail. You must call start() to begin loading assets again.

Starts this font loader.

This method bootstraps the loader with any initial resources that it needs to load assets. Attempts to load an asset before this method is called will fail.

By separating this call from the constructor, this allows us to construct loaders and attach them to the AssetManager before we are ready to load assets.

Reimplemented from BaseLoader.

void FontLoader::stop ( )
overridevirtual

Stops this resource loader removing all assets.

Any assets loaded by this loader will be immediately released by the loader. However, an asset may still be available if it is attached to another loader.

Once the loader is stopped, any attempts to load a new asset will fail. You must call start() to begin loading assets again.

Stops this resource loader removing all assets.

Any assets loaded by this loader will be immediately released by the loader. However, an asset may still be available if it is attached to another loader. The asset manager is backed by a central coordinator that allows the sharing of assets.

Once the loader is stopped, any attempts to load a new asset will fail. You must call start() to begin loading assets again.

Reimplemented from BaseLoader.

void FontLoader::unload ( std::string  key)
overridevirtual

Unloads the font for the given key.

This method simply unloads the font for the scene associated with this loader. The font will not be deleted or removed from memory until it is removed from all instances of FontLoader.

Parameters
keythe key referencing the font

the font for the given key

Reimplemented from BaseLoader.

void FontLoader::unloadAll ( )
overridevirtual

Unloads all assets present in this loader.

This method simply unloads the fonts for the scene associated with this loader. The fonts will not be deleted or removed from memory until they are removed from all instances of FontLoader.

all loaded fonts

Unloads all assets present in this loader.

This method simply unloads the fonts for the scene associated with this loader. The founds will not be deleted or removed from memory until they are removed from all instances of FontLoader.

all loaded fonts

Reimplemented from BaseLoader.

size_t FontLoader::waitCount ( ) const
inlineoverridevirtual

Returns the number of fonts waiting to load.

This is a rough way to determine how many fonts are still pending. A font is pending if it has been loaded asychronously, and the loading process has not yet finished. This method counts each font equally regardless of the memory requirements of the format.

Returns
the number of fonts waiting to load.

Reimplemented from BaseLoader.

Member Data Documentation

float FontLoader::_default
protected

The default size

std::unordered_set<std::string> FontLoader::_fqueue
protected

The fonts we are expecting that are not yet loaded

NS_CC_BEGIN FontLoader::Coordinator * FontLoader::_gCoordinator = nullptr
staticprotected

The static coordinator singleton


The documentation for this class was generated from the following files: