Main Page   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

/cygdrive/d/Eigene Dateien/!DProcs/code_gen/src/DrawText.cpp

Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <stdarg.h>
00004 
00005 //#include <Windows.h>
00006 #include <GL/gl.h>
00007 
00008 #include "DrawText.h"
00009 #include <assert.h>
00010 
00011 #include <string.h>
00012 
00013 #include "tga.h"
00014 
00015 // Util functions.  Global as they may come in useful elsewhere.
00016 
00017 // Switches to 2d ortho projection mode.
00018 // Use in conjunction with Exit2d()
00019 void Go2d()
00020 {
00021 
00022     glMatrixMode(GL_PROJECTION);
00023     glPushMatrix();                                 // Store The Projection Matrix
00024     glLoadIdentity();                               // Reset The Projection Matrix
00025     glOrtho(0,1024,0,768,-1,1);                     // Set Up An Ortho Screen
00026     glMatrixMode(GL_MODELVIEW);
00027     glPushMatrix();                                 // Store old Modelview Matrix
00028     glLoadIdentity();                               // Reset The Modelview Matrix
00029 }
00030 
00031 
00032 // Switches out of 2d ortho projection mode.
00033 // Use in conjunction with Go2d()
00034 void Exit2d()
00035 {
00036     glMatrixMode(GL_PROJECTION);
00037     glPopMatrix();                                  // Restore old Projection Matrix
00038     glMatrixMode(GL_MODELVIEW);
00039     glPopMatrix();                                  // Restore old Projection Matrix
00040 }
00041 
00042 
00043 // The single instance of our class.
00044 CTextDrawer* CTextDrawer::mSingleton = NULL;
00045 
00046 
00047 // Provides access to the single global CTextDrawer object.
00048 CTextDrawer &CTextDrawer::GetSingleton()
00049 {
00050     if (!mSingleton)
00051     {
00052         mSingleton = new CTextDrawer;
00053         Initialise();
00054     }
00055 
00056     return *mSingleton;
00057 }
00058 
00059 
00060 // Call when finished.
00061 void CTextDrawer::Destroy()
00062 {
00063     if (mSingleton)
00064     {
00065         // Delete the gl display lists for each letter.
00066         glDeleteLists(mSingleton->mBaseListId, NUMCHARS);           // Delete All Display Lists
00067 
00068                 glDeleteTextures(1,&(mSingleton->mFontTexId));
00069         delete mSingleton;
00070         mSingleton = NULL;
00071     }
00072 }
00073 
00074 
00075 CTextDrawer::CTextDrawer()
00076 {
00077     mBaseListId = 0;
00078     mR = mG = mB = mA = 1.0f;
00079 }
00080 
00081 
00082 // Sets the font texture GL texture id.
00083 // Param: aFontTextureId - A GL texture enum for the font texture.
00084 void CTextDrawer::SetFontTextureId(GLuint aFontTextureId)
00085 {
00086     mSingleton->mFontTexId = aFontTextureId;
00087 }
00088 
00089 
00090 void CTextDrawer::Initialise()      // Build Our Font Display List
00091 {
00092     assert(mSingleton->mFontTexId);     // No font texture id set: Use TEXTDRAWER.SetFontTextureId(aFontTextureId);
00093 
00094     float   cx;                                     // Holds Our X Character Coord
00095     float   cy;                                     // Holds Our Y Character Coord
00096     int     loop;
00097 
00098         mSingleton->mFontTexId = createTexture("font_arial.tga");
00099 
00100     mSingleton->mBaseListId = glGenLists(NUMCHARS);             // Creating NUMCHARS Display Lists
00101     glBindTexture(GL_TEXTURE_2D, mSingleton->mFontTexId);
00102     for (loop=0; loop<NUMCHARS; loop++)
00103     {
00104          // alexander kramer :  sligthly modified
00105           
00106         cx=((float)(loop%16))/16.0f;                // X Position Of Current Character
00107         cy=(((float)(loop/16))/16.0f);              // Y Position Of Current Character
00108 
00109         glNewList(mSingleton->mBaseListId+loop, GL_COMPILE);    // Start Building A List
00110             glBegin(GL_QUADS);                      // Use A Quad For Each Character
00111                 glTexCoord2f(cx,cy);        // Texture Coord (Bottom Left)
00112                 glVertex2i(0,0);                    // Vertex Coord (Bottom Left)
00113                 glTexCoord2f(cx+0.0625f,cy);// Texture Coord (Bottom Right)
00114                 glVertex2i(16,0);                   // Vertex Coord (Bottom Right)
00115                 glTexCoord2f(cx+0.0625f,cy+0.0625f);        // Texture Coord (Top Right)
00116                 glVertex2i(16,16);                  // Vertex Coord (Top Right)
00117                 glTexCoord2f(cx,cy+0.0625f);                // Texture Coord (Top Left)
00118                 glVertex2i(0,16);                   // Vertex Coord (Top Left)
00119             glEnd();                                // Done Building Our Quad (Character)
00120             glTranslatef(12.0f,0,0);                // Move To The Right Of The Character
00121         glEndList();                                // Done Building The Display List
00122     }
00123 }
00124 
00125 
00126 // Sets the colour of the text.
00127 void CTextDrawer::SetColor(float aR, float aG, float aB, float aA)
00128 {
00129     mR = aR;
00130     mG = aG;
00131     mB = aB;
00132     mA = aA;
00133 }
00134 
00135 
00136 /*
00137 INFO From siggraph:
00138 
00139 The GL_INTENSITY texture format is a good texture format for textures containing glyphs
00140 because the texture format is compact. The intensity texture format replicates the single
00141 intensity component value in the red, green, blue, and alpha channels. When rendering
00142 colored glyphs, use the GL_MODULATE texture environment and set the current color to the
00143 desired glyph color.
00144 */
00145 
00146 // alexander kramer : original code was changed by me  
00147 bool CTextDrawer::PrintText(bool bold, int maxLength, const char *fmt, ...)
00148 {
00149     assert(mBaseListId != 0);
00150 
00151     char        text[1024];                         // Holds Our String
00152     va_list     ap;                                 // Pointer To List Of Arguments
00153 
00154     if (fmt == NULL)
00155         return true;
00156 
00157     // Parse The String For Variables
00158     va_start(ap, fmt);                          
00159     vsprintf(text, fmt, ap);
00160     va_end(ap);
00161 
00162     glEnable(GL_TEXTURE_2D);
00163     glBindTexture(GL_TEXTURE_2D, mFontTexId);
00164          //glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00165        
00166     //glDisable(GL_DEPTH_TEST);
00167     glEnable(GL_BLEND);                             // For transparent background
00168     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00169 
00170     //Go2d();
00171 
00172     //glTranslated(x, y, 0);                            // Position The Text (0,0 - Top Left)
00173 
00174     // Note: the 128 below refers to the font texture containing 2 font sets.
00175     // Use 0 for the first set, or 128 for the second set.
00176 
00177         if (bold)
00178           glListBase(mBaseListId - ' ' + 0);            // Font bitmaps starts at ' ' (space/32).
00179         else
00180           glListBase(mBaseListId - ' ' + 128);          // Font bitmaps starts at ' ' (space/32).
00181 
00182     //glColor4f(mR, mG, mB, mA);            // Set text colour.
00183 
00184 
00185         if (maxLength == -1)
00186           glCallLists(strlen(text), GL_BYTE, text);     // Write The Text To The Screen
00187         else
00188           glCallLists(maxLength, GL_BYTE, text);        // Write The Text To The Screen
00189 
00190 
00191         glDisable(GL_TEXTURE_2D);
00192     //Exit2d();
00193 
00194         glDisable(GL_BLEND);
00195     //glEnable(GL_DEPTH_TEST);
00196 
00197     return true;
00198 }
00199 

Generated on Thu Mar 18 18:33:47 2004 for miniQT by doxygen1.2.18