CUGL 2.3
Cornell University Game Library
|
#include <CUFont.h>
Public Member Functions | |
Atlas () | |
~Atlas () | |
void | dispose () |
bool | init (Font *parent, std::deque< Uint32 > &glyphset) |
bool | hasGlyph (Uint32 a) const |
bool | hasGlyphs (const std::vector< Uint32 > &glyphs) const |
bool | hasGlyphs (const std::string glyphs) const |
bool | getQuad (Uint32 thechar, Vec2 &offset, Mesh< SpriteVertex2 > &mesh, const Rect rect) const |
void | getQuad (Uint32 thechar, Vec2 &offset, Mesh< SpriteVertex2 > &mesh) const |
bool | build () |
bool | materialize () |
Static Public Member Functions | |
static std::shared_ptr< Atlas > | alloc (Font *parent, std::deque< Uint32 > &glyphset) |
Public Attributes | |
std::shared_ptr< Texture > | texture |
std::unordered_map< Uint32, Rect > | glyphmap |
This class represents a single font atlas.
A font atlas is a collection of pre-rendered glyphs, together with a directory of the bounds for each glyph. This directory information makes it very easy to quickly construct a textured quad mesh for a series of glyphys.
An font may have more than one atlas, particulary if the font size is large and there are a large number of supported glyphs. In that case, the atlases typically support a disjoint set of glyphs. However, we do not enforce this.
cugl::Font::Atlas::Atlas | ( | ) |
Creates an uninitialized atlas with no parent font.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.
|
inline |
Deletes this atlas, diposing of all its resources.
|
inlinestatic |
Returns a newly allocated atlas for the given font and glyphset
This allocator will perform the layout computation, but it will not create any textures or SDL surfaces. It will consume glyphs from the provided glyphset as it adds them to the atlas. So if it successfully adds all glyphs, the value glyphset will be emptied.
It is possible for the atlas to reject some glyphs. This is typically because the resulting texture size would exceed the maximum allowable texture size. In that case, the remaining elements in glyphset are glyphs that must be processed by another atlas.
If this atlas cannot process any of the elements in glyphset (because they are unsupported), then this method returns nullptr.
parent | The parent font of this atlas |
glyphset | The glyphs to add to this atlas |
bool cugl::Font::Atlas::build | ( | ) |
Builds the texture data for this given atlas.
This method does not generate the OpenGL texture, but does all other work in creates the atlas. In particular it creates the image buffer so that texture creation is just one OpenGL call. This creation will happen once materialize()
is called. As a result, it is safe to call this method outside of the main thread.
void cugl::Font::Atlas::dispose | ( | ) |
Deletes the atlas resources and resets all attributes.
This will delete the parent font as well. You must reinitialize the atlas to use it.
void cugl::Font::Atlas::getQuad | ( | Uint32 | thechar, |
Vec2 & | offset, | ||
Mesh< SpriteVertex2 > & | mesh | ||
) | const |
Creates a single quad to render this character and stores it in mesh
This method will append the vertices to the provided mesh and update the indices to include these new vertices. Once the quad is generated, the offset will be adjusted to contain the next place to render a character. This method will not generate anything if the character is not supported by this atlas.
thechar | The character to convert to render data |
offset | The (unkerned) starting position of the quad |
mesh | The mesh to store the vertices |
bool cugl::Font::Atlas::getQuad | ( | Uint32 | thechar, |
Vec2 & | offset, | ||
Mesh< SpriteVertex2 > & | mesh, | ||
const Rect | rect | ||
) | const |
Creates a single quad to render this character and stores it in mesh
This method will append the vertices to the provided mesh and update the indices to include these new vertices. Once the quad is generated, the offset will be adjusted to contain the next place to render a character. This method will not generate anything if the character is not supported by this atlas.
The quad is adjusted so that all of the vertices fit in the provided rectangle. This may mean that no quad is generated at all.
thechar | The character to convert to render data |
offset | The (unkerned) starting position of the quad |
rect | The bounding box for the quad |
mesh | The mesh to store the vertices |
bool cugl::Font::Atlas::hasGlyph | ( | Uint32 | a | ) | const |
Returns true if this font has a glyph for the given (UNICODE) character.
The Unicode representation uses the endianness native to the platform. Therefore, this value should not be serialized. Use UTF8 to represent unicode in a platform-independent manner.
Note that control characters (like newline) never have glyphs. However, spaces do.
a | The Unicode character to check. |
bool cugl::Font::Atlas::hasGlyphs | ( | const std::string | glyphs | ) | const |
Returns true if this atlas has all of the given glyphs
We assume that the string represents the glyphs in a UTF8 encoding.
Note that control characters (like newline) never have glyphs. However, spaces do.
glyphs | The UTF8 glyphs to check. |
bool cugl::Font::Atlas::hasGlyphs | ( | const std::vector< Uint32 > & | glyphs | ) | const |
Returns true if this atlas has all of the given unicode glyphs
The Unicode representation uses the endianness native to the platform. Therefore, this value should not be serialized. Use UTF8 to represent unicode in a platform-independent manner.
Note that control characters (like newline) never have glyphs. However, spaces do.
glyphs | The Unicode characters to check. |
bool cugl::Font::Atlas::init | ( | Font * | parent, |
std::deque< Uint32 > & | glyphset | ||
) |
Initializes an atlas for the given font and glyphset
This initializer will perform the layout computation, but it will not create any textures or SDL surfaces. It will consume glyphs from the provided glyphset as it adds them to the atlas. So if it successfully adds all glyphs, the value glyphset will be emptied.
It is possible for the atlas to reject some glyphs. This is typically because the resulting texture size would exceed the maximum allowable texture size. In that case, the remaining elements in glyphset are glyphs that must be processed by another atlas.
If this atlas cannot process any of the elements in glyphset (because they are unsupported), then this method returns false.
parent | The parent font of this atlas |
glyphset | The glyphs to add to this atlas |
bool cugl::Font::Atlas::materialize | ( | ) |
Creates the OpenGL texture for this atlas.
This method must be called on the main thread. It is only safe to call this method after a succesful call to build()
.
std::unordered_map<Uint32,Rect> cugl::Font::Atlas::glyphmap |
The location of each glyph in the atlas texture. This includes padding.
std::shared_ptr<Texture> cugl::Font::Atlas::texture |
The texture (may be null if not materialized)