Question

I am trying to create a menu after inputting text from where the cursor is located...

I am trying to create a menu after inputting text from where the cursor is located in openGL window in C++. 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 click on a color it quits the program. How do I fix this?

My code is below:

#include // include iostream library
#include // include cmath library
#include <GL/glut.h> // include GLUT library
#include

//***********************************************************************************
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);
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 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);

int sub1 = glutCreateMenu(myMenuCallback);

glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Blue", 2);
glutAddMenuEntry("Green", 3);
glutCreateMenu(myMenuCallback);
glutAddSubMenu("Colors", sub1);

glutAddMenuEntry("Exit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);

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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// when start your program curser must be within this window

#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;
}

Add a comment
Know the answer?
Add Answer to:
I am trying to create a menu after inputting text from where the cursor is located...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help with this C++ code. I am trying to compile and I keep running...

    Can someone help with this C++ code. I am trying to compile and I keep running into these 4 errors. #include <iostream> #include <cassert> #include <string> using namespace std; typedef int fsm_state; typedef char fsm_input; bool is_final_state(fsm_state state) { return (state == 3) ? true : false; } fsm_state get_start_state(void) { return 0; } fsm_state move(fsm_state state, fsm_input input) { // our alphabet includes only 'a' and 'b' if (input != 'a' && input != 'b') assert(0); switch (state) {...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

    Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested for file open.” Append to the program to output to the text log file a new line starting with day time date followed by the message "SUCCESSFUL". Append that message to a file “7Error_Log_File.txt” . ?newline Remember to be using fprintf using stderr using return using exit statements. Test for file existence, test 7NoInputFileResponse.txt file not null (if null...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • Please do a flowchart which will be on the computer not on paper so i can...

    Please do a flowchart which will be on the computer not on paper so i can read it and be able to take screenshot for this c++ code this code for MasterMind //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> #include <ctime> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //main int main(int argc, char** argv) { //Set the random number seed srand(time(0)); int randomint = (rand()%5)+1;...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • In C++ I am trying to implement a log base 2 hash table utilizing the shift...

    In C++ I am trying to implement a log base 2 hash table utilizing the shift function. So, the code determines the position of where the value will be stored using log2. But am having issues using the shift part as everything just wants to pile on to zero. //ChainingHash - log2 #include<iostream> #include<vector> #include<iterator> #include<string> #include<cmath> using namespace std; #define BUCKET 10 //no. of buckets class Hash {    vector<int>*table; //ptr containing buckets       public:        Hash();...

  • Has to be in Unix! Lab 7 Programs 1. Create a prints.c file, and copy the...

    Has to be in Unix! Lab 7 Programs 1. Create a prints.c file, and copy the following into it: #include <stdio.h> int main(int argc, char * argv[]) { double x = 1.0, y = 5.1e7; float pi = 3.14159f; int i=0, j=5115; short int m= 6; unsigned long int n = 51151151; return 0; Now, write printf commands to: • print x with a field width of 6; print y with a field width of 8 and a precision of...

  • LW: Class Constructors Objectives Write a default constructor for a class. Note that this const...

    LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT