1 | /*! |
---|
2 | \file text_engine.h |
---|
3 | * Definition of textEngine, the Font and the Text |
---|
4 | |
---|
5 | Text is the text outputed. |
---|
6 | Font is a class that loads a certain ttf-file with a specific height into memory |
---|
7 | TextEngine is used to manage the all the different Fonts that might be included |
---|
8 | |
---|
9 | for more information see the specific classes. |
---|
10 | |
---|
11 | !! IMPORTANT !! When using ttf fonts clear the license issues prior to |
---|
12 | adding them to orxonox. This is really important, because we do not want |
---|
13 | to offend anyone. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef _TEXT_ENGINE_H |
---|
17 | #define _TEXT_ENGINE_H |
---|
18 | |
---|
19 | |
---|
20 | #include "glincl.h" |
---|
21 | |
---|
22 | #ifdef HAVE_SDL_IMAGE_H |
---|
23 | #include "SDL_ttf.h" |
---|
24 | #else |
---|
25 | #include "SDL/SDL_ttf.h" |
---|
26 | #endif |
---|
27 | |
---|
28 | #include "base_object.h" |
---|
29 | #include "element_2d.h" |
---|
30 | |
---|
31 | #include "vector.h" |
---|
32 | |
---|
33 | // FORWARD DECLARATION |
---|
34 | class PNode; |
---|
35 | class Font; |
---|
36 | template<class T> class tList; |
---|
37 | |
---|
38 | //! An enumerator for the text alignment. |
---|
39 | enum TEXT_ALIGNMENT |
---|
40 | { |
---|
41 | TEXT_ALIGN_LEFT, |
---|
42 | TEXT_ALIGN_RIGHT, |
---|
43 | TEXT_ALIGN_CENTER, |
---|
44 | TEXT_ALIGN_SCREEN_CENTER |
---|
45 | }; |
---|
46 | |
---|
47 | /* some default values */ |
---|
48 | #define FONT_DEFAULT_SIZE 50 //!< default size of the Text |
---|
49 | #define FONT_DEFAULT_TEXT "orxonox 1234567890" //!< default text to display |
---|
50 | #define FONT_DEFAULT_COLOR_R 255 //!< default red part (color) of the text |
---|
51 | #define FONT_DEFAULT_COLOR_G 255 //!< default red green (color) of the text |
---|
52 | #define FONT_DEFAULT_COLOR_B 255 //!< default red blue (color) of the text |
---|
53 | #define FONT_NUM_COLORS 256 //!< number of colors. |
---|
54 | |
---|
55 | #define FONT_HIGHEST_KNOWN_CHAR 128 //!< The highest character known to the textEngine. |
---|
56 | |
---|
57 | #define TEXT_DEFAULT_ALIGNMENT TEXT_ALIGN_CENTER //!< default alignment |
---|
58 | #define TEXT_STATIC 0 //!< Static Text |
---|
59 | #define TEXT_DYNAMIC 1 //!< Dynamic Text |
---|
60 | /** |
---|
61 | * STATIC means: a font, that is only one GL-face. |
---|
62 | ** it is very fast, and can be used for all text |
---|
63 | ** that does not have to be changed anymore, or if |
---|
64 | ** the the text should look very nice |
---|
65 | * DYNAMIC means: a very fast font, that will is build |
---|
66 | ** from multiple quads. |
---|
67 | ** Use this type, if you want to create fast changing |
---|
68 | ** text like a counter. |
---|
69 | */ |
---|
70 | |
---|
71 | |
---|
72 | //! A Struct to handel Texture Coordinates for quads |
---|
73 | struct TexCoord |
---|
74 | { |
---|
75 | float minU; //!< The minimum U-Coordinate |
---|
76 | float maxU; //!< The maximum U-Coordinate |
---|
77 | float minV; //!< The minimum V-Coordinate |
---|
78 | float maxV; //!< The maximum V-Coordinate |
---|
79 | }; |
---|
80 | |
---|
81 | //! A struct for handling glyphs |
---|
82 | /** |
---|
83 | a Glyph is one letter of a certain font |
---|
84 | */ |
---|
85 | struct Glyph |
---|
86 | { |
---|
87 | // Glyph-specific (size and so on) |
---|
88 | Uint16 character; //!< The character |
---|
89 | int minX; //!< The minimum distance from the origin in X |
---|
90 | int maxX; //!< The maximum distance from the origin in X |
---|
91 | int minY; //!< The minimum distance from the origin in Y |
---|
92 | int maxY; //!< The maximum distance from the origin in Y |
---|
93 | int width; //!< The width of the Glyph |
---|
94 | int height; //!< The height of the Glyph |
---|
95 | int bearingX; //!< How much is right of the Origin |
---|
96 | int bearingY; //!< How much is above the Origin |
---|
97 | int advance; //!< How big a Glyph would be in monospace-mode |
---|
98 | |
---|
99 | // OpenGL-specific |
---|
100 | // TexCoord texCoord; //!< A Texture Coordinate for this glyph. |
---|
101 | GLuint displayList; //!< DiplayList to render this Glyph. |
---|
102 | }; |
---|
103 | |
---|
104 | //////////// |
---|
105 | /// TEXT /// |
---|
106 | //////////// |
---|
107 | //! Represents one textElement. |
---|
108 | class Text : public Element2D |
---|
109 | { |
---|
110 | friend class TextEngine; |
---|
111 | public: |
---|
112 | ~Text(); |
---|
113 | |
---|
114 | void setType(int type); |
---|
115 | void setText(const char* text); |
---|
116 | void setPosition(int x, int y); |
---|
117 | void setAlignment(TEXT_ALIGNMENT alignment); |
---|
118 | /** @param blending the blending intensity to set (between 0.0 and 1.0) */ |
---|
119 | inline void setBlending(float blending) {this->blending = blending;} |
---|
120 | |
---|
121 | // Static Text |
---|
122 | void setColor(Uint8 r, Uint8 g, Uint8 b); |
---|
123 | |
---|
124 | void createTexture(); |
---|
125 | |
---|
126 | virtual void draw() const; |
---|
127 | |
---|
128 | void debug() const; |
---|
129 | |
---|
130 | private: |
---|
131 | Text(Font* font, int type = TEXT_DYNAMIC); |
---|
132 | |
---|
133 | static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord); |
---|
134 | static int powerOfTwo(int input); |
---|
135 | |
---|
136 | private: |
---|
137 | Font* font; //!< Font of this text |
---|
138 | |
---|
139 | int type; //!< The type of this Font. |
---|
140 | char* text; //!< The text to display |
---|
141 | SDL_Color color; //!< The color of the font. |
---|
142 | TEXT_ALIGNMENT alignment; //!< The aignment of the text. |
---|
143 | float blending; //!< The blending intensity. |
---|
144 | |
---|
145 | // placement in openGL |
---|
146 | GLuint texture; //!< A GL-texture to hold the text |
---|
147 | TexCoord texCoord; //!< Texture-coordinates @todo fix this to have a struct |
---|
148 | SDL_Rect posSize; //!< An SDL-Rectangle representing the position and size of the Text on the screen. |
---|
149 | }; |
---|
150 | |
---|
151 | //////////// |
---|
152 | /// FONT /// |
---|
153 | //////////// |
---|
154 | //! A class to handle a Font of a certain ttf-File, Size and Color. |
---|
155 | class Font : public BaseObject |
---|
156 | { |
---|
157 | friend class Text; |
---|
158 | |
---|
159 | public: |
---|
160 | Font(const char* fontFile, |
---|
161 | unsigned int fontSize = FONT_DEFAULT_SIZE, |
---|
162 | Uint8 r = FONT_DEFAULT_COLOR_R, |
---|
163 | Uint8 g = FONT_DEFAULT_COLOR_G, |
---|
164 | Uint8 b = FONT_DEFAULT_COLOR_B); |
---|
165 | |
---|
166 | virtual ~Font(); |
---|
167 | |
---|
168 | // font |
---|
169 | bool setFont(const char* fontFile); |
---|
170 | void setSize(unsigned int fontSize); |
---|
171 | void setFastColor(Uint8 r, Uint8 g, Uint8 b); |
---|
172 | void setStyle(const char* renderStyle); |
---|
173 | |
---|
174 | /** @returns a Pointer to the Array of Glyphs */ |
---|
175 | inline Glyph** getGlyphArray() const {return glyphArray;} |
---|
176 | /** @returns the texture to the fast-texture */ |
---|
177 | inline GLuint getFastTextureID() const {return fastTextureID;} |
---|
178 | |
---|
179 | private: |
---|
180 | int getMaxHeight(); |
---|
181 | int getMaxAscent(); |
---|
182 | int getMaxDescent(); |
---|
183 | Glyph* getGlyphMetrics(Uint16 character); |
---|
184 | |
---|
185 | GLuint createFastTexture(); |
---|
186 | |
---|
187 | void initGlyphs(Uint16 from, Uint16 count); |
---|
188 | int findOptimalFastTextureSize(); |
---|
189 | |
---|
190 | void debug(); |
---|
191 | |
---|
192 | private: |
---|
193 | // general purpose |
---|
194 | GLdouble projMat[16]; //!< The Projection Matrix |
---|
195 | |
---|
196 | // information about the Font |
---|
197 | TTF_Font* font; //!< The font we use for this. |
---|
198 | char* fontFile; //!< The fontfile from whitch the font was loaded. |
---|
199 | unsigned int fontSize; //!< The size of the font in pixels. each Font has one size. |
---|
200 | int renderStyle; //!< The Renderstyle |
---|
201 | |
---|
202 | Glyph** glyphArray; //!< An Array of all the Glyphs stored in the Array of Glyphs. |
---|
203 | GLuint fastTextureID; //!< The fast textureID. |
---|
204 | SDL_Color fastColor; //!< A Color for the fast Texture. |
---|
205 | |
---|
206 | tList<Text>* textList; //!< A list of texts this Font is mapped to. |
---|
207 | }; |
---|
208 | |
---|
209 | /////////////////// |
---|
210 | /// TEXT-ENGINE /// |
---|
211 | /////////////////// |
---|
212 | //! A singleton Class that operates as a Handler for generating and rendering Text in 2D |
---|
213 | class TextEngine : public BaseObject |
---|
214 | { |
---|
215 | public: |
---|
216 | virtual ~TextEngine(); |
---|
217 | /** @returns a Pointer to the only object of this Class */ |
---|
218 | inline static TextEngine* getInstance() { if (!singletonRef) singletonRef = new TextEngine(); return singletonRef; }; |
---|
219 | |
---|
220 | Text* createText(const char* fontFile, |
---|
221 | unsigned int fontSize = FONT_DEFAULT_SIZE, |
---|
222 | int textType = TEXT_DYNAMIC, |
---|
223 | Uint8 r = FONT_DEFAULT_COLOR_R, |
---|
224 | Uint8 g = FONT_DEFAULT_COLOR_G, |
---|
225 | Uint8 b = FONT_DEFAULT_COLOR_B); |
---|
226 | |
---|
227 | void deleteText(Text* text); |
---|
228 | void flush(); |
---|
229 | |
---|
230 | void draw() const; |
---|
231 | |
---|
232 | void debug() const; |
---|
233 | |
---|
234 | private: |
---|
235 | TextEngine(); |
---|
236 | static TextEngine* singletonRef; |
---|
237 | |
---|
238 | // general |
---|
239 | static void enableFonts(); |
---|
240 | static void disableFonts(); |
---|
241 | static bool checkVersion(); |
---|
242 | |
---|
243 | private: |
---|
244 | // tList<Font>* fontList; |
---|
245 | tList<Text>* textList; //!< a list of all texts of the textEngine |
---|
246 | |
---|
247 | }; |
---|
248 | |
---|
249 | #endif /* _TEXT_ENGINE_H */ |
---|