| 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 main difference to the version of Marius is, that textures get * | 
|---|
| 18 |    * loaded by orxonox Texture-class, and that the comments are set up, * | 
|---|
| 19 |    * in doxygen style                                                   * | 
|---|
| 20 |    * the Copyright of the original file is below this copyright, and we * | 
|---|
| 21 |    * hope not to offend Marius through this.                            * | 
|---|
| 22 |                                                                       *** | 
|---|
| 23 |  | 
|---|
| 24 | */ | 
|---|
| 25 |  | 
|---|
| 26 | /*************************************************************************** | 
|---|
| 27 |                           cone3dfont.cpp  -  description | 
|---|
| 28 |                              ------------------- | 
|---|
| 29 |     copyright            : (C) 2001 by Marius Andra aka Cone3D | 
|---|
| 30 |     email                : marius@hot.ee | 
|---|
| 31 |     ICQ                  : 43999510 | 
|---|
| 32 |  ***************************************************************************/ | 
|---|
| 33 |  | 
|---|
| 34 | /*************************************************************************** | 
|---|
| 35 |  *                                                                         * | 
|---|
| 36 |  *   This program is free software; you can redistribute it and/or modify  * | 
|---|
| 37 |  *   it under the terms of the GNU General Public License as published by  * | 
|---|
| 38 |  *   the Free Software Foundation; either version 2 of the License, or     * | 
|---|
| 39 |  *   (at your option) any later version.                                   * | 
|---|
| 40 |  *                                                                         * | 
|---|
| 41 |  ***************************************************************************/ | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | #include "fontset.h" | 
|---|
| 45 | #include <stdarg.h> | 
|---|
| 46 |  | 
|---|
| 47 | #include "texture.h" | 
|---|
| 48 | #include <string.h> | 
|---|
| 49 |  | 
|---|
| 50 | /**  | 
|---|
| 51 |     \brief initializes a new fontset | 
|---|
| 52 | */ | 
|---|
| 53 | FontSet::FontSet() | 
|---|
| 54 | { | 
|---|
| 55 |   this->sizex=1.0f; | 
|---|
| 56 |   this->sizey=1.0f; | 
|---|
| 57 |   this->font = NULL; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | /**  | 
|---|
| 61 |     \brief initializes a new fontset with a texture for the font | 
|---|
| 62 |     \param fontFile The fontfile to load | 
|---|
| 63 | */ | 
|---|
| 64 | FontSet::FontSet(char* fontFile) | 
|---|
| 65 | { | 
|---|
| 66 |   this->sizex=1.0f; | 
|---|
| 67 |   this->sizey=1.0f; | 
|---|
| 68 |   this->font = NULL; | 
|---|
| 69 |    | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | /** | 
|---|
| 73 |    \brief deletes the Fontset by freeing all allocated space | 
|---|
| 74 |    and then deleting the Texture. | 
|---|
| 75 | */ | 
|---|
| 76 | FontSet::~FontSet() | 
|---|
| 77 | { | 
|---|
| 78 |   killFont(); | 
|---|
| 79 |   // delete the texture; | 
|---|
| 80 |   delete font; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 |    \brief sets the size of the font | 
|---|
| 85 |    \param x the width of the font | 
|---|
| 86 |    \param y the height of the font | 
|---|
| 87 | */ | 
|---|
| 88 | int FontSet::setSize(float x, float y) | 
|---|
| 89 | { | 
|---|
| 90 |   sizex=x; | 
|---|
| 91 |   sizey=y; | 
|---|
| 92 |    | 
|---|
| 93 |   return 1; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | /** | 
|---|
| 97 |    \brief builds the fontset | 
|---|
| 98 |    \param file the name of the file to get the fontset from. | 
|---|
| 99 | */ | 
|---|
| 100 | int FontSet::buildFont(char *file) | 
|---|
| 101 | { | 
|---|
| 102 |   int loop1; | 
|---|
| 103 |   float cx, cy; | 
|---|
| 104 |    | 
|---|
| 105 |   if (!font) | 
|---|
| 106 |     delete font; | 
|---|
| 107 |   font = new Texture(); | 
|---|
| 108 |   font->loadImage(file); | 
|---|
| 109 |    | 
|---|
| 110 |   base=glGenLists(256);                          // Creating 256 Display Lists | 
|---|
| 111 |   glBindTexture(GL_TEXTURE_2D, font->getTexture()); // Select Our Font Texture | 
|---|
| 112 |   for (loop1=0; loop1<256; loop1++)              // Loop Through All 256 Lists | 
|---|
| 113 |     { | 
|---|
| 114 |       cx=(float)(loop1%16)/16.0f;                  // X Position Of Current Character | 
|---|
| 115 |       cy=(float)(loop1/16)/16.0f;                  // Y Position Of Current Character | 
|---|
| 116 |        | 
|---|
| 117 |       glNewList(base+loop1,GL_COMPILE);            // Start Building A List | 
|---|
| 118 |       glBegin(GL_QUADS);                           // Use A Quad For Each Character | 
|---|
| 119 |       glTexCoord2f(cx,1.0f-cy-0.0625f);          // Texture Coord (Bottom Left) | 
|---|
| 120 |       glVertex2d(0,16);                          // Vertex Coord (Bottom Left) | 
|---|
| 121 |       glTexCoord2f(cx+0.0625f,1.0f-cy-0.0625f);  // Texture Coord (Bottom Right) | 
|---|
| 122 |       glVertex2i(16,16);                         // Vertex Coord (Bottom Right) | 
|---|
| 123 |       glTexCoord2f(cx+0.0625f,1.0f-cy-0.001f);   // Texture Coord (Top Right) | 
|---|
| 124 |       glVertex2i(16,0);                          // Vertex Coord (Top Right) | 
|---|
| 125 |       glTexCoord2f(cx,1.0f-cy-0.001f);           // Texture Coord (Top Left) | 
|---|
| 126 |       glVertex2i(0,0);                           // Vertex Coord (Top Left) | 
|---|
| 127 |       glEnd();                                     // Done Building Our Quad (Character) | 
|---|
| 128 |       glTranslated(12,0,0);                        // Move To The Right Of The Character | 
|---|
| 129 |       glEndList();                                 // Done Building The Display List | 
|---|
| 130 |     }                                              // Loop Until All 256 Are Built | 
|---|
| 131 |   return 1; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 |    \brief deletes the display lists. | 
|---|
| 136 | */ | 
|---|
| 137 | int FontSet::killFont(void)                          // Delete The Font From Memory | 
|---|
| 138 | { | 
|---|
| 139 |   glDeleteLists(base,256);                       // Delete All 256 Display Lists | 
|---|
| 140 |   return 1; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /** | 
|---|
| 144 |    \brief prints out some text | 
|---|
| 145 |    \param x most left coordinate of the text. | 
|---|
| 146 |    \param y the top coordinate of the text | 
|---|
| 147 |    \param type Type of text | 
|---|
| 148 |    \param fmt The text to display | 
|---|
| 149 |    \param ... more text | 
|---|
| 150 | */ | 
|---|
| 151 | int FontSet::printText(int x, int y, char type, char *fmt,...)// Where The Printing Happens | 
|---|
| 152 | { | 
|---|
| 153 |   char    text[1024];                            // Holds Our String | 
|---|
| 154 |   int blendOn,scissorOn,textureOn,lightOn;  // Holds the previous GL settings | 
|---|
| 155 |   int depthOn,matrixMode,screenStats[4],blendSrc,blendDst; | 
|---|
| 156 |   char typ=type; | 
|---|
| 157 |  | 
|---|
| 158 |   va_list ap;                                    // Pointer To List Of Arguments | 
|---|
| 159 |  | 
|---|
| 160 |   if (fmt == NULL)                               // If There's No Text | 
|---|
| 161 |     return 1;                                      // Do Nothing | 
|---|
| 162 |    | 
|---|
| 163 |   va_start(ap, fmt);                             // Parses The String For Variables | 
|---|
| 164 |   vsprintf(text, fmt, ap);                       // And Converts Symbols To Actual Numbers | 
|---|
| 165 |   va_end(ap);                                    // Results Are Stored In Text | 
|---|
| 166 |    | 
|---|
| 167 |   if (type>3)                                     // Did User Choose An Invalid Character Set? | 
|---|
| 168 |     type=3;                                       // If So, Select Set 2 (Italic) | 
|---|
| 169 |    | 
|---|
| 170 |   textureOn = glIsEnabled(GL_TEXTURE_2D); // Were textures enabled? | 
|---|
| 171 |   depthOn = glIsEnabled(GL_DEPTH_TEST);  // Was GL_DEPTH_TEST enabled? | 
|---|
| 172 |   lightOn = glIsEnabled(GL_LIGHTING); // Was GL_LIGHTING enabled? | 
|---|
| 173 |   scissorOn = glIsEnabled(GL_SCISSOR_TEST);  // etc. | 
|---|
| 174 |   glGetIntegerv(GL_MATRIX_MODE, &matrixMode); | 
|---|
| 175 |   glGetIntegerv(GL_VIEWPORT, screenStats); | 
|---|
| 176 |   blendOn= glIsEnabled(GL_BLEND); | 
|---|
| 177 |   glGetIntegerv(GL_BLEND_SRC, &blendSrc); | 
|---|
| 178 |   glGetIntegerv(GL_BLEND_DST, &blendDst); | 
|---|
| 179 |    | 
|---|
| 180 |   if (depthOn) glDisable(GL_DEPTH_TEST);  // If they were enabled/disabled | 
|---|
| 181 |   if (!textureOn) glEnable(GL_TEXTURE_2D); // then enable/disable them | 
|---|
| 182 |   if (!blendOn) glEnable(GL_BLEND); | 
|---|
| 183 |   if (!scissorOn) glEnable(GL_SCISSOR_TEST); | 
|---|
| 184 |   if (lightOn) glDisable(GL_LIGHTING); | 
|---|
| 185 |   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the correct blending mode | 
|---|
| 186 |    | 
|---|
| 187 |   glMatrixMode(GL_PROJECTION); // and initalize Ortho mode | 
|---|
| 188 |   glPushMatrix(); | 
|---|
| 189 |   glLoadIdentity(); | 
|---|
| 190 |   glOrtho(0.0f,screenStats[2],screenStats[3],0.0f,-1.0f,1.0f); | 
|---|
| 191 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 192 |   glPushMatrix(); | 
|---|
| 193 |   glBindTexture(GL_TEXTURE_2D, font->getTexture()); | 
|---|
| 194 |  | 
|---|
| 195 |   if(type>1) typ=typ-2; | 
|---|
| 196 |   glLoadIdentity(); | 
|---|
| 197 |   glTranslated(x,y,0); | 
|---|
| 198 |   glListBase(base-32+(128*typ)); | 
|---|
| 199 |   glScalef(sizex,sizey,1.0f); | 
|---|
| 200 |   glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen | 
|---|
| 201 |    | 
|---|
| 202 |   if(type>1) | 
|---|
| 203 |     { | 
|---|
| 204 |       glLoadIdentity(); | 
|---|
| 205 |       glTranslated(x+1,y,0); | 
|---|
| 206 |       glListBase(base-32+(128*(type-2))); | 
|---|
| 207 |       glScalef(sizex,sizey,1.0f); | 
|---|
| 208 |       glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen | 
|---|
| 209 |     } | 
|---|
| 210 |  | 
|---|
| 211 |   glBindTexture(GL_TEXTURE_2D, 0); // Fix everything up | 
|---|
| 212 |   glMatrixMode(GL_PROJECTION); | 
|---|
| 213 |   glPopMatrix(); | 
|---|
| 214 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 215 |   glPopMatrix(); | 
|---|
| 216 |  | 
|---|
| 217 |   if (depthOn) glEnable(GL_DEPTH_TEST); | 
|---|
| 218 |   if (!textureOn) glDisable(GL_TEXTURE_2D); | 
|---|
| 219 |   if (!blendOn) glDisable(GL_BLEND); | 
|---|
| 220 |   if (!scissorOn) glDisable(GL_SCISSOR_TEST); | 
|---|
| 221 |   if (lightOn) glEnable(GL_LIGHTING); | 
|---|
| 222 |  | 
|---|
| 223 |   glBlendFunc(blendSrc, blendDst); | 
|---|
| 224 |   glMatrixMode(matrixMode); | 
|---|
| 225 |  | 
|---|
| 226 |   return 1; | 
|---|
| 227 | } | 
|---|