Sign in to follow this  
hahaha

Bitmap Font Memory Problem

Recommended Posts

If I create 3 different bitmap fonts with the CreateFont function and i switch between the three fonts every time i render the frame my memory usage goes up and up. Eg: void init() { unsigned int base; HFONT Font1 =CreateFont(20,0,0,0,FW_BOLD,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,FF_DONTCARE|DEFAULT_PITCH,"Courier"); HFONT Font2 = ... HFONT Font3 = ... Current = Font1; } void ChangeFont(int type) { if(type == 0)Current = Normal; else if(type == 1)Current = Underline; else if(type == 2)Current = Underline2; else Current = Large; } void DrawText(int x,int y,char *str) { SelectObject(g_hDC,Current); base = glGenLists(96); wglUseFontBitmaps(g_hDC,32,96,base); glRasterPos2i(x,y); glPushAttrib(GL_LIST_BIT); glListBase(base - 32); glCallLists((int)strlen(str),GL_UNSIGNED_BYTE,str); glPopAttrib(); } Is there a way to fix this?

Share this post


Link to post
Share on other sites
From what I can tell in your code you're creating the display list for the fonts every time you draw the font.

My suggestion would be to create the display lists for all three fonts in your startup routine. and then just have a function that calls the correct display lists to print the font to the screen.

At the moment you're creating 96 new display lists every frame. This is going to make your memory usage shoot up, and your framerate shoot down.

Share this post


Link to post
Share on other sites
Thanks for the help!
So if I'm understanding you then is should use x = glGenLists(96); for each font and call wglUseFontBitmaps(g_hDC,32,96,x); for changing fonts. Can i leave SelectObject(g_hDC,x); out when changing fonts?

Share this post


Link to post
Share on other sites
No no, below is a code snippet of how you should do it.

void init()
{
unsigned int base;
HFONT Font1 =CreateFont(20,0,0,0,FW_BOLD,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,FF_DONTCARE|DEFAULT_PITCH,"Courier");

HFONT Font2 = ...
HFONT Font3 = ...
Current = Font1;
}

void createFonts()
{
SelectObject(g_hDC,Current);
base = glGenLists(96);
wglUseFontBitmaps(g_hDC,32,96,base);
}

void ChangeFont(int type)
{
if(type == 0)Current = Normal;
else if(type == 1)Current = Underline;
else if(type == 2)Current = Underline2;
else Current = Large;
}

void DrawText(int x,int y,char *str)
{
glRasterPos2i(x,y);
glPushAttrib(GL_LIST_BIT);
glListBase(base - 32);
glCallLists((int)strlen(str),GL_UNSIGNED_BYTE,str);
glPopAttrib();
}

This way you have a function that creates all your display lists during startup. Then your drawtext font is simply just drawing the display list

Share this post


Link to post
Share on other sites

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