Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 30, 2005, 9:14:35 PM (19 years ago)
Author:
bensch
Message:

orxonox/branches/textEngine: configure.ac adapded, and some minor changes at glfont

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc

    r3681 r3682  
    5555#include <SDL/SDL_ttf.h>
    5656
    57 #include "../glincl.h"
    58 #include "../debug.h"
     57#include "glincl.h"
     58#include "debug.h"
    5959
    6060
     
    276276
    277277char Usage[100] = "orxonox test" ;
    278 void SDL_GL_Enter2DMode()
    279 {
    280 }
    281 
    282 void SDL_GL_Leave2DMode()
    283 {
    284 }
     278void 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
     310void GLFont::leave2DMode()
     311{
     312        glMatrixMode(GL_MODELVIEW);
     313        glPopMatrix();
     314
     315        glMatrixMode(GL_PROJECTION);
     316        glPopMatrix();
     317
     318        glPopAttrib();
     319}
     320
     321
     322GLuint 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;
     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;
     394}
     395
    285396
    286397/* Quick utility function for texture creation */
    287 static int power_of_two(int input)
    288 {
    289 }
    290 
    291 
    292 
    293 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
    294 {
    295 
    296 }
    297 
    298 int setup(int argc, char *argv[])
    299 {
    300         char *argv0 = argv[0];
    301         SDL_Surface *screen;
    302         TTF_Font *font;
    303         SDL_Surface *text;
    304         int ptsize;
    305         int i, done;
    306         SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
    307         SDL_Color black = { 0x00, 0x00, 0x00, 0 };
    308         SDL_Color *forecol;
    309         SDL_Color *backcol;
    310         GLenum gl_error;
    311         GLuint texture;
    312         int x, y, w, h;
    313         GLfloat texcoord[4];
    314         GLfloat texMinX, texMinY;
    315         GLfloat texMaxX, texMaxY;
    316 
    317         int renderstyle;
    318         int dump;
    319         enum {
    320                 RENDER_LATIN1,
    321                 RENDER_UTF8,
    322                 RENDER_UNICODE
    323         } rendertype;
    324         char *message;
    325 
    326         /* Look for special execution mode */
    327         dump = 0;
    328         /* Look for special rendering types */
    329         rendertype = RENDER_LATIN1;
    330         /* Default is black and white */
    331         forecol = &black;
    332         backcol = &white;
    333         for ( i=1; argv[i] && argv[i][0] == '-'; ++i ) {
    334                 if ( strcmp(argv[i], "-utf8") == 0 ) {
    335                         rendertype = RENDER_UTF8;
    336                 } else
    337                 if ( strcmp(argv[i], "-unicode") == 0 ) {
    338                         rendertype = RENDER_UNICODE;
    339                 } else
    340                 if ( strcmp(argv[i], "-b") == 0 ) {
    341                         renderstyle |= TTF_STYLE_BOLD;
    342                 } else
    343                 if ( strcmp(argv[i], "-i") == 0 ) {
    344                         renderstyle |= TTF_STYLE_ITALIC;
    345                 } else
    346                 if ( strcmp(argv[i], "-u") == 0 ) {
    347                         renderstyle |= TTF_STYLE_UNDERLINE;
    348                 } else
    349                 if ( strcmp(argv[i], "-dump") == 0 ) {
    350                         dump = 1;
    351                 } else
    352                 if ( strcmp(argv[i], "-fgcol") == 0 ) {
    353                         int r, g, b;
    354                         if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
    355                                 fprintf(stderr, Usage, argv0);
    356                                 return(1);
    357                         }
    358                         forecol->r = (Uint8)r;
    359                         forecol->g = (Uint8)g;
    360                         forecol->b = (Uint8)b;
    361                 } else
    362                 if ( strcmp(argv[i], "-bgcol") == 0 ) {
    363                         int r, g, b;
    364                         if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
    365                                 fprintf(stderr, Usage, argv0);
    366                                 return(1);
    367                         }
    368                         backcol->r = (Uint8)r;
    369                         backcol->g = (Uint8)g;
    370                         backcol->b = (Uint8)b;
    371                 } else {
    372                         fprintf(stderr, Usage, argv0);
    373                         return(1);
    374                 }
    375         }
    376         argv += i;
    377         argc -= i;
    378 
    379         /* Check usage */
    380         if ( ! argv[0] ) {
    381                 fprintf(stderr, Usage, argv0);
    382                 return(1);
    383         }
    384 
    385         /* Initialize SDL */
    386         if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    387                 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    388                 return(2);
    389         }
    390         atexit(SDL_Quit);
    391 
    392         /* Initialize the TTF library */
    393         if ( TTF_Init() < 0 ) {
    394                 fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
    395                 return(2);
    396         }
    397         atexit(TTF_Quit);
    398 
    399         /* Open the font file with the requested point size */
    400         ptsize = 0;
    401         if ( argc > 1 ) {
    402                 ptsize = atoi(argv[1]);
    403         }
    404         if ( ptsize == 0 ) {
    405                 i = 2;
    406                 ptsize = DEFAULT_PTSIZE;
    407         } else {
    408                 i = 3;
    409         }
    410         font = TTF_OpenFont(argv[0], ptsize);
    411         if ( font == NULL ) {
    412                 fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",
    413                                         ptsize, argv[0], SDL_GetError());
    414                 return(2);
    415         }
    416 
    417         if( dump ) {
    418 
    419                 for( i = 48; i < 123; i++ ) {
    420                         SDL_Surface* glyph = NULL;
    421 
    422                         glyph = TTF_RenderGlyph_Shaded( font, i, *forecol, *backcol );
    423 
    424                         if( glyph ) {
    425                                 char outname[64];
    426                                 sprintf( outname, "glyph-%d.bmp", i );
    427                                 SDL_SaveBMP( glyph, outname );
    428                         }
    429 
    430                 }
    431 
    432                 return( 0 );
    433         }
    434 
    435         /* Set a 640x480 video mode */
    436         screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
    437         if ( screen == NULL ) {
    438                 fprintf(stderr, "Couldn't set 640x480 OpenGL mode: %s\n",
    439                                                         SDL_GetError());
    440                 return(2);
    441         }
    442 
    443         /* Render and center the message */
    444         if ( argc > 2 ) {
    445                 message = argv[2];
    446         } else {
    447                 message = DEFAULT_TEXT;
    448         }
    449         switch (rendertype) {
    450             case RENDER_LATIN1:
    451                 text = TTF_RenderText_Blended(font, message, *forecol);
    452                 break;
    453 
    454             case RENDER_UTF8:
    455                 text = TTF_RenderUTF8_Blended(font, message, *forecol);
    456                 break;
    457 
    458             case RENDER_UNICODE:
    459                 {
    460                         /* This doesn't actually work because you can't pass
    461                            UNICODE text in via command line, AFAIK, but...
    462                          */
    463                         Uint16 unicode_text[BUFSIZ];
    464                         int index;
    465                         for ( index = 0; (message[0] || message[1]); ++index ) {
    466                                 unicode_text[index]  = ((Uint8 *)message)[0];
    467                                 unicode_text[index] <<= 8;
    468                                 unicode_text[index] |= ((Uint8 *)message)[1];
    469                                 message += 2;
    470                         }
    471                         text = TTF_RenderUNICODE_Blended(font,
    472                                         unicode_text, *forecol);
    473                 }
    474                 break;
    475             default:
    476                 text = NULL; /* This shouldn't happen */
    477                 break;
    478         }
    479         if ( text == NULL ) {
    480                 fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError());
    481                 TTF_CloseFont(font);
    482                 return(2);
    483         }
    484         x = (screen->w - text->w)/2;
    485         y = (screen->h - text->h)/2;
    486         w = text->w;
    487         h = text->h;
    488         printf("Font is generally %d big, and string is %hd big\n",
    489                                                 TTF_FontHeight(font), text->h);
    490 
    491         /* Convert the text into an OpenGL texture */
    492         glGetError();
    493         texture = SDL_GL_LoadTexture(text, texcoord);
    494         if ( (gl_error = glGetError()) != GL_NO_ERROR ) {
    495                 /* If this failed, the text may exceed texture size limits */
    496                 printf("Warning: Couldn't create texture: 0x%x\n", gl_error);
    497         }
    498 
    499         /* Make texture coordinates easy to understand */
    500         texMinX = texcoord[0];
    501         texMinY = texcoord[1];
    502         texMaxX = texcoord[2];
    503         texMaxY = texcoord[3];
    504 
    505         /* We don't need the original text surface anymore */
    506         SDL_FreeSurface(text);
    507 
    508         /* Initialize the GL state */
    509         glViewport( 0, 0, screen->w, screen->h );
    510         glMatrixMode( GL_PROJECTION );
    511         glLoadIdentity( );
    512 
    513         glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );
    514 
    515         glMatrixMode( GL_MODELVIEW );
    516         glLoadIdentity( );
    517 
    518         glEnable(GL_DEPTH_TEST);
    519 
    520         glDepthFunc(GL_LESS);
    521 
    522         glShadeModel(GL_SMOOTH);
    523 
    524         /* Wait for a keystroke, and blit text on mouse press */
    525         done = 0;
    526         while ( ! done ) {
    527 
    528           /* Show the text on the screen */
    529           SDL_GL_Enter2DMode();
    530           glBindTexture(GL_TEXTURE_2D, texture);
    531           glBegin(GL_QUADS);
    532           glTexCoord2f(texMinX, texMinY); glVertex2i(x,   y  );
    533           glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y  );
    534           glTexCoord2f(texMinX, texMaxY); glVertex2i(x,   y+h);
    535           glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
    536           glEnd();
    537           SDL_GL_Leave2DMode();
    538          
    539           /* Swap the buffers so everything is visible */
    540           SDL_GL_SwapBuffers( );
    541         }
    542         TTF_CloseFont(font);
    543         return(0);
    544 }
    545 
     398int GLFont::power_of_two(int input)
     399{
     400  int value = 1;
     401 
     402  while ( value < input ) {
     403    value <<= 1;
     404  }
     405  return value;
     406}
Note: See TracChangeset for help on using the changeset viewer.