Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Scores in game

Credits go to Arthur Kalliokoski for helping out!
This thread is locked; no one can reply to it. rss feed Print
Scores in game
Specter Phoenix
Member #1,425
July 2001
avatar

I've been trying to implement a score in game and did:

int score;
score++;
al_draw_textf(font, al_map_rgb(255, 255, 255), 12, 12, ALLEGRO_ALIGN_LEFT, "%d", score);

The compiler is telling me I can't convert from const char* to int. Was basically following what I found on the forums http://www.allegro.cc/forums/thread/610620

Arthur Kalliokoski
Second in Command
February 2005
avatar

I copied and pasted your code and neither gcc or g++ complained. What compiler are you using?

They all watch too much MSNBC... they get ideas.

Specter Phoenix
Member #1,425
July 2001
avatar

Gnu 4.6.3 under Ubuntu.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'm using 4.5.2 in slackware, doesn't it say which parameter is at fault in the error?

[EDIT]

I get this if I remove the flags parameter

pong.c:111:1: warning: passing argument 5 of 'al_draw_textf' makes integer from pointer without a cast
/usr/local/include/allegro5/allegro_font.h:89:1: note: expected 'int' but argument is of type 'char *'
pong.c:111:1: warning: passing argument 6 of 'al_draw_textf' makes pointer from integer without a cast
/usr/local/include/allegro5/allegro_font.h:89:1: note: expected 'const char *' but argument is of type 'int'

They all watch too much MSNBC... they get ideas.

Specter Phoenix
Member #1,425
July 2001
avatar

Nevermind, figured it out, forgot the y value but my mind was completing the code when I read it. For x I had SCREEN_W - 50 and for some reason my mind was doing SCREEN_W for x and 50 for y when I read it. facepalm

Audric
Member #907
January 2001

These are warnings, I'm surprised you don't get an error about al_draw_textf() not having the right number of arguments. At least at the link step.

SiegeLord
Member #7,827
October 2006
avatar

Audric said:

I'm surprised you don't get an error about al_draw_textf() not having the right number of arguments.

It had the "right" (valid is a better word) number of arguments.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Specter Phoenix
Member #1,425
July 2001
avatar

I got errors, but it was errors going on about conversion for the arguments. Unless I misread them it was saying argument 5 was int to const char* and argument 6 was const char* to int but nothing was said about the fact that I was passing one less argument than needed for what I was trying to use it for.

Audric
Member #907
January 2001

Oh my mistake, it's one of these functions with variable arguments : ... in prototype, so it cannot detect the missing argument.

Specter Phoenix
Member #1,425
July 2001
avatar

Audric said:

Oh my mistake, it's one of these functions with variable arguments : ... in prototype, so it cannot detect the missing argument.

You mean function overloading?

Arthur Kalliokoski
Second in Command
February 2005
avatar

You mean function overloading?

He means the parameters to the format, such as "%d %d %d",var_x,var_y,var_z.

They all watch too much MSNBC... they get ideas.

Audric
Member #907
January 2001

I just remembered GCC provides a way to make it check format arguments, it's the __attribute__ ((format)).
I just checked A5's code, and al_draw_textf() is declared this way, but strangely, only on GCC/Windows :

...
#elif defined ALLEGRO_MINGW32
   #define ALLEGRO_FONT_PRINTFUNC(type, name, args, a, b) ALLEGRO_FONT_FUNC(type, name, args) __attribute__ ((format (printf, a, b)))
...

So with mingw you get a warning if you mismatch your format and arguments of al_draw_textf(), but not on Linux... ???

Luiji99
Member #12,254
September 2010

Perhaps someone should patch it?

Programming should be fun. That's why I hate Java.

Audric
Member #907
January 2001

Ah no I misread, they have it already. (alconfig.h : definition of AL_PRINTFUNC when _GNUC_ is defined)
All is well citizen :)

Arthur Kalliokoski
Second in Command
February 2005
avatar

I tried it again with -O2 -Wall -Wextra (instead of the previous -g) and it still didn't complain, just warnings about unused argv and argc, and "score" was uninitialized.

They all watch too much MSNBC... they get ideas.

Audric
Member #907
January 2001

Strange, -Wformat is implied by -Wall, so you should get it.
I just tested in allegro4 (can't compile a5 on this machine), with -Wall, errors on printf() or textprintf_ex() give message like:

warning: too many arguments for format
warning: too few arguments for format
warning: format '%d' expects type 'int', but argument 8 has type 'char *'

This is on GCC 4.4.1 TDM-2 mingw32. Maybe you're using an older version that didn't make all these checks.
edit: Arthur, you mention GCC 4.5.2 earlier. You may have missed OP's post where he said he made an error when transcribing the code: What's in his first post has actually no format error. If you tweak his example by giving more or less paramers than needed, or incompatible parameters for the string's formatters, you'll see the warnings fly.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Audric said:

Strange, -Wformat is implied by -Wall, so you should get it.

Not the same.

pepsi@fractal:~ 05:19 PM $ gcc -O2 -Wall -Wextra -c pong.c
pong.c: In function 'main':
pong.c:275:14: warning: unused parameter 'argc'
pong.c:275:27: warning: unused parameter 'argv'
pepsi@fractal:~ 05:19 PM $ gcc -O2 -Wall -c pong.c
pepsi@fractal:~ 05:19 PM $ gcc -O2 -Wall -c pong.c

They all watch too much MSNBC... they get ideas.

Luiji99
Member #12,254
September 2010

It's not giving a warning because that's not a problem. As far as the compiler's concerned, due to the missing argument, score is being passed as the format message. As such, it can't detect any formatting issues because the format is, itself, invalid.

Programming should be fun. That's why I hate Java.

Temia Eszteri
Member #14,459
July 2012
avatar

Nevermind, figured it out, forgot the y value but my mind was completing the code when I read it. For x I had SCREEN_W - 50 and for some reason my mind was doing SCREEN_W for x and 50 for y when I read it. facepalm

And this is why it's always best to copy-paste code directly when troubleshooting.

“Nothing is withheld from us what we have conceived to do.” —Russell Kirsch

Specter Phoenix
Member #1,425
July 2001
avatar

And this is why it's always best to copy-paste code directly when troubleshooting.

Not really, I remember one time on here I posted my code for something asking about doing menus with Allegro's GUI code and there were like 20 posts where everyone said it was something else and it ultimately turned out to be a similar issue as this, had just forgot to pass a 0 for a flag. Took about the same number of posts as here before anyone even caught it because everyone did what I did and kept putting it in mentally :P. Speaking of which I need to learn menus (game screens) for the start up screens and such :).

Thomas Fjellstrom
Member #476
June 2000
avatar

Not really,

Depends if you're showing the actual code you're using or not. Some people tend to not actually show the code that they are actually using.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Specter Phoenix
Member #1,425
July 2001
avatar

Depends if you're showing the actual code you're using or not. Some people tend to not actually show the code that they are actually using.

True.

Go to: