Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/textEngine: compiling again, many movements… still working

File size: 6.7 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 public:
95  Text(Font* font, int type = TEXT_DYNAMIC);
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  // Dynamic Text
110 
111
112
113  virtual void draw(void);
114
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  void enter2DMode(void);
173  void leave2DMode(void);
174
175
176  GLuint createFastTexture();
177
178  void initGlyphs(Uint16 from, Uint16 count);
179  int findOptimalFastTextureSize(void);
180
181  void debug(void);
182
183};
184GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
185int powerOfTwo(int input);
186
187
188///////////////////
189/// TEXT-ENGINE ///
190///////////////////
191//! A singleton Class that operates as a Handler for generating and rendering Text in 2D
192class TextEngine : public BaseObject {
193
194 public:
195  static TextEngine* getInstance(void);
196  virtual ~TextEngine(void);
197
198  Text* createText(const char* fontFile,
199                   unsigned int fontSize = FONT_DEFAULT_SIZE,
200                   Uint8 r = FONT_DEFAULT_COLOR_R,
201                   Uint8 g = FONT_DEFAULT_COLOR_G,
202                   Uint8 b = FONT_DEFAULT_COLOR_B);
203
204  void draw(void);
205
206 private:
207  TextEngine(void);
208  static TextEngine* singletonRef;
209
210  // general
211  static void enableFonts(void);
212  static void disableFonts(void);
213  static bool checkVersion(void);
214
215
216  //  tList<Font>* fontList;
217  tList<Text>* textList;
218
219};
220
221#endif /* _TEXT_ENGINE_H */
Note: See TracBrowser for help on using the repository browser.