Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/text_engine.h @ 4227

Last change on this file since 4227 was 3845, checked in by bensch, 20 years ago

orxonox/trunk: now some text gets displayed, when changing the Track

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