Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

glfont.cpp

Go to the documentation of this file.
00001 //*********************************************************
00002 //GLFONT.CPP -- glFont routines
00003 //Copyright (c) 1998 Brad Fish
00004 //See glFont.txt for terms of use
00005 //November 10, 1998
00006 //*********************************************************
00007 
00008 #include <windows.h>
00009 #include <stdio.h>
00010 #include <malloc.h>
00011 #include <string.h>
00012 #include <gl\gl.h>
00013 #include "glfont.h"
00014 #include "mmgr.h"
00015 
00016 //*********************************************************
00017 //Variables
00018 //*********************************************************
00019 
00020 //Current font
00021 GLFONT *glFont;
00022 
00023 //*********************************************************
00024 //Functions
00025 //*********************************************************
00026 int glFontCreate (GLFONT *Font, char *FileName, int Tex)
00027 {
00028     FILE *Input;
00029     char *TexBytes;
00030     int Num;
00031 
00032     //Open font file
00033     if ((Input = fopen(FileName, "rb")) == NULL)
00034         return FALSE;
00035 
00036     //Read glFont structure
00037     fread(Font, sizeof(GLFONT), 1, Input);
00038 
00039     //Save texture number
00040     Font->Tex = Tex;
00041 
00042     //Get number of characters
00043     Num = Font->IntEnd - Font->IntStart + 1;
00044 
00045     //Allocate memory for characters
00046     if ((Font->Char = (GLFONTCHAR *)malloc(
00047         sizeof(GLFONTCHAR) * Num)) == NULL)
00048         return FALSE;
00049 
00050     //Read glFont characters
00051     fread(Font->Char, sizeof(GLFONTCHAR), Num, Input);
00052 
00053     //Get texture size
00054     Num = Font->TexWidth * Font->TexHeight * 2;
00055 
00056     //Allocate memory for texture data
00057     if ((TexBytes = (char *)malloc(Num)) == NULL)
00058         return FALSE;
00059 
00060     //Read texture data
00061     fread(TexBytes, sizeof(char), Num, Input);
00062 
00063     //Set texture attributes
00064     glBindTexture(GL_TEXTURE_2D, Font->Tex);
00065     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
00066         GL_CLAMP);
00067     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
00068         GL_CLAMP);
00069     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
00070         GL_LINEAR);
00071     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
00072         GL_LINEAR);
00073     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
00074         GL_MODULATE);
00075 
00076     //Create texture
00077     glTexImage2D(GL_TEXTURE_2D, 0, 2, Font->TexWidth,
00078         Font->TexHeight, 0, GL_LUMINANCE_ALPHA,
00079         GL_UNSIGNED_BYTE, (void *)TexBytes);
00080 
00081     //Clean up
00082     free(TexBytes);
00083     fclose(Input);
00084 
00085     //Return pointer to new font
00086     return TRUE;
00087 }
00088 //*********************************************************
00089 void glFontDestroy (GLFONT *Font)
00090 {
00091     //Free character memory
00092     free(Font->Char);
00093 }
00094 //*********************************************************
00095 void glFontBegin (GLFONT *Font)
00096 {
00097     //Save pointer to font structure
00098     if (Font->Char != NULL)
00099         glFont = Font;
00100     else
00101         glFont = NULL;
00102 
00103     //Bind to font texture
00104     glBindTexture(GL_TEXTURE_2D, Font->Tex);
00105 }
00106 //*********************************************************
00107 void glFontEnd (void)
00108 {
00109     //Font no longer current
00110     glFont = NULL;
00111 }
00112 //*********************************************************
00113 void glFontTextOut (char *String, float x, float y,
00114     float z)
00115 {
00116     int Length, i;
00117     GLFONTCHAR *Char;
00118 
00119     //Return if we don't have a valid glFont
00120     if (glFont == NULL)
00121         return;
00122 
00123     //Get length of string
00124     Length = strlen(String);
00125 
00126     //Begin rendering quads
00127     glBegin(GL_QUADS);
00128 
00129     //Loop through characters
00130     for (i = 0; i < Length; i++)
00131     {
00132         //Get pointer to glFont character
00133         Char = &glFont->Char[(int)String[i] -
00134             glFont->IntStart];
00135 
00136         //Specify vertices and texture coordinates
00137         glTexCoord2f(Char->tx1, Char->ty1);
00138         glVertex3f(x, y, z);
00139         glTexCoord2f(Char->tx1, Char->ty2);
00140         glVertex3f(x, y - Char->dy, z);
00141         glTexCoord2f(Char->tx2, Char->ty2);
00142         glVertex3f(x + Char->dx, y - Char->dy, z);
00143         glTexCoord2f(Char->tx2, Char->ty1);
00144         glVertex3f(x + Char->dx, y, z);
00145 
00146         //Move to next character
00147         x += Char->dx;
00148     }
00149 
00150     //Stop rendering quads
00151     glEnd();
00152 }
00153 //*********************************************************
00154 
00155 //End of file
00156 
00157 
00158 

Generated on Fri Dec 23 05:20:18 2005 for Portals by doxygen1.2.15