/*=============================================================================
text.cpp: Basic OpenGL Framework text function definitions
Revision history:
* Created by Zack Smith October 9, 2000
=============================================================================*/
#include <windows.h> // Header File For Windows
#include <math.h> // Header File For Windows Math Library
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include "text.h"
#include "application.h"
CText::CText()
: pTexture(NULL),
hFont(0)
{
pApp->pLog->Log("Text Class...initialized");
}
CText::~CText()
{
// Delete The Font From Memory
glDeleteLists(base,256); // Delete All 256 Display Lists
if (hFont)
DeleteObject(hFont);
if (pTexture)
delete pTexture;
pApp->pLog->Log("Text Class...destroyed");
}
CText::LoadFont(char* szTexture)
{
pTexture = new CTexture();
if (!pTexture->LoadGLTexture(szTexture)) // Jump To Texture Loading Routine
{
return FALSE; // If Texture Didn't Load Return FALSE
}
// Build Our Font Display List
float cx; // Holds Our X Character Coord
float cy; // Holds Our Y Character Coord
base=glGenLists(256); // Creating 256 Display Lists
glBindTexture(GL_TEXTURE_2D, pTexture->texture2d); // Select Our Font Texture
for (int loop=0; loop<256; loop++) // Loop Through All 256 Lists
{
cx=float(loop%16)/16.0f; // X Position Of Current Character
cy=float(loop/16)/16.0f; // Y Position Of Current Character
glNewList(base+loop,GL_COMPILE); // Start Building A List
glBegin(GL_QUADS); // Use A Quad For Each Character
glTexCoord2f(cx,1-cy-0.0625f); // Texture Coord (Bottom Left)
glVertex2i(0,0); // Vertex Coord (Bottom Left)
glTexCoord2f(cx+0.0625f,1-cy-0.0625f); // Texture Coord (Bottom Right)
glVertex2i(16,0); // Vertex Coord (Bottom Right)
glTexCoord2f(cx+0.0625f,1-cy); // Texture Coord (Top Right)
glVertex2i(16,16); // Vertex Coord (Top Right)
glTexCoord2f(cx,1-cy); // Texture Coord (Top Left)
glVertex2i(0,16); // Vertex Coord (Top Left)
glEnd(); // Done Building Our Quad (Character)
glTranslated(10,0,0); // Move To The Right Of The Character
glEndList(); // Done Building The Display List
}
return TRUE;
}
GLvoid CText::glPrint(const char *fmt, ...)
{
char text[256]; // Holds our string
va_list ap; // Pointer to list of arguments
if (fmt == NULL) // If there's no text
return; // Do nothing;
va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text
// Pushes The Display List Bits
glPushAttrib(GL_LIST_BIT);
glListBase(base - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}
void CText::BuildFont(char *szFontFace, int iSize, int fnWeight)
{
hFont = CreateFont(-iSize,
0, 0, 0,
fnWeight,
FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_TT_PRECIS, 0, ANTIALIASED_QUALITY, 0,
szFontFace);
// make the system font the device context's selected font
SelectObject (pApp->pView->hDC, hFont);
// create the bitmap display lists
// we're making images of glyphs 32 thru 128
base = glGenLists(96);
pApp->pLog->Log("Font created, base = %d", base);
wglUseFontBitmaps (pApp->pView->hDC, 32, 96, base);
}