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