Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Menge::SceneGraph::TextWriter Class Reference

A singleton class responsible for writing text to an OpenGL context. More...

#include <TextWriter.h>

Public Types

enum  Alignment {
  NO_ALIGN = 0x0, VCENTER = 0x1, TOP = 0x2, BOTTOM = 0x4,
  LEFT = 0x8, LEFT_CENTER = 0x9, LEFT_TOP = 0xA, LEFT_BOTTOM = 0xC,
  RIGHT = 0x10, RIGHT_CENTER = 0x11, RIGHT_TOP = 0x12, RIGHT_BOTTOM = 0x14,
  HCENTER = 0x20, CENTERED = 0x21, CENTER_TOP = 0x22, CENTER_BOTTOM = 0x24
}
 Enumeration for controlling text alignment.
 

Public Member Functions

void newGLContext ()
 Resets the text writer's context-dependent data Called when the OpenGL context is called.
 
void printText (const std::string &text, Alignment alignment, float anchorX, float anchorY, int fontSize, bool currColor=false, bool transparent=true)
 Print the given text with an alignment relative to the given anchor point. More...
 
void printText (const std::string &text, float left, float bottom, int fontSize, bool currColor=false, bool transparent=true)
 Print the given text to the screen at the given coordinates. More...
 
void printAlignText (const std::string &text, Alignment alignment, int fontSize, float hpad, float vpad, bool currColor=false, bool transparent=true)
 Print the given text to the screen with the specified alignment. More...
 
void printAnchorText (const std::string &text, float anchorX, float anchorY, float xWeight, float yWeight, int fontSize, bool currColor=false, bool transparent=true)
 Prints the text anchored to the position. The relationship to the anchor depends on the anchor weights. It interpolates them from bottom left top top right. More...
 
void textSize (const std::string &text, int fontSize, float &textWidth, float &textHeight)
 Reports the size, in pixels, the given text will consume on the screen, given current font and indicated font size. More...
 
void resize (int w, int h)
 Inform the text writer that the viewport has resized. More...
 
int getViewWidth () const
 Reports TextWriter's belief of the viewport width. More...
 
int getViewHeight () const
 Reports TextWriter's belief of the viewport width. More...
 
bool setFont (const std::string &fontName)
 Changes the font family. More...
 
void setDefaultColor (float r, float g, float b, float alpha=1.f)
 Sets the default font color. More...
 
int flipY (int y)
 A utility function to map from screen to image coordinates. More...
 

Static Public Member Functions

static void setDefaultFont (const std::string &fontName)
 Sets the default font to use. More...
 
static TextWriterInstance ()
 Acquire pointer to a singleton text writer instance.
 

Protected Member Functions

TTF_Font * getFont (int size)
 Returns a pointer to a font for the given size. More...
 
void closeFonts ()
 Close out the current set of fonts.
 
 TextWriter ()
 Protected constructor. No instantiation allowed.
 
 ~TextWriter ()
 Protected destructor.
 
void makeTextDList ()
 Generate the display list for the quad upon which text is printed.
 
void makeGLTexture ()
 Generate the OpenGL texture object for the text.
 
void sdlSurfaceToGLTex (SDL_Surface *surf)
 Set the surface upon which the text has been rendered to a texture. More...
 
void printLine (TTF_Font *font, const char *text, float left, float bottom, int lineNum)
 The work necessary to print a single line of text. More...
 

Protected Attributes

int _width
 The width of the viewport the TextWriter considers when computing text positions.
 
int _height
 The height of the viewport the TextWriter considers when computing text positions.
 
FontMap _fonts
 Map of all font sizes for this font face.
 
std::string _fontName
 Name of font.
 
GLuint _textID
 Texture object for texturing the rendered text.
 
GLuint _textQuadID
 Call list for the text surface.
 
float _defColor [4]
 The default font color.
 

Static Protected Attributes

static TextWriter_instance = 0x0
 The singleton pointer.
 

Detailed Description

A singleton class responsible for writing text to an OpenGL context.

It has numerous functions for drawing text. It typically works in screen space, where the upper, left-hand corner is (0, 0).

Member Function Documentation

int Menge::SceneGraph::TextWriter::flipY ( int  y)
inline

A utility function to map from screen to image coordinates.

The screen coordiantes have the origin in the top, left corner. Image coordinates have the origin at the bottom, left corner.

Parameters
yThe vertical position in screen coordinates.
Returns
The vertical position in corresponding image coordinates.
TTF_Font * Menge::SceneGraph::TextWriter::getFont ( int  size)
protected

Returns a pointer to a font for the given size.

Parameters
sizeThe desired size of the font.
Returns
A pointer to a font instance at the desired size. If size is negative, NULL is returned.
int Menge::SceneGraph::TextWriter::getViewHeight ( ) const
inline

Reports TextWriter's belief of the viewport width.

Returns
The effective viewport width the TextWrtier is using.
int Menge::SceneGraph::TextWriter::getViewWidth ( ) const
inline

Reports TextWriter's belief of the viewport width.

Returns
The effective viewport width the TextWrtier is using.
void Menge::SceneGraph::TextWriter::printAlignText ( const std::string &  text,
Alignment  alignment,
int  fontSize,
float  hpad,
float  vpad,
bool  currColor = false,
bool  transparent = true 
)

Print the given text to the screen with the specified alignment.

Parameters
textThe string to draw.
alignmentThe alignment, relative to the screen.
fontSizeThe size of the text, in pixels.
hpadThe amount of horizontal padding to apply in the horizontal alignment direction. For example, if left aligned, it goes on the left, if right aligned, the padding is on the right.
vpadThe amount of vertical padding to apply in the vertical direction.
currColorIndicates if the current opengl color should be used (true) or the text writer's default color should be used (false).
transparentIndicates if the text is super-imposed over the screen or given a matte. True –> superimposed, false –> matted.
void Menge::SceneGraph::TextWriter::printAnchorText ( const std::string &  text,
float  anchorX,
float  anchorY,
float  xWeight,
float  yWeight,
int  fontSize,
bool  currColor = false,
bool  transparent = true 
)

Prints the text anchored to the position. The relationship to the anchor depends on the anchor weights. It interpolates them from bottom left top top right.

Parameters
textThe string to draw.
anchorXThe x-position of the anchor point (in screen coords).
anchorYThe y-position of the anchor point (in screen coords).
xWeightThe horizontal weight in the range [0, 1]. 0–> left, 1 –> right alignment.
yWeightThe vertical weight in the range [0, 1]. 0–> bottom, 1 –> top alignment.
fontSizeThe size of the text, in pixels.
currColorIndicates if the current opengl color should be used (true) or the text writer's default color should be used (false).
transparentIndicates if the text is super-imposed over the screen or given a matte. True –> superimposed, false –> matted.
void Menge::SceneGraph::TextWriter::printLine ( TTF_Font *  font,
const char *  text,
float  left,
float  bottom,
int  lineNum 
)
protected

The work necessary to print a single line of text.

The text is displayed relative to the anchor point of an entire block of text, (left, bottom), withthe text extending above and to the right of the anchor point.

Parameters
fontThe font to render the text.
textThe text to draw.
leftThe horizontal value of the anchor point.
bottomThe vertical value of the anchor point.
lineNumThe line number of the given text (zero-indexed). Used to offset this line relative to the anchor point.

scale and translate the quad

void Menge::SceneGraph::TextWriter::printText ( const std::string &  text,
Alignment  alignment,
float  anchorX,
float  anchorY,
int  fontSize,
bool  currColor = false,
bool  transparent = true 
)

Print the given text with an alignment relative to the given anchor point.

Parameters
textThe string to draw.
alignmentThe alignment, relative to the screen.
anchorXThe x-position of the anchor point (in screen coords).
anchorYThe y-position of the anchor point (in screen coords).
fontSizeThe size of the text, in pixels.
currColorIndicates if the current opengl color should be used (true) or the text writer's default color should be used (false).
transparentIndicates if the text is super-imposed over the screen or given a matte. True –> superimposed, false –> matted.
void Menge::SceneGraph::TextWriter::printText ( const std::string &  text,
float  left,
float  bottom,
int  fontSize,
bool  currColor = false,
bool  transparent = true 
)

Print the given text to the screen at the given coordinates.

Parameters
textThe string to draw.
leftThe position of the left extent of the text (in screen coords).
bottomThe position of the bottom extent of the text (in screen coords).
fontSizeThe size of the text, in pixels.
currColorIndicates if the current opengl color should be used (true) or the text writer's default color should be used (false).
transparentIndicates if the text is super-imposed over the screen or given a matte. True –> superimposed, false –> matted.
void Menge::SceneGraph::TextWriter::resize ( int  w,
int  h 
)

Inform the text writer that the viewport has resized.

This must be called by the viewer when the viewport resizes. This information is how the TextWriter correctly interprets alignment.

Parameters
wThe viewport width (in pixels).
hThe viewport height (in pixels).
void Menge::SceneGraph::TextWriter::sdlSurfaceToGLTex ( SDL_Surface *  surf)
protected

Set the surface upon which the text has been rendered to a texture.

Parameters
surfThe surface upon which the text has been rendered.
void Menge::SceneGraph::TextWriter::setDefaultColor ( float  r,
float  g,
float  b,
float  alpha = 1.f 
)

Sets the default font color.

Parameters
rThe red component of the color.
gThe green component of the color.
bThe blue component of the color.
alphaThe alpha component of the color. Default value is 1.0.
void Menge::SceneGraph::TextWriter::setDefaultFont ( const std::string &  fontName)
static

Sets the default font to use.

Parameters
fontNameThe path to the font to use.
bool Menge::SceneGraph::TextWriter::setFont ( const std::string &  fontName)

Changes the font family.

The first font is loaded at the default size If it is unable to load the font, the old family is left in place

Parameters
fontNameThe name of the font family to use.
Returns
True if the font was successfully loaded, false otherwise. If false, it uses the default font.
void Menge::SceneGraph::TextWriter::textSize ( const std::string &  text,
int  fontSize,
float &  textWidth,
float &  textHeight 
)

Reports the size, in pixels, the given text will consume on the screen, given current font and indicated font size.

Parameters
textThe string to draw.
fontSizeThe size of the font (in points).
textWidthThe width of the text will be stored in this value.
textHeightThe height of the text will be stored in this value.

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