Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] How to optimize al_draw_textf

Credits go to dthompson and Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
[A5] How to optimize al_draw_textf
wixy00
Member #17,122
September 2019

Hi, I'm trying to optimize my Snake game so it uses less and less of RAM and I came to the conclusion that al_draw_textf() function uses the most memory in my case. I'm using this function to draw score on the screen every frame and just wanted to know if is there any way to optimize that.

Niunio
Member #1,975
March 2002
avatar

Use smaller text font? Or less glyphs? ???

-----------------
Current projects: Allegro.pas | MinGRo

dthompson
Member #5,749
April 2005
avatar

Are you saying you've profiled your game's memory usage and found that al_draw_textf is allocating large amounts of memory? Or is it your usage of the fonts addon in general (allocating ALLEGRO_FONTs et al)?

______________________________________________________
Website. It was freakdesign.bafsoft.net.
This isn't a game!

wixy00
Member #17,122
September 2019

I've profiled memory usage using VS 2017.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

wixy00
Member #17,122
September 2019

I'm sorry I'm new here. How can I send you my code and resources?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

wixy00, welcome to allegro.cc.

When you reply to this post, add a zip file of your source and resources using the Drop Attachments here button.

You can then link to them using an href tag.

Allegro.cc uses XHTML tags, like <code>code goes here...</code> and <a href="URL">Link name</a> so. Images are similar - <img src="https://www.allegro.cc/files/attachment/612136" /> yields :

{"name":"612136","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/f\/5f3f6e41c7c17d71258fffdae7417326.png","w":1210,"h":907,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/f\/5f3f6e41c7c17d71258fffdae7417326"}612136

Right click on the attachment and copy the link location. Then paste it in your post.

https://www.allegro.cc/files/attachment/612136

wixy00
Member #17,122
September 2019

Thank you, good to know.

Snake.rar

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I've managed to build and successfully run your game.

I profiled the memory usage, and I don't see anything unusual. I see two large sets of allocation, one on startup, and one on entering the game.

I'll admit I've never used the VS profiler before, so.... how did you come to your conclusion that al_draw_textf was using too much memory? Please tell me the steps I can take to reproduce this.

wixy00
Member #17,122
September 2019

It's actually not that this function uses too much memory, it's using more than for example drawing bitmap. I wanted to know if there is any way to optimize that. I tried drawing regions of digits.png and it seems to be working just fine but it requires too much work to prepare (I mean in graphics program). If you want you can comment out current version of main game loop located in Run() and use the other one to check memory usage.

{"name":"612138","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/2\/72118338fdd91d49b5de3e60985a550b.png","w":749,"h":183,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/2\/72118338fdd91d49b5de3e60985a550b"}612138

dthompson
Member #5,749
April 2005
avatar

Yeah, that sounds about right. You're essentially paying for al_draw_textf's versatility here with a tiny, tiny bit more runtime memory than with a custom solution with al_draw_bitmap_region. If you want to optimise the heck out of your game, go ahead and create that (more specialised) solution.

However - and sorry to be that guy - I think it's worth asking whether the optimisation is worth your time. Unless you're rendering thousands of formatted strings of text, I'd be very happy with that memory usage; it may drop even further if you're using the non-debug DLLs.

______________________________________________________
Website. It was freakdesign.bafsoft.net.
This isn't a game!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

The reason al_draw_textf is allocating memory is because the glyphs haven't been cached yet. Allegro 5 caches glyphs on demand.

I'll second dthompson here, and say that this is really a non-issue. Unless you want to spend your time optimizing allegro instead of your own code, there's really no point in worrying about it. There's no memory leak.

You might reduce memory usage by disabling memory bitmap backups. This only applies on Windows with D3D but you can disable bitmap backups using the following code :

/// create display

al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP | ALLEGRO_NO_PRESERVE_TEXTURE);

/// Now create the font, which respects the new bitmap flags
ALLEGRO_FONT* font = al_load_ttf_font(...);

/// Now precache ONLY the glyphs that we want
al_draw_text("0123456789" , ...);

wixy00
Member #17,122
September 2019

I know there is no point optimizing such a simple game, but now I have a lot of free time so it was rather for learning purposes. Anyway, thank you for your time, I'm glad that finally someone is willing to help me.

Go to: