1

我正在尝试在一个小型且非常基本的游戏中在屏幕上显示分数。

我用这个函数来显示这个词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); }
}

我使用以下方法调用上述function()内容:drawBitmapText("score: ",score,0,1,0,10,220,0);

它成功地Score:在正确的位置显示了这个词,但我遇到的问题是包括int表示它旁边的分数的实际。

如何合并int要显示的内容?我顺利通过了。

我已经尝试将它转换为 astring/char并添加/连接它,但它只显示随机字母......谢谢。

4

3 回答 3

1

由于您使用的是 C++,因此开始使用 C++ 库来处理字符串会容易得多。您可以使用std::stringstream来连接标题和分数。

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); 
   }
}
于 2013-03-28T21:54:56.720 回答
0

采用std::stringstream

例如

std::stringstream ss;

ss << "score: " << score;

然后打电话

ss.str().c_str();

输出交流字符串

于 2013-03-28T21:50:16.480 回答
0

您可以使用它snprintf来创建格式化字符串,就像使用 printf 将格式化字符串打印到控制台一样。这是重写它的一种方法:

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);
}
于 2013-03-28T21:53:19.103 回答