[3478] | 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 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | glfont: An example of using the SDL_ttf library with OpenGL. |
---|
| 24 | Copyright (C) 1997-2004 Sam Lantinga |
---|
| 25 | |
---|
| 26 | This library is free software; you can redistribute it and/or |
---|
| 27 | modify it under the terms of the GNU Library General Public |
---|
| 28 | License as published by the Free Software Foundation; either |
---|
| 29 | version 2 of the License, or (at your option) any later version. |
---|
| 30 | |
---|
| 31 | This library is distributed in the hope that it will be useful, |
---|
| 32 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 33 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 34 | Library General Public License for more details. |
---|
| 35 | |
---|
| 36 | You should have received a copy of the GNU Library General Public |
---|
| 37 | License along with this library; if not, write to the Free |
---|
| 38 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 39 | |
---|
| 40 | The SDL_GL_* functions in this file are available in the public domain. |
---|
| 41 | |
---|
| 42 | Sam Lantinga |
---|
| 43 | slouken@libsdl.org |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | #include "glfont.h" |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | #include <stdlib.h> |
---|
| 51 | #include <stdio.h> |
---|
| 52 | #include <string.h> |
---|
| 53 | |
---|
| 54 | #include <SDL/SDL.h> |
---|
| 55 | #include <SDL/SDL_ttf.h> |
---|
| 56 | |
---|
[3682] | 57 | #include "glincl.h" |
---|
| 58 | #include "debug.h" |
---|
[3478] | 59 | |
---|
| 60 | |
---|
| 61 | #define DEFAULT_PTSIZE 18 |
---|
| 62 | #define DEFAULT_TEXT "orxonox 1234567890" |
---|
| 63 | #define NUM_COLORS 256 |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | GLFont::GLFont() |
---|
| 68 | { |
---|
| 69 | this->init(""); |
---|
| 70 | |
---|
| 71 | } |
---|
| 72 | GLFont::GLFont(const char* fontFile) |
---|
| 73 | { |
---|
| 74 | this->init(fontFile); |
---|
| 75 | } |
---|
| 76 | GLFont::~GLFont() |
---|
| 77 | { |
---|
| 78 | |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | bool GLFont::init(const char* fontFile) |
---|
| 82 | { |
---|
| 83 | // setting default values. |
---|
| 84 | renderStyle = TTF_STYLE_NORMAL; |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | // Initializing lib-TTF if necesary. |
---|
| 88 | if (TTF_WasInit() && TTF_Init() < 0 ) |
---|
| 89 | { |
---|
| 90 | PRINTF(1)("Couldn't initialize TTF: %s\n", SDL_GetError()); |
---|
| 91 | return false; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | bool GLFont::ttfInitialized = false; |
---|
| 96 | |
---|
| 97 | bool GLFont::setFont(const char* fonFile) |
---|
| 98 | { |
---|
| 99 | |
---|
| 100 | } |
---|
| 101 | void GLFont::setText(const char* text) |
---|
| 102 | { |
---|
| 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) |
---|
| 112 | this->renderStyle |= TTF_STYLE_BOLD; |
---|
| 113 | else if (strncmp(style+i, "i", 1) == 0) |
---|
| 114 | this->renderStyle |= TTF_STYLE_ITALIC; |
---|
| 115 | else if (strncmp(style+i, "u", 1) == 0) |
---|
| 116 | this->renderStyle |= TTF_STYLE_UNDERLINE; |
---|
| 117 | |
---|
| 118 | // TTF_SetFontStyle(font, renderstyle); |
---|
| 119 | |
---|
| 120 | } |
---|
| 121 | void GLFont::setSize(void) |
---|
| 122 | { |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | void GLFont::setPosition(int x, int y) |
---|
| 126 | { |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void GLFont::renderText(void) |
---|
| 131 | { |
---|
| 132 | |
---|
| 133 | } |
---|
| 134 | void GLFont::renderText(const char* text, int x, int y) |
---|
| 135 | { |
---|
| 136 | // enter the 2D-Mode |
---|
| 137 | |
---|
| 138 | SDL_Surface *screen = SDL_GetVideoSurface(); |
---|
| 139 | |
---|
| 140 | /* Note, there may be other things you need to change, |
---|
| 141 | depending on how you have your OpenGL state set up. |
---|
| 142 | */ |
---|
| 143 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 144 | glDisable(GL_DEPTH_TEST); |
---|
| 145 | glDisable(GL_CULL_FACE); |
---|
| 146 | glEnable(GL_TEXTURE_2D); |
---|
| 147 | |
---|
| 148 | /* This allows alpha blending of 2D textures with the scene */ |
---|
| 149 | glEnable(GL_BLEND); |
---|
| 150 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
| 151 | |
---|
| 152 | glViewport(0, 0, screen->w, screen->h); |
---|
| 153 | |
---|
| 154 | glMatrixMode(GL_PROJECTION); |
---|
| 155 | glPushMatrix(); |
---|
| 156 | glLoadIdentity(); |
---|
| 157 | |
---|
| 158 | glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); |
---|
| 159 | |
---|
| 160 | glMatrixMode(GL_MODELVIEW); |
---|
| 161 | glPushMatrix(); |
---|
| 162 | glLoadIdentity(); |
---|
| 163 | |
---|
| 164 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | // Leave the 2D-Mode |
---|
| 171 | glMatrixMode(GL_MODELVIEW); |
---|
| 172 | glPopMatrix(); |
---|
| 173 | |
---|
| 174 | glMatrixMode(GL_PROJECTION); |
---|
| 175 | glPopMatrix(); |
---|
| 176 | |
---|
| 177 | glPopAttrib(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | bool GLFont::loadTexture() |
---|
| 182 | { |
---|
| 183 | int w, h; |
---|
| 184 | SDL_Surface *image; |
---|
| 185 | SDL_Rect area; |
---|
| 186 | Uint32 saved_flags; |
---|
| 187 | Uint8 saved_alpha; |
---|
| 188 | |
---|
| 189 | /* Use the surface width and height expanded to powers of 2 */ |
---|
| 190 | w = powerOfTwo(surface->w); |
---|
| 191 | h = powerOfTwo(surface->h); |
---|
| 192 | texcoord[0] = 0.0f; /* Min X */ |
---|
| 193 | texcoord[1] = 0.0f; /* Min Y */ |
---|
| 194 | texcoord[2] = (GLfloat)surface->w / w; /* Max X */ |
---|
| 195 | texcoord[3] = (GLfloat)surface->h / h; /* Max Y */ |
---|
| 196 | |
---|
| 197 | image = SDL_CreateRGBSurface( |
---|
| 198 | SDL_SWSURFACE, |
---|
| 199 | w, h, |
---|
| 200 | 32, |
---|
| 201 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
| 202 | 0x000000FF, |
---|
| 203 | 0x0000FF00, |
---|
| 204 | 0x00FF0000, |
---|
| 205 | 0xFF000000 |
---|
| 206 | #else |
---|
| 207 | 0xFF000000, |
---|
| 208 | 0x00FF0000, |
---|
| 209 | 0x0000FF00, |
---|
| 210 | 0x000000FF |
---|
| 211 | #endif |
---|
| 212 | ); |
---|
| 213 | if ( image == NULL ) { |
---|
| 214 | return 0; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /* Save the alpha blending attributes */ |
---|
| 218 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
| 219 | saved_alpha = surface->format->alpha; |
---|
| 220 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 221 | SDL_SetAlpha(surface, 0, 0); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /* Copy the surface into the GL texture image */ |
---|
| 225 | area.x = 0; |
---|
| 226 | area.y = 0; |
---|
| 227 | area.w = surface->w; |
---|
| 228 | area.h = surface->h; |
---|
| 229 | SDL_BlitSurface(surface, &area, image, &area); |
---|
| 230 | |
---|
| 231 | /* Restore the alpha blending attributes */ |
---|
| 232 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 233 | SDL_SetAlpha(surface, saved_flags, saved_alpha); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /* Create an OpenGL texture for the image */ |
---|
| 237 | glGenTextures(1, &texture); |
---|
| 238 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
| 239 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
| 240 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
| 241 | glTexImage2D(GL_TEXTURE_2D, |
---|
| 242 | 0, |
---|
| 243 | GL_RGBA, |
---|
| 244 | w, h, |
---|
| 245 | 0, |
---|
| 246 | GL_RGBA, |
---|
| 247 | GL_UNSIGNED_BYTE, |
---|
| 248 | image->pixels); |
---|
| 249 | SDL_FreeSurface(image); /* No longer needed */ |
---|
| 250 | |
---|
| 251 | // return texture; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /* Quick utility function for texture creation */ |
---|
| 255 | int GLFont::powerOfTwo(int input) |
---|
| 256 | { |
---|
| 257 | int value = 1; |
---|
| 258 | |
---|
| 259 | while ( value < input ) { |
---|
| 260 | value <<= 1; |
---|
| 261 | } |
---|
| 262 | return value; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
| 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" ; |
---|
[3682] | 278 | void GLFont::enter2DMode() |
---|
[3478] | 279 | { |
---|
[3682] | 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); |
---|
[3478] | 307 | } |
---|
| 308 | |
---|
[3682] | 309 | |
---|
| 310 | void GLFont::leave2DMode() |
---|
[3478] | 311 | { |
---|
[3682] | 312 | glMatrixMode(GL_MODELVIEW); |
---|
| 313 | glPopMatrix(); |
---|
[3478] | 314 | |
---|
[3682] | 315 | glMatrixMode(GL_PROJECTION); |
---|
| 316 | glPopMatrix(); |
---|
| 317 | |
---|
| 318 | glPopAttrib(); |
---|
[3478] | 319 | } |
---|
| 320 | |
---|
| 321 | |
---|
[3682] | 322 | GLuint GLFont::loadTexture(SDL_Surface *surface, GLfloat *texcoord) |
---|
[3478] | 323 | { |
---|
[3682] | 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; |
---|
| 357 | } |
---|
| 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; |
---|
[3478] | 394 | } |
---|
| 395 | |
---|
[3682] | 396 | |
---|
| 397 | /* Quick utility function for texture creation */ |
---|
| 398 | int GLFont::power_of_two(int input) |
---|
[3478] | 399 | { |
---|
[3682] | 400 | int value = 1; |
---|
| 401 | |
---|
| 402 | while ( value < input ) { |
---|
| 403 | value <<= 1; |
---|
| 404 | } |
---|
| 405 | return value; |
---|
[3478] | 406 | } |
---|