Question about Bitmap Font!!!!

Started by
3 comments, last by koalacui 18 years, 7 months ago
Hi, guys, my first time to try bitmap font of OpenGL, i have tried a lot of ways, but still cannot display the font, this is the code, i write these code into AppMain.cpp file, and with 5 errors, it's all about g_HDC, i dont know how to solve it, can anybody help me on it!! thanks a lot and best regards!!!

unsigned int listBase;
.
.
.
unsigned int CreateBitmpFont(char *fontName, int fontSize)
{
	HFONT hFont;
	unsigned int base;

	base = glGenLists(96);

	if (stricmp(fontName, "symbol")== 0)
	{
		hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
						   SYMBOL_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
						   ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH,
						   fontName);
	}
	else
	{
		hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
						   ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
						   ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH,
						   fontName);
	}

	if (!hFont)
		return 0;

	SelectObject(g_HDC, hFont);
	wglUseFontBitmaps(g_HDC, 32, 96, base);

	return base;
}

void PrintString(unsigned int base, char *str)
{
	if ((base == 0 || (str == NULL))
		return;

	glPushAttrib(GL_LIST_BIT);
		glListBase(base - 32);
		glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
	glPopAttrib();
}

void ClearFont(unsigned int base)
{
	if (base != 0)
		glDeleteLists(base, 96);
}

void Initialize()
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);

	listBase = CreateBitmapFont("Comic Sans MS", 48);
}

void Render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glTranslatef(0.0f, 0.0f, -1.0f);

	glColor3f(1.0f, 1.0f, 1.0f);

	glRasterPos2f(-0.35f, 0.0f);

	PrintString(listBase, "OpenGL Bitmap Fonts!");

	glFlush();
	SwapBuffers(g_HDC);
}
.
.
void main()
.
.
.

-=[LOVE always, LIVE forever]=-
Advertisement
ok, for the second time:

HDC g_HDC;

at the top of your file. outside of any brackets. to make it work, call GetDC( ) on the handle you get from CreateWindow( ) when you create it.

HWND your_window = CreateWindow(...);g_HDC = GetDC(your_window);
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
I'm so sorry to trouble you, but I cannot understand your code, can u explain it more in detail? how to call GetDC(), write what in it? ....emmm.....HWND is ....hhe...sorry for these troubles, or you can help me insert the general idea into my code, i am not sure put these coe into which part?? thanks a lot and best regards!!!!

[Edited by - koalacui on September 23, 2005 1:03:58 AM]
-=[LOVE always, LIVE forever]=-
You need the handle from your created window.
Pass it to GetDC(); the get the device context.

You can find a sample here.
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=13
thanks a lot, guys, appreciate it!!!
-=[LOVE always, LIVE forever]=-

This topic is closed to new replies.

Advertisement