Sign in to follow this  
darenking

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

Recommended Posts

darenking    122
Allegro lets you display text like this: textprintf(m_Buffer, font, 0, i*10, WHITE, "Test: %s", input[i] ); input[i] 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!

Share this post


Link to post
Share on other sites
markr    1692
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

Share this post


Link to post
Share on other sites
darenking    122
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[i] );
}

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.

Share this post


Link to post
Share on other sites
bytecoder    100
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[i].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.

Share this post


Link to post
Share on other sites
bytecoder    100
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.

Share this post


Link to post
Share on other sites
mrbastard    1577
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.

Share this post


Link to post
Share on other sites
darenking    122
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?

Share this post


Link to post
Share on other sites
mrbastard    1577
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[i] = 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.

Share this post


Link to post
Share on other sites
darenking    122
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[i] = 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[i];
But I get 'cannot convert 'std::string' to 'char' in assignment.

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

Share this post


Link to post
Share on other sites
nobodynews    3126
Quote:
Have tried this:
char* m_Input[30];//this is a private member
then in the method:
m_Input[0] = m_sText[i];


But I get 'cannot convert 'std::string' to 'char' in assignment.

If I try:


m_Input[0] = m_sText[i].c_str();

I get 'invalid conversion from 'const' to 'char'.


You're very close, I think.

1)m_Input is a pointer to a char
2)m_Input is a char
3)m_sText[i] is a string
4)m_sText[i].c_str() is a pointer to a char

It looks like in this case you need to use the C function strncpy. It is used as follows:


#include <cstring> // contains strncpy and strcpy functions
#include <string>

// prototype: char *strncpy(char *dest, const char *src, size_t n);
// dest is the destination of the array
// source i the source of the array
// n is the maximum number of letters to copy(ie, if you're destination is 30
// and your source is 100, you won't want to write out of bounds

int main() {
char * name[30] = "Programmer Dan";
char * temp[30];
std::string name2 = "Programmer Ted";

strncpy(temp, name, 30);
//temp now contains "Programmer Dan"

strncpy(temp, name2.c_str(), 30);
//temp now contains "Programmer Ted"

return 0;
}


Hope this helps

Share this post


Link to post
Share on other sites
Fruny    1658
How about std::string::copy() if you want to copy the string to a buffer?

Otherwise, a const_cast may save your bacon.

Share this post


Link to post
Share on other sites
morx    176
Joe,

What version of allegro are you using? I'm thinking that textprintf might actually be written without a 'const' for the last parameter, and this would be the cause of the warning. I checked versions 4.1.17 and 4.0.3 and they had the 'const', but perhaps you are using an older version? Just an idea.. One thing to look for, would be in allegro's documentation, under "text output". The function should be:

textprintf( BITMAP *, FONT *, int, int, int color, const char *, ... );

If you see it like this:

textprintf( BITMAP *, FONT *, int, int, int color, char *, ... );
^ -- no const

If it is old-style, it may not have the 'const' keyboard there.. in which case, you could force-cast the std::string into non const (bad idea), or it is time to upgrade Allegro.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this