0

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;
}
  • 1
    I think you are asking "How can I tell the difference between clicking and dragging?" - (check the distance between start and finish points). But the question needs reworking to be useful to others. – meesern Apr 27 '21 at 09:14
  • you need to remember last state of mouse button from previous mouse event call... then just compare if button was or was not clicked before ... see [simple C++ Drag&Drop](https://stackoverflow.com/a/20924609/2521214) especially pay attention to variables `q0,q1` (last and actual mouse button state) in lines like: `if ((!q0)&&( q1))` ...this way you can easily distinquish start of click, drag, end of click .... – Spektre Apr 27 '21 at 09:19

0 Answers0