Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: color is now a parameter of text not font

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