How can I draw text in a glut window with the keyboard;
I have tried doing cin with

void TextBox::Keys(unsigned char key,int x,int y){
   switch(key){
      case 13://enter key
      cin >> String;
       DrawString = true;
      break;    
      case 27:
        exit(0);        
      break;                    
   }

and then in the draw function

fontx = tx + 3;
          fonty = ty + (th+8)/2;
         if(DrawString == true){
         glColor3f(0.0,0.0,0.0);               
         Font(GLUT_BITMAP_HELVETICA_12,(char*)String,fontx,fonty);
}

the intention here was to get the string from cin and put it in the string variable and then draw it to the screen. I know how to get
input with cin and put it in a variable with a console program, but i don't know how to do it with the glut keyboard. I also know how to draw text to the screen with glut. I have other questions but I will post them in seperate threads. Much thanks, Jody Bush

Dani AI

Generated

, cin blocks on stdin and does not integrate with GLUT’s event loop. You want to collect characters in the keyboard callback and redraw each frame. is on the right track: register glutKeyboardFunc, append printable chars to a buffer, handle backspace/enter yourself, and call glutPostRedisplay() so your draw routine runs again. Classic GLUT only renders one character at a time (glutBitmapCharacter), while freeglut also offers helpers like glutBitmapString. (freeglut.sourceforge.net)

Here is a minimal pattern that avoids console I/O and draws in window space so your text is never clipped by your current projection/modelview:

static std::string text;

void onKey(unsigned char key, int, int) {
    switch (key) {
        case 27: std::exit(0); break;           // ESC
        case 13: /* commit/submit text */ break; // Enter
        case 8:  if (!text.empty()) text.pop_back(); break; // Backspace
        default: if (key >= 32 && key <= 126) text.push_back((char)key);
    }
    glutPostRedisplay();
}

void drawText2D(int x, int y, const std::string& s) {
    int winH = glutGet(GLUT_WINDOW_HEIGHT);
    glWindowPos2i(x, winH - y);                 // window coords, top-left origin adjustment
    for (unsigned char ch : s)
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ch);
}

Notes:

  • Use glutSpecialFunc for arrows, function keys, etc., if you need editing/navigation keys. (manpages.opensuse.org)
  • glWindowPos2i sets the raster position directly in window coordinates and avoids the common gotcha where glRasterPos becomes invalid due to transforms/clipping. (docs.gl)
  • On freeglut you can tame key repeat with glutIgnoreKeyRepeat(GL_TRUE) during setup, or use glutBitmapString to render an entire C-string in one call. (freeglut.sourceforge.net)

This keeps input responsive and rendering deterministic without relying on the console.

You could use

Combined with function you can make this :

void renderBitmapString(
		float x, 
		float y, 
		float z, 
		void *font, 
		char *string) {  
  char *c;
  glRasterPos3f(x, y,z);
  for (c=string; *c != '\0'; c++) {
    glutBitmapCharacter(font, *c);
  }
}

That is provided by lighthouse, Their link

Now all you need is a little bit of logic.

For example :

string input = "";

void keyBoard(unsigned char key, int x, int y){ 
    input += key;
   void updateString();
}

and :

void updateString(){
   glutBimapString(0,0,0, GLUT_BITMAP_9_BY_15, input.c_str());
}

You can then call this function also in you draw function.

void drawFunc()
{
    glClear(someBITSGoesHerE);
    glLoadIdentity();
    updateString();
 //blah blah
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.