glutBitmapString/glutStrokeString似乎需要常量无符号字符* - 字符串不起作用



Ubuntu 11.04、G++、freeglut和GLUT。

我一点也不明白。这是我得到的错误:

whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutStrokeString(void*, const unsigned char*)’

如果我尝试glutBitmapString:

whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutBitmapString(void*, const unsigned char*)’

这是相关的代码(我想)。

scoreStream << "Score: " << score << "";
scoreString = scoreStream.str();
// ...in another method:
glRasterPos2i(0, 0);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString);

这个答案告诉我它应该起作用,但它就是不起作用。

那些不想跳的人的报价:

// Draw blue text at screen coordinates (100, 120), where (0, 0) is the top-left of the
// screen in an 18-point Helvetica font
glRasterPos2i(100, 120);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");

(另外,我试着在那里留下一个直接的字符串,比如"要渲染的文本"。没有骰子。)

whatever.cc:315:64: error: invalid conversion from ‘const char*’ to ‘const unsigned char*’

我很困惑。据我记忆所及,这是我关于SO的第一个问题,如果组合不太好,请道歉。我会提供任何我能提供的额外信息。

没有std::stringchar const*的自动转换。但是,您可以使用std::string::c_str()std::string中获取char const*

例如glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString.c_str());

注意:您链接到的答案并没有说明您可以使用std::string。它给出的示例(glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");)使用字符串文字,它是char的数组,而不是std::string

请记住OpenGL是一个C库,std::string在C中不存在。

最新更新