\brief This file contains routines for drawing text on the (OpenGL-)screen using texture fonts.
Usage is quite simple. First of all you have to obtain a valid pointer to a texFont_s struct (usually done with #FontHandler::getFont()).
Then you can write text on the screen using the \c drawString functions.
The fonts are defined in separate .font files. They are made up as follows.
The first keyword should be \c texture followed by a filename. This is the texture with the characters.
In the next two lines \c height and \c spacing should be specified in pixels.
Then come the character describtions in the format \c character \c C \c x \c y \c width. \c C is the character whoose specification follows, \c x and \c y are the charcters coordinates in the texture (from bottom left) and \c width is the characters width in pixels (so variable widths are allowed).
\c C can be enclosed in quotation marks or not, it doesn't matter ("C"=C) - however the character " itself is represented through double inverted commas (''). You should have a look into one of the .font files in the gui/fonts directory and everything will become clear.
The \c drawString() functions allow dynamic color changing IN ONE string through the '^0-9' characters (just like in Quake 3). An example shows it best:
In
\c "^0I am ^1a colored ^2string"
"I am" will be drawed using color 0, "a colored" will appear in color 1 and "string" in color 2. A special character is 'b'; it will result in a blinking string (red-black).
*/
#include "Texture.h"
#define TEX_FONT_MAX_CHARACTERS 128 //!< number of characters in a #texFont_t
//! enum of possible text alignments (to be passed to drawString()-functions)
enum textAlignments_e{
TEXT_ALIGN_LEFT, //!< text should be aligned left (relative to the passed x-coordinate)
TEXT_ALIGN_RIGHT, //!< text should be aligned right (relative to the passed x-coordinate)
TEXT_ALIGN_CENTER, //!< text should be aligned centered (relative to the passed x-coordinate)
NUM_TEXT_ALIGNMENTS //!< number of allowed alignments (just for counting)
};
//! represents ONE texFont character
typedef struct texFontCharacter_s{
float u,v; //!< texture coordinates of this character in the fonts texture
float texCoordWidth; //!< width of this character in texture coordinates
int width; //!< width of this character in pixels
}texFontCharacter_t;
//! represents a texFont
typedef struct texFont_s{
char* filename;
Texture* tex; //!< texture of this font
texFontCharacter_t* chars[TEX_FONT_MAX_CHARACTERS]; //!< array with characters (may be NULL for some chars!)
int height; //!< height of this font in pixels
float texCoordHeight; //!< height of the font in texture coordinates
int spacing; //!< spacing between characters in pixels
}texFont_t;
//! loads a texFont (should be used directly - use #FontHandler::getFont() instead!)
texFont_t* loadFont(const char* filename);
//! frees a texFont allocated with #loadFont (should not be used directly - use #FontHandler::freeFont() instead!)
void freeFont(texFont_t* font);
//! returns how many pixels a string occupies in a particular font
int getStringWidth(texFont_t* font, const char* string);
//! sets OpenGL color to color number c (REALLY THE CHARACTER: '1'=1)
bool setFontColor(char c);
//! draws a string on the screen using a particluar font
void drawString(int x, int y, texFont_t* font, const char* string);
void drawScaledString(int x, int y, float scaleX, float scaleY, texFont_t* font, const char* string);
void drawAlignedString(int x, int y, texFont_t* font, int alignment, const char* string);
void drawScaledAndAlignedString(int x, int y, float scaleX, float scaleY, texFont_t* font, int alignment, const char* string);
void drawShadowedString(float x, float y, float scale, int shadowOffset, texFont_t* font, int alignment, const char* string);
void drawFormatString(int x, int y, texFont_t* font, const char* formatString, ...);
void drawScaledFormatString(int x, int y, float scaleX, float scaleY, texFont_t* font, const char* formatString, ...);
void drawAlignedFormatString(int x, int y, texFont_t* font, int alignment, const char* formatString, ...);
void drawScaledAndAlignedFormatString(int x, int y, float scaleX, float scaleY, texFont_t* font, int alignment, const char* formatString, ...);
void drawShadowedFormatString(float x, float y, float scale, int shadowOffset, texFont_t* font, int alignment, const char* formatString, ...);