Mouse Interaction

I am trying to draw a moving rectangle such that it moves one step, waits for the mouse key and then moves again.
here is my code:

void mymouse(int button, int state, int x, int y)
{
if(state== GLUT_DOWN && button == GLUT_LEFT_BUTTON) exit(0);
}

and i call mymouse function using glutMouseFunc(mymouse).

the problem is that im trying to change the exit(0) to a return, but its not wokring. any suggestions?

You know that glutMouseFunc works when the mouse is click inside the window, should be called before glutMainFunc.

example of what you need to do:

void mymouse(int button, int state, int x, int y)
{
if(state== GLUT_DOWN && button == GLUT_LEFT_BUTTON)
{
// When left button down pass current x/y mouse values to rectangle
rectangle_x = x;
rectangle_y = y;
}

}

my_display()
{

draw_rectangle(rectangle_x, rectangle_y);

}

Originally posted by melmiligui:
[b]I am trying to draw a moving rectangle such that it moves one step, waits for the mouse key and then moves again.
here is my code:

void mymouse(int button, int state, int x, int y)
{
if(state== GLUT_DOWN && button == GLUT_LEFT_BUTTON) exit(0);
}

and i call mymouse function using glutMouseFunc(mymouse).

the problem is that im trying to change the exit(0) to a return, but its not wokring. any suggestions? [/b]

actually I’m reading in the coordinates from a file and I just need it to stop after each coordinate reading, until I click the mouse and then it reads the next coordinates again and moves and on and on. You’re help is greatly appreciated. Thanks. melmiligui

All im trying to do is draw a rectangle and then wait for a mouse key and draw another rectangle someplace else.
I did this by makig a for loop in the display function to draw the first rectangle then wait for the mouse then draw the second rectangle and so on…
the problem is that mymouse function doesnt work except when i use the exit(0) function and that too only works when the for loop has gone through all its iterations…

ANY HELP PLEASE???

Hi,
i agree with the nexus . this is the way ur main should be.

int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitWindowSize(1200,900);
glutInitWindowPosition(0,0);
pw=glutCreateWindow(" opening first ");
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB );
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

that means , mouse function should be before glutMainLoop.

and ur mouse functin can be,

void mouse(int button,int state, int x,int y)
{
if(state== GLUT_DOWN && button ==GLUT_LEFT_BUTTON)
{
for(;
{
// read the coordinates
Rectangle(); // draw rectangle
}
}

hope this helps.