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ですか?無事合格です。

変換してstring/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();

ac文字列を出力する

于 2013-03-28T21:50:16.480 に答える
0

snprintfprintf を使用して書式設定された文字列をコンソールに出力するのと同じ方法で、 を使用して書式設定された文字列を作成できます。これを書き換える 1 つの方法を次に示します。

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 に答える