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