Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

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