Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem using al_draw_textf()

This thread is locked; no one can reply to it. rss feed Print
Problem using al_draw_textf()
Francis Langlais
Member #15,985
June 2015

I'm a second year computer science student and I'm currently doing a port of a game running with Allegro 4.4 to Allegro 5.0. The old version used void textprintf_ex and it was used like this:

textprintf_ex(text, font, 770, 20 + offset * i, robot->color, -1, "%s",robot->name);

in which robot->name is a string.

Now in Allegro 5.0 I'm trying to use al_draw_textf like this:

#SelectExpand
1al_set_target_bitmap(text); 2al_draw_textf(font, robot->color, 770, 20 + offset * i, 0, "%s", robot->name);

I create a font because it seems that Allegro 5 doesn't have one by default like Allegro 4.4 did, I changed the robot->color from an int to an ALLEGRO_COLOR.

When I run my game it start like it's supposed to do but at a certain point it terminate without any error. Then I run the debbuger of Eclipse and when I enter al_draw_textf I have multiple error in a certain variable. I have attached an image of the error. I don't have a single clue on what to do to fix this.

Is al_draw_textf the best way to draw some text on the screen? If not what function should I use?

Gideon Weems
Member #3,925
October 2003

I create a font because it seems that Allegro 5 doesn't have one by default like Allegro 4.4 did...

It does! See al_create_builtin_font.

The al_draw_textf function is fine, but you might also be interested in the multiline text drawing functions.

bamccaig
Member #7,536
July 2006
avatar

The debugger image doesn't appear to indicate any problem, per se. I read that as informing you that the debugger can't do its job properly because the Allegro library has been optimized (debugging symbols and the like that a debugger uses to make sense of the binary are missing). That's normal for a release, but it isn't very useful during development. You should be able to switch to a debug build of Allegro to get better debugger assistance.

In general, "terminating" and "crashing" are two different things. If the program "terminates" then it is graceful and by design. Likely your design. If it "crashes" then it's likely you've done something you can't, like attempt to access an invalid memory location. Make sure that robot is pointing to a valid memory address containing the object or data structure. Usually a crash will tell you something about what caused it. Often Windows will prompt you about it, and in Linux I believe you should at least get a message about a segfault or something from the runtime.

There's always the poor man's debugger. Liter the code with print statements where you think the program terminates and examine the output, narrowing it down until you find the real culprit. The Allegro library should be relatively stable, depending on which version you're using and what features you're using. Expect the problem to be subtle in your own code until you're sure it's in Allegro. Also consider "upgrading" to a more recent release (or even the unstable 5.1.x branch, which I think has numerous bug fixes and additional features).

Bruce Pascoe
Member #15,931
April 2015
avatar

Yeah, I've found 5.1 to be very reliable. Really the "unstable" moniker is misleading--it's been quite stable for me in minisphere.

beoran
Member #12,636
March 2011

That's historic. Nowadays it simply means the API of 5.1 is unstable and subject to change. When it comes to fixed bugs 5.1 is actually better than 5.0. In 5.2 we will get rid of this distinction and simply mark "experimental" API's as such with some preprocessor magic.

torhu
Member #2,727
September 2002
avatar

If you haven't fixed the bug already, it would be easier to help if you post more of the surrounding code.

Yeah, I've found 5.1 to be very reliable. Really the "unstable" moniker is misleading--it's been quite stable for me in minisphere.

It think "unstable" in this context means that the API is liable to change.

Francis Langlais
Member #15,985
June 2015

I've been on vacation for the last week and can't access the code right now. I will try to use al_draw_textf in an other program (just to see if I use it properly with less code to check) and using al_create_builtin_font. I will update the post as soon as I have tried those thing.

Thanks a lot for the help you guys give me, it's really appreciated.

EDIT :

So I tried it alone and it worked just fine. Now I tried in my game and it didn't stop for no reason so I guess the problem is solved by using al_create_builtin_font(). My game is still not working properly. By reading your comment it seems I shouldn't use the built-in debugger from Eclipse. Is there a way to debug without using a printf()?

Neil Roy
Member #2,229
April 2002
avatar

By reading your comment it seems I shouldn't use the built-in debugger from Eclipse. Is there a way to debug without using a printf()?

What he meant was that you need to compile your program with a debug build of Allegro so you know more about the error. You're probably compiling with a release build which doesn't have any debugging symbols.

Also, while developing my own game, I check all return values that functions provide, if there is a problem I like to use printf() to print the error and any other pertinent information. I then compile my debug version of my game in console mode so I get a separate console window to see the printf() text.

You might try printing out the value of all the variables you are passing to the al_draw_textf() function before it calls it, that could help you see where the problem lies.

Francis Langlais
Member #15,985
June 2015

Thank you for all your answer. The problem was the font I was using and al_create_builtin_font work perfectly.

@Neil Roy
Thank you for the information. As I said I'm only in second year and my experience programming is really small and I have always work with very small program. This one, on the other hand, is bigger than anything I worked with before, I am not the author and it is using Allegro which I never used so it's a bit overwhelming.

Neil Roy
Member #2,229
April 2002
avatar

No problem. That's what this forum is for. We all were new at some point or another.

I actually didn't know about al_create_builtin_font() either, I usually load one, but it's handy to know. I do remember the discussion about implementing it a while ago, nice to see it made it in.

Go to: