Search code examples
c++openglglut

Is there an easier way to display text using C++ and OpenGL?


Using C++ and OpenGL (with GLUT) I am trying to display some dynamic text on the users screen. The text I am trying to display is the time it took to calculate everything needed to render Mandelbrot set.

So far I have managed to do it this way:

std::ostringstream stream;
stream << "Time: " << end_time-start_time;
const std::string& tmp = stream.str();
const char* cstr = tmp.c_str();
strcpy(message, cstr );
int len = (int) strlen(message);
glPushMatrix();
glTranslatef(20.0, 80.0, 0.0);
glScalef(0.1, 0.1, 0.0);
glColor3f(1.0,1.0,1.0);
for (int i = 0; i < len; i++) {
    glutStrokeCharacter(GLUT_STROKE_ROMAN, message[i]);
}
glPopMatrix();

As I am not very familliar with C++ and OpenGL I couldn't figure out a more concise way to do it but this seems to me to be way too complicated to display just a single sentance.

Can you recommend a better or less complicated way to do this? Any advice or cricism is welcome.


Solution

  • This is probably more succinct (c++11):

    void showTime()
    {
      glPushMatrix();
      glTranslatef(20.0, 80.0, 0.0);
      glScalef(0.1, 0.1, 0.0);
      glColor3f(1.0,1.0,1.0);
      for (auto ch : std::to_string(end_time - start_time))
      {
        glutStrokeCharacter(GLUT_STROKE_ROMAN, ch);
      }
      glPopMatrix();
    }