Gosu
Public Member Functions | List of all members
Gosu::Font Class Reference

A font can be used to draw text on a Graphics object very flexibly. More...

Public Member Functions

 Font (unsigned font_height, const std::string &font_name=default_font_name(), unsigned font_flags=FF_BOLD)
 Constructs a font that can be drawn onto the graphics object. More...
 
std::string name () const
 Returns the name of the font that was used to create it. More...
 
unsigned height () const
 Returns the height of the font, in pixels. More...
 
unsigned flags () const
 Returns the flags used to create the font characters. More...
 
double text_width (const std::string &text, double scale_x=1) const
 Returns the width, in pixels, the given text would occupy if drawn. More...
 
void draw (const std::string &text, double x, double y, ZPos z, double scale_x=1, double scale_y=1, Color c=Color::WHITE, AlphaMode mode=AM_DEFAULT) const
 Draws text so the top left corner of the text is at (x; y). More...
 
void draw_rel (const std::string &text, double x, double y, ZPos z, double rel_x, double rel_y, double scale_x=1, double scale_y=1, Color c=Color::WHITE, AlphaMode mode=AM_DEFAULT) const
 Draws text at a position relative to (x; y). More...
 
void set_image (wchar_t wc, unsigned font_flags, const Gosu::Image &image)
 Maps a letter to a specific image instead of generating one using Gosu's built-in text rendering. More...
 
void set_image (wchar_t wc, const Gosu::Image &image)
 A shortcut for mapping a character to an image regardless of font_flags. More...
 
GOSU_DEPRECATED void draw_rot (const std::string &text, double x, double y, ZPos z, double angle, double scale_x=1, double scale_y=1, Color c=Color::WHITE, AlphaMode mode=AM_DEFAULT) const
 DEPRECATED: Analogous to draw, but rotates the text by a given angle. More...
 

Detailed Description

A font can be used to draw text on a Graphics object very flexibly.

Fonts are ideal for small texts that change regularly. For large, static texts you should use create_bitmap and turn the result into an image.

Definition at line 20 of file Font.hpp.

Constructor & Destructor Documentation

◆ Font()

Gosu::Font::Font ( unsigned  font_height,
const std::string &  font_name = default_font_name(),
unsigned  font_flags = FF_BOLD 
)

Constructs a font that can be drawn onto the graphics object.

Parameters
font_nameName of a system font, or a filename to a TTF file (must contain '/', does not work on Linux).
font_heightHeight of the font, in pixels.
font_flagsFlags used to render individual characters of the font.

Member Function Documentation

◆ draw()

void Gosu::Font::draw ( const std::string &  text,
double  x,
double  y,
ZPos  z,
double  scale_x = 1,
double  scale_y = 1,
Color  c = Color::WHITE,
AlphaMode  mode = AM_DEFAULT 
) const

Draws text so the top left corner of the text is at (x; y).

Parameters
textFormatted text without line-breaks.

◆ draw_rel()

void Gosu::Font::draw_rel ( const std::string &  text,
double  x,
double  y,
ZPos  z,
double  rel_x,
double  rel_y,
double  scale_x = 1,
double  scale_y = 1,
Color  c = Color::WHITE,
AlphaMode  mode = AM_DEFAULT 
) const

Draws text at a position relative to (x; y).

Parameters
rel_xDetermines where the text is drawn horizontally. If rel_x is 0.0, the text will be to the right of x, if it is 1.0, the text will be to the left of x, if it is 0.5, it will be centered on x. Of course, all real numbers are possible values.
rel_ySee rel_x.

◆ draw_rot()

GOSU_DEPRECATED void Gosu::Font::draw_rot ( const std::string &  text,
double  x,
double  y,
ZPos  z,
double  angle,
double  scale_x = 1,
double  scale_y = 1,
Color  c = Color::WHITE,
AlphaMode  mode = AM_DEFAULT 
) const

DEPRECATED: Analogous to draw, but rotates the text by a given angle.

Use Graphics::push_transform to achieve the same effect.

◆ flags()

unsigned Gosu::Font::flags ( ) const

Returns the flags used to create the font characters.

◆ height()

unsigned Gosu::Font::height ( ) const

Returns the height of the font, in pixels.

◆ name()

std::string Gosu::Font::name ( ) const

Returns the name of the font that was used to create it.

◆ set_image() [1/2]

void Gosu::Font::set_image ( wchar_t  wc,
unsigned  font_flags,
const Gosu::Image image 
)

Maps a letter to a specific image instead of generating one using Gosu's built-in text rendering.

This can only be called once per character, and the character must not have been drawn before. This ensures that Fonts are still (sort of) immutable.

◆ set_image() [2/2]

void Gosu::Font::set_image ( wchar_t  wc,
const Gosu::Image image 
)

A shortcut for mapping a character to an image regardless of font_flags.

Later versions might apply faux italics or faux bold to it (to be decided!).

◆ text_width()

double Gosu::Font::text_width ( const std::string &  text,
double  scale_x = 1 
) const

Returns the width, in pixels, the given text would occupy if drawn.


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