Jump to content
  • Advertisement
Sign in to follow this  
Kjansen92

OpenGL How to Use glutBitMapString / glutBitMapCharacter

This topic is 1474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

 

I am working on an openGL program, and would like to display a string of text on the screen.

 

I have attempted many variations of glutBitMapCharacter and glutBitMapString, and am having difficulty in converting a string to const unsigned char *, as well as getting it to display on the screen after all.

 

My best results were simply a "1" displayed, nothing displayed, as well as a mess of characters displayed in repeating order.

 

I've pasted the current code below. I believe there must be a correct way to accomplish this, as many games have been made in openGL.

 

Could anyone please assist me with where to go with this? Please keep in mind that I wish to use a string, and not a literal string (i.e. "Hello Gamedev.net!"). I am not averse to learning new code, and would like the best approach to accomplish this.

 

 

This is my display function:

 

void displayMapInfo() {
size_t max = mapName.size();
char *filePath = new char[strlen(mapName.c_str()) + 1];
strcpy_s(filePath, max, mapName.c_str());
RenderString(mapInfoXPos, mapInfoYPos, filePath);
}
 
 
..and this is my render string function:
 
void RenderString(GLdouble x, GLdouble y, const char *string)
{
const char *c;
glColor3d(1.0, 0.0, 0.0);
glRasterPos2d(x, y);
for (c = string; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
}
}
 
Thank you in advance for reading this.
 
 

 

Share this post


Link to post
Share on other sites
Advertisement

Your code leaks memory; you never release what you allocate in displayMapInfo. Get rid of all those char pointers and the allocation entirely and just use the string directly. There is no point in converting the string from one type to another just to access its characters, when the first type is perfectly fine for accessing the characters.

void displayMapInfo() {
    RenderString(mapInfoXPos, mapInfoYPos, mapName);
}
 
void RenderString(GLdouble x, GLdouble y, const std::string &string)
{
    glColor3d(1.0, 0.0, 0.0);
    glRasterPos2d(x, y);
    for (int n=0; n<string.size(); ++n) {
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[s]);
    }
}

Share this post


Link to post
Share on other sites

Thank you Bob,

 

Although it still does not display anything unless I explicitly send an unsigned char *, such as "Hello Bob!"

 

For some reason, a stored string variable will not display in this way. 

Share this post


Link to post
Share on other sites

Do you mean that this works

RenderString(x, y, "hello");

but this doesn't?

std::string string myString = "hello";
RenderString(x, y, myString);

Share this post


Link to post
Share on other sites

No, both of those work, but only if the string is written that way within the function.

 

What I want is to be able to access string variables - particularly as private members of objects.

Share this post


Link to post
Share on other sites

Have you verified then that the string actually contains what you think it does? Use a debugger and inspect the actual value of the string at the time you attempt to print it. If printing a string ultimately works, then the contents of the string should be the next point on your list to verify.

 

Can you show a complete program that attempts to just display a string, but doesn't work? Strip away everything that isn't necessary to reproduce your problem.

Share this post


Link to post
Share on other sites

Here is the basic operation. I know that within my program, the string values are correct, as I can get a std::cout output of them.

 

This is a very simplified version, although I'm more interested in finding a correct library to work with, or more intelligent way of going about this, than getting my code to run the way it is.

 

class Object {

public:

Object(string s) {

       name = s;

}

~Object() {}

string getName() {

       return name;

}

private:

string name;

};

 

**** This is within the openGL display() function

displayInfo

 

int main() {

Object object = Object("Freedom");

}

 

void drawText(const string &name, GLdouble x, GLdouble y)
{
       glColor3f(1.0, 0.0, 0.0);  
       glRasterPos2f(x, y);
       for (int i = 0; i < name.size(); ++i)
       {
               glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, name);
       }
}

 

void displayInfo() {
       drawText(object.getName(), 10.0, 10.0);
}
 
**edited main function
** did not include any of the openGL initializations.
**
Edited by Kjansen92

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!