ge211  2021.5.1
A student game engine
Text_sprite Class Reference

Detailed Description

A Sprite that displays text.

Definition at line 231 of file ge211_sprites.hxx.

+ Inheritance diagram for Text_sprite:
+ Collaboration diagram for Text_sprite:

Classes

class  Builder
 Builder-style API for configuring and constructing Text_sprites. More...
 

Public Member Functions

 Text_sprite ()
 Constructs an empty text sprite. More...
 
 Text_sprite (std::string const &, Font const &)
 Constructs a white text sprite with the given text and font. More...
 
bool empty () const
 Is this Text_sprite empty? (If so, you shouldn't try to use it.)
 
 operator bool () const
 Is this Text_sprite non-empty (and thus renderable)?
 
void reconfigure (Builder const &)
 Resets this text sprite with the configuration from the given Builder.
 

Constructor & Destructor Documentation

◆ Text_sprite() [1/2]

Constructs an empty text sprite.

This is useful when you don't yet know the message at the point where the sprite is created. It's an error to pass the an empty Text_sprite to Sprite_set::add_sprite(Sprite const&, Posn<int>, int, Transform), but you can use Text_sprite::reconfigure(Builder const&) to make it non-empty.

Example

struct View
{
void draw(ge211::Sprite_set& set,
std::string const& name,
int score);
ge211::Font sans30{"sans.ttf", 30};
ge211::Text_sprite name_sprite;
ge211::Text_sprite score_sprite;
}
void View::draw(ge211::Sprite_set& set,
std::string const& name,
int score)
{
ge211::Text_sprite::Builder name_builder(sans30);
name_builder.color(NAME_COLOR) << name;
name_sprite.reconfigure(name_builder);
set.add_sprite(name_sprite, NAME_POSITION);
ge211::Text_sprite::Builder score_builder(sans30);
score_builder.color(SCORE_COLOR) << score;
score_sprite.reconfigure(score_builder);
set.add_sprite(score_sprite, SCORE_POSITION);
}

Definition at line 239 of file ge211_sprites.cxx.

◆ Text_sprite() [2/2]

Text_sprite ( std::string const &  message,
Font const &  font 
)

Constructs a white text sprite with the given text and font.

For more control (color, wrapping, turning off anti-aliasing), use the Builder API instead.

While it is okay to construct a text sprite with no text, it cannot be rendered into a scene. Use empty() const to check if you haven't kept track.

Example

struct View
{
ge211::Font sans72 {"sans.ttf", 72},
sans30 {"sans.ttf", 30};
ge211::Text_sprite title {"My Fun Game", sans72},
score_label {"Score:", sans30},
level_label {"Level:", sans30},
score_value,
level_value;
};

Definition at line 242 of file ge211_sprites.cxx.


The documentation for this class was generated from the following files:
ge211::Sprite_set
A collection of positioned Sprites ready to be rendered to the screen.
Definition: ge211_sprites.hxx:509
std::string
ge211::sprites::Text_sprite
A Sprite that displays text.
Definition: ge211_sprites.hxx:232
ge211::Font
Represents a font that can be used to render a sprites::Text_sprite.
Definition: ge211_resource.hxx:70
ge211::Sprite_set::add_sprite
Sprite_set & add_sprite(Sprite const &sprite, Posn< int > xy, int z=0, Transform const &transform=Transform())
Adds the given sprite to the sprite set to render it in the next frame.
Definition: ge211_sprites.cxx:18