Changeset 3691 in orxonox.OLD for orxonox/branches/textEngine/src/lib/graphics/font
- Timestamp:
- Mar 31, 2005, 2:14:02 AM (20 years ago)
- Location:
- orxonox/branches/textEngine/src/lib/graphics/font
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc
r3682 r3691 18 18 * *** 19 19 20 for some fonts and licenses visit: =http://www.dafont.com/en/font.php= 20 21 */ 21 22 … … 46 47 #include "glfont.h" 47 48 48 49 50 49 #include <stdlib.h> 51 50 #include <stdio.h> 52 51 #include <string.h> 53 52 54 #include <SDL/SDL.h>55 #include <SDL/SDL_ttf.h>56 57 #include "glincl.h"58 53 #include "debug.h" 59 54 … … 74 69 this->init(fontFile); 75 70 } 71 76 72 GLFont::~GLFont() 77 73 { 78 79 } 74 if (this->font) 75 TTF_CloseFont(this->font); 76 } 77 78 void GLFont::enableFonts(void) 79 { 80 if (!GLFont::ttfInitialized) 81 { 82 if(TTF_Init()==-1) { 83 PRINTF(1)("TTF_Init: %s\n", TTF_GetError()); 84 exit(2); 85 } 86 GLFont::checkVersion(); 87 GLFont::ttfInitialized = true; 88 } 89 else 90 PRINTF(4)("Fonts already initialized\n"); 91 92 } 93 void GLFont::disableFonts(void) 94 { 95 if (GLFont::ttfInitialized) 96 { 97 TTF_Quit(); 98 GLFont::ttfInitialized = false; 99 } 100 else 101 PRINTF(4)("Fonts were not initialized.\n"); 102 } 103 104 bool GLFont::ttfInitialized = false; 105 80 106 81 107 bool GLFont::init(const char* fontFile) 82 108 { 83 109 // setting default values. 84 renderStyle = TTF_STYLE_NORMAL; 85 86 87 // Initializing lib-TTF if necesary. 88 if (TTF_WasInit() && TTF_Init() < 0 ) 110 this->font = NULL; 111 this->fontFile = NULL; 112 this->fontSize = 16; 113 114 this->renderStyle = TTF_STYLE_NORMAL; 115 116 117 if (!GLFont::ttfInitialized) 118 GLFont::enableFonts(); 119 } 120 121 122 bool GLFont::setFont(const char* fontFile) 123 { 124 if (!this->fontFile) 89 125 { 90 PRINTF(1)("Couldn't initialize TTF: %s\n", SDL_GetError()); 91 return false; 126 this->fontFile = new char[strlen(fontFile)+1]; 127 strcpy(this->fontFile, fontFile); 128 129 this->font = TTF_OpenFont(this->fontFile, this->fontSize); 130 if(!this->font) 131 { 132 PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError()); 133 // handle error 134 } 92 135 } 93 } 94 95 bool GLFont::ttfInitialized = false; 96 97 bool GLFont::setFont(const char* fonFile) 98 { 99 100 } 136 else 137 PRINTF(2)("Font already initialized, unable to change it now.\n"); 138 } 139 140 101 141 void GLFont::setText(const char* text) 102 142 { 103 104 } 105 106 void GLFont::setStyle(char* style) 107 { 108 renderStyle = TTF_STYLE_NORMAL; 109 110 for (int i = 0; i < strlen(style); i++) 111 if (strncmp(style+i, "b", 1) == 0) 143 this->text = new char[strlen(text)+1]; 144 strcpy(this->text, text); 145 } 146 147 /** 148 \brief sets a specific renderStyle 149 \param renderStyle the Style to render: a char-array containing: 150 i: italic, b: bold, u, underline 151 */ 152 void GLFont::setStyle(char* renderStyle) 153 { 154 this->renderStyle = TTF_STYLE_NORMAL; 155 156 for (int i = 0; i < strlen(renderStyle); i++) 157 if (strncmp(renderStyle+i, "b", 1) == 0) 112 158 this->renderStyle |= TTF_STYLE_BOLD; 113 else if (strncmp( style+i, "i", 1) == 0)159 else if (strncmp(renderStyle+i, "i", 1) == 0) 114 160 this->renderStyle |= TTF_STYLE_ITALIC; 115 else if (strncmp( style+i, "u", 1) == 0)161 else if (strncmp(renderStyle+i, "u", 1) == 0) 116 162 this->renderStyle |= TTF_STYLE_UNDERLINE; 117 163 118 // TTF_SetFontStyle(font, renderstyle); 119 120 } 164 if (this->font) 165 TTF_SetFontStyle(this->font, this->renderStyle); 166 else 167 PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n"); 168 } 169 121 170 void GLFont::setSize(void) 122 171 { 123 172 124 173 } 174 175 /** 176 \returns the maximum height of the Font, if the font was initialized, 0 otherwise 177 */ 178 int GLFont::getMaxHeight(void) 179 { 180 if (this->font) 181 return TTF_FontHeight(this->font); 182 else 183 return 0; 184 } 185 186 /** 187 \returns the maximum ascent of the Font, if the font was initialized, 0 otherwise 188 189 the ascent is the pixels of the font above the baseline 190 */ 191 int GLFont::getMaxAscent(void) 192 { 193 if (this->font) 194 return TTF_FontAscent(this->font); 195 else 196 return 0; 197 } 198 199 /** 200 \returns the maximum descent of the Font, if the font was initialized, 0 otherwise 201 202 the descent is the pixels of the font below the baseline 203 */ 204 int GLFont::getMaxDescent(void) 205 { 206 if (this->font) 207 return TTF_FontDescent(this->font); 208 else 209 return 0; 210 } 211 212 /** 213 \param character The character to get info about. 214 \returns a Glyph struct of a character. 215 216 This only works for horizontal fonts. see 217 http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html 218 for more info about vertical Fonts 219 */ 220 glyph GLFont::getGlyphMetrics(Uint16 character) 221 { 222 glyph rg; 223 rg.character = character; 224 TTF_GlyphMetrics(this->font, rg.character, 225 &rg.minX, &rg.maxX, 226 &rg.minY, &rg.maxY, 227 &rg.advance); 228 rg.height = rg.maxY - rg.minY; 229 rg.width = rg.maxX - rg.minX; 230 rg.bearingX = (rg.advance - rg.width) / 2; 231 rg.bearingY = rg.maxY; 232 return rg; 233 } 234 125 235 void GLFont::setPosition(int x, int y) 126 236 { … … 178 288 } 179 289 180 181 bool GLFont::loadTexture() 182 { 290 void GLFont::enter2DMode() 291 { 292 SDL_Surface *screen = SDL_GetVideoSurface(); 293 294 /* Note, there may be other things you need to change, 295 depending on how you have your OpenGL state set up. 296 */ 297 glPushAttrib(GL_ENABLE_BIT); 298 glDisable(GL_DEPTH_TEST); 299 glDisable(GL_CULL_FACE); 300 glEnable(GL_TEXTURE_2D); 301 302 /* This allows alpha blending of 2D textures with the scene */ 303 glEnable(GL_BLEND); 304 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 305 306 glViewport(0, 0, screen->w, screen->h); 307 308 glMatrixMode(GL_PROJECTION); 309 glPushMatrix(); 310 glLoadIdentity(); 311 312 glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); 313 314 glMatrixMode(GL_MODELVIEW); 315 glPushMatrix(); 316 glLoadIdentity(); 317 318 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 319 } 320 321 322 void GLFont::leave2DMode() 323 { 324 glMatrixMode(GL_MODELVIEW); 325 glPopMatrix(); 326 327 glMatrixMode(GL_PROJECTION); 328 glPopMatrix(); 329 330 glPopAttrib(); 331 } 332 333 334 GLuint GLFont::loadTexture(SDL_Surface *surface, GLfloat *texcoord) 335 { 336 GLuint texture; 183 337 int w, h; 184 338 SDL_Surface *image; … … 249 403 SDL_FreeSurface(image); /* No longer needed */ 250 404 251 // return texture; 252 } 405 return texture; 406 } 407 408 409 bool GLFont::checkVersion(void) 410 { 411 SDL_version compile_version; 412 SDL_version link_version; 413 TTF_VERSION(&compile_version); 414 link_version = *TTF_Linked_Version(); 415 416 if (compile_version.major == link_version.major && 417 compile_version.minor == link_version.minor && 418 compile_version.patch == link_version.patch) 419 { 420 return true; 421 } 422 else 423 { 424 PRINTF(2)("compiled with SDL_ttf version: %d.%d.%d\n", 425 compile_version.major, 426 compile_version.minor, 427 compile_version.patch); 428 429 PRINTF(2)("running with SDL_ttf version: %d.%d.%d\n", 430 link_version.major, 431 link_version.minor, 432 link_version.patch); 433 return false; 434 } 435 } 436 437 253 438 254 439 /* Quick utility function for texture creation */ … … 264 449 265 450 266 /* 267 private: 268 char* fontFile; 269 char* text; 270 int positionX; 271 int positionY; 272 */ 273 274 275 276 277 char Usage[100] = "orxonox test" ; 278 void GLFont::enter2DMode() 279 { 280 SDL_Surface *screen = SDL_GetVideoSurface(); 281 282 /* Note, there may be other things you need to change, 283 depending on how you have your OpenGL state set up. 284 */ 285 glPushAttrib(GL_ENABLE_BIT); 286 glDisable(GL_DEPTH_TEST); 287 glDisable(GL_CULL_FACE); 288 glEnable(GL_TEXTURE_2D); 289 290 /* This allows alpha blending of 2D textures with the scene */ 291 glEnable(GL_BLEND); 292 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 293 294 glViewport(0, 0, screen->w, screen->h); 295 296 glMatrixMode(GL_PROJECTION); 297 glPushMatrix(); 298 glLoadIdentity(); 299 300 glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); 301 302 glMatrixMode(GL_MODELVIEW); 303 glPushMatrix(); 304 glLoadIdentity(); 305 306 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 307 } 308 309 310 void GLFont::leave2DMode() 311 { 312 glMatrixMode(GL_MODELVIEW); 313 glPopMatrix(); 314 315 glMatrixMode(GL_PROJECTION); 316 glPopMatrix(); 317 318 glPopAttrib(); 319 } 320 321 322 GLuint GLFont::loadTexture(SDL_Surface *surface, GLfloat *texcoord) 323 { 324 GLuint texture; 325 int w, h; 326 SDL_Surface *image; 327 SDL_Rect area; 328 Uint32 saved_flags; 329 Uint8 saved_alpha; 330 331 /* Use the surface width and height expanded to powers of 2 */ 332 w = power_of_two(surface->w); 333 h = power_of_two(surface->h); 334 texcoord[0] = 0.0f; /* Min X */ 335 texcoord[1] = 0.0f; /* Min Y */ 336 texcoord[2] = (GLfloat)surface->w / w; /* Max X */ 337 texcoord[3] = (GLfloat)surface->h / h; /* Max Y */ 338 339 image = SDL_CreateRGBSurface( 340 SDL_SWSURFACE, 341 w, h, 342 32, 343 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 344 0x000000FF, 345 0x0000FF00, 346 0x00FF0000, 347 0xFF000000 348 #else 349 0xFF000000, 350 0x00FF0000, 351 0x0000FF00, 352 0x000000FF 353 #endif 354 ); 355 if ( image == NULL ) { 356 return 0; 451 452 void GLFont::debug(void) 453 { 454 455 // print the loaded font's style 456 int style; 457 style=TTF_GetFontStyle(this->font); 458 PRINTF(0)("The font style is:"); 459 if(style==TTF_STYLE_NORMAL) 460 PRINTF(0)(" normal"); 461 else { 462 if(style&TTF_STYLE_BOLD) 463 PRINTF(0)(" bold"); 464 if(style&TTF_STYLE_ITALIC) 465 PRINTF(0)(" italic"); 466 if(style&TTF_STYLE_UNDERLINE) 467 PRINTF(0)(" underline"); 357 468 } 358 359 /* Save the alpha blending attributes */ 360 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); 361 saved_alpha = surface->format->alpha; 362 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 363 SDL_SetAlpha(surface, 0, 0); 364 } 365 366 /* Copy the surface into the GL texture image */ 367 area.x = 0; 368 area.y = 0; 369 area.w = surface->w; 370 area.h = surface->h; 371 SDL_BlitSurface(surface, &area, image, &area); 372 373 /* Restore the alpha blending attributes */ 374 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 375 SDL_SetAlpha(surface, saved_flags, saved_alpha); 376 } 377 378 /* Create an OpenGL texture for the image */ 379 glGenTextures(1, &texture); 380 glBindTexture(GL_TEXTURE_2D, texture); 381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 382 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 383 glTexImage2D(GL_TEXTURE_2D, 384 0, 385 GL_RGBA, 386 w, h, 387 0, 388 GL_RGBA, 389 GL_UNSIGNED_BYTE, 390 image->pixels); 391 SDL_FreeSurface(image); /* No longer needed */ 392 393 return texture; 394 } 395 396 397 /* Quick utility function for texture creation */ 398 int GLFont::power_of_two(int input) 399 { 400 int value = 1; 401 402 while ( value < input ) { 403 value <<= 1; 404 } 405 return value; 406 } 469 PRINTF(0)("\n"); 470 471 472 } -
orxonox/branches/textEngine/src/lib/graphics/font/glfont.h
r3682 r3691 8 8 9 9 #include "glincl.h" 10 #include "SDL_ttf.h" 11 12 13 //! A struct for handling glyphs 14 struct glyph 15 { 16 Uint16 character; 17 int minX; 18 int maxX; 19 int minY; 20 int maxY; 21 int width; 22 int height; 23 int bearingX; 24 int bearingY; 25 int advance; 26 }; 27 10 28 11 29 class GLFont 12 30 { 31 32 private: 33 34 35 13 36 public: 14 37 GLFont(); 15 38 GLFont(const char* fontFile); 16 ~GLFont(); 17 39 virtual ~GLFont(); 40 41 static void enableFonts(void); 42 static void disableFonts(void); 18 43 19 44 … … 21 46 void setText(const char* text); 22 47 23 void setStyle(char* style);48 void setStyle(char* renderStyle); 24 49 void setSize(void); 25 50 void setPosition(int x, int y); … … 29 54 30 55 private: 56 // information about the Font 57 TTF_Font* font; 31 58 char* fontFile; 32 59 char* text; 60 unsigned int fontSize; 61 62 // placement in openGL 33 63 GLuint texture; 34 64 int positionX; … … 39 69 40 70 bool init(const char* fontFile); 41 bool loadTexture(); 42 static int powerOfTwo(int input); 71 int getMaxHeight(void); 72 int getMaxAscent(void); 73 int getMaxDescent(void); 74 glyph getGlyphMetrics(Uint16 character); 43 75 44 76 static bool ttfInitialized; … … 47 79 void leave2DMode(void); 48 80 81 static bool checkVersion(void); 82 49 83 GLuint loadTexture(SDL_Surface* surface, GLfloat* texcoord); 50 84 51 static int power_of_two(int input); 85 static int powerOfTwo(int input); 86 87 void debug(void); 52 88 53 89 };
Note: See TracChangeset
for help on using the changeset viewer.