Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3458 in orxonox.OLD for orxonox/trunk/src/sfont/glfont.cc


Ignore:
Timestamp:
Mar 5, 2005, 11:49:35 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: some minor changes in the framework, and the font is 25% on the go.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/sfont/glfont.cc

    r3457 r3458  
    4444*/
    4545
    46 /* $Id: glfont.c,v 1.6 2004/01/04 17:41:55 slouken Exp $ */
    47 
    48 /* A simple program to test the text rendering feature of the TTF library */
     46#include "glfont.h"
     47
     48
    4949
    5050#include <stdlib.h>
     
    5656
    5757#include "../glincl.h"
     58#include "../debug.h"
     59
    5860
    5961#define DEFAULT_PTSIZE  18
     
    6163#define NUM_COLORS      256
    6264
    63 static char *Usage =
    64 "Usage: %s [-utf8|-unicode] [-b] [-i] [-u] [-fgcol r,g,b] [-bgcol r,g,b] \
    65 <font>.ttf [ptsize] [text]\n";
    66 
     65
     66
     67GLFont::GLFont()
     68{
     69  this->init("");
     70
     71}
     72GLFont::GLFont(const char* fontFile)
     73{
     74  this->init(fontFile);
     75}
     76GLFont::~GLFont()
     77{
     78 
     79}
     80
     81bool 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
     95bool GLFont::ttfInitialized = false;
     96
     97bool GLFont::setFont(const char* fonFile)
     98{
     99
     100}
     101void GLFont::setText(const char* text)
     102{
     103
     104}
     105
     106void 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}
     121void GLFont::setSize(void)
     122{
     123
     124}
     125void GLFont::setPosition(int x, int y)
     126{
     127
     128}
     129
     130void GLFont::renderText(void)
     131{
     132
     133}
     134void 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
     181bool 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 */
     255int 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/*
     267private:
     268char* fontFile;
     269char* text;
     270int positionX;
     271int positionY;
     272*/
     273
     274
     275
     276
     277char Usage[100] = "orxonox test" ;
    67278void SDL_GL_Enter2DMode()
    68279{
    69         SDL_Surface *screen = SDL_GetVideoSurface();
    70 
    71         /* Note, there may be other things you need to change,
    72            depending on how you have your OpenGL state set up.
    73         */
    74         glPushAttrib(GL_ENABLE_BIT);
    75         glDisable(GL_DEPTH_TEST);
    76         glDisable(GL_CULL_FACE);
    77         glEnable(GL_TEXTURE_2D);
    78 
    79         /* This allows alpha blending of 2D textures with the scene */
    80         glEnable(GL_BLEND);
    81         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    82 
    83         glViewport(0, 0, screen->w, screen->h);
    84 
    85         glMatrixMode(GL_PROJECTION);
    86         glPushMatrix();
    87         glLoadIdentity();
    88 
    89         glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
    90 
    91         glMatrixMode(GL_MODELVIEW);
    92         glPushMatrix();
    93         glLoadIdentity();
    94 
    95         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    96280}
    97281
    98282void SDL_GL_Leave2DMode()
    99283{
    100         glMatrixMode(GL_MODELVIEW);
    101         glPopMatrix();
    102 
    103         glMatrixMode(GL_PROJECTION);
    104         glPopMatrix();
    105 
    106         glPopAttrib();
    107284}
    108285
     
    110287static int power_of_two(int input)
    111288{
    112         int value = 1;
    113 
    114         while ( value < input ) {
    115                 value <<= 1;
    116         }
    117         return value;
    118 }
     289}
     290
     291
    119292
    120293GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
    121294{
    122         GLuint texture;
    123         int w, h;
    124         SDL_Surface *image;
    125         SDL_Rect area;
    126         Uint32 saved_flags;
    127         Uint8  saved_alpha;
    128 
    129         /* Use the surface width and height expanded to powers of 2 */
    130         w = power_of_two(surface->w);
    131         h = power_of_two(surface->h);
    132         texcoord[0] = 0.0f;                     /* Min X */
    133         texcoord[1] = 0.0f;                     /* Min Y */
    134         texcoord[2] = (GLfloat)surface->w / w;  /* Max X */
    135         texcoord[3] = (GLfloat)surface->h / h;  /* Max Y */
    136 
    137         image = SDL_CreateRGBSurface(
    138                         SDL_SWSURFACE,
    139                         w, h,
    140                         32,
    141 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    142                         0x000000FF,
    143                         0x0000FF00,
    144                         0x00FF0000,
    145                         0xFF000000
    146 #else
    147                         0xFF000000,
    148                         0x00FF0000,
    149                         0x0000FF00,
    150                         0x000000FF
    151 #endif
    152                        );
    153         if ( image == NULL ) {
    154                 return 0;
    155         }
    156 
    157         /* Save the alpha blending attributes */
    158         saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
    159         saved_alpha = surface->format->alpha;
    160         if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    161                 SDL_SetAlpha(surface, 0, 0);
    162         }
    163 
    164         /* Copy the surface into the GL texture image */
    165         area.x = 0;
    166         area.y = 0;
    167         area.w = surface->w;
    168         area.h = surface->h;
    169         SDL_BlitSurface(surface, &area, image, &area);
    170 
    171         /* Restore the alpha blending attributes */
    172         if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    173                 SDL_SetAlpha(surface, saved_flags, saved_alpha);
    174         }
    175 
    176         /* Create an OpenGL texture for the image */
    177         glGenTextures(1, &texture);
    178         glBindTexture(GL_TEXTURE_2D, texture);
    179         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    180         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    181         glTexImage2D(GL_TEXTURE_2D,
    182                      0,
    183                      GL_RGBA,
    184                      w, h,
    185                      0,
    186                      GL_RGBA,
    187                      GL_UNSIGNED_BYTE,
    188                      image->pixels);
    189         SDL_FreeSurface(image); /* No longer needed */
    190 
    191         return texture;
     295
    192296}
    193297
     
    223327        dump = 0;
    224328        /* Look for special rendering types */
    225         renderstyle = TTF_STYLE_NORMAL;
    226329        rendertype = RENDER_LATIN1;
    227330        /* Default is black and white */
     
    311414                return(2);
    312415        }
    313         TTF_SetFontStyle(font, renderstyle);
    314416
    315417        if( dump ) {
Note: See TracChangeset for help on using the changeset viewer.