来自:http://www.codeproject.com/Articles/30775/TetroGL-An-OpenGL-Game-Tutorial-in-C-for-Win32-pla
1、Drawing Text CGameFont 类
2、CGameFont 类的辅助CTextControl类
3、如何输出汉字
1、Drawing Text CGameFont 类
For a lot of games, displaying images loaded from files is not enough: Sometimes you would like to display some text which is not known when you design your game. For example, the player name used in the high-scores, the current player score, etc. You can draw text with OpenGL by making use of display lists. A display list is a list of OpenGL commands that are stored together and which can be executed later as often as you need. Suppose that you have a very complex object for which you have a lot of OpenGL calls (e.g. an object made of a lot of textures) that you would like to draw often. Instead of each time repeating the same OpenGL commands, you can 'execute' the commands once and store them in a display list. You can then call your display list later whenever you want to display the objects instead of calling all the OpenGL functions. When the list is invoked, all the commands in the list are executed in the order in which they were issued. The major advantage of using display lists is optimization: The OpenGL commands are already evaluated and might be stored in a format that is more suitable for your graphic card. For example, a rotation transformation requires quite a lot of calculations because a rotation matrix has to be generated from this command. If you use a display list, the final rotation matrix will be saved, which avoids redoing the complex calculation each time.
So, for what will those display list be useful to draw text? Well, when you create your font (with a specific typeface, height and weight), all the characters that need to be reused later can be drawn in a display list (one list for each character). You can then easily display text later by calling the different display lists of the characters you want to draw. Let's look at the header of theCGameFont
class, which is used to draw the text:
// Utility class used to draw text on the screen using a
// specific font.
class CGameFont
{
public:
// Default constructor
CGameFont();
// Default destructor
~CGameFont();
// Create the font with a specific height and weight.
void CreateFont(const std::string& strTypeface ,
int iFontHeight,
int iFontWeight);
// Draw text on the screen at the specified location with
// the specified colour.
void DrawText(const std::string& strText, int XPos,
int YPos, GLfloat fRed, GLfloat fGreen,
GLfloat fBlue);
// Returns the size of the text. The top and right fields
// of the returned rectangle are set to 0.
TRectanglei GetTextSize(const std::string& strText);
static void SetDeviceContext(HDC hDevContext)
{ m_hDeviceContext = hDevContext; }
private:
// The device context used to create the font.
static HDC m_hDeviceContext;
// The index of the base of the lists.
GLuint m_uiListBase;
// The win32 font
HFONT m_hFont;
};
The CreateFont
function is used to create the font for the specified typeface (e.g. "Arial", "Times New Roman",...), with a specific height and weight (the weight specifies the thickness of the font). Once the font has been created su