Sign in to follow this  

fonts don't work

This topic is 4594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a small game (2d), and I want to use fonts in it. I did everything described in tutorial #13, but the text doesn't draw. The weird thing is that I used this code (I just copy-pasted for a large part) already in another game I made (a 3d one). This is the relevant code :
void buildLargeFont(){
	HFONT	font;
	HFONT	oldfont;

	base = glGenLists(96);

	font = CreateFont(16, 0, 0, 0, 100, FALSE, FALSE, FALSE,
				ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
				ANTIALIASED_QUALITY, FF_DONTCARE|DEFAULT_PITCH, "Comic Sans MS");
	
	oldfont = (HFONT)SelectObject (hdc, font);
	wglUseFontBitmaps (hdc, 32, 96, base);
	SelectObject (hdc, oldfont);
	DeleteObject (font);
}

/******************************************************************************/

void buildSmallFont(){
	HFONT	font;
	HFONT	oldfont;

	base = glGenLists(96);

	font = CreateFont(24, 0, 0, 0, 100, FALSE, FALSE, FALSE,
				ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
				ANTIALIASED_QUALITY, FF_DONTCARE|DEFAULT_PITCH, "Comic Sans MS");
	
	oldfont = (HFONT)SelectObject (hdc, font);
	wglUseFontBitmaps (hdc, 32, 96, base);
	SelectObject (hdc, oldfont);
	DeleteObject (font);
}

/******************************************************************************/

void freeFonts()
{
	glDeleteLists(1, 96);
	glDeleteLists(97, 96);
}

/******************************************************************************/

void printText (const char *input, ...)
{
	char		text[256];
	va_list		ap;

	if (input == NULL) return;

	va_start(ap, input);
	vsprintf(text, input, ap);
	va_end(ap);

	glPushAttrib(GL_LIST_BIT);
	glListBase(base - 32);

	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
	glPopAttrib();
}

/******************************************************************************/

void resizeScene (int width, int height)
{
	if (height==0){
		height=1;
	}

	glViewport (0, 0, width, height);

	glMatrixMode (GL_PROJECTION);
	glLoadIdentity();
	glOrtho (0.0, 64.0, 48.0, 0.0, 1.0, -1.0);

	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity();
}

/******************************************************************************/

int initGl()
{
	glShadeModel (GL_SMOOTH);

	setClearColor (0.0f, 0.0f, 0.0f, 0.5f);

	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	buildSmallFont();
	buildLargeFont();
	
	return 1;
}

/******************************************************************************/

//this is called last, so it draws on top of everything
void drawSpelText(){
	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -1.0f);
	glColor3f(0.0f, 0.0f, 0.0f);

	glDisable(GL_DEPTH_TEST);

	base = SKY_LARGE_FONT;
	glRasterPos2f(-0.22, 0.0);
	printText ("Test 1");

	base = SKY_SMALL_FONT;
	glRasterPos2f(-0.22, -0.02);
	printText ("Test 2");

	glEnable(GL_DEPTH_TEST);
}

Share this post


Link to post
Share on other sites
I used about the same code as you did, but there are some differences.

First i think its better to use not twice the GLuint base for storing your display lists in.
Better :
LargeFont : GLuint LargeBase
SmallFont : GLuint SmallBase

And Also when you free your display list :
glDeleteLists(LargeBase, 96);
glDeleteLists(SmallBase, 96);

Now you have 2 seperated Display List with each there own Font
If you now want to print your text you'll have to adjust your glPrint function

GLvoid printText(GLuint base, const char* fmt, ...) {
-> Same Code
}

Now you can call the function like this :
printText(LargeBase, "Your text to write to the screen %d", Integer);
printText(SmallBase, "your text to write to the screen %s", String);

He now knows which Display list to use to create/write your text.
Don't forget to initialize your SmallBase and LargeBase as globals.

[Edited by - Codorke on May 17, 2005 11:59:34 AM]

Share this post


Link to post
Share on other sites
This doesn't solve the problem. Even when I try to make only one font, it doesn't work. Is this problem caused because I use glOrtho() instead of gluPerspective() ?

Share this post


Link to post
Share on other sites
It's not a glOrtho problem. You're just drawing your text off the top off the screen. You're starting at the origin (top-left from your glOrtho call) and moving the raster position up for the first string and up and to the left for the second string. Since the raster position defines the bottom left hand corner of the text it is being rendered entirely off screen. Move it down and to the right. Also, your printText function is just begging to be buffer-overflowed. I know it's just copied from NeHe's tutorials, but it's still horrible code. If you're using C++ use a stringstream or boost::format. If you're working in C then look into _vsnprintf.

Enigma

Share this post


Link to post
Share on other sites

This topic is 4594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this