繁体   English   中英

在Window C ++中显示文本

[英]Displaying Text to Window C++

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

我使用此功能来显示单词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); }
}

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

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

如何合并要显示的int 我成功通过了。

我试过将其转换为string/char并添加/连接它,但它仅显示随机字母...谢谢。

由于您使用的是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); 
   }
}

使用std::stringstream

例如

std::stringstream ss;

ss << "score: " << score;

然后打电话

ss.str().c_str();

输出交流弦

您可以使用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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM