Creating a menu in openGL C++

I am trying to create a menu after inputting text from where the cursor is located. The menu needs to be able to change color and font of text and also have an option to quit the program. For some reason my program is not working. First the words won’t display when I start typing. Then when I try to open the menu it quits the program. How do I fix this?

My code is below:

#include <iostream>					// include iostream library
#include <cmath>					// include cmath library
#include <GL/glut.h>				// include GLUT library
#include <string>
void colorMenu(int value);
void fontMenu(int value);
void mainMenu(int value);
//***********************************************************************************
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 mainMenu(int value) 
{
	switch (value)
	{
	case 1:
		exit(0);
		break;
	default:
		break;
	}
}

void colorMenu(int value) 
{
	switch (value)
	{
	case 1:
		r = 1;
		g = 0;
		b = 0;
		glColor3f(r, g, b);
		glRasterPos2i(x, y);
		for (int i = 0; i < message.length(); i++) {
			glutBitmapCharacter(GLUT_BITMAP_9_BY_15, message[i]);
		}
	case 2:
		r = 0;
		g = 0;
		b = 1;
		glColor3f(r, g, b);
		glRasterPos2i(x, y);
		for (int i = 0; i < message.length(); i++) {
			glutBitmapCharacter(GLUT_BITMAP_9_BY_15, message[i]);
		}
	case 3:
		r = 0;
		g = 1;
		b = 0;
		glColor3f(r, g, b);
		glRasterPos2i(x, y);
		for (int i = 0; i < message.length(); i++) {
			glutBitmapCharacter(GLUT_BITMAP_9_BY_15, message[i]);
		}
	case 4:
		exit(0);
		break;
	}

	myDisplayCallback();
	
	glFlush();
}

void fontMenu(int value)
{
	switch (value)
	{
	case 1:
		glColor3f(r, g, b);
		glRasterPos2i(x, y);
		for (int i = 0; i < message.length(); i++) {
			glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, message[i]);
		}
	case 2:
		glColor3f(r, g, b);
		glRasterPos2i(x, y);
		for (int i = 0; i < message.length(); i++) {
			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, message[i]);
		}
	}
}

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++) {
		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);
	glutMotionFunc(myMotionCallback);
	
	glutCreateMenu(colorMenu);
		glutAddMenuEntry("Red", 1);
		glutAddMenuEntry("Blue", 2);
		glutAddMenuEntry("Green", 3);

	glutCreateMenu(fontMenu);
		glutAddMenuEntry("Times New Roman", 1);
		glutAddMenuEntry("Helvetica", 2);
	
	glutCreateMenu(mainMenu);
		glutAddSubMenu("Color", glutCreateMenu(colorMenu));
		glutAddSubMenu("Font", glutCreateMenu(fontMenu));
		glutAddMenuEntry("Exit", 1);

	glutAttachMenu(GLUT_RIGHT_BUTTON);

	
	glutMainLoop();							// get into an infinite loop
	return 0;
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.