How do I display a C++ string object in Allegro?

Started by
13 comments, last by darenking 18 years, 10 months ago
Allegro lets you display text like this: textprintf(m_Buffer, font, 0, i*10, WHITE, "Test: %s", input ); input is a pointer to an array of chars. Does anyone know if you can print text from a C++ string object? Maybe it has to be converted to an array of chars with a pointer? It seems rather messy!
Advertisement
The method c_str of std::string gives you a (probably const) char * containing the string as a null-terminated C string.

What I don't know is how long this is guaranteed to be valid until. Probably until you change the contents of the string.

Mark
Thanks markr. Trying to make that work.

Have my loop that displays the messages:
for (int i=0 ; i<30 ; i++)
{
textprintf(m_Buffer, font, 0, i*10, WHITE, "Test: %s", m_Input );
}

Am now trying to copy a C++ string object into one of those messages.

I've created a new string object like this:
std::string newstring="help!";

Then I try to copy it like this:
m_Input[1]=newstring.c_str;

But I get an error:
ISO C++ forbids taking the address of a bound member function to form a pointer
and
Cannot convert 'const char'

Any ideas? I get so baffled with pointers.
Why don't you just store the text as strings and convert them during rendering?
for (int i=0 ; i<30 ; i++) {  textprintf(m_Buffer, font, 0, i*10, WHITE, "Test: %s", m_Input.c_str() );}

where m_Input is an array of strings. If you want to do it your way still, c_str is a member function, not a variable.
When you say an array of strings do you mean C++ string objects? Or old c style strings?
Well, I suppose it would depend which is easiest. There's no sense in converting a char array to a std::string just to turn it back. Just use whatever you feel comfortable with.
Quote:Original post by joebarnslondon
Then I try to copy it like this:
m_Input[1]=newstring.c_str;

But I get an error:
ISO C++ forbids taking the address of a bound member function to form a pointer
and
Cannot convert 'const char'

Any ideas? I get so baffled with pointers.


cstr() is a function. The syntax your using suggests to the compiler that you want to set m_Input[1] to the address of the member function, which it doesn't like unsurprisingly.

add the brackets to show you are calling the functon.

It's one of those errors that is baffling until you've experienced it a few times.

[size="1"]
mrbastard, I've put in the brackets, but get this error:
invalid conversion from 'const char' to 'char'

bytecoder, I'm not sure which to store as really. All I know for (fairly) sure is that Allegro needs each message to be a pointer to an array of characters.

Is an array of characters the same as an "old C style string"? I know it's not the same as a string object, but beyond that I'm confused.

Which is the easiest for reading in from a Windows .txt file? I need to read a line in from a text file and display it on the screen (for which it needs to be a pointer to an array of chars).

I guess I choose the type that is easiest for reading in, then convert it when I render?

But the question is, which is easiest? I'm not experienced with any of them. Am willing to learn but perhaps someone can suggest which is easiest?

Or, maybe I can read in as an array of chars and not have to convert?

An array of std::string is easiest IMHO and safer - std::string has a destructor that clears up after itself, unlike a null terminated string.

Have an array of strings: std::string m_Input[num];
copy the input to the array: m_Input = this_string;
(where this_sting is a std:string)
then use .c_str() whenever you need to pass it as a parameter to a function expecting c style strings.
[size="1"]
mrbastard (and friends), I'm having trouble with the syntax.

I've created a temporary string and stored it with some letters to test:
std::string tempstring="howdy";

Then I send it to a new method in my Test object:
m_Test.Add(7, tempstring);

The 7 tells it which message to store it in. The Test object has a private array of string objects:
std::string m_sText[100];

The method receives the 7 and the temp string like this:
void Test::Add(int line, std::string text)
{
m_sText[line]=text;
}

So far so good (or correct me if I'm wrong - it certainly compiles fine).

Finally, the Test object also has a method that draws all the m_sText[] strings to the screen (supposedly). But of course they need to be converted to an array pointers to chars (Allegro only seems to let you draw to the screen this way). As mrbastard says:
>copy the input to the array: m_Input = this_string;
>(where this_sting is a std:string)
>then use .c_str() whenever you need to pass it as a parameter to a function expecting c style strings.

But what's the syntax to do that?

Have tried this:
char* m_Input[30];//this is a private member
then in the method:
m_Input[0] = m_sText;
But I get 'cannot convert 'std::string' to 'char' in assignment.

If I try:
m_Input[0] = m_sText.c_str();
I get 'invalid conversion from 'const' to 'char'.

This topic is closed to new replies.

Advertisement