Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this program that starts a loop when the key is pressed

The problem is that when i start the loop, i want to be able to stop it halfway with my mousepress.

The problem is that it doesn't register my mousepress until my loop is done.

How do I stop it halfway if the mouse was pressed in my loop? I tried a flag, if(mousePressed) but the flag doesnt change until the loop is done

share|improve this question
Random guess: are you polling the event queue anywhere in that loop? – thedaian Oct 3 '11 at 14:09
up vote 2 down vote accepted

You have to register the function that handles the callback function to be executed when a button is mouse pressed during your display() loop. This has to be done before running the main loop, when you are initialising everything that GLUT requires.

glutMouseFunc (MouseButton); 

Then you need to define the callback function itself, for example, something like this.

void MouseButton(int button, int state, int x, int y)
  // Respond to mouse button presses.
  // If button1 pressed, mark this state 

  if (button == GLUT_LEFT_BUTTON)
      g_bButton1Down = (state == GLUT_DOWN) ? TRUE : FALSE;

Note that g_bButton1Down is the global variable that you have to check to see if the mouse button is being pressed or not inside your main display() GLUT loop.

Take a loop at the glutMouseFunc specifications.

share|improve this answer
Thanks. I was trying to change a flag while I was inside a drawing loop (not an event loop). I failed. So i ended up changing my loop to be a timed function and since every time my timed step would end it would allow the mouse and keyboard functions to register changes it worked perfectly – Andre Oct 3 '11 at 18:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.