PDA

View Full Version : Mouse Interaction



melmiligui
11-26-2002, 09:45 AM
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?

nexusone
11-26-2002, 10:09 AM
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:
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?

melmiligui
11-26-2002, 10:40 AM
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

melmiligui
11-26-2002, 04:53 PM
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??????

prashantgp
11-28-2002, 01:28 AM
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(; http://www.opengl.org/discussion_boards/ubb/wink.gif
{
// read the coordinates
Rectangle(); // draw rectangle
}
}


hope this helps.