Gamedev Framework (gf)  0.9.0
A C++14 framework for 2D games
Classes | Public Types | Public Member Functions | List of all members
gf::Console Class Reference

A virtual console. More...

#include <gf/Console.h>

Inheritance diagram for gf::Console:
Inheritance graph
[legend]

Public Types

enum  PrintAction {
  PrintAction::None,
  PrintAction::Clear
}
 An action when printing. More...
 

Public Member Functions

 Console (const ConsoleFont &font, Vector2i size)
 Constructor. More...
 
int getWidth () const
 Get the width of the console. More...
 
int getHeight () const
 Get the height of the console. More...
 
void blit (const RectI &src, Console &con, Vector2i dst, float foregroundAlpha=1.0f, float backgroundAlpha=1.0f) const
 Blit a console on another console. More...
 
virtual void draw (RenderTarget &target, RenderStates states) override
 Draw the object to a render target. More...
 
Basic printing
void setDefaultBackground (const Color4f &color)
 Set the default background color. More...
 
const Color4fgetDefaultBackground () const
 Get the default background color. More...
 
void setDefaultForeground (const Color4f &color)
 Set the default foreground color. More...
 
const Color4fgetDefaultForeground () const
 Get the default foreground color. More...
 
void clear ()
 Clear the console. More...
 
void setCharBackground (Vector2i position, const Color4f &color, ConsoleEffect effect=ConsoleEffect::Set)
 Set the character background color. More...
 
const Color4fgetCharBackground (Vector2i position) const
 Get the character background color. More...
 
void setCharForeground (Vector2i position, const Color4f &color)
 Set the character foreground color. More...
 
const Color4fgetCharForeground (Vector2i position) const
 Get the character foreground color. More...
 
void setChar (Vector2i position, char16_t c)
 Set a character. More...
 
char16_t getChar (Vector2i position) const
 Get a character. More...
 
void putChar (Vector2i position, char16_t c, ConsoleEffect effect=ConsoleEffect::Default)
 Modify a cell in the console. More...
 
void putChar (Vector2i position, char16_t c, const Color4f &foreground, const Color4f &background)
 Modify a cell in the console. More...
 
String printing
void setDefaultConsoleEffect (ConsoleEffect effect)
 Set the default console effect. More...
 
ConsoleEffect getDefaultConsoleEffect () const
 Get the default console effect. More...
 
void setDefaultAlignment (ConsoleAlignment alignment)
 Set the default alignment. More...
 
ConsoleAlignment getDefaultAlignment () const
 Get the default alignment. More...
 
void print (Vector2i position, const char *fmt,...)
 Print a formatted string. More...
 
void print (Vector2i position, ConsoleEffect effect, ConsoleAlignment alignment, const char *fmt,...)
 Print a formatted string. More...
 
int printRect (const RectI &rect, const char *fmt,...)
 Print a multi-line formatted string. More...
 
int printRect (const RectI &rect, ConsoleEffect effect, ConsoleAlignment alignment, const char *fmt,...)
 Print a multi-line formatted string. More...
 
int getHeight (const RectI &rect, const char *fmt,...)
 Get the expected number of console lines of a multi-line formatted string. More...
 
void setColorControl (ConsoleColorControl ctrl, const Color4f &foreground, const Color4f &background)
 Set a color for color control. More...
 
Advanced printing
void drawRectangle (const RectI &rect, PrintAction action=PrintAction::None, ConsoleEffect effect=ConsoleEffect::Default)
 Draw a filled rectangle. More...
 
void drawHorizontalLine (Vector2i left, int width, ConsoleEffect effect=ConsoleEffect::Default)
 Draw a horizontal line. More...
 
void drawVerticalLine (Vector2i top, int height, ConsoleEffect effect=ConsoleEffect::Default)
 Draw a vertical line. More...
 
void drawFrame (const RectI &rect, PrintAction action=PrintAction::None, ConsoleEffect effect=ConsoleEffect::Default, const char *title=nullptr,...)
 Draw a frame. More...
 
Screen fading
void setFade (float amount, const Color4f &color)
 Set the fading parameters. More...
 
float getFadingAmount () const
 Get the fading amount. More...
 
const Color4fgetFadingColor () const
 Get the fading color. More...
 
- Public Member Functions inherited from gf::Transformable
 Transformable ()
 Default constructor. More...
 
void setOrigin (Vector2f origin)
 Set the local origin of the object. More...
 
Vector2f getOrigin () const
 Get the local origin of the object. More...
 
void setPosition (Vector2f position)
 Set the position of the object. More...
 
Vector2f getPosition () const
 Get the position of the object. More...
 
void move (Vector2f offset)
 Move the object by a given offset. More...
 
void setRotation (float angle)
 Set the orientation of the object. More...
 
float getRotation () const
 Get the orientation of the object. More...
 
void rotate (float angle)
 Rotate the object. More...
 
void setScale (Vector2f factors)
 Set the scale factors of the object. More...
 
void setScale (float factor)
 Set the scale factor of the object. More...
 
Vector2f getScale () const
 Get the current scale of the object. More...
 
void scale (Vector2f factors)
 Scale the object. More...
 
void scale (float factor)
 Scale the object. More...
 
Matrix3f getTransform () const
 Get the combined transform of the object. More...
 
Matrix3f getInverseTransform () const
 Get the inverse of the combined transform of the object. More...
 
- Public Member Functions inherited from gf::Drawable
virtual ~Drawable ()
 Virtual desctructor. More...
 

Additional Inherited Members

- Protected Member Functions inherited from gf::Transformable
void setOriginFromAnchorAndBounds (Anchor anchor, const RectF &bounds)
 Set the origin from an anchor and bounds. More...
 

Detailed Description

A virtual console.

A console is a virtual terminal where you can print the characters from a console font. Each cell of the console has a background color, a foreground color and a 8-bit character.

A console has a state with default values for different aspects:

Several functions use this state to determine actual values.

See also
gf::ConsoleFont

Member Enumeration Documentation

◆ PrintAction

An action when printing.

Enumerator
None 

No action.

Clear 

Fill the area with space characters.

Constructor & Destructor Documentation

◆ Console()

gf::Console::Console ( const ConsoleFont font,
Vector2i  size 
)

Constructor.

Parameters
fontA console font
sizeThe size of the console, in characters

Member Function Documentation

◆ blit()

void gf::Console::blit ( const RectI src,
Console con,
Vector2i  dst,
float  foregroundAlpha = 1.0f,
float  backgroundAlpha = 1.0f 
) const

Blit a console on another console.

Parameters
srcThe source rectangle on the source console
conThe destination console
dstThe destination position on the destination console
foregroundAlphaThe alpha of the blitted console foreground color, 0 is a completely transparent source console and 1 is a completely opaque source console
backgroundAlphaThe alpha of the blitted console background color, 0 is a completely transparent source console and 1 is a completely opaque source console

◆ clear()

void gf::Console::clear ( )

Clear the console.

For each cell of the console, this function:

  • sets the background color to the default background color
  • sets the foreground color to the default foreground color
  • sets the character to space (ASCII 32)

◆ draw()

virtual void gf::Console::draw ( RenderTarget target,
RenderStates  states 
)
overridevirtual

Draw the object to a render target.

This is a pure virtual function that has to be implemented by the derived class to define how the drawable should be drawn.

Parameters
targetRender target to draw to
statesCurrent render states

Implements gf::Drawable.

◆ drawFrame()

void gf::Console::drawFrame ( const RectI rect,
PrintAction  action = PrintAction::None,
ConsoleEffect  effect = ConsoleEffect::Default,
const char *  title = nullptr,
  ... 
)

Draw a frame.

This funtion first draws a rectangle using the specified action and effect. Then it draws a frame. If a title is given, it is printed on the top of the frame with inverted colors.

Parameters
rectThe rectangle to print the rectangle
actionThe action when drawing
effectThe effect to apply to the background
titleThe format string of the title

◆ drawHorizontalLine()

void gf::Console::drawHorizontalLine ( Vector2i  left,
int  width,
ConsoleEffect  effect = ConsoleEffect::Default 
)

Draw a horizontal line.

This function draws a horizontal line using gf::SpecialChar::WallHorizontalLine.

Parameters
leftThe left end point of the line
widthThe width of the line
effectThe effect to apply to the background

◆ drawRectangle()

void gf::Console::drawRectangle ( const RectI rect,
PrintAction  action = PrintAction::None,
ConsoleEffect  effect = ConsoleEffect::Default 
)

Draw a filled rectangle.

This function fills the specified rectangle with the default background color. If the print action is gf::PrintAction::Clear, then the characters inside the rectangle are set to space (ASCII 32).

Parameters
rectThe rectangle to print the rectangle
actionThe action when drawing
effectThe effect to apply to the background

◆ drawVerticalLine()

void gf::Console::drawVerticalLine ( Vector2i  top,
int  height,
ConsoleEffect  effect = ConsoleEffect::Default 
)

Draw a vertical line.

This function draws a vertical line using gf::SpecialChar::WallVerticalLine.

Parameters
topThe top end point of the line
heightThe height of the line
effectThe effect to apply to the background

◆ getChar()

char16_t gf::Console::getChar ( Vector2i  position) const

Get a character.

Parameters
positionThe position of the cell
Returns
The current character at the position
See also
setChar()

◆ getCharBackground()

const Color4f& gf::Console::getCharBackground ( Vector2i  position) const

Get the character background color.

Parameters
positionThe position of the cell
Returns
The current character background color
See also
setCharBackground()

◆ getCharForeground()

const Color4f& gf::Console::getCharForeground ( Vector2i  position) const

Get the character foreground color.

Parameters
positionThe position of the cell
Returns
The current character foreground color
See also
setCharForeground()

◆ getDefaultAlignment()

ConsoleAlignment gf::Console::getDefaultAlignment ( ) const
inline

Get the default alignment.

Returns
The current default alignment
See also
setDefaultAlignment()

◆ getDefaultBackground()

const Color4f& gf::Console::getDefaultBackground ( ) const
inline

Get the default background color.

Returns
The current default background color
See also
setDefaultBackground()

◆ getDefaultConsoleEffect()

ConsoleEffect gf::Console::getDefaultConsoleEffect ( ) const
inline

Get the default console effect.

Returns
The current default console effect
See also
setDefaultConsoleEffect()

◆ getDefaultForeground()

const Color4f& gf::Console::getDefaultForeground ( ) const
inline

Get the default foreground color.

Returns
The current default foreground color
See also
setDefaultForeground()

◆ getFadingAmount()

float gf::Console::getFadingAmount ( ) const
inline

Get the fading amount.

Returns
The current fading amount

◆ getFadingColor()

const Color4f& gf::Console::getFadingColor ( ) const
inline

Get the fading color.

Returns
The current fading color

◆ getHeight() [1/2]

int gf::Console::getHeight ( ) const
inline

Get the height of the console.

◆ getHeight() [2/2]

int gf::Console::getHeight ( const RectI rect,
const char *  fmt,
  ... 
)

Get the expected number of console lines of a multi-line formatted string.

This function does not print anything, it only computes the number of lines that a formatted string would take if it was printed with printRect()

Parameters
rectThe rectangle to print the string
fmtThe format string
See also
printRect()

◆ getWidth()

int gf::Console::getWidth ( ) const
inline

Get the width of the console.

◆ print() [1/2]

void gf::Console::print ( Vector2i  position,
const char *  fmt,
  ... 
)

Print a formatted string.

This function uses the default values for background color, foreground color, console effect and alignment.

The specified position indicates:

Parameters
positionThe position of a cell
fmtThe format string

◆ print() [2/2]

void gf::Console::print ( Vector2i  position,
ConsoleEffect  effect,
ConsoleAlignment  alignment,
const char *  fmt,
  ... 
)

Print a formatted string.

This function uses the default values for background color, foreground color but uses the specified console effect and alignment.

The specified position indicates:

Parameters
positionThe position of a cell
effectThe effect to apply to the background
alignmentThe alignment of the text
fmtThe format string

◆ printRect() [1/2]

int gf::Console::printRect ( const RectI rect,
const char *  fmt,
  ... 
)

Print a multi-line formatted string.

The string is split in paragraphs according to new lines and then split in lines to fit the width of the specified rectangle. If the specified height is reached, the string is truncated. I the height is 0, then the string is truncated at the bottom of the console.

Parameters
rectThe rectangle to print the string
fmtThe format string
Returns
The height in console lines of the printed string

◆ printRect() [2/2]

int gf::Console::printRect ( const RectI rect,
ConsoleEffect  effect,
ConsoleAlignment  alignment,
const char *  fmt,
  ... 
)

Print a multi-line formatted string.

The string is split in paragraphs according to new lines and then split in lines to fit the width of the specified rectangle. If the specified height is reached, the string is truncated. I the height is 0, then the string is truncated at the bottom of the console.

Parameters
rectThe rectangle to print the string
effectThe effect to apply to the background
alignmentThe alignment of the text
fmtThe format string
Returns
The height in console lines of the printed string

◆ putChar() [1/2]

void gf::Console::putChar ( Vector2i  position,
char16_t  c,
ConsoleEffect  effect = ConsoleEffect::Default 
)

Modify a cell in the console.

At the specified position, this function:

  • sets the background color thanks to the effect and the default background color
  • sets the foreground color thanks to the default foreground color
  • sets the character to a new value
Parameters
positionThe position of the cell
cThe character to set
effectThe effect to apply to the background

◆ putChar() [2/2]

void gf::Console::putChar ( Vector2i  position,
char16_t  c,
const Color4f foreground,
const Color4f background 
)

Modify a cell in the console.

At the specified position, this function:

  • sets the background color to the specified color
  • sets the foreground color to the specified color
  • sets the character to a new value
Parameters
positionThe position of the cell
cThe character to set
foregroundThe foreground color to set
backgroundThe background color to set

◆ setChar()

void gf::Console::setChar ( Vector2i  position,
char16_t  c 
)

Set a character.

Parameters
positionThe position of the cell
cThe character
See also
putChar(), getChar()

◆ setCharBackground()

void gf::Console::setCharBackground ( Vector2i  position,
const Color4f color,
ConsoleEffect  effect = ConsoleEffect::Set 
)

Set the character background color.

Parameters
positionThe position of the cell
colorThe new background color
effectThe effect to apply to background
See also
getCharBackground()

◆ setCharForeground()

void gf::Console::setCharForeground ( Vector2i  position,
const Color4f color 
)

Set the character foreground color.

Parameters
positionThe position of the cell
colorThe new foreground color
See also
getCharForeground()

◆ setColorControl()

void gf::Console::setColorControl ( ConsoleColorControl  ctrl,
const Color4f foreground,
const Color4f background 
)

Set a color for color control.

Color control codes allow to change color in a string on the fly. There are five color control codes: gf::ConsoleColorControl1, gf::ConsoleColorControl2, gf::ConsoleColorControl3, gf::ConsoleColorControl4 and gf::ConsoleColorControl5. Each one is associated with a foreground and a background color that you can set with this function. The color control code gf::ConsoleColorControlStop changes the colors back to their default.

gf::Console console(font, size);
// A string with a red over black word, using predefined color control codes
console.print({ 1, 1 }, "String with a %cred%c word.", gf::ConsoleColorControl1, gf::ConsoleColorControlStop);

◆ setDefaultAlignment()

void gf::Console::setDefaultAlignment ( ConsoleAlignment  alignment)
inline

Set the default alignment.

Parameters
alignmentThe new alignment
See also
getDefaultAlignment()

◆ setDefaultBackground()

void gf::Console::setDefaultBackground ( const Color4f color)
inline

Set the default background color.

Parameters
colorThe new background color
See also
getDefaultBackground()

◆ setDefaultConsoleEffect()

void gf::Console::setDefaultConsoleEffect ( ConsoleEffect  effect)
inline

Set the default console effect.

Parameters
effectThe new effect
See also
getDefaultConsoleEffect()

◆ setDefaultForeground()

void gf::Console::setDefaultForeground ( const Color4f color)
inline

Set the default foreground color.

Parameters
colorThe new foreground color
See also
getDefaultForeground()

◆ setFade()

void gf::Console::setFade ( float  amount,
const Color4f color 
)
inline

Set the fading parameters.

Parameters
amountThe fading amount, with 0 meaning the fading color and 1 meaning no fading
colorThe fading color