Jump to content
  • Advertisement
Sign in to follow this  
hahaha

Bitmap Font Memory Problem

This topic is 4028 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!