| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | |
|---|
| 15 | *** |
|---|
| 16 | * This file is extended to the needs of the orxonox-project. * |
|---|
| 17 | * the Copyright of the original file is below this copyright * |
|---|
| 18 | * *** |
|---|
| 19 | |
|---|
| 20 | for some fonts and licenses visit: =http://www.dafont.com/en/font.php= |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | /* |
|---|
| 24 | glfont: An example of using the SDL_ttf library with OpenGL. |
|---|
| 25 | Copyright (C) 1997-2004 Sam Lantinga |
|---|
| 26 | |
|---|
| 27 | This library is free software; you can redistribute it and/or |
|---|
| 28 | modify it under the terms of the GNU Library General Public |
|---|
| 29 | License as published by the Free Software Foundation; either |
|---|
| 30 | version 2 of the License, or (at your option) any later version. |
|---|
| 31 | |
|---|
| 32 | This library is distributed in the hope that it will be useful, |
|---|
| 33 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 34 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 35 | Library General Public License for more details. |
|---|
| 36 | |
|---|
| 37 | You should have received a copy of the GNU Library General Public |
|---|
| 38 | License along with this library; if not, write to the Free |
|---|
| 39 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 40 | |
|---|
| 41 | The SDL_GL_* functions in this file are available in the public domain. |
|---|
| 42 | |
|---|
| 43 | Sam Lantinga |
|---|
| 44 | slouken@libsdl.org |
|---|
| 45 | */ |
|---|
| 46 | |
|---|
| 47 | #include "glfont.h" |
|---|
| 48 | |
|---|
| 49 | #include <stdlib.h> |
|---|
| 50 | #include <stdio.h> |
|---|
| 51 | #include <string.h> |
|---|
| 52 | |
|---|
| 53 | #include "debug.h" |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | #define DEFAULT_PTSIZE 18 |
|---|
| 57 | #define DEFAULT_TEXT "orxonox 1234567890" |
|---|
| 58 | #define NUM_COLORS 256 |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | GLFont::GLFont() |
|---|
| 63 | { |
|---|
| 64 | this->init(""); |
|---|
| 65 | |
|---|
| 66 | } |
|---|
| 67 | GLFont::GLFont(const char* fontFile) |
|---|
| 68 | { |
|---|
| 69 | this->init(fontFile); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | GLFont::~GLFont() |
|---|
| 73 | { |
|---|
| 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 | |
|---|
| 106 | |
|---|
| 107 | bool GLFont::init(const char* fontFile) |
|---|
| 108 | { |
|---|
| 109 | // setting default values. |
|---|
| 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) |
|---|
| 125 | { |
|---|
| 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 | } |
|---|
| 135 | } |
|---|
| 136 | else |
|---|
| 137 | PRINTF(2)("Font already initialized, unable to change it now.\n"); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | void GLFont::setText(const char* text) |
|---|
| 142 | { |
|---|
| 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) |
|---|
| 158 | this->renderStyle |= TTF_STYLE_BOLD; |
|---|
| 159 | else if (strncmp(renderStyle+i, "i", 1) == 0) |
|---|
| 160 | this->renderStyle |= TTF_STYLE_ITALIC; |
|---|
| 161 | else if (strncmp(renderStyle+i, "u", 1) == 0) |
|---|
| 162 | this->renderStyle |= TTF_STYLE_UNDERLINE; |
|---|
| 163 | |
|---|
| 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 | |
|---|
| 170 | void GLFont::setSize(void) |
|---|
| 171 | { |
|---|
| 172 | |
|---|
| 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 | |
|---|
| 235 | void GLFont::setPosition(int x, int y) |
|---|
| 236 | { |
|---|
| 237 | |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | void GLFont::renderText(void) |
|---|
| 241 | { |
|---|
| 242 | |
|---|
| 243 | } |
|---|
| 244 | void GLFont::renderText(const char* text, int x, int y) |
|---|
| 245 | { |
|---|
| 246 | // enter the 2D-Mode |
|---|
| 247 | |
|---|
| 248 | SDL_Surface *screen = SDL_GetVideoSurface(); |
|---|
| 249 | |
|---|
| 250 | /* Note, there may be other things you need to change, |
|---|
| 251 | depending on how you have your OpenGL state set up. |
|---|
| 252 | */ |
|---|
| 253 | glPushAttrib(GL_ENABLE_BIT); |
|---|
| 254 | glDisable(GL_DEPTH_TEST); |
|---|
| 255 | glDisable(GL_CULL_FACE); |
|---|
| 256 | glEnable(GL_TEXTURE_2D); |
|---|
| 257 | |
|---|
| 258 | /* This allows alpha blending of 2D textures with the scene */ |
|---|
| 259 | glEnable(GL_BLEND); |
|---|
| 260 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|---|
| 261 | |
|---|
| 262 | glViewport(0, 0, screen->w, screen->h); |
|---|
| 263 | |
|---|
| 264 | glMatrixMode(GL_PROJECTION); |
|---|
| 265 | glPushMatrix(); |
|---|
| 266 | glLoadIdentity(); |
|---|
| 267 | |
|---|
| 268 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
|---|
| 269 | |
|---|
| 270 | glMatrixMode(GL_MODELVIEW); |
|---|
| 271 | glPushMatrix(); |
|---|
| 272 | glLoadIdentity(); |
|---|
| 273 | |
|---|
| 274 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | // Leave the 2D-Mode |
|---|
| 281 | glMatrixMode(GL_MODELVIEW); |
|---|
| 282 | glPopMatrix(); |
|---|
| 283 | |
|---|
| 284 | glMatrixMode(GL_PROJECTION); |
|---|
| 285 | glPopMatrix(); |
|---|
| 286 | |
|---|
| 287 | glPopAttrib(); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 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; |
|---|
| 337 | int w, h; |
|---|
| 338 | SDL_Surface *image; |
|---|
| 339 | SDL_Rect area; |
|---|
| 340 | Uint32 saved_flags; |
|---|
| 341 | Uint8 saved_alpha; |
|---|
| 342 | |
|---|
| 343 | /* Use the surface width and height expanded to powers of 2 */ |
|---|
| 344 | w = powerOfTwo(surface->w); |
|---|
| 345 | h = powerOfTwo(surface->h); |
|---|
| 346 | texcoord[0] = 0.0f; /* Min X */ |
|---|
| 347 | texcoord[1] = 0.0f; /* Min Y */ |
|---|
| 348 | texcoord[2] = (GLfloat)surface->w / w; /* Max X */ |
|---|
| 349 | texcoord[3] = (GLfloat)surface->h / h; /* Max Y */ |
|---|
| 350 | |
|---|
| 351 | image = SDL_CreateRGBSurface( |
|---|
| 352 | SDL_SWSURFACE, |
|---|
| 353 | w, h, |
|---|
| 354 | 32, |
|---|
| 355 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
|---|
| 356 | 0x000000FF, |
|---|
| 357 | 0x0000FF00, |
|---|
| 358 | 0x00FF0000, |
|---|
| 359 | 0xFF000000 |
|---|
| 360 | #else |
|---|
| 361 | 0xFF000000, |
|---|
| 362 | 0x00FF0000, |
|---|
| 363 | 0x0000FF00, |
|---|
| 364 | 0x000000FF |
|---|
| 365 | #endif |
|---|
| 366 | ); |
|---|
| 367 | if ( image == NULL ) { |
|---|
| 368 | return 0; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | /* Save the alpha blending attributes */ |
|---|
| 372 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
|---|
| 373 | saved_alpha = surface->format->alpha; |
|---|
| 374 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
|---|
| 375 | SDL_SetAlpha(surface, 0, 0); |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | /* Copy the surface into the GL texture image */ |
|---|
| 379 | area.x = 0; |
|---|
| 380 | area.y = 0; |
|---|
| 381 | area.w = surface->w; |
|---|
| 382 | area.h = surface->h; |
|---|
| 383 | SDL_BlitSurface(surface, &area, image, &area); |
|---|
| 384 | |
|---|
| 385 | /* Restore the alpha blending attributes */ |
|---|
| 386 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
|---|
| 387 | SDL_SetAlpha(surface, saved_flags, saved_alpha); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | /* Create an OpenGL texture for the image */ |
|---|
| 391 | glGenTextures(1, &texture); |
|---|
| 392 | glBindTexture(GL_TEXTURE_2D, texture); |
|---|
| 393 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|---|
| 394 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|---|
| 395 | glTexImage2D(GL_TEXTURE_2D, |
|---|
| 396 | 0, |
|---|
| 397 | GL_RGBA, |
|---|
| 398 | w, h, |
|---|
| 399 | 0, |
|---|
| 400 | GL_RGBA, |
|---|
| 401 | GL_UNSIGNED_BYTE, |
|---|
| 402 | image->pixels); |
|---|
| 403 | SDL_FreeSurface(image); /* No longer needed */ |
|---|
| 404 | |
|---|
| 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 | |
|---|
| 438 | |
|---|
| 439 | /* Quick utility function for texture creation */ |
|---|
| 440 | int GLFont::powerOfTwo(int input) |
|---|
| 441 | { |
|---|
| 442 | int value = 1; |
|---|
| 443 | |
|---|
| 444 | while ( value < input ) { |
|---|
| 445 | value <<= 1; |
|---|
| 446 | } |
|---|
| 447 | return value; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 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"); |
|---|
| 468 | } |
|---|
| 469 | PRINTF(0)("\n"); |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | } |
|---|