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