[英]Displaying Text to Window C++
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:
: 我使用此功能来显示单词
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);
我使用以下
drawBitmapText("score: ",score,0,1,0,10,220,0);
调用上述function()
: 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. 它成功显示了单词
Score:
并且在正确的位置,但是我遇到的问题是在其旁边包括表示分数的实际int
。
How do I incorporate the int
to be displayed too? 如何合并要显示的
int
? I pass it successfully. 我成功通过了。
I've tried converting it a string/char
and adding/concatenating it but it just displays random letters... Thanks. 我试过将其转换为
string/char
并添加/连接它,但它仅显示随机字母...谢谢。
Since you are using C++ it's going to be so much easier to start using C++ libraries to work with strings. 由于您使用的是C ++,因此开始使用C ++库来处理字符串会容易得多。 You can use
std::stringstream
to concatenate the caption and score. 您可以使用
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);
}
}
use std::stringstream
使用
std::stringstream
for example 例如
std::stringstream ss;
ss << "score: " << score;
then call 然后打电话
ss.str().c_str();
to output ac string 输出交流弦
You can use snprintf
to create a formatted string, the same way you use printf to print a formatted string to the console. 您可以使用
snprintf
创建格式化的字符串,就像使用printf将格式化的字符串打印到控制台一样。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.