🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to pass a char variable to glPrint?

Started by
8 comments, last by Mike00 23 years, 11 months ago
I''m pretty new to openGL, and want to use a non-constant char array with glPrint. Something like this: GLvoid MyText(char thetext[200]){ glLoadIdentity(); glTranslatef(0.0f,0.0f,-15.0f); glColor3f(1.0f,0.0f,0.0f); glPrint(text[200]); } int DrawGLScene(GLvoid) glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT); MyText("Hello"); return TRUE; } I get this error: error C2664: ''glPrint'' : cannot convert parameter 1 from ''char'' to ''const char *'' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast By the way, using MS Visual C++ 6.0. Thanks!
Advertisement
try this declaration instead

glvoid mytext(char* pText)
{
...
}


the call will be the same, but if you need to know the length of the string passed, u can find it using strlen, or pass the length as an additional parameter


By calling glPrint with text[200], you''re not sending a 200 bytes array, but the 200th element of this array (the 201st, in fact), hence a char. By using glPrint(text) instead, you''ll send the memory location of the array, which really is a char*, what the function is asking.

Hope this helps.

EL
----------------------------------------"Inash neteia haeg joa kavari quilm..." SD4
Hmm, thanks, I changed it to this:

char mytext[200]; (at top)

GLvoid TextBox(mytext){
glLoadIdentity();
glTranslatef(0.0f,0.0f,-15.0f);
glColor3f(1.0f,0.0f,0.0f);
glPrint(mytext);
}

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT); TextBox("Hello");
return TRUE;
}

And I get these errors:

error C2448: '''' : function-style initializer appears to be a function definition
error C2065: ''TextBox'' : undeclared identifier
I think your problem is with your definition of your TextBox function. Between the parenthesis (sp?), you have to put both the type and the name of your parameters.

This should do:
    GLvoid TextBox(char *Text){  glLoadIdentity();  glTranslatef(0.0f,0.0f,-15.0f);   glColor3f(1.0f,0.0f,0.0f);  glPrint(Text); }int DrawGLScene(GLvoid) {  glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT);   TextBox("Hello");  return TRUE; }    


As you might have seen, I didn''t declare any global Text variable. This variable beeing declared in the definition of TextBox, it will only be used within this function.
If you want your code to be some more portable, I suggest you push and pop matrices in your TextBox function instead of resetting the matrix.

It should work.

EL
----------------------------------------"Inash neteia haeg joa kavari quilm..." SD4
Thanks!
whats this glPrint command
ive never seen it before
It''s from NeHe''s tutorial on doing text from a texture (instead of a regular font).

Morgan
I get things like that alot, but im just a newbie to c++ and am still completely confused by pointers and casting!


Its life jim, but not as we know it!
Its life jim, but not as we know it!
The ''nature'' of the C++ language definition means that even ''experts'' get that alot. It''s very unconstrained, of course this can cause all kinds of problems if you''re not careful.

The usual sequence of events:

1. Call a function with some pointer related thing doesn''t work properly.
2. You add an ''*'' in the vain hope that it will fix it.
3. That doesn''t work so you add an ''&'' in the function declaration.
4. Nope, still not working - you spray random ''*'' and ''&'' symbol all over the place and crash the computer big time.
5. You ask someone who knows. They fix it. You ask them how they fixed it, "Dunno, it jus works for me"...

Oh dear.
Paul Grovespauls opengl page

This topic is closed to new replies.

Advertisement