开发者

glutBitmapString/glutStrokeString seem to require const unsigned char* - strings don't work

Ubuntu 11.04, G++, freeglut and GLUT.

I don't understand this at all. Here's the error I get:

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

and if I try glutBitmapString:

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

Here's the relevant code (I think).

scoreStream << "Score: " << score << "\0";
scoreString = scoreStream.str();

// ...in another method:

glRasterPos2i(0, 0);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString);

This answer tells me it should work, but it just doesn't.

Quote for those who don't want to jump:

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

(Also, I have tried leaving a direct string like "text to render" in there. No dice.)

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

I am confused. This is my first question on SO, as far as I remember, so apologies if it isn't too well put together. I'll provide any extra information I can.


There is no automatic conversion of an std::string to a char const*. However, you can use std::string::c_str() to get the char const* out of an std::string.

e.g. glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString.c_str());

Note: The answer you linked to doesn't say that you can use std::string. The example it gives (glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");) uses a string literal, which is an array of char, not an std::string.

Remember that OpenGL is a C library, and std::string doesn't exist in C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜