Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/textEngine: some more simple movement around classes

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