Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3691 in orxonox.OLD


Ignore:
Timestamp:
Mar 31, 2005, 2:14:02 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/textEngine: implemented some basic functions

Location:
orxonox/branches/textEngine
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/textEngine/config.h.in

    r3681 r3691  
    5959#undef HAVE_SDL_SDL_IMAGE_H
    6060
     61/* Define to 1 if you have the <SDL/SDL_ttf.h> header file. */
     62#undef HAVE_SDL_SDL_TTF_H
     63
    6164/* Define to 1 if you have the `sqrt' function. */
    6265#undef HAVE_SQRT
  • orxonox/branches/textEngine/configure

    r3682 r3691  
    50455045 Linux="yes"
    50465046
    5047 CPPFLAGS="-I/usr/X11R6/include"
     5047CPPFLAGS="-I/usr/X11R6/include -I/usr/include/SDL"
    50485048LDFLAGS="-L/usr/lib/opengl/xorg-x11/lib -L/usr/Mesa-6.0.1/lib -L/usr/X11R6/lib $LDFLAGS"
    50495049# checking gl header
     
    61106110# SDL_Image #
    61116111#-----------#
    6112 #if test x$def_sdl_image = xyes; then
    6113 # checking for SDL_image-headers
     6112#if test x$def_sdl_ttf = xyes; then
     6113# checking for SDL_ttf-headers
    61146114
    61156115for ac_header in SDL/SDL_ttf.h
  • orxonox/branches/textEngine/configure.ac

    r3682 r3691  
    291291 Linux="yes"
    292292
    293 CPPFLAGS="-I/usr/X11R6/include"
     293CPPFLAGS="-I/usr/X11R6/include -I/usr/include/SDL"
    294294LDFLAGS="-L/usr/lib/opengl/xorg-x11/lib -L/usr/Mesa-6.0.1/lib -L/usr/X11R6/lib $LDFLAGS"
    295295# checking gl header
  • orxonox/branches/textEngine/src/defs/glincl.h

    r3681 r3691  
    99#define _GLINCL_H
    1010
     11#include <SDL.h>
     12
    1113#ifndef __APPLE__
    12 #include <SDL/SDL.h>
    1314#include <GL/gl.h>
    1415#include <GL/glu.h>
    1516#else
    16 #include <SDL.h>
    1717#include <OpenGL/gl.h>
    1818#include <OpenGL/glu.h>
  • orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc

    r3682 r3691  
    1818   *                                                                  ***
    1919
     20   for some fonts and licenses visit: =http://www.dafont.com/en/font.php=
    2021*/
    2122
     
    4647#include "glfont.h"
    4748
    48 
    49 
    5049#include <stdlib.h>
    5150#include <stdio.h>
    5251#include <string.h>
    5352
    54 #include <SDL/SDL.h>
    55 #include <SDL/SDL_ttf.h>
    56 
    57 #include "glincl.h"
    5853#include "debug.h"
    5954
     
    7469  this->init(fontFile);
    7570}
     71
    7672GLFont::~GLFont()
    7773{
    78  
    79 }
     74  if (this->font)
     75    TTF_CloseFont(this->font);
     76}
     77
     78void 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}
     93void 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
     104bool GLFont::ttfInitialized = false;
     105
    80106
    81107bool GLFont::init(const char* fontFile)
    82108{
    83109  // 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
     122bool GLFont::setFont(const char* fontFile)
     123{
     124  if (!this->fontFile)
    89125    {
    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      }
    92135    }
    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
    101141void GLFont::setText(const char* text)
    102142{
    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*/
     152void 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)
    112158      this->renderStyle |= TTF_STYLE_BOLD;
    113     else if (strncmp(style+i, "i", 1) == 0)
     159    else if (strncmp(renderStyle+i, "i", 1) == 0)
    114160      this->renderStyle |= TTF_STYLE_ITALIC;
    115     else if (strncmp(style+i, "u", 1) == 0)
     161    else if (strncmp(renderStyle+i, "u", 1) == 0)
    116162      this->renderStyle |= TTF_STYLE_UNDERLINE;
    117163
    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
    121170void GLFont::setSize(void)
    122171{
    123172
    124173}
     174
     175/**
     176   \returns the maximum height of the Font, if the font was initialized, 0 otherwise
     177*/
     178int 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*/
     191int 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*/
     204int 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*/
     220glyph 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
    125235void GLFont::setPosition(int x, int y)
    126236{
     
    178288}
    179289
    180 
    181 bool GLFont::loadTexture()
    182 {
     290void 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
     322void GLFont::leave2DMode()
     323{
     324        glMatrixMode(GL_MODELVIEW);
     325        glPopMatrix();
     326
     327        glMatrixMode(GL_PROJECTION);
     328        glPopMatrix();
     329
     330        glPopAttrib();
     331}
     332
     333
     334GLuint GLFont::loadTexture(SDL_Surface *surface, GLfloat *texcoord)
     335{
     336  GLuint texture;
    183337  int w, h;
    184338  SDL_Surface *image;
     
    249403  SDL_FreeSurface(image); /* No longer needed */
    250404 
    251   //  return texture;
    252 }
     405  return texture;
     406}
     407
     408
     409bool 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
    253438
    254439/* Quick utility function for texture creation */
     
    264449
    265450
    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
     452void 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");
    357468  }
    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  
    88
    99#include "glincl.h"
     10#include "SDL_ttf.h"
     11
     12
     13//! A struct for handling glyphs
     14struct 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
    1028
    1129class GLFont
    1230{
     31 
     32 private:
     33
     34   
     35
    1336 public:
    1437  GLFont();
    1538  GLFont(const char* fontFile);
    16   ~GLFont();
    17  
     39  virtual ~GLFont();
     40
     41  static void enableFonts(void);
     42  static void disableFonts(void);
    1843
    1944
     
    2146  void setText(const char* text);
    2247
    23   void setStyle(char* style);
     48  void setStyle(char* renderStyle);
    2449  void setSize(void);
    2550  void setPosition(int x, int y);
     
    2954
    3055 private:
     56  // information about the Font
     57  TTF_Font* font;
    3158  char* fontFile;
    3259  char* text;
     60  unsigned int fontSize;
     61
     62  // placement in openGL
    3363  GLuint texture;
    3464  int positionX;
     
    3969
    4070  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);
    4375
    4476  static bool ttfInitialized;
     
    4779  void leave2DMode(void);
    4880
     81  static bool checkVersion(void);
     82
    4983  GLuint loadTexture(SDL_Surface* surface, GLfloat* texcoord);
    5084
    51   static int power_of_two(int input);
     85  static int powerOfTwo(int input);
     86
     87  void debug(void);
    5288
    5389};
Note: See TracChangeset for help on using the changeset viewer.