[4597] | 1 | /*! |
---|
[5039] | 2 | * @file text_engine.h |
---|
[4836] | 3 | * Definition of textEngine, the Font and the Text |
---|
[3766] | 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. |
---|
[3767] | 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. |
---|
[3655] | 14 | */ |
---|
| 15 | |
---|
[3766] | 16 | #ifndef _TEXT_ENGINE_H |
---|
| 17 | #define _TEXT_ENGINE_H |
---|
[3655] | 18 | |
---|
[3766] | 19 | |
---|
| 20 | #include "glincl.h" |
---|
[4662] | 21 | |
---|
| 22 | #ifdef HAVE_SDL_IMAGE_H |
---|
[3766] | 23 | #include "SDL_ttf.h" |
---|
[4662] | 24 | #else |
---|
| 25 | #include "SDL/SDL_ttf.h" |
---|
| 26 | #endif |
---|
[3766] | 27 | |
---|
[3655] | 28 | #include "base_object.h" |
---|
[4850] | 29 | #include "element_2d.h" |
---|
[3655] | 30 | |
---|
[4850] | 31 | #include "vector.h" |
---|
| 32 | |
---|
[3766] | 33 | // FORWARD DECLARATION |
---|
| 34 | class PNode; |
---|
[3768] | 35 | class Font; |
---|
[3655] | 36 | |
---|
[4856] | 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 |
---|
[5122] | 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) |
---|
[3655] | 43 | |
---|
[3766] | 44 | /* some default values */ |
---|
[5122] | 45 | #define FONT_DEFAULT_SIZE 50 //!< default size of the Text |
---|
| 46 | #define FONT_NUM_COLORS 256 //!< number of colors. |
---|
[3766] | 47 | |
---|
[5122] | 48 | #define FONT_HIGHEST_KNOWN_CHAR 128 //!< The highest character known to the textEngine. |
---|
[3766] | 49 | |
---|
[5122] | 50 | #define TEXT_DEFAULT_ALIGNMENT TEXT_ALIGN_CENTER //!< default alignment |
---|
| 51 | |
---|
| 52 | typedef enum TEXT_RENDER_TYPE |
---|
| 53 | { |
---|
| 54 | TEXT_RENDER_STATIC = 1, |
---|
| 55 | TEXT_RENDER_DYNAMIC = 2 |
---|
| 56 | }; |
---|
[3766] | 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 |
---|
| 70 | struct TexCoord |
---|
| 71 | { |
---|
[4458] | 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 |
---|
[3766] | 76 | }; |
---|
| 77 | |
---|
| 78 | //! A struct for handling glyphs |
---|
| 79 | /** |
---|
| 80 | a Glyph is one letter of a certain font |
---|
| 81 | */ |
---|
| 82 | struct Glyph |
---|
| 83 | { |
---|
| 84 | // Glyph-specific (size and so on) |
---|
[4458] | 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 |
---|
[4597] | 95 | |
---|
[3766] | 96 | // OpenGL-specific |
---|
[4536] | 97 | // TexCoord texCoord; //!< A Texture Coordinate for this glyph. |
---|
| 98 | GLuint displayList; //!< DiplayList to render this Glyph. |
---|
[3766] | 99 | }; |
---|
| 100 | |
---|
[3768] | 101 | //////////// |
---|
| 102 | /// TEXT /// |
---|
| 103 | //////////// |
---|
[3767] | 104 | //! Represents one textElement. |
---|
[4850] | 105 | class Text : public Element2D |
---|
[3767] | 106 | { |
---|
| 107 | public: |
---|
[5179] | 108 | Text(Font* font = NULL, TEXT_RENDER_TYPE type = TEXT_RENDER_DYNAMIC); |
---|
[4746] | 109 | ~Text(); |
---|
[3768] | 110 | |
---|
[5179] | 111 | void setFont(Font* font); |
---|
| 112 | void setFont(const char* fontFile, unsigned int fontSize); |
---|
[5122] | 113 | void setType(TEXT_RENDER_TYPE type); |
---|
| 114 | void setText(const char* text, bool isExtern = false); |
---|
[5181] | 115 | /** @returns the String this Text displays */ |
---|
| 116 | inline const char* getText() const { return (externText == NULL)?this->text:this->externText; }; |
---|
[4836] | 117 | /** @param blending the blending intensity to set (between 0.0 and 1.0) */ |
---|
[4856] | 118 | inline void setBlending(float blending) { this->blending = blending; }; |
---|
[3768] | 119 | |
---|
[5121] | 120 | /** sets the Color of the Text to render (values in [0-1]) @param r red @param g green @param b blue */ |
---|
| 121 | void setColor(float r, float g, float b) { this->color = Vector(r,g,b); }; |
---|
[3843] | 122 | |
---|
[3769] | 123 | void createTexture(); |
---|
| 124 | |
---|
[4850] | 125 | virtual void draw() const; |
---|
[4597] | 126 | |
---|
[4746] | 127 | void debug() const; |
---|
[3769] | 128 | |
---|
[3773] | 129 | private: |
---|
[4458] | 130 | static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord); |
---|
| 131 | static int powerOfTwo(int input); |
---|
[3768] | 132 | |
---|
[4458] | 133 | private: |
---|
| 134 | Font* font; //!< Font of this text |
---|
[3768] | 135 | |
---|
[5122] | 136 | TEXT_RENDER_TYPE type; //!< The type of this Font. |
---|
[4458] | 137 | char* text; //!< The text to display |
---|
[5122] | 138 | const char* externText; //!< the text to Display from an external Source. |
---|
[5121] | 139 | Vector color; //!< The color of the font. |
---|
[4458] | 140 | float blending; //!< The blending intensity. |
---|
| 141 | |
---|
[3767] | 142 | // placement in openGL |
---|
[4458] | 143 | GLuint texture; //!< A GL-texture to hold the text |
---|
[4836] | 144 | TexCoord texCoord; //!< Texture-coordinates @todo fix this to have a struct |
---|
[5123] | 145 | float height; |
---|
| 146 | float width; |
---|
[3767] | 147 | }; |
---|
| 148 | |
---|
[3768] | 149 | //////////// |
---|
| 150 | /// FONT /// |
---|
| 151 | //////////// |
---|
[3767] | 152 | //! A class to handle a Font of a certain ttf-File, Size and Color. |
---|
[4597] | 153 | class Font : public BaseObject |
---|
[3766] | 154 | { |
---|
[3769] | 155 | friend class Text; |
---|
[4458] | 156 | |
---|
[3655] | 157 | public: |
---|
[4458] | 158 | Font(const char* fontFile, |
---|
[5121] | 159 | unsigned int fontSize = FONT_DEFAULT_SIZE); |
---|
[4458] | 160 | |
---|
[3766] | 161 | virtual ~Font(); |
---|
[3655] | 162 | |
---|
[3766] | 163 | // font |
---|
[5124] | 164 | bool loadFont(const char* fontFile); |
---|
[3766] | 165 | void setSize(unsigned int fontSize); |
---|
[4458] | 166 | void setStyle(const char* renderStyle); |
---|
[3766] | 167 | |
---|
[4836] | 168 | /** @returns a Pointer to the Array of Glyphs */ |
---|
[5121] | 169 | inline Glyph** getGlyphArray() const { return glyphArray; }; |
---|
[4836] | 170 | /** @returns the texture to the fast-texture */ |
---|
[5124] | 171 | inline GLuint getFastTextureID() const { return fastTextureID; }; |
---|
[4597] | 172 | |
---|
[3655] | 173 | private: |
---|
[4746] | 174 | int getMaxHeight(); |
---|
| 175 | int getMaxAscent(); |
---|
| 176 | int getMaxDescent(); |
---|
[3766] | 177 | Glyph* getGlyphMetrics(Uint16 character); |
---|
| 178 | |
---|
| 179 | GLuint createFastTexture(); |
---|
| 180 | |
---|
| 181 | void initGlyphs(Uint16 from, Uint16 count); |
---|
[4746] | 182 | int findOptimalFastTextureSize(); |
---|
[3766] | 183 | |
---|
[4746] | 184 | void debug(); |
---|
[3766] | 185 | |
---|
[4458] | 186 | private: |
---|
| 187 | // general purpose |
---|
| 188 | GLdouble projMat[16]; //!< The Projection Matrix |
---|
| 189 | |
---|
| 190 | // information about the Font |
---|
| 191 | TTF_Font* font; //!< The font we use for this. |
---|
| 192 | char* fontFile; //!< The fontfile from whitch the font was loaded. |
---|
| 193 | unsigned int fontSize; //!< The size of the font in pixels. each Font has one size. |
---|
| 194 | int renderStyle; //!< The Renderstyle |
---|
[4597] | 195 | |
---|
[4458] | 196 | Glyph** glyphArray; //!< An Array of all the Glyphs stored in the Array of Glyphs. |
---|
| 197 | GLuint fastTextureID; //!< The fast textureID. |
---|
| 198 | |
---|
| 199 | tList<Text>* textList; //!< A list of texts this Font is mapped to. |
---|
[3655] | 200 | }; |
---|
| 201 | |
---|
[3766] | 202 | /////////////////// |
---|
| 203 | /// TEXT-ENGINE /// |
---|
| 204 | /////////////////// |
---|
| 205 | //! A singleton Class that operates as a Handler for generating and rendering Text in 2D |
---|
[4597] | 206 | class TextEngine : public BaseObject |
---|
[3773] | 207 | { |
---|
[3766] | 208 | public: |
---|
[4746] | 209 | virtual ~TextEngine(); |
---|
[4836] | 210 | /** @returns a Pointer to the only object of this Class */ |
---|
[4746] | 211 | inline static TextEngine* getInstance() { if (!singletonRef) singletonRef = new TextEngine(); return singletonRef; }; |
---|
[3766] | 212 | |
---|
[3769] | 213 | Text* createText(const char* fontFile, |
---|
[4597] | 214 | unsigned int fontSize = FONT_DEFAULT_SIZE, |
---|
[5122] | 215 | int textType = TEXT_RENDER_DYNAMIC); |
---|
[4597] | 216 | |
---|
[4746] | 217 | void debug() const; |
---|
[3774] | 218 | |
---|
[3769] | 219 | private: |
---|
[4746] | 220 | TextEngine(); |
---|
[3769] | 221 | static TextEngine* singletonRef; |
---|
| 222 | |
---|
[3766] | 223 | // general |
---|
[4746] | 224 | static void enableFonts(); |
---|
| 225 | static void disableFonts(); |
---|
| 226 | static bool checkVersion(); |
---|
[3766] | 227 | }; |
---|
| 228 | |
---|
| 229 | #endif /* _TEXT_ENGINE_H */ |
---|