glut - print the text entered on keyboard onto the window created in opengl -


trying display characters typed on keyboard,i using following code.

 void mykey(unsigned char key, int x, int y) {   if (key == 13) // enter key {     return; }    glrasterpos2f(xpos, 600);    glcolor3f(0.0, 0.0, 1.0); // text color    glutbitmapcharacter(glut_bitmap_times_roman_24, key); // print color     glflush();    xpos += 15;    player1[i] = key;    += 1; } 

it prints text entered onto screen but, doesnt exit supposed when press enter. want the code display player name of player1 , store array exit when press enter , continue accept second player name.

only opengl stuff in display callback.

you need break text entry 2 pieces:

  1. keyboard/array handling in glutkeyboardfunc() callback. once you're done modifying name list post redisplay event.
  2. string rendering in glutdisplayfunc() callback, iterate on name vector , display each string.

like so:

#include <gl/freeglut.h> #include <sstream> #include <string> #include <vector>  using namespace std;  vector< string > names( 1 ); void keyboard( unsigned char key, int x, int y ) {     if( key == 13 )     {         // enter key         names.push_back( "" );     }     else if( key == 8 )     {         // backspace         names.back().pop_back();     }     else     {         // regular text         names.back().push_back( key );     }      glutpostredisplay(); }  void display() {     glclear( gl_color_buffer_bit );      glmatrixmode( gl_projection );     glloadidentity();     double w = glutget( glut_window_width );     double h = glutget( glut_window_height );     glortho( 0, w, 0, h, -1, 1 );      glmatrixmode( gl_modelview );     glloadidentity();      for( size_t = 0; < names.size(); ++i )     {         ostringstream oss;         oss << ( + 1 ) << ": " << names[i];          void* font = glut_bitmap_9_by_15;         const int fontheight = glutbitmapheight( font );         glrasterpos2i( 10, h - ( fontheight * ( + 1 ) ) );         glutbitmapstring( font, (const unsigned char*)( oss.str().c_str() ) );     }      glutswapbuffers(); }  int main( int argc, char **argv ) {     glutinit( &argc, argv );     glutinitdisplaymode( glut_rgba | glut_double );     glutinitwindowsize( 640, 480 );     glutcreatewindow( "glut" );     glutdisplayfunc( display );     glutkeyboardfunc( keyboard );     glutmainloop();     return 0; } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -