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