Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4516 was 4458, checked in by bensch, 20 years ago

orxonox/trunk: graphicsEngine and TextEngine documented

File size: 7.6 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
113  void createTexture();
114
115  void draw(void) const;
116 
117  void debug(void) const;
118
119 private:
120  Text(Font* font, int type = TEXT_DYNAMIC);
121 
122  static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
123  static int powerOfTwo(int input);
124
125 private:
126  Font*             font;           //!< Font of this text
127
128  int               type;           //!< The type of this Font.
129  char*             text;           //!< The text to display
130  SDL_Color         color;          //!< The color of the font.
131  TEXT_ALIGNMENT    alignment;      //!< The aignment of the text.
132  float             blending;       //!< The blending intensity.
133
134  // placement in openGL
135  GLuint            texture;        //!< A GL-texture to hold the text
136  TexCoord          texCoord;       //!< Texture-coordinates \todo fix this to have a struct
137  SDL_Rect          posSize;        //!< An SDL-Rectangle representing the position and size of the Text on the screen.
138 
139  PNode*            bindNode;       //!< A node the Text is bind to. (if NULL thr node will not be bound to anything.)
140};
141
142////////////
143/// FONT ///
144////////////
145//! A class to handle a Font of a certain ttf-File, Size and Color.
146class Font
147{
148  friend class Text;
149
150 public:
151  Font(const char* fontFile,
152       unsigned int fontSize = FONT_DEFAULT_SIZE,
153       Uint8 r = FONT_DEFAULT_COLOR_R,
154       Uint8 g = FONT_DEFAULT_COLOR_G,
155       Uint8 b = FONT_DEFAULT_COLOR_B);
156
157  virtual ~Font();
158
159  // font
160  bool setFont(const char* fontFile);
161  void setSize(unsigned int fontSize);
162  void setFastColor(Uint8 r, Uint8 g, Uint8 b);
163  void setStyle(const char* renderStyle);
164
165  /** \returns a Pointer to the Array of Glyphs */
166  inline Glyph** getGlyphArray(void) const {return glyphArray;}
167  /** \returns the texture to the fast-texture */
168  inline GLuint getFastTextureID(void) const {return fastTextureID;}
169 
170 private:
171  int getMaxHeight(void);
172  int getMaxAscent(void);
173  int getMaxDescent(void);
174  Glyph* getGlyphMetrics(Uint16 character);
175
176  GLuint createFastTexture();
177
178  void initGlyphs(Uint16 from, Uint16 count);
179  int findOptimalFastTextureSize(void);
180
181  void debug(void);
182
183 private:
184  // general purpose
185  GLdouble      projMat[16];         //!< The Projection Matrix
186
187  // information about the Font
188  TTF_Font*     font;                //!< The font we use for this.
189  char*         fontFile;            //!< The fontfile from whitch the font was loaded.
190  unsigned int  fontSize;            //!< The size of the font in pixels. each Font has one size.
191  int           renderStyle;         //!< The Renderstyle
192 
193  Glyph**       glyphArray;          //!< An Array of all the Glyphs stored in the Array of Glyphs.
194  GLuint        fastTextureID;       //!< The fast textureID.
195  SDL_Color     fastColor;           //!< A Color for the fast Texture.
196
197  tList<Text>*  textList;            //!< A list of texts this Font is mapped to.
198};
199
200///////////////////
201/// TEXT-ENGINE ///
202///////////////////
203//! A singleton Class that operates as a Handler for generating and rendering Text in 2D
204class TextEngine : public BaseObject
205{
206 public:
207  static TextEngine* getInstance(void);
208  virtual ~TextEngine(void);
209
210  Text* createText(const char* fontFile,
211                   unsigned int fontSize = FONT_DEFAULT_SIZE,
212                   int textType = TEXT_DYNAMIC,
213                   Uint8 r = FONT_DEFAULT_COLOR_R,
214                   Uint8 g = FONT_DEFAULT_COLOR_G,
215                   Uint8 b = FONT_DEFAULT_COLOR_B);
216 
217  void deleteText(Text* text);
218  void flush(void);
219
220  void draw(void) const;
221 
222  void debug(void) const;
223
224 private:
225  TextEngine(void);
226  static TextEngine* singletonRef;
227
228  // general
229  static void enableFonts(void);
230  static void disableFonts(void);
231  static bool checkVersion(void);
232
233 private:
234  //  tList<Font>* fontList;
235  tList<Text>*       textList;      //!< a list of all texts of the textEngine
236
237};
238
239#endif /* _TEXT_ENGINE_H */
Note: See TracBrowser for help on using the repository browser.