My program opens a menu after right clicking. In the menu there is a help link that opens a second glut window called help. I am trying to make it where I can exit that window by right clicking and pressing exit but when I press exit, it closes the whole program. How do I just make it hide the help window and just go back to the main window?
My C++ code is below:
#include <iostream>// include iostream library
#include <cmath>// include cmath library
#include <GL/glut.h> // include GLUT library
bool started = false;
int x, y = -1000;
int r, g, b = 0;
float offset = 0;
std::string input = "";
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();
glColor3f(r, g, b);
glRasterPos2i(x, y);
for (int i = 0; i < input.length(); i++) {
if (ch == 5) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, input[i]); }
if (ch == 6) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, input[i]); }
if (ch == 7) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, input[i]); }
if (ch == 8) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, input[i]); }
if (ch == 9) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, input[i]); }
if (ch == 1 || ch == 2 || ch == 3) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, input[i]);
}
}
glFlush(); // flush out the buffer contents
}
void menuText() {
glRasterPos2i(-20, 150);
std::string titleText = "Help";
for (int i = 0; i <= titleText.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, titleText[i]);
}
glRasterPos2i(-195, 130);
std::string firstlineText = "This program allows you to type text where ever the cursor is located in the window.";
for (int i = 0; i <= firstlineText.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, firstlineText[i]);
}
glRasterPos2i(-195, 118);
std::string secondlineText = "Just place the cursor where you want it on the window and start typing.";
for (int i = 0; i <= secondlineText.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, secondlineText[i]);
}
glRasterPos2i(-195, 106);
std::string thirdlineText = "Right click to open the menu where you can change the color and font of the text.";
for (int i = 0; i <= thirdlineText.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, thirdlineText[i]);
}
glRasterPos2i(-195, 93);
std::string fourthlineText = "You also have the option to exit the program from the menu.";
for (int i = 0; i <= fourthlineText.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, fourthlineText[i]);
}
glRasterPos2i(-195, 81);
std::string fifthlineText = "Left click and hold to move the text anywhere in the window.";
for (int i = 0; i <= fifthlineText.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, fifthlineText[i]);
}
glRasterPos2i(-195, 69);
std::string sixthlineText = "To exit help window right click and click exit.";
for (int i = 0; i <= sixthlineText.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, sixthlineText[i]);
}
}
void myMenuDisplayCallback() {
glClear(GL_COLOR_BUFFER_BIT);
menuText();
glFlush();
}
void myKeyboardCallback(unsigned char key, int cursorX, int cursorY) {
input += key;
myDisplayCallback();
if (started == false) {
x = cursorX - 200;
y = 200 - cursorY;
}
glColor3f(r, g, b);
glRasterPos2i(x, y);
for (int i = 0; i < input.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, input[i]);
}
started = true;
glFlush();
}
void myMenuCallback(int value)
{
switch (value)
{
case 1: { //turn red
r = 1;
g = 0;
b = 0;
ch = 1;
break; }
case 2: { //turn blue
r = 0;
g = 0;
b = 1;
ch = 2;
break;
}
case 3: { //turn green
r = 0;
g = 1;
b = 0;
ch = 3;
break; }
case 4: { //exit program
exit(0);
break;
}
case 5: { //change font
ch = 5;
break;
}
case 6: { //change font
ch = 6;
break;
}
case 7: { //change font
ch = 7;
break;
}
case 8: { //change font
ch = 8;
break;
}
case 9: { //change font
ch = 9;
break;
}
case 10:{
glutInitWindowSize(400, 400); // specify a window size
glutInitWindowPosition(550, 0); // specify a window position
glutCreateWindow("Help"); // create a titled window
myInit(); // setting up
glutDisplayFunc(myMenuDisplayCallback);
glutCreateMenu(myMenuCallback);
glutAddMenuEntry("Exit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glFlush();
break;
}
}
myDisplayCallback();
}
void myMotionCallback(int cursorX, int cursorY) {
myDisplayCallback();
glColor3f(r, g, b);
x = cursorX - 200;
y = 200 - cursorY;
glRasterPos2i(x, y);
glFlush();
}
//***********************************************************************************
int main(int argc, char** argv)
{
glutInit(&argc, argv); // optional in our environment
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("Help", 10);
glutAddMenuEntry("Exit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMotionFunc(myMotionCallback);
glutMainLoop(); // get into an infinite loop
return 0;
}