Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/textEngine/src/lib/graphics/font/text_engine.h @ 3774

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

orxonox/branches/textEngine: debug() implemented

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