Jump to content
  • Advertisement
Sign in to follow this  

drawString()

This topic is 4515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a function like this:
void drawString (const char *s)    //How do display text 
{
  unsigned int i; 
  for (i = 0; i < strlen (s); i++)
    glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, s);

};

and i want to call a function like this:
[source lange="cpp"]
drawString("Health");    //this line works just fine
drawString(player1.getHealth()+""); //player1.getHealth() returns an int

thats not possible because it says that i can't convert a const char to a char. How do i solve this problem? I tried different combinations of casting and maybe changing the drawString parameter but nothing seems to work. the function doesn't like me. :( why? BTW, thanks for putting up with all my questions. I know sometimes i ask a lot. :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
drawString(player1.getHealth()+""); //player1.getHealth() returns an int

You can not add an int and a char * string. You may change your drawString () function to:


//Draw a wsprintf () formatted text
#define MAX_STR_LEN (static_cast<unsigned int> (96))
void drawString (char const*const pFmtText, ...)
{
//Allocate memory for a temporary result string
char *const pTemp = static_cast<char *> (_alloca (MAX_STR_LEN * sizeof (*pTemp))) ;

//Pass these virtual arguments to vsprintf ()
va_list vaList ;
va_start (vaList, pFmtText) ;
int const TextLen = vsprintf (pTemp, pFmtText, vaList) ;
va_end (vaList) ;

unsigned int Counter = 0 ;
for (Counter = 0; Counter < TextLen ; ++Counter)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, pTemp[Counter]);
};
#undef MAX_STR_LEN


and you call drawString () as:
drawString ("The player health: %d", player1.getHealth()) ;

If you don't feel comfortable with these virtual arguments, you must convert player1.getHealth() to a string then pass it to your function.

Share this post


Link to post
Share on other sites
Original post by Skeleton_V@T
Quote:


If you don't feel comfortable with these virtual arguments, you must convert player1.getHealth() to a string then pass it to your function.


thats what i try to do. Like this:


std::ostringstream ostr; //turn int score into a string
ostr <<player1.getHealth();
std::string s = ostr.str();
drawString("Health"); //i have all the appropriate include statements
drawString(s);



but it won't let me do that. It says:

131 C:\Documents and Settings\mitchell\Desktop\Everything Game\MyGame.cpp cannot convert `std::string' to `const char*' for argument `1' to `void drawString(const char*)'

:?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!