Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing TTF Font takes 20% CPU

This thread is locked; no one can reply to it. rss feed Print
Drawing TTF Font takes 20% CPU
Addison Elliott
Member #12,394
November 2010

I am having a problem with al_draw_text taking up 20% more CPU that it usually would when I comment out the drawing. I am confused on why it's doing this.
There's not a lot of code I can show you but I can explain what is happening around this.

#SelectExpand
1ALLEGRO_FONT *font = al_load_font("arial.ttf", 12, 0); //This is the font that I load 2al_draw_text(font, al_map_rgb(255, 255, 255), 0, 0, "Testing"); //This is the drawing function I use

These two lines are not continuous, they are just information to help you get some background on the problem. I do have al_hold_bitmap_drawing set to true, if that means anything, and I do have this running under OpenGL. I am using Allegro 5.0.5. It is rendering it at 20FPS. I just checked to see if the bitmap drawing is still held afterwards and yes it is.

Matthew Leverton
Supreme Loser
January 1999
avatar

Are you loading the font after you create the display?

Addison Elliott
Member #12,394
November 2010

No I am not. Let me check to see if that fixes it :P
I guess that does make sense since it works the same with bitmaps. If you try drawing it on a different display than it was created on, it will take up like 50% CPU.

Edit: I am loading it after the display is created, but does it have to be on the same thread as it? I am using a multi-threaded application

Matthew Leverton
Supreme Loser
January 1999
avatar

It needs to be loaded on the same thread that is using the display.

Addison Elliott
Member #12,394
November 2010

Well shit, that ruins my plan. I will need to rethink that then. I currently have a seperate thread for loading the Bitmaps(I load it in memory here first, then convert to video in the rendering thread), Fonts, and Audio. Just to make sure, Audio doesn't need to be loaded on the display thread, right? I could just set the current display on the content loading thread.

Matthew Leverton
Supreme Loser
January 1999
avatar

Audio is fine in another thread.

Allegro 5.1 (precursor to 5.2) has additional functionality meant to solve this problem that lets you automatically convert any bitmap (including fonts) from memory to video.

AMCerasoli
Member #11,955
May 2010
avatar

Quote:

Well , that ruins my plan.

Hahaha I had the exactly same problem once, I had to reimplement some code, because the features that Matthew said weren't available, but you could build Allegro 5.1 and still using threads to load the fonts. If you haven't built Allegro 5.x in the past, check the wiki there is a tutorial, but it will always take you some time... so it's up to you.

Peter Wang
Member #23
April 2000

Does it really take that long to load the font anyway? Note you have to account for glyphs being cached the first time they are used as well.

Addison Elliott
Member #12,394
November 2010

I've built it before, I do know it can be a pain sometimes. Wait, so if I get the revision 5.1, then I can just use my threads without having to set the current display? That is quite convenient.

Matthew Leverton
Supreme Loser
January 1999
avatar

Wait, so if I get the revision 5.1, then I can just use my threads without having to set the current display?

5.1 adds a conversion flag (ALLEGRO_CONVERT_BITMAP) that can optionally be set. So if you do something like:

al_set_new_bitmap_flags(ALLEGRO_CONVERT_BITMAP);

then Allegro will try to create video bitmaps, but then fall back to memory bitmaps tagged for conversion. The next time a display is created they will be converted to video bitmaps compatible with that display.

If a display is already present (because it was created prior to loading via a thread), then you can use al_convert_bitmaps() in the display thread to force the issue.

If you don't want to convert them all at once, you can just use al_convert_bitmap() one by one.

AMCerasoli
Member #11,955
May 2010
avatar

Does it really take that long to load the font anyway?

Are you really asking that?, it is not about the loading time, it is because when you load a resource when the game already started, you don't want to freeze the game not even a millisecond... Why it is so difficult to understand this?.. Not all games work with a "Load -> Play -> Load -> Play" strategy, and even if they do, there could be some hybrid strategies where you want to load some resources before starting the game, and other resources after starting the game.

Quote:

Note you have to account for glyphs being cached the first time they are used as well.

I thought this was solved...

You could use al_get_text_width(yourFont, "AaBbCcDdEe123...etc"); to prepare the glyphs. It's a little hack that I still thinking should be done automatically by the load font function.

Edgar Reynaldo
Member #8,592
May 2007
avatar

I'm not sure how it works internally, but NOT caching every glyph until you draw it saves (video) memory. :-/ Loading a font literally only takes milliseconds. Using a separate thread to do it seems silly, especially if your program gets run on a single core machine. Just do something entertaining while you load your stuff.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Just do something entertaining while you load your stuff.

I thought that was called a splash screen. :o

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

AMCerasoli
Member #11,955
May 2010
avatar

So you don't include the font loading in your resource manager...

Peter Wang
Member #23
April 2000

Are you really asking that?

Of course I ask. Without measuring you don't know if an 'optimisation' is worth the effort or not.

Quote:

it is not about the loading time, it is because when you load a resource when the game already started, you don't want to freeze the game not even a millisecond... Why it is so difficult to understand this?

It's not difficult to understand premature optimisation...

By the way, do you know if converting the bitmaps afterwards causes a smaller pause than loading the font in the original thread in the first place? (This is not a rhetorical question, nor was my first question. I didn't measure it.)

AMCerasoli
Member #11,955
May 2010
avatar

It's not difficult to understand premature optimisation...

I don't think we can call this premature optimization. To me it's just "the right" way to do things, but of course that is what I think.

Quote:

By the way, do you know if converting the bitmaps afterwards causes a smaller pause than loading the font in the original thread in the first place? (This is not a rhetorical question, nor was my first question. I didn't measure it.)

To be honest I haven't tried it either, but something made me thought that if such function exist it's because the difference in time, between loading a bitmap and convert it to 'video bitmap' is huge, but I don't know, I always though that the biggest amount of time when loading a bitmap was spent in the loading process, but now that you mention it, and seeing how fast I can load text and display it, I'm starting to change my mind. Maybe the biggest amount of time is not spent in the loading process but rather in transferring those bites to the graphic memory, etc...

jmasterx
Member #11,410
October 2009

I know for my games I load sizes 10 to 72 of 1 font so it dynamically resizes and those milliseconds add up.

Go to: