CUGL 2.1
Cornell University Game Library
|
#include <CUFontLoader.h>
Public Member Functions | |
FontLoader () | |
void | dispose () override |
bool | load (const std::string &key, const std::string &source, int size) |
bool | load (const char *key, const std::string &source, int size) |
bool | load (const std::string &key, const char *source, int size) |
bool | load (const char *key, const char *source, int size) |
void | loadAsync (const std::string &key, const std::string &source, int size, LoaderCallback callback) |
void | loadAsync (const char *key, const std::string &source, int size, LoaderCallback callback) |
void | loadAsync (const std::string &key, const char *source, int size, LoaderCallback callback) |
void | loadAsync (const char *key, const char *source, int size, LoaderCallback callback) |
int | getFontSize () const |
void | setDefaultSize (int size) |
const std::string & | getCharacterSet () const |
void | setCharacterSet (const std::string &charset) |
Public Member Functions inherited from cugl::Loader< Font > | |
Loader () | |
std::shared_ptr< Font > | get (const std::string key) const |
std::shared_ptr< Font > | operator[] (const std::string key) const |
size_t | loadCount () const override |
size_t | waitCount () const override |
void | unloadAll () override |
Public Member Functions inherited from cugl::BaseLoader | |
BaseLoader () | |
~BaseLoader () | |
virtual bool | init () |
virtual bool | init (const std::shared_ptr< ThreadPool > &threads) |
std::shared_ptr< BaseLoader > | getHook () |
std::shared_ptr< ThreadPool > | getThreadPool () const |
void | setThreadPool (const std::shared_ptr< ThreadPool > &threads) |
void | setManager (AssetManager *manager) |
const AssetManager * | getManager () const |
bool | load (const std::string key, const std::string source) |
bool | load (const std::shared_ptr< JsonValue > &json) |
void | loadAsync (const std::string key, const std::string source, LoaderCallback callback) |
void | loadAsync (const std::shared_ptr< JsonValue > &json, LoaderCallback callback) |
bool | unload (const std::string key) |
bool | unload (const std::shared_ptr< JsonValue > &json) |
bool | contains (const std::string key) const |
bool | complete () const |
float | progress () const |
Static Public Member Functions | |
static std::shared_ptr< FontLoader > | alloc () |
static std::shared_ptr< FontLoader > | alloc (const std::shared_ptr< ThreadPool > &threads) |
Protected Member Functions | |
std::shared_ptr< Font > | preload (const std::string source, const std::string charset, int size) |
std::shared_ptr< Font > | preload (const std::shared_ptr< JsonValue > &json) |
void | materialize (const std::string key, const std::shared_ptr< Font > &font, LoaderCallback callback) |
bool | read (const std::string key, const std::string source, LoaderCallback callback, bool async) override |
bool | read (const std::string key, const std::string source, int size, LoaderCallback callback, bool async) |
bool | read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) override |
Protected Member Functions inherited from cugl::Loader< Font > | |
virtual std::vector< std::string > | keys () override |
bool | purge (const std::string key) override |
bool | verify (const std::string key) const override |
Protected Member Functions inherited from cugl::BaseLoader | |
virtual bool | purge (const std::shared_ptr< JsonValue > &json) |
Protected Attributes | |
int | _fontsize |
std::string | _charset |
Protected Attributes inherited from cugl::Loader< Font > | |
std::unordered_map< std::string, std::shared_ptr< Font > > | _assets |
std::unordered_set< std::string > | _queue |
Protected Attributes inherited from cugl::BaseLoader | |
std::shared_ptr< ThreadPool > | _loader |
AssetManager * | _manager |
This class is a implementation of Loader<Font>
This asset loader allows us to allocate fonts from the associated TrueType files. At load time you must specify both the TrueType file and the font size. Hence you may wish to load a font asset multiple times. If you do this, you may wish to adjust the character set. The size of the font atlas texture is determined by both the font size and the character set.
Note that this implementation uses a two phase loading system. First, it loads as much of the asset as possible without using OpenGL. This allows us to load the texture in a separate thread. It then finishes off the remainder of asset loading (particularly the OpenGL atlas generation) using Application#schedule. This is a good template for asset loaders in general.
As with all of our loaders, this loader is designed to be attached to an asset manager. Use the method getHook() to get the appropriate pointer for attaching the loader.
cugl::FontLoader::FontLoader | ( | ) |
Creates a new, uninitialized font loader
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a loader on the heap, use one of the static constructors instead.
|
inlinestatic |
Returns a newly allocated font loader.
This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.
This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool.
|
inlinestatic |
Returns a newly allocated font loader.
This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.
threads | The thread pool for asynchronous loading |
|
inlineoverridevirtual |
Disposes all resources and assets of this loader
Any assets loaded by this object will be immediately released by the loader. However, a font may still be available if it is referenced by another smart pointer. OpenGL will only release a font atlas once all smart pointer attached to the asset are null.
Once the loader is disposed, any attempts to load a new asset will fail. You must reinitialize the loader to begin loading assets again.
Reimplemented from cugl::BaseLoader.
|
inline |
Returns the default atlas character set
The character set determines exactly those characters in the font atlas. You should keep this value at a minimum, as it reduces the size of the atlas texture. Once set, any font processed by this loader will use this character set for its atlas.
If character set is the empty string, the atlas will contain all of the ASCII characters. This is the default value.
|
inline |
Returns the default font size
Once set, any future font processed by this loader will have this size unless otherwise specified. The default is 12 point.
|
inline |
Synchronously loads the given asset with the specified key.
The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.
This version of load allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
|
inline |
Synchronously loads the given asset with the specified key.
The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.
This version of load allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
|
inline |
Synchronously loads the given asset with the specified key.
The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.
This version of load allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
|
inline |
Synchronously loads the given asset with the specified key.
The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.
This version of load allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
|
inline |
Asynchronously loads the given asset with the specified key.
The asset will be loaded asynchronously. When it is finished loading, the asset 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 asset until it is complete again.
The optional callback function will be called with the asset status when it either finishes loading or fails to load.
This version of loadAsync allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
callback | An optional callback for asynchronous loading |
|
inline |
Asynchronously loads the given asset with the specified key.
The asset will be loaded asynchronously. When it is finished loading, the asset 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 asset until it is complete again.
The optional callback function will be called with the asset status when it either finishes loading or fails to load.
This version of loadAsync allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
callback | An optional callback for asynchronous loading |
|
inline |
Asynchronously loads the given asset with the specified key.
The asset will be loaded asynchronously. When it is finished loading, the asset 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 asset until it is complete again.
The optional callback function will be called with the asset status when it either finishes loading or fails to load.
This version of loadAsync allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
callback | An optional callback for asynchronous loading |
|
inline |
Asynchronously loads the given asset with the specified key.
The asset will be loaded asynchronously. When it is finished loading, the asset 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 asset until it is complete again.
The optional callback function will be called with the asset status when it either finishes loading or fails to load.
This version of loadAsync allows you to specify the font size, overriding the default value.
key | The key to access the font after loading |
source | The pathname to the font |
size | The font size |
callback | An optional callback for asynchronous loading |
|
protected |
Creates an atlas for the font asset, and assigns it the given key.
This method finishes the asset loading started in preload. As atlas generation requires OpenGL, this step is not safe to be done in a separate thread. Instead, it takes place in the main CUGL thread via ( Application#schedule.
The font atlas will use the character set specified in the asset.
This method supports an optional callback function which reports whether the asset was successfully materialized.
key | The key to access the asset after loading |
font | The font for atlas generation |
callback | An optional callback for asynchronous loading |
|
protected |
Loads the portion of this asset that is safe to load outside the main thread.
It is not safe to create an font atlas (which requires OpenGL) in a separate thread. However, it is safe to load the TTF data from the file. Hence this method does the maximum amount of work that can be done in asynchronous font loading.
This version of preload provides support for JSON directories. A font directory entry has the following values
"file": The path to the asset "size": This font size (int) "charset": The set of characters for the font atlas (string) "padding": The atlas padding (to prevent blur bleedthrough) "hinting": The rendering hints ("normal", "light", "mono", "none") "bold": Whether to make the font an (ad hoc) bold "italic": Whether to make the font an (ad hoc) italic "underline": Whether to underline the font "strike": Whether to strikethrough the font
json | The directory entry for the asset |
|
protected |
Loads the portion of this asset that is safe to load outside the main thread.
It is not safe to create an font atlas (which requires OpenGL) in a separate thread. However, it is safe to load the TTF data from the file. Hence this method does the maximum amount of work that can be done in asynchronous font loading.
source | The pathname to the asset |
charset | The atlas character set |
size | The font size |
|
overrideprotectedvirtual |
Internal method to support asset loading.
This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.
This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.
This version of read provides support for JSON directories. A font directory entry has the following values
"file": The path to the asset "size": This font size (int) "charset": The set of characters for the font atlas (string) "padding": The atlas padding (to prevent blur bleedthrough) "hinting": The rendering hints ("normal", "light", "mono", "none") "bold": Whether to make the font an (ad hoc) bold "italic": Whether to make the font an (ad hoc) italic "underline": Whether to underline the font "strike": Whether to strikethrough the font
json | The directory entry for the asset |
callback | An optional callback for asynchronous loading |
async | Whether the asset was loaded asynchronously |
Reimplemented from cugl::BaseLoader.
|
protected |
Internal method to support asset loading.
This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.
This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.
key | The key to access the asset after loading |
source | The pathname to the asset |
size | The font size (overriding the default) |
callback | An optional callback for asynchronous loading |
async | Whether the asset was loaded asynchronously |
|
inlineoverrideprotectedvirtual |
Internal method to support asset loading.
This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.
This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.
key | The key to access the asset after loading |
source | The pathname to the asset |
callback | An optional callback for asynchronous loading |
async | Whether the asset was loaded asynchronously |
Reimplemented from cugl::BaseLoader.
|
inline |
Sets the default atlas character set
The character set determines exactly those characters in the font atlas. You should keep this value at a minimum, as it reduces the size of the atlas texture. Once set, any font processed by this loader will use this character set for its atlas.
If character set is the empty string, the atlas will contain all of the ASCII characters. This is the default value.
charset | The default atlas character set |
|
inline |
Sets the default font size
Once set, any future font processed by this loader will have this size unless otherwise specified. The default is 12 point.
size | The default font size |
|
protected |
The default atlas character set ("" for ASCII)
|
protected |
The default font size