Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Variables in textout_ex...

Credits go to ReyBrujo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Variables in textout_ex...
Jim Simon
Member #4,744
June 2004
avatar

I have the following line of code:

textprintf_ex(buffer, font, p1stuff.chp + "/" + p1stuff.mhp, 705, 501, color_text, -1);

I am getting errors concerning using the variables in the string. The variables represent current hit points and max hit points. How can I make this work properly?

Thanks in advance,
Jim

ReyBrujo
Moderator
January 2001
avatar

Are you using C++? It seems so. If you are using C:

char temp[512];

snprintf(temp, sizeof(temp), "%d/%d", p1stuff.chp, p1stuff.mhp);

textprintf_ex(buffer,
              font,
              temp,
              705,
              501,
              color_text,
              -1);

If you are using C++ and STL (not tested):

// textprintf_ex needs a char pointer, not a string, so I construct a
// temporary string and call c_str(), which returns "a pointer to a
// null-terminated array of characters representing the string's contents"
// according to the STL documentation.
textprintf_ex(buffer,
              font,
              string(p1stuff.chp + "/" + p1stuff.mhp).c_str(),
              705,
              501,
              color_text,
              -1);

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Jim Simon
Member #4,744
June 2004
avatar

Would this work with textout_ex as well?

Jim

ReyBrujo
Moderator
January 2001
avatar

Yes, or with any other function that needs a char pointer as argument.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Jim Simon
Member #4,744
June 2004
avatar

Thanks Rey

Jim

Erkle
Member #3,493
May 2003
avatar

void textprintf_ex(BITMAP *bmp, const FONT *f, int x, y, color, bg, const char *fmt, ...);

Try:

textprintf_ex(buffer, font, 705, 501, color_text, -1, "%d/%d", p1stuff.chp, p1stuff.mhp);

If the writing above has offended you, you've read it wrong.....fool.
And if you read that wrong it means 'All of the above is opinion and is only to be treated as such.'

X-G
Member #856
December 2000
avatar

Quote:

char temp[512];

snprintf(temp, sizeof(temp), "%d/%d", p1stuff.chp, p1stuff.mhp);

textprintf_ex(buffer,
font,
temp,
705,
501,
color_text,
-1);

... why in the world of jumping jehosephats would anyone do that?

textprintf_ex(buffer, font, 705, 501, color_text, -1, "%d/%d", p1stuff.chp, p1stuff.mhp);

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

ReyBrujo
Moderator
January 2001
avatar

Because it is 5:19 AM and at this hour I focus either on C++ or C :P

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

X-G
Member #856
December 2000
avatar

Do we need to point out that the syntax you used for textprintf_ex isn't even valid..? ;)

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Don Freeman
Member #5,110
October 2004
avatar

X-G said:

Do we need to point out that the syntax you used for textprintf_ex isn't even valid..?

Hehehehe...:D

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

ReyBrujo
Moderator
January 2001
avatar

:D I just copy/pasted his example :P

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Don Freeman
Member #5,110
October 2004
avatar

I know...but I couldn't pass up a :D! Hehehe... 8-)

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

ReyBrujo
Moderator
January 2001
avatar

That is Matthew's fault for not adding intellisense to the reply box ;)

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Go to: