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

Generated on Fri Dec 23 05:21:20 2005 for Skybox by doxygen1.2.15