[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 | { |
---|
| 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 |
---|
| 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) |
---|
| 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 |
---|
| 85 | |
---|
| 86 | // OpenGL-specific |
---|
[3767] | 87 | // TexCoord texCoord; //!< A Texture Coordinate for this glyph. |
---|
[3766] | 88 | GLuint displayList; //!< DiplayList to render this Glyph. |
---|
| 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); |
---|
| 112 | void setStyle(char* renderStyle); |
---|
[3843] | 113 | |
---|
[3769] | 114 | void createTexture(); |
---|
| 115 | |
---|
[3774] | 116 | void draw(void) const; |
---|
[3769] | 117 | |
---|
[3774] | 118 | void debug(void) const; |
---|
[3769] | 119 | |
---|
[3773] | 120 | private: |
---|
| 121 | Text(Font* font, int type = TEXT_DYNAMIC); |
---|
[3768] | 122 | |
---|
| 123 | Font* font; |
---|
| 124 | |
---|
[3767] | 125 | int type; //!< The type of this Font. |
---|
| 126 | char* text; //!< The text to display |
---|
| 127 | SDL_Color color; //!< The color of the font. |
---|
[3843] | 128 | TEXT_ALIGNMENT alignment; //!< The aignment of the text. |
---|
[3777] | 129 | float blending; //!< The blending intensity. |
---|
[3767] | 130 | // placement in openGL |
---|
| 131 | GLuint texture; //!< A GL-texture to hold the text |
---|
| 132 | TexCoord texCoord; //!< Texture-coordinates \todo fix this to have a struct |
---|
[3769] | 133 | SDL_Rect posSize; //!< An SDL-Rectangle representing the position and size of the Text on the screen. |
---|
[3767] | 134 | |
---|
| 135 | PNode* bindNode; //!< A node the Text is bind to. (if NULL thr node will not be bound to anything.) |
---|
| 136 | }; |
---|
| 137 | |
---|
[3768] | 138 | //////////// |
---|
| 139 | /// FONT /// |
---|
| 140 | //////////// |
---|
[3767] | 141 | //! A class to handle a Font of a certain ttf-File, Size and Color. |
---|
[3766] | 142 | class Font |
---|
| 143 | { |
---|
[3769] | 144 | friend class Text; |
---|
[3655] | 145 | public: |
---|
[3767] | 146 | Font(const char* fontFile, unsigned int fontSize = FONT_DEFAULT_SIZE, |
---|
| 147 | Uint8 r = FONT_DEFAULT_COLOR_R, Uint8 g = FONT_DEFAULT_COLOR_G, Uint8 b = FONT_DEFAULT_COLOR_B); |
---|
[3766] | 148 | virtual ~Font(); |
---|
[3655] | 149 | |
---|
[3766] | 150 | // font |
---|
| 151 | bool setFont(const char* fontFile); |
---|
| 152 | void setSize(unsigned int fontSize); |
---|
[3769] | 153 | void setFastColor(Uint8 r, Uint8 g, Uint8 b); |
---|
[3768] | 154 | void setStyle(char* renderStyle); |
---|
[3766] | 155 | |
---|
[3768] | 156 | inline Glyph** getGlyphArray(void) {return glyphArray;} |
---|
| 157 | inline GLuint getFastTextureID(void) {return fastTextureID;} |
---|
[3766] | 158 | |
---|
[3768] | 159 | |
---|
[3655] | 160 | private: |
---|
[3766] | 161 | // general purpose |
---|
| 162 | GLdouble projMat[16]; //!< The Projection Matrix |
---|
| 163 | |
---|
| 164 | // information about the Font |
---|
| 165 | TTF_Font* font; //!< The font we use for this. |
---|
| 166 | char* fontFile; //!< The fontfile from whitch the font was loaded. |
---|
| 167 | unsigned int fontSize; //!< The size of the font in pixels. each Font has one size. |
---|
[3769] | 168 | int renderStyle; //!< The Renderstyle |
---|
[3766] | 169 | |
---|
| 170 | Glyph** glyphArray; //!< An Array of all the Glyphs stored in the Array of Glyphs. |
---|
| 171 | GLuint fastTextureID; //!< The fast textureID. |
---|
[3769] | 172 | SDL_Color fastColor; //!< A Color for the fast Texture. |
---|
[3766] | 173 | |
---|
| 174 | tList<Text>* textList; |
---|
| 175 | |
---|
| 176 | int getMaxHeight(void); |
---|
| 177 | int getMaxAscent(void); |
---|
| 178 | int getMaxDescent(void); |
---|
| 179 | Glyph* getGlyphMetrics(Uint16 character); |
---|
| 180 | |
---|
| 181 | GLuint createFastTexture(); |
---|
| 182 | |
---|
| 183 | void initGlyphs(Uint16 from, Uint16 count); |
---|
| 184 | int findOptimalFastTextureSize(void); |
---|
| 185 | |
---|
| 186 | void debug(void); |
---|
| 187 | |
---|
[3655] | 188 | }; |
---|
[3769] | 189 | GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord); |
---|
| 190 | int powerOfTwo(int input); |
---|
[3655] | 191 | |
---|
[3766] | 192 | |
---|
| 193 | /////////////////// |
---|
| 194 | /// TEXT-ENGINE /// |
---|
| 195 | /////////////////// |
---|
| 196 | //! A singleton Class that operates as a Handler for generating and rendering Text in 2D |
---|
[3773] | 197 | class TextEngine : public BaseObject |
---|
| 198 | { |
---|
[3766] | 199 | public: |
---|
| 200 | static TextEngine* getInstance(void); |
---|
| 201 | virtual ~TextEngine(void); |
---|
| 202 | |
---|
[3769] | 203 | Text* createText(const char* fontFile, |
---|
| 204 | unsigned int fontSize = FONT_DEFAULT_SIZE, |
---|
[3773] | 205 | int textType = TEXT_DYNAMIC, |
---|
[3769] | 206 | Uint8 r = FONT_DEFAULT_COLOR_R, |
---|
| 207 | Uint8 g = FONT_DEFAULT_COLOR_G, |
---|
| 208 | Uint8 b = FONT_DEFAULT_COLOR_B); |
---|
[3772] | 209 | |
---|
| 210 | void deleteText(Text* text); |
---|
[3774] | 211 | void flush(void); |
---|
[3769] | 212 | |
---|
[3773] | 213 | void draw(void) const; |
---|
| 214 | |
---|
[3774] | 215 | void debug(void) const; |
---|
| 216 | |
---|
[3769] | 217 | private: |
---|
| 218 | TextEngine(void); |
---|
| 219 | static TextEngine* singletonRef; |
---|
| 220 | |
---|
[3766] | 221 | // general |
---|
| 222 | static void enableFonts(void); |
---|
| 223 | static void disableFonts(void); |
---|
| 224 | static bool checkVersion(void); |
---|
| 225 | |
---|
| 226 | |
---|
[3769] | 227 | // tList<Font>* fontList; |
---|
| 228 | tList<Text>* textList; |
---|
[3768] | 229 | |
---|
[3766] | 230 | }; |
---|
| 231 | |
---|
| 232 | #endif /* _TEXT_ENGINE_H */ |
---|