Working on my pacman project I have come across something strange. Im using the outline font method from NeHe and have used the code from there for text printing. When I copied it to my main method and texted it worked fine. But a lot of my objects need to print text so I decided to move the code to a text printing object. I didn't change anything in the text code but now that it's been moved when glPrintf() is called the symbol printed is one less than the one asked (e.g: "w" prints "v", "!" prints a space) I have tried to rectify it by adding 1 to the variables in the method but nothing works.

It's not a big problem as I can work around it but for printing the score it is a big problem.

Here are the h and cpp files for it:

//Header file for text printer
#ifndef TEXTPRINTER_H_
#define TEXTPRINTER_H_

#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 <stdarg.h>			// Header File For Variable Argument Routines
#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

class textPrinter
{
      private:
      GLuint	base;				// Base Display List For The Font Set
      GLfloat	rot;				// Used To Rotate The Text
      

      GLYPHMETRICSFLOAT gmf[256];	// Storage For Information About Our Outline Font Characters

      public:
      GLvoid BuildFont(GLvoid);								// Build Our Bitmap Font

      GLvoid KillFont(GLvoid);									// Delete The Font

      GLvoid glPrint(const char *fmt, ...);					// Custom GL "Print" Routine
};

#endif



//Class file for textPrinter
//Note: This code is taken from NeHe tutorial 14: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=14
#include "textPrinter.h"

GLvoid textPrinter::BuildFont(GLvoid)								// Build Our Bitmap Font
{
	HFONT	font;										// Windows Font ID

	this->base = glGenLists(256);								// Storage For 256 Characters

	font = CreateFont(	-12,							// Height Of Font
						0,								// Width Of Font
						0,								// Angle Of Escapement
						0,								// Orientation Angle
						FW_BOLD,						// Font Weight
						FALSE,							// Italic
						FALSE,							// Underline
						FALSE,							// Strikeout
						ANSI_CHARSET,					// Character Set Identifier
						OUT_TT_PRECIS,					// Output Precision
						CLIP_DEFAULT_PRECIS,			// Clipping Precision
						ANTIALIASED_QUALITY,			// Output Quality
						FF_DONTCARE|DEFAULT_PITCH,		// Family And Pitch
						"Comic Sans MS");				// Font Name

	SelectObject(wglGetCurrentDC(), font);							// Selects The Font We Created

	wglUseFontOutlines(	wglGetCurrentDC(),							// Select The Current DC
						0,								// Starting Character
						255,							// Number Of Display Lists To Build
						this->base,							// Starting Display Lists
						0.0f,							// Deviation From The True Outlines
						0.2f,							// Font Thickness In The Z Direction
						WGL_FONT_POLYGONS,				// Use Polygons, Not Lines
						gmf);							// Address Of Buffer To Recieve Data
}

GLvoid textPrinter::KillFont(GLvoid)									// Delete The Font
{
  glDeleteLists(this->base, 256);								// Delete All 256 Characters
}

GLvoid textPrinter::glPrint(const char *fmt, ...)					// Custom GL "Print" Routine
{
	float		length=0;								// Used To Find The Length Of The Text
	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

	for (unsigned int loop=0;loop<(strlen(text));loop++)	// Loop To Find Text Length
	{
		length+=gmf[text[loop]].gmfCellIncX;			// Increase Length By Each Characters Width
	}

	glTranslatef(-length/2,0.0f,0.0f);					// Center Our Text On The Screen

	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
	glListBase(this->base);									// Sets The Base Character to 0
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	glPopAttrib();										// Pops The Display List Bits
}

I don't trust NEHE code much. Its outdated. If you are going to
use someone else's code, why not use a better code than theirs.
There are many libraries for printing text in opengl. Pick one an use it.
It is much more flexible and you can create better and cooler looking
text. BTW, I suggest your textprinter to be a singleton. And I am
surprised that glaux is supported in your library. But if you insist
on using this code, then step through the debugger and see whats
happening. It will give you practice and you will eventually find the
error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.