/
gl_font.h
189 lines (157 loc) · 5.38 KB
/
gl_font.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//-----------------------------------------------------------------------------
// Copyright (c) 2007 dhpoware. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#if !defined(GL_FONT_H)
#define GL_FONT_H
#include <windows.h>
#include <GL/gl.h>
#include <cassert>
#include <string>
#include "bitmap.h"
//-----------------------------------------------------------------------------
// This GLFont class draws text as a bunch of textured quads. The GLFont class
// only stores the displayable ASCII characters in the range ASCII 32 to ASCII
// 126 inclusive. This range represents the displayable ASCII characters.
// ClearType text smoothing is used if the code is running under WinXP or
// higher. Otherwise standard text anti-aliasing is used.
//
// To use the GLFont class:
// GLFont font;
// font.create("Arial", 12, GLFont::NORMAL);
// font.begin();
// font.setColor(1.0f, 0.0f, 0.0f);
// font.drawTextFormat(1, 1, "%s", "Hello, World!");
// font.end();
// font.destroy();
//-----------------------------------------------------------------------------
class GLFont
{
public:
enum Style
{
NORMAL,
BOLD
};
struct Glyph
{
int width;
float upperLeft[2];
float lowerLeft[2];
float upperRight[2];
float lowerRight[2];
};
GLFont();
~GLFont();
bool create(const char *pszName, int ptSize, Style style = NORMAL);
void destroy();
void begin();
void end();
void drawChar(char c, int x, int y);
void drawText(int x, int y, const char *pszText);
void drawTextFormat(int x, int y, const char *pszFmt, ...);
int getCellHeight() const;
int getCellWidth() const;
const Glyph &getChar(char ch) const;
int getCharHeight() const;
int getCharWidthAvg() const;
int getCharWidthMax() const;
int getCharWidth(char ch) const;
const char *getName() const;
int getPointSize() const;
int getStrWidth(const char *pszText) const;
int getStrWidth(const char *pChar, int length) const;
bool isNull() const;
void setColor(float r, float g, float b);
void setDrawDropShadows(bool enableShadows);
void setDropShadowOffset(int offset);
private:
GLFont(const GLFont &);
GLFont &operator=(const GLFont &);
struct Vertex
{
int x, y;
float s, t;
float r, g, b, a;
};
bool createTexture(const Bitmap &bitmap);
bool createFontBitmap();
void drawBatchOfChars();
void drawTextBegin();
void drawTextEnd();
bool extractFontMetrics();
void generateTexCoords(const Bitmap &bitmap);
int nextPower2(int x) const;
static const int CHAR_FIRST = 32;
static const int CHAR_LAST = 126;
static const int TAB_SPACES = 4;
static const int TOTAL_CHARS = 95;
static const int MAX_CHARS_PER_BATCH = 256;
static const int MAX_STR_SIZE = 1024;
static const int MAX_VERTICES = MAX_CHARS_PER_BATCH * 4;
static int m_logPixelsY;
static BYTE m_lfQuality;
static char m_szBuffer[MAX_STR_SIZE];
std::string m_name;
int m_dropShadowOffset;
int m_pointSize;
int m_cellHeight;
int m_cellWidth;
int m_charHeight;
int m_charAvgWidth;
int m_charMaxWidth;
int m_numCharsToDraw;
unsigned int m_textureObject;
bool m_drawDropShadows;
float m_color[4];
Vertex *m_pVertex;
HFONT m_hFont;
LOGFONT m_lf;
Glyph m_glyphs[TOTAL_CHARS];
Vertex m_vertices[MAX_VERTICES];
};
//-----------------------------------------------------------------------------
inline int GLFont::getCellHeight() const
{ return m_cellHeight; }
inline int GLFont::getCellWidth() const
{ return m_cellWidth; }
inline const GLFont::Glyph &GLFont::getChar(char ch) const
{
assert(ch >= CHAR_FIRST && ch <= CHAR_LAST);
return m_glyphs[ch - 32];
}
inline int GLFont::getCharHeight() const
{ return m_charHeight; }
inline int GLFont::getCharWidthAvg() const
{ return m_charAvgWidth; }
inline int GLFont::getCharWidthMax() const
{ return m_charMaxWidth; }
inline int GLFont::getCharWidth(char ch) const
{
assert(ch >= CHAR_FIRST && ch <= CHAR_LAST);
return m_glyphs[ch - 32].width;
}
inline const char *GLFont::getName() const
{ return m_name.c_str(); }
inline int GLFont::getPointSize() const
{ return m_pointSize; }
inline bool GLFont::isNull() const
{ return !m_hFont; }
#endif