Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/textEngine: some reimplementation

File size: 6.1 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;
28template<class T> class tList;
29
30
31/* some default values */
32#define FONT_DEFAULT_SIZE       50                   //!< default size of the Text
33#define FONT_DEFAULT_TEXT       "orxonox 1234567890" //!< some default text to display
34#define FONT_DEFAULT_COLOR_R    255                  //!< the default red part (color) of the text
35#define FONT_DEFAULT_COLOR_G    255                  //!< the default red green (color) of the text
36#define FONT_DEFAULT_COLOR_B    255                  //!< the default red blue (color) of the text
37#define FONT_NUM_COLORS         256                  //!< The number of colors.
38
39#define FONT_HIGHEST_KNOWN_CHAR 128                  //!< The highest character known to the textEngine.
40
41#define TEXT_STATIC             0                    //!< Static Text
42#define TEXT_DYNAMIC            1                    //!< Dynamic Text
43/**
44 * STATIC means: a font, that is only one GL-face.
45 ** it is very fast, and can be used for all text
46 ** that does not have to be changed anymore, or if
47 ** the the text should look very nice
48 * DYNAMIC means: a very fast font, that will is build
49 ** from multiple quads.
50 ** Use this type, if you want to create fast changing
51 ** text like a counter.
52 */
53
54
55//! A Struct to handel Texture Coordinates for quads
56struct TexCoord
57{
58  float minU;                      //!< The minimum U-Coordinate
59  float maxU;                      //!< The maximum U-Coordinate
60  float minV;                      //!< The minimum V-Coordinate
61  float maxV;                      //!< The maximum V-Coordinate
62};
63
64//! A struct for handling glyphs
65/**
66   a Glyph is one letter of a certain font
67*/
68struct Glyph
69{
70  // Glyph-specific (size and so on)
71  Uint16 character;              //!< The character
72  int minX;                      //!< The minimum distance from the origin in X
73  int maxX;                      //!< The maximum distance from the origin in X
74  int minY;                      //!< The minimum distance from the origin in Y
75  int maxY;                      //!< The maximum distance from the origin in Y
76  int width;                     //!< The width of the Glyph
77  int height;                    //!< The height of the Glyph
78  int bearingX;                  //!< How much is right of the Origin
79  int bearingY;                  //!< How much is above the Origin
80  int advance;                   //!< How big a Glyph would be in monospace-mode
81 
82  // OpenGL-specific
83  //  TexCoord texCoord;         //!< A Texture Coordinate for this glyph.
84  GLuint displayList;            //!< DiplayList to render this Glyph.
85};
86
87//! Represents one textElement.
88class Text
89{
90 public:
91  int type;                      //!< The type of this Font.
92  char* text;                    //!< The text to display
93  SDL_Color color;               //!< The color of the font.
94  // placement in openGL
95  GLuint texture;                //!< A GL-texture to hold the text
96  TexCoord texCoord;             //!< Texture-coordinates \todo fix this to have a struct
97  SDL_Rect textPosSize;          //!< An SDL-Rectangle representing the position and size of the Text on the screen.
98  int renderStyle;               //!< The Renderstyle
99 
100  PNode* bindNode;               //!< A node the Text is bind to. (if NULL thr node will not be bound to anything.)
101};
102
103
104//! A class to handle a Font of a certain ttf-File, Size and Color.
105class Font
106{
107 public:
108  Font(const char* fontFile, unsigned int fontSize = FONT_DEFAULT_SIZE,
109       Uint8 r = FONT_DEFAULT_COLOR_R, Uint8 g = FONT_DEFAULT_COLOR_G, Uint8 b = FONT_DEFAULT_COLOR_B);
110  virtual ~Font();
111
112  // font
113  bool setFont(const char* fontFile);
114  void setSize(unsigned int fontSize);
115  void setColor(Uint8 r, Uint8 g, Uint8 b);
116
117  // text
118  void setBindNode(PNode* bindNode);
119  void setType(int type);
120  void setText(const char* text);
121  void setStyle(char* renderStyle);
122  void setPosition(int x, int y);
123  void createTexture(void);
124 
125  virtual void draw(void);
126
127 private:
128  // general purpose
129  GLdouble projMat[16];             //!< The Projection Matrix
130
131  // information about the Font
132  TTF_Font* font;                   //!< The font we use for this.
133  char* fontFile;                   //!< The fontfile from whitch the font was loaded.
134  unsigned int fontSize;            //!< The size of the font in pixels. each Font has one size.
135 
136  Glyph** glyphArray;               //!< An Array of all the Glyphs stored in the Array of Glyphs.
137  GLuint fastTextureID;             //!< The fast textureID.
138
139  tList<Text>* textList;
140  Text* currentText;
141
142  int getMaxHeight(void);
143  int getMaxAscent(void);
144  int getMaxDescent(void);
145  Glyph* getGlyphMetrics(Uint16 character);
146
147  void enter2DMode(void);
148  void leave2DMode(void);
149
150
151  GLuint createFastTexture();
152  GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
153
154  void initGlyphs(Uint16 from, Uint16 count);
155  int findOptimalFastTextureSize(void);
156  static int powerOfTwo(int input);
157
158  void debug(void);
159
160};
161
162void m_inverse(const float *m, float *out);
163Vector mvMult(const float *mat, const Vector* vec);
164
165
166///////////////////
167/// TEXT-ENGINE ///
168///////////////////
169//! A singleton Class that operates as a Handler for generating and rendering Text in 2D
170class TextEngine : public BaseObject {
171
172 public:
173  static TextEngine* getInstance(void);
174  virtual ~TextEngine(void);
175
176  // general
177  static void enableFonts(void);
178  static void disableFonts(void);
179  static bool checkVersion(void);
180
181 private:
182  TextEngine(void);
183  static TextEngine* singletonRef;
184
185};
186
187#endif /* _TEXT_ENGINE_H */
Note: See TracBrowser for help on using the repository browser.