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