Utility class to render text overlays on screen
Usage:
(1) During setup, call loadFont() to load a ttf font file
(2) Immediately prior to rendering, call bindFont() to load font context
(3) Subsequently call renderText() for every string to be rendered to screen
(4) During cleanup, call releaseFont() to deallocate font memory
To make placement of text within window easier, such that the (x, y) coordinate corresponds to the pixel location, call setWindowSize() after loading the font (and anytime the window size changes)..
More...
|
| bool | loadFont (const char *filename) |
| | registers a TrueTypeFont file with a shader program to render text to screen
|
| |
|
void | releaseFont () |
| | deallocates CPU and GPU memory related to text rendering
|
| |
| void | bindFont () |
| | sets all necessary font contexts
|
| |
| void | setColor (glm::vec3 color) |
| | specify the color to render the text
|
| |
| void | setColor (glm::vec4 color) |
| | specify the color to render the text
|
| |
| void | setWindowSize (GLint width, GLint height) |
| | store the size of the window
|
| |
| void | setWindowSize (GLfloat width, GLfloat height) |
| | store the size of the window
|
| |
| void | setFontSize (GLfloat scaleX, GLfloat scaleY) |
| | set the amount to scale font when drawing
|
| |
| void | renderText (const char *str, GLfloat x, GLfloat y) |
| | draws the given text starting at the top left (x, y) coordinate specified
|
| |
Utility class to render text overlays on screen
Usage:
(1) During setup, call loadFont() to load a ttf font file
(2) Immediately prior to rendering, call bindFont() to load font context
(3) Subsequently call renderText() for every string to be rendered to screen
(4) During cleanup, call releaseFont() to deallocate font memory
To make placement of text within window easier, such that the (x, y) coordinate corresponds to the pixel location, call setWindowSize() after loading the font (and anytime the window size changes)..
◆ bindFont()
| void CSCI441::FontUtils::bindFont |
( |
| ) |
|
|
inline |
sets all necessary font contexts
- Note
- binds VertexArray and GL_ARRAY_BUFFER
-
binds GL_TEXTURE_2D to GL_TEXTURE0
-
uses shader program
- Precondition
- MUST call CSCI441::FontUtility::loadFont(const char*) prior to this call
◆ loadFont()
| bool CSCI441::FontUtils::loadFont |
( |
const char * |
filename | ) |
|
|
inline |
registers a TrueTypeFont file with a shader program to render text to screen
- Parameters
-
| filename | ttf font file to load |
- Returns
- true if font file was successfully loaded AND internal font shader program successfully compiled
◆ renderText()
| void CSCI441::FontUtils::renderText |
( |
const char * |
str, |
|
|
GLfloat |
x, |
|
|
GLfloat |
y |
|
) |
| |
|
inline |
draws the given text starting at the top left (x, y) coordinate specified
- Parameters
-
| str | text to render to screen |
| x | x coordinate in screen space to place text within window (corresponds to left edge of text) |
| y | y coordinate in screen space to place text within window (corresponds to top edge of text) |
- Precondition
- MUST call CSCI441::FontUtility::bindFont() prior to this call
- Note
- see CSCI441::FontUtils::setWindowSize() for help in specifying the (x, y) coordinate
-
see CSCI441::FontUtility::setColor() to change the displayed color
◆ setColor() [1/2]
| void CSCI441::FontUtils::setColor |
( |
glm::vec3 |
color | ) |
|
|
inline |
specify the color to render the text
- Parameters
-
- Note
- sets alpha channel to 1.0
◆ setColor() [2/2]
| void CSCI441::FontUtils::setColor |
( |
glm::vec4 |
color | ) |
|
|
inline |
specify the color to render the text
- Parameters
-
◆ setFontSize()
| void CSCI441::FontUtils::setFontSize |
( |
GLfloat |
scaleX, |
|
|
GLfloat |
scaleY |
|
) |
| |
|
inline |
set the amount to scale font when drawing
- Parameters
-
| scaleX | amount to scale font horizontally |
| scaleY | amount to scale font vertically |
- Note
- values typically refer to inverse screen size, e.g. the size of each pixel
◆ setWindowSize() [1/2]
| void CSCI441::FontUtils::setWindowSize |
( |
GLfloat |
width, |
|
|
GLfloat |
height |
|
) |
| |
|
inline |
store the size of the window
- Parameters
-
| width | window width |
| height | window height |
- Note
- this allows CSCI441::FontUtils::renderText() to specify the (x, y) coordinate in screen space. The (x, y) coordinate then corresponds to the top left pixel to begin drawing at.
◆ setWindowSize() [2/2]
| void CSCI441::FontUtils::setWindowSize |
( |
GLint |
width, |
|
|
GLint |
height |
|
) |
| |
|
inline |
store the size of the window
- Parameters
-
| width | window width |
| height | window height |
- Note
- this allows CSCI441::FontUtils::renderText() to specify the (x, y) coordinate in screen space. The (x, y) coordinate then corresponds to the top left pixel to begin drawing at.