I am trying to write text where ever the cursor is located in the opengl window. But my code will not display the text when I type. it only displays when i click first. and the it disappears when i open the menu and reappears when I click again. How do I fix this?
Code is in C++ below:
#include <iostream>// include iostream library
#include <cmath>// include cmath library
#include <GL/glut.h> // include GLUT library
using namespace std;
int ch = 0;
//***********************************************************************************
void drawAxis()
{
glPointSize(1); // change point size back to 1
glBegin(GL_POINTS); // use points to form X-/Y-axes
glColor3f(0, 0, 0); // change drawing color to black
for (int x = -150; x <= 150; x++) // draw X-axis
glVertex2i(x, 0);
for (int y = -150; y <= 150; y++) // draw Y-axis
glVertex2i(0, y);
glEnd();
glRasterPos2i(150, 5);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'X');
glRasterPos2i(5, 150);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'Y');
}
//***********************************************************************************
void myInit()
{
glClearColor(1, 1, 1, 0); // specify a background clor: white
gluOrtho2D(-200, 200, -200, 200); // specify a viewing area
}
//***********************************************************************************
void myDisplayCallback()
{
glClear(GL_COLOR_BUFFER_BIT); // draw the background
drawAxis();
glFlush(); // flush out the buffer contents
}
bool started = false;
int x, y = -1000;
int r, g, b = 0;
float offset = 0;
std::string message = "";
void myKeyboardCallback(unsigned char key, int cursorX, int cursorY) {
message += key;
myDisplayCallback();
if (started == false) {
x = cursorX - 200;
y = 200 - cursorY;
}
glColor3f(r, g, b);
glRasterPos2i(x, y);
for (int i = 0; i < message.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, message[i]);
}
started = true;
glFlush();
}
void myMenuCallback(int value)
{
switch (value)
{
case 1: {
r = 1;
g = 0;
b = 0;
glColor3f(r, g, b);
glRasterPos2i(x, y);
ch = 1;
break; }
case 2: {
r = 0;
g = 0;
b = 1;
glColor3f(r, g, b);
glRasterPos2i(x, y);
ch = 2;
break;
}
case 3: {
r = 0;
g = 1;
b = 0;
glColor3f(r, g, b);
glRasterPos2i(x, y);
ch = 3;
break; }
case 4: {
exit(0);
break;
}
case 5: {
ch = 5;
break;
}
case 6: {
ch = 6;
break;
}
case 7: {
ch = 7;
break;
}
case 8: {
ch = 8;
break;
}
case 9: {
ch = 9;
break;
}
}
myDisplayCallback();
}
void myMotionCallback(int cursorX, int cursorY) {
myDisplayCallback();
glColor3f(r, g, b);
x = cursorX - 200;
y = 200 - cursorY;
glRasterPos2i(x, y);
for (int i = 0; i < message.length(); i++) {
if (ch == 5) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, message[i]); }
if (ch == 6) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, message[i]); }
if (ch == 7) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, message[i]); }
if (ch == 8) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, message[i]); }
if (ch == 9) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, message[i]); }
if (ch == 1 || ch == 2 || ch == 3) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, message[i]);
}
}
glFlush();
}
//***********************************************************************************
int main(int argc, char** argv)
{
glutInit(&argc, argv); // optional in our environment
//getUserInput();
glutInitWindowSize(400, 400); // specify a window size
glutInitWindowPosition(100, 0); // specify a window position
glutCreateWindow("Simple Text Drawing"); // create a titled window
myInit(); // setting up
glutDisplayFunc(myDisplayCallback); // register a callback
glutKeyboardFunc(myKeyboardCallback);
int sub1 = glutCreateMenu(myMenuCallback);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Blue", 2);
glutAddMenuEntry("Green", 3);
glutCreateMenu(myMenuCallback);
int sub2 = glutCreateMenu(myMenuCallback);
glutAddMenuEntry("TIMES_ROMAN_10", 5);
glutAddMenuEntry("TIMES_ROMAN_24", 6);
glutAddMenuEntry("HELVETICA_10", 7);
glutAddMenuEntry("HELVETICA_12", 8);
glutAddMenuEntry("HELVETICA_18", 9);
glutCreateMenu(myMenuCallback);
glutAddSubMenu("Colors", sub1);
glutAddSubMenu("Fonts", sub2);
glutAddMenuEntry("Exit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMotionFunc(myMotionCallback);
glutMainLoop(); // get into an infinite loop
return 0;
}