Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: text is now Rendered as expected (in FastMode)

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#define  TEXT_DEFAULT_COLOR          Vector(1.0, 1.0, 1.0)      //!< the default Color (white)
42#define  TEXT_DEFAULT_BLENDING       1.0f                       //!< the default blending of the text, (no blending at all)
43
44/* some default values */
45#define FONT_DEFAULT_SIZE            50                         //!< default size 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
52typedef enum TEXT_RENDER_TYPE
53{
54  TEXT_RENDER_STATIC      = 1,
55  TEXT_RENDER_DYNAMIC     = 2
56};
57/**
58 * STATIC means: a font, that is only one GL-face.
59 ** it is very fast, and can be used for all text
60 ** that does not have to be changed anymore, or if
61 ** the the text should look very nice
62 * DYNAMIC means: a very fast font, that will is build
63 ** from multiple quads.
64 ** Use this type, if you want to create fast changing
65 ** text like a counter.
66 */
67
68
69//! A Struct to handel Texture Coordinates for quads
70struct TexCoord
71{
72  float    minU;              //!< The minimum U-Coordinate
73  float    maxU;              //!< The maximum U-Coordinate
74  float    minV;              //!< The minimum V-Coordinate
75  float    maxV;              //!< The maximum V-Coordinate
76};
77
78//! A struct for handling glyphs
79/**
80   a Glyph is one letter of a certain font
81*/
82struct Glyph
83{
84  // Glyph-specific (size and so on)
85  Uint16   character;         //!< The character
86  int      minX;              //!< The minimum distance from the origin in X
87  int      maxX;              //!< The maximum distance from the origin in X
88  int      minY;              //!< The minimum distance from the origin in Y
89  int      maxY;              //!< The maximum distance from the origin in Y
90  int      width;             //!< The width of the Glyph
91  int      height;            //!< The height of the Glyph
92  int      bearingX;          //!< How much is right of the Origin
93  int      bearingY;          //!< How much is above the Origin
94  int      advance;           //!< How big a Glyph would be in monospace-mode
95
96  // OpenGL-specific
97  //  TexCoord texCoord;      //!< A Texture Coordinate for this glyph.
98  GLuint   displayList;       //!< DiplayList to render this Glyph.
99};
100
101////////////
102/// TEXT ///
103////////////
104//! Represents one textElement.
105class Text : public Element2D
106{
107 public:
108   Text(Font* font, TEXT_RENDER_TYPE type = TEXT_RENDER_DYNAMIC);
109  ~Text();
110
111  void setType(TEXT_RENDER_TYPE type);
112  void setText(const char* text, bool isExtern = false);
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  /** sets the Color of the Text to render (values in [0-1]) @param r red @param g green @param b blue */
117  void setColor(float r, float g, float b) { this->color = Vector(r,g,b); };
118
119  void createTexture();
120
121  virtual void draw() const;
122
123  void debug() const;
124
125 private:
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  TEXT_RENDER_TYPE  type;           //!< The type of this Font.
133  char*             text;           //!< The text to display
134  const char*       externText;     //!< the text to Display from an external Source.
135  Vector            color;          //!< The color of the font.
136  float             blending;       //!< The blending intensity.
137
138  // placement in openGL
139  GLuint            texture;        //!< A GL-texture to hold the text
140  TexCoord          texCoord;       //!< Texture-coordinates @todo fix this to have a struct
141  float             height;
142  float             width;
143};
144
145////////////
146/// FONT ///
147////////////
148//! A class to handle a Font of a certain ttf-File, Size and Color.
149class Font : public BaseObject
150{
151  friend class Text;
152
153 public:
154  Font(const char* fontFile,
155       unsigned int fontSize = FONT_DEFAULT_SIZE);
156
157  virtual ~Font();
158
159  // font
160  bool loadFont(const char* fontFile);
161  void setSize(unsigned int fontSize);
162  void setStyle(const char* renderStyle);
163
164  /** @returns a Pointer to the Array of Glyphs */
165  inline Glyph** getGlyphArray() const { return glyphArray; };
166  /** @returns the texture to the fast-texture */
167  inline GLuint getFastTextureID() const { return fastTextureID; };
168
169 private:
170  int getMaxHeight();
171  int getMaxAscent();
172  int getMaxDescent();
173  Glyph* getGlyphMetrics(Uint16 character);
174
175  GLuint createFastTexture();
176
177  void initGlyphs(Uint16 from, Uint16 count);
178  int findOptimalFastTextureSize();
179
180  void debug();
181
182 private:
183  // general purpose
184  GLdouble      projMat[16];         //!< The Projection Matrix
185
186  // information about the Font
187  TTF_Font*     font;                //!< The font we use for this.
188  char*         fontFile;            //!< The fontfile from whitch the font was loaded.
189  unsigned int  fontSize;            //!< The size of the font in pixels. each Font has one size.
190  int           renderStyle;         //!< The Renderstyle
191
192  Glyph**       glyphArray;          //!< An Array of all the Glyphs stored in the Array of Glyphs.
193  GLuint        fastTextureID;       //!< The fast textureID.
194
195  tList<Text>*  textList;            //!< A list of texts this Font is mapped to.
196};
197
198///////////////////
199/// TEXT-ENGINE ///
200///////////////////
201//! A singleton Class that operates as a Handler for generating and rendering Text in 2D
202class TextEngine : public BaseObject
203{
204 public:
205  virtual ~TextEngine();
206  /** @returns a Pointer to the only object of this Class */
207  inline static TextEngine* getInstance() { if (!singletonRef) singletonRef = new TextEngine();  return singletonRef; };
208
209  Text* createText(const char* fontFile,
210                   unsigned int fontSize = FONT_DEFAULT_SIZE,
211                   int textType = TEXT_RENDER_DYNAMIC);
212
213  void debug() const;
214
215 private:
216  TextEngine();
217  static TextEngine* singletonRef;
218
219  // general
220  static void enableFonts();
221  static void disableFonts();
222  static bool checkVersion();
223};
224
225#endif /* _TEXT_ENGINE_H */
Note: See TracBrowser for help on using the repository browser.