PDA

View Full Version : paint program



Speedy moto
03-28-2015, 09:41 PM
Hello.. I do not know why it cannot functioning well. I think I already put all function to draw points and lines. also I do not know why when it in full screen mode, the button not function at all. why???

#include <GL\glut.h>
#include <cmath>
#include <string.h>

#define LINE 1
#define POINT 2
//#define CLEAR 3

//initialize global variables
int point_size=1;
int line_size=1;
int X1=-1, X2=-1, Y1=-1, Y2=-1;
bool P1= false, P2=false, P3=false, Cleared=false, Cleared2=false, Line=false,Point=false;
char *shape="No Shape Selected";

//void init() {
// glClearColor(0.0, 0.0, 0.0, 1.0); // Set background (clear) color to black
//}
//this function draws the program's panels
void drawPanel()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

//top panel backgound
glBegin(GL_QUADS);
glColor3f(1,0,0.2);
glVertex2i(0,50);
glVertex2i(0,600);
glVertex2i(130,600);
glVertex2i(130,50);
glEnd();

//bottom panel background
glBegin(GL_QUADS);
glColor3f(0.4,0.6,0.4);
glVertex2i(0,0);
glVertex2i(0,50);
glVertex2i(130,50);
glVertex2i(130,0);
glEnd();

//panel box
glBegin(GL_QUADS);
glColor3f(1,1,0);

glVertex2i(10,550);
glVertex2i(10,590);
glVertex2i(50,590);
glVertex2i(50,550);

glVertex2i(70,550);
glVertex2i(70,590);
glVertex2i(110,590);
glVertex2i(110,550);

glVertex2i(10,500);
glVertex2i(10,540);
glVertex2i(50,540);
glVertex2i(50,500);

glVertex2i(70,500);
glVertex2i(70,540);
glVertex2i(110,540);
glVertex2i(110,500);

glVertex2i(10,450);
glVertex2i(10,490);
glVertex2i(50,490);
glVertex2i(50,450);

glVertex2i(70,450);
glVertex2i(70,490);
glVertex2i(110,490);
glVertex2i(110,450);
glEnd();


//this draws image of a point on its button
glBegin(GL_POINTS);
glColor3f(0,0,0);

glVertex2i(29,569);
glVertex2i(29,570);
glVertex2i(30,570);
glVertex2i(30,569);
glVertex2f(29.3,569.3);
glVertex2f(29.3,569.6);
glVertex2f(29.3,569.9);
glVertex2f(29.6,569.3);
glVertex2f(29.6,569.6);
glVertex2f(29.6,569.9);
glVertex2f(29.9,569.3);
glVertex2f(29.9,569.6);
glVertex2f(29.9,569.9);
glEnd();

//this draws image of a line on its button
glBegin(GL_LINES);
glColor3f(0,0,0);
glVertex2i(80,560);
glVertex2i(100,580);
glEnd();

//this draws image of a triangle on its button
glBegin(GL_TRIANGLES);
glColor3f(0,0,0);
glVertex2i(20,510);
glVertex2i(30,530);
glVertex2i(40,510);
glEnd();

//this draws image of a square on its button
glBegin(GL_QUADS);
glColor3f(0,0,0);
glVertex2i(80,510);
glVertex2i(80,530);
glVertex2i(100,530);
glVertex2i(100,510);
glEnd();


//this displays the shape status on the panel
glColor3f(0,0,0);
int tlength = strlen(shape);
glRasterPos3f(20, 20,0);
for (int counter=0; counter <=tlength; counter++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, shape[counter]);
}
}

//this function draws points on the drawing area when the point button is selected
void drawPoint()
{
if(P1==true)
{
glPointSize(point_size); //set point size
glBegin(GL_POINTS);
glColor3f(0,0,0);
glVertex2i(X1,Y1);
glEnd();
P1=false;
}
}

//this draws lines on the drawing area when the line button is selected
//a line is drawn by clicking twice on the drawing area
void drawLine()
{
if(P1==true && P2==true)
{
glLineWidth(line_size);
glBegin(GL_LINES);
glColor3f(0,0,0);
glVertex2i(X1,Y1);
glVertex2i(X2,Y2);
glEnd();

P1=false;
P2=false;
}
}

//this is where all the calls to the functions are made
void draw()
{
//the GL_COLOR_BUFFER_BIT is cleared only once to keep the content of the drawing page
if(Cleared == false)
{
glClear(GL_COLOR_BUFFER_BIT);
Cleared = true;
}

//the drawPanel() function is called everytime the draw function is called
drawPanel();

//checks if the object is selected using the boolean, and calls the appropriate function
if (Line==true)
drawLine();

if (Point==true)
drawPoint();

glFlush();
}

//this function is responsible for the action that happens when we select from the menu panel
void processMenuEvents(int option)
{
if(Cleared2 == false)
{
glClear(GL_COLOR_BUFFER_BIT);
Cleared2 = true;
}
switch (option)
{
case LINE :
Line= true;
Point= false;
P1=false;P2=false;P3=false;
shape="Drawing a Line";
break;

case POINT :
Line= false;
Point= true;
P1=false;P2=false;P3=false;
shape="Drawing a Point";
break;

/*case CLEAR :
glClear(GL_COLOR_BUFFER_BIT);
P1= false,P2=false, P3= false;
break;*/
}
glutPostRedisplay();
}

//this function is responsible for mouse interaction
void mouse (int btn, int state, int x, int y)
{
//these are listeners for left mouse click events in the drawing area
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN && 600-y > 590)
{
if (Line==true)
{
if(P1==true && P2==false)
{
X2=x;Y2=600-y;
P2=true;
}
if(P1==false)
{
X1=x;Y1=600-y;
P1=true;
}
}
if (Point==true)
{
if(P1==false)
{
X1=x;Y1=600-y;
P1=true;
}
}
}

//these are listeners for buttons in the panel
if( x > 70 && x < 110 && 600-y > 550 && 600-y < 590 )
processMenuEvents(LINE); // line button

if( x > 10 && x < 50 && 600-y > 550 && 600-y < 590 )
processMenuEvents(POINT); // point button

glutPostRedisplay();
}

//this function is responsible for creating a pop-up menu when the right mouse button is clicked
//void createGLUTMenus()
//{
// int menu;
// menu = glutCreateMenu(processMenuEvents);
// glutAddMenuEntry("Clear",CLEAR);
// glutAttachMenu(GLUT_RIGHT_BUTTON);
//}

//this function is responsible for keyboard interaction
void keyboard (unsigned char key, int x, int y)
{
switch(key)
{
case 99:
glClear(GL_COLOR_BUFFER_BIT);
break;

case 114:
glClear(GL_COLOR_BUFFER_BIT);
break;

case 101:
exit(0);
break;
}
}

/* this is the main function where the toolkit is initialized, the display mode is set,
the window is created and the callback functions are registered. */
void main()
{
//init();
glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(20,20);
glutCreateWindow("Simple Paint Program ");

glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glClearColor(1,1,1,1);

glutDisplayFunc(draw);
glutIdleFunc(draw);
glOrtho(0,600,0,600,0,1);
//createGLUTMenus();

glutMainLoop();
}