I'm writing the program that drawing an unfilled rectangle by dragging the mouse, and it works but the problem is that every time I click on the screen (not dragging yet) the rectangle always appear on the screen, how can I make the rectangle not appear on the screen when I click but only appear when I drag the mouse, here is my program:
struct Position
{
Position() : x(0), y(0) {}
float x;
float y;
};
Position start;
Position finish;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2f(start.x, start.y);
glVertex2f(finish.x, start.y);
glVertex2f(finish.x, finish.y);
glVertex2f(start.x, finish.y);
glEnd();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
start.x = x;
start.y = y;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
finish.x = x;
finish.y = y;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("");
gluOrtho2D(0, 600, 400, 0);
glutMainLoop();
return 0;
}