Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4536 was 4536, checked in by bensch, 19 years ago

orxonox/trunk: saver font: now the implementation of NON-loading font must be done

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