1

I'm trying to display a score to the screen on a small and very basic game.

I use this function to display the word Score::

void drawBitmapText(char *string, int score, float r, float g, float b, float x,float y,float z) {  
   char *c;
   glColor3f(r,g,b);
   glRasterPos3f(x,y,z);
   for (c=string; *c != '\0'; c++) { 
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *c); }
}

I call the above function() using: drawBitmapText("score: ",score,0,1,0,10,220,0);

It successfully displays the word Score: and in the right place, but the problem I'm having is including the actually int that represents the score next to it.

How do I incorporate the int to be displayed too? I pass it successfully.

I've tried converting it a string/char and adding/concatenating it but it just displays random letters... Thanks.

Reanimation
  • 3,151
  • 10
  • 50
  • 88

3 Answers3

1

Since you are using C++ it's going to be so much easier to start using C++ libraries to work with strings. You can use std::stringstream to concatenate the caption and score.

using namespace std;

void drawBitmapText(string caption, int score, float r, float g, float b, 
   float x,float y,float z) {  
   glColor3f(r,g,b);
   glRasterPos3f(x,y,z);
   stringstream strm;
   strm << caption << score;
   string text = strm.str();
   for(string::iterator it = text.begin(); it != text.end(); ++it) {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *it); 
   }
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Perfect. Solves the problem brilliantly. Nice code to remember. Thanks for your time. – Reanimation Mar 28 '13 at 22:02
  • 1
    I'm glad it helped. I can't encourage you enough to try to forget about null-terminated C strings and start using C++ strings. It makes life so much easier. – David Heffernan Mar 28 '13 at 22:03
0

use std::stringstream

for example

std::stringstream ss;

ss << "score: " << score;

then call

ss.str().c_str();

to output a c string

kkuryllo
  • 340
  • 3
  • 12
0

You can use snprintf to create a formatted string, the same way you use printf to print a formatted string to the console. Here's one way of rewriting it:

void drawBitmapText(char *string, int score, float r, float g, float b, float x,float y,float z) {
    char buffer[64]; // Arbitrary limit of 63 characters
    snprintf(buffer, 64, "%s %d", string, score);
    glColor3f(r,g,b);
    glRasterPos3f(x,y,z);
    for (char* c = buffer; *c != '\0'; c++)
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *c);
}
Tony
  • 3,470
  • 1
  • 18
  • 23
  • This is really cool, but `snprintf` threw an error, apparently it's not part of C89. It's standard only in C99. see: http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 Thanks for your time anyway :D – Reanimation Mar 28 '13 at 22:05
  • 1
    If you want C89 compatibility, you can use `sprintf` (no max-length parameter) as long as you make sure your buffer has enough room for all possible values of score. Alternatively, you can use the Microsoft-specific `_snprintf`, in which case you need to add a null character to the end of your buffer after you call it. – Tony Mar 29 '13 at 12:41
  • Ah thanks @Tony. I appreciate your time. Thats nice to know :D I will look into it for future reference. – Reanimation Mar 29 '13 at 14:21