Variables in textout_ex...
Jim Simon

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

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);

Jim Simon

Would this work with textout_ex as well?

Jim

ReyBrujo

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

Jim Simon

Thanks Rey

Jim

Erkle
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);

X-G

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);

ReyBrujo

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

X-G

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

Don Freeman
X-G said:

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

Hehehehe...:D

ReyBrujo

:D I just copy/pasted his example :P

Don Freeman

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

ReyBrujo

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

Thread #452010. Printed from Allegro.cc