program6.cpp: In function ‘void handleButton(int, int, int, int)’:
program6.cpp:176:40: error: ‘incolormenuindex’ cannot be used as a function
program6.cpp:180:42: error: ‘intoolmenuindex’ cannot be used as a function
program6.cpp:217:49: error: ‘intoolmenuindex’ cannot be used as a function
program6.cpp:242:1: error: a function-definition is not allowed here before ‘{’ token
program6.cpp:633:1: error: expected ‘}’ at end of input
with this code:
Expand|Select|Wrap|Line Numbers
- /*Written by: Reid Tracy
- 11/26/12
- The purpose of this program is to be able to draw with different colors and different tools.
- * Color Menu Example Program
- To compile:
- * Linux: g++ -o program5 program5.cpp -lGL -lGLU -lglut -lm
- * Mac: g++ -o program5 program5.cpp -framework Carbon -framework OpenGL -framework GLUT -lm
- */
- #ifdef __APPLE__
- # include <OpenGL/gl.h> // definitions for GL graphics routines
- # include <OpenGL/glu.h> // definitions for GL input device handling
- # include <GLUT/glut.h> // deginitions for the GLUT utility toolkit
- #else
- # include <GL/gl.h> // definitions for GL graphics routines
- # include <GL/glu.h> // definitions for GL input device handling
- # include <GL/glut.h> // deginitions for the GLUT utility toolkit
- #endif
- #include <iostream> // definitions for standard I/O routines
- #include <cmath> // definitions for math library
- using namespace std;
- #define WIDTH 800 // window dimensions
- #define HEIGHT 700
- #define MENUWIDTH 200 // menu dimensions
- #define BOXHEIGHT (HEIGHT / 10)
- #define TOOLMENUWIDTH 200
- #define TOOLBOXHEIGHT (HEIGHT / 10)
- #define NCOLORS 10 // number of colors
- #define NTOOLS 10
- #define WLEFT MENUWIDTH
- #define WRIGHT WIDTH-TOOLMENUWIDTH //makes drawing window
- #define WBOTTOM 0
- #define WTOP HEIGHT
- #define NCOLORS 10 // number of colors
- #define RGBRED 1, 0, 0 // RGB Colors for menu
- #define RGBGREEN 0, 1, 0
- #define RGBBLUE 0, 0, 1
- #define RGBPURPLE .556863, .137255, .419608
- #define RGBORANGE 1, .5, 0
- #define RGBYELLOW 1, 1, 0
- #define RGBWHITE 1, 1, 1
- #define RGBINDIGO .29, .46, .43
- #define RGBBROWN .556863, .419608, .137255
- #define RGBTURQ .678431, .917647, .917647
- #define RGBBLACK 0, 0, 0 // black for screen background
- #define R 0
- #define G 1
- #define BL 2
- #define P 3
- #define O 4
- #define Y 0
- #define W 1
- #define I 2
- #define BR 3
- #define T 4
- #define PI 3.1415926536
- struct Point
- {
- int x, y;
- };
- struct EndPoint //line segments and angles
- {
- Point pt1, pt2;
- };
- struct Circle //circles
- {
- Point center;
- int radius;
- };
- union Tool //either endpoints or circles
- {
- EndPoint endpoints;
- Circle circle;
- };
- struct Object //contains information stored for each object
- {
- short int colorType; //index of object's color
- short int toolType; //index of object's tool (line segment, rectangle, circle)
- Tool tool; //line segment, rectangle, circle
- };
- Object objects[1000];
- bool tracking;
- int colorindex=0, toolindex=5;
- float prevx, prevy;
- static float colormenu[][10] = {{RGBRED}, {RGBGREEN}, {RGBBLUE}, {RGBPURPLE}, {RGBORANGE}, {RGBYELLOW}, {RGBWHITE}, {RGBINDIGO}, {RGBBROWN}, {RGBTURQ}};
- int numobjects = 0, intoolmenuindex = 0;
- /*
- Returns true if point (x, y) is in the color menu
- */
- int incolormenu(int x, int y)
- {
- return (x >= 0 && x <= MENUWIDTH && y >= 0 && y <= HEIGHT);
- }
- /*
- Returns index of point (x, y) in the color menu
- */
- int colormenuindex(int x, int y)
- {
- if(!incolormenu(x, y))
- return -1;
- else
- return(y / BOXHEIGHT);
- }
- int intoolmenu(int x, int y)
- {
- return (x >= WIDTH-MENUWIDTH && x <= WIDTH && y >= 0 && y <= HEIGHT);
- }
- int toolmenuidex(int x, int y)
- {
- if(!intoolmenu(x, y))
- return -1;
- else
- return(y / TOOLBOXHEIGHT);
- }
- /*
- Returns true if point (x, y) is in the window
- */
- int inwindow(int x, int y)
- {
- return (x > WLEFT && x < WRIGHT && y > WBOTTOM && y < WTOP);
- }
- /*
- Watch mouse button presses and handle them
- */
- void handleButton(int button, int state, int x, int y)
- {
- static int menuindex = 0;
- static int toolmenuindex = 0;
- static int incolormenuindex = 0;
- y = WTOP - y;
- if(button == GLUT_RIGHT_BUTTON) //right click to clear screen
- glutPostRedisplay();
- if(button != GLUT_LEFT_BUTTON && GLUT_RIGHT_BUTTON)
- return;
- if(state == GLUT_DOWN)
- {
- if(incolormenu(x, y))
- {
- menuindex = incolormenuindex(x, y);
- }
- else if(intoolmenu(x,y))
- {
- toolmenuindex = intoolmenuindex(x,y);
- }
- else if (inwindow (x,y))
- {
- tracking = 1;
- if(toolindex!=5)
- {
- numobjects++;
- objects[numobjects-1].colorType=colorindex;
- objects[numobjects-1].toolType=toolindex;
- }
- switch(toolindex)
- {
- case 0:
- case 1:
- case 4:
- objects[numobjects-1].tool.endpoints.pt1.x=x;
- objects[numobjects-1].tool.endpoints.pt1.y=y;
- objects[numobjects-1].tool.endpoints.pt2.x=x;
- objects[numobjects-1].tool.endpoints.pt2.y=y;
- break;
- case 2:
- case 3:
- objects[numobjects-1].tool.circle.center.x=x;
- objects[numobjects-1].tool.circle.center.y=y;
- objects[numobjects-1].tool.circle.radius=0;
- break;
- case 5:
- prevx = x;
- prevy = y;
- break;
- }
- }
- else
- {
- if(incolormenu(x,y) && colormenuindex(x,y) == menuindex)
- colorindex=menuindex;
- else if(intoolmenu(x,y)&&intoolmenuindex(x,y)==toolmenuindex)
- {
- if(toolmenuindex<=5)
- toolindex=toolmenuindex;
- else
- {
- switch(toolmenuindex)
- {
- case 9:
- exit(0); //quit
- case 8:
- numobjects = 0;
- glutPostRedisplay(); //clear
- break;
- }
- }
- }
- tracking = 0;
- }
- }
- /*
- Draw a line to new mouse position
- */
- void m_Motion(int x, int y)
- {
- y = WTOP - y;
- float r;
- float x1, y1;
- if(tracking && inwindow(x, y))
- {
- glColor3f(colormenu[colorindex][R], colormenu[colorindex][G],
- colormenu[colorindex][BL]);
- switch(toolindex)
- {
- case 0:
- case 1:
- case 4:
- objects[numobjects-1].tool.endpoints.pt2.x=x;
- objects[numobjects-1].tool.endpoints.pt2.y=y;
- break;
- case 2:
- case 3:
- x1=objects[numobjects-1].tool.circle.center.x;
- y1=objects[numobjects-1].tool.circle.center.y;
- r=sqrt(pow(x-x1,2)+pow(y-y1,2));
- objects[numobjects-1].tool.circle.radius=r;
- break;
- case 5:
- numobjects ++;
- objects[numobjects-1].colorType=colorindex;
- objects[numobjects-1].toolType=toolindex;
- objects[numobjects-1].tool.endpoints.pt1.x=prevx;
- objects[numobjects-1].tool.endpoints.pt1.y=prevy;
- objects[numobjects-1].tool.endpoints.pt2.x=prevx;
- objects[numobjects-1].tool.endpoints.pt2.y=prevy;
- prevx = x;
- prevy = y;
- break;
- }
- glutPostRedisplay();
- }
- }
- void drawRect(bool filled, float x1, float y1, float x2, float y2)
- {
- if(filled)
- glBegin(GL_POLYGON);
- else
- glBegin(GL_LINE_LOOP);
- glVertex2f(x1,y1);
- glVertex2f(x1,y2);
- glVertex2f(x2,y2);
- glVertex2f(x2,y1);
- glEnd();
- }
- void Circle(bool filled, int i)
- {
- int k;
- float angle, ainc;
- ainc = 2 * PI / i;
- if(filled)
- glBegin(GL_POLYGON);
- else
- glBegin(GL_LINE_LOOP);
- for(k=0; k<i; k++)
- {
- angle=k*ainc;
- glVertex2f(cos(angle), sin(angle));
- }
- glEnd();
- }
- void drawCircle(bool filled, float x, float y, float radius)
- {
- glPushMatrix();
- glTranslatef(x,y,0);
- glScalef(radius, radius, 1);
- Circle(filled, 32);
- glPopMatrix();
- }
- void drawLine(float x, float y, float prevx, float prevy)
- {
- glBegin(GL_LINES);
- glVertex2f(prevx, prevy);
- glVertex2f(x, y);
- glEnd;
- prevx=x;
- prevy=y;
- }
- void drawStraight()
- {
- glPushMatrix();
- glScalef(2, .5, 1);
- glTranslatef(0, -3, 0);
- drawRect(true, -1, 1, 1, -1);
- glPopMatrix();
- }
- void triangle(void)
- {
- glBegin(GL_POLYGON);
- glVertex2f(1,-1);
- glVertex2f(0,1);
- glVertex2f(-1,-1);
- glEnd();
- }
- void drawPencil()
- {
- glPushMatrix();
- glScalef(1,2,1);
- glColor3f(255,255,0);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix();
- glScalef(1,.5,1);
- glTranslatef(0,3,0);
- glColor3f(204,0,204);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix();
- glRotatef(180,0,0,1);
- glTranslatef(0,3,0);
- glColor3f(1, .498039,0);
- triangle();
- glPopMatrix();
- }
- void drawLoad()
- {
- glPushMatrix();
- glScalef(1.5,1,1);
- glTranslatef(-.5,.25,0);
- glColor3f(0,0,1);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix();
- glScalef(1.5,1,1);
- glTranslatef(0,-.25,0);
- glColor3f(0,153,153);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- }
- void drawSave()
- {
- glPushMatrix();
- glScalef(1.5,1.5,1);
- glColor3f(0,0,1);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix();
- glScalef(.5,.5,1);
- glTranslatef(0,2,0);
- glColor3f(0,153,153);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- }
- void drawClear()
- {
- glPushMatrix();
- glScalef(1.5,.5,1);
- glTranslatef(0,-1,0);
- glColor3f(1,1,1);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix();
- glScalef(2,.5,1);
- glColor3f(204,0,204);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- }
- void drawQuit()
- {
- glPushMatrix();
- glRotatef(45,0,0,1);
- glScalef(.5,2,1);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix();
- glRotatef(45,0,0,1);
- glScalef(2,.5,1);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- }
- /*
- Draw the colored box and the color menu
- */
- void drawMenu()
- {
- int i;
- // clear window
- glClear(GL_COLOR_BUFFER_BIT);
- // draw the color menu
- for(i = 0; i < NCOLORS; i++)
- {
- glColor3f(colormenu[i][R], colormenu[i][G], colormenu[i][BL]);
- glRecti(1, BOXHEIGHT * i + 1,
- MENUWIDTH - 1, BOXHEIGHT * (i + 1) - 1);
- }
- glFlush();
- }
- void drawToolMenu()
- {
- int j;
- glRecti(WRIGHT,0, WIDTH, HEIGHT);
- glPushMatrix(); //Tool0
- glTranslatef(WIDTH-MENUWIDTH+ MENUWIDTH/2, TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- glColor3f(1, 0, 0);
- drawRect(false,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix(); //Tool1
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- glColor3f(1, 0, 0);
- drawRect(true,-1,1,1,-1);
- glPopMatrix();
- glPushMatrix(); //Tool2
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 2*TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- glColor3f(1, 0, 0);
- Circle(false,32);
- glPopMatrix();
- glPushMatrix(); //Tool3
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 3*TOOLBOXHEIGHT+TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- glColor3f(1, 0, 0);
- Circle(true,32);
- glPopMatrix();
- glPushMatrix(); //Tool4
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 4*TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- glColor3f(1, 0, 0);
- drawStraight();
- glPopMatrix();
- glPushMatrix(); //Tool5
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 5*TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- drawPencil();
- glPopMatrix();
- glPushMatrix(); //Tool6
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 6*TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- drawLoad();
- glPopMatrix();
- glPushMatrix(); //Tool7
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 7*TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- drawSave();
- glPopMatrix();
- glPushMatrix(); //Tool8
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 8*TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- drawClear();
- glPopMatrix();
- glPushMatrix(); //Tool9
- glTranslatef(WIDTH-MENUWIDTH + MENUWIDTH/2, 9*TOOLBOXHEIGHT + TOOLBOXHEIGHT/2,0);
- glScalef(25,25,1);
- glColor3f(1,0,0);
- drawQuit();
- glPopMatrix();
- }
- void drawObjects()
- {
- int c,i;
- for(i=0; i<numobjects; i++)
- {
- c=objects[i].colorType;
- glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][b]);
- switch(objects[i].toolType)
- {
- case 0:
- drawRect(false,objects[i].tool.endpoints.pt1.x,objects[i].tool.endpoints.pt1.y,objects[i].tool.endpoints.pt2.x,objects[i].tool.endpoints.pt2.y);
- break;
- case 1:
- drawRect(true,objects[i].tool.endpoints.pt1.x,objects[i].tool.endpoints.pt1.y,objects[i].tool.endpoints.pt2.x,objects[i].tool.endpoints.pt2.y);
- break;
- case 2:
- drawCircle(false,objects[i].tool.circle.center.x,objects[i].tool.circle.center.y,objects[i].tool.circle.radius);
- break;
- case 3:
- drawCircle(true,objects[i].tool.circle.center.x,objects[i].tool.circle.center.y,objects[i].tool.circle.radius);
- break;
- case 4:
- case 5:
- drawLine(objects[i].tool.endpoints.pt1.x, objects[i].tool.endpoints.pt1.y, objects[i].tool.endpoints.pt2.x, objects[i].tool.endpoints.pt2.y);
- break;
- }
- }
- }
- void drawScene()
- {
- glClearColor(RGBBLACK,1);
- glClear(GL_COLOR_BUFFER_BIT);
- drawObjects();
- drawMenu();
- drawToolMenu();
- glutSwapBuffers();
- }
- /*
- Main program
- */
- int main(int argc, char* argv[])
- {
- //example to store values for first object as endpoints
- objects[0].tool.endpoints.pt1.x = 10;
- objects[0].tool.endpoints.pt1.y = 10;
- objects[0].tool.endpoints.pt2.x = 90;
- objects[0].tool.endpoints.pt2.y = 90;
- //example to store values for second object as circle
- objects[1].tool.circle.center.x = 50;
- objects[1].tool.circle.center.y = 50;
- objects[1].tool.circle.radius = 25;
- glutInit(&argc, argv);
- // open window and establish coordinate system on it
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
- glutInitWindowSize(WIDTH, HEIGHT);
- glutCreateWindow("Sketch Program");
- gluOrtho2D(0, WIDTH, 0, HEIGHT);
- // register display and mouse-button callback routines
- glutDisplayFunc(drawMenu);
- glutMouseFunc(handleButton);
- glutMotionFunc(m_Motion); // mouse movement event callback
- // Set up to clear screen to black
- glClearColor(RGBBLACK, 1);
- glutMainLoop();
- }
- return 0;
- }