EDITED WITH SOLUTION
So I am making a particle system in opengl. I am working in Linux. I want to make it more interactive, so, for example, when the user presses the spacebar, a new firework shoots out.
I am using GLUT to capture keyboard input, and when the user hits the spacebar, it shoots a firework out, then goes in a loop that loops for a while, and if I press spacebar again, it has to wait til that loop (mentioned above) is done executing before it registers the keypress.
So my question is this:
Is there a way for me to, at the start of the loop, to check the buffer that glut stores the keypress in? I feel like there has to be some sort of a buffer that it stores keypresses in, because yeah, once that loop above is finished executing, it registers the keypress.
SOLUTION:
I do not actually do any loops now anywhere (except to loop through arrays to update locations of my fireworks, etc), I do something like
in main, add this:
int main(int argc, char** argv){
//Usual glut initializations
glutTimerFunc(30, update, -1);//says in 30miliseconds call update
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Then in your update function (in the parameters for glutTimerFunc, so something like this:
void update(int k){
//UPDATE POSITION OF FIREWORKS
glutTimerFunc(30, update, -1);//it is important to add this
glutPostRedisplay();//calls display() on the next iteration of glut's main loop
}
Then finally in your display function, do what ever you need to do to draw one frame (in my case, loop through each firework and draw it!)
Hope this helps 🙂
You’re going about this the wrong way.
Your keypress handler should not be looping over anything. Your keypress handler should simply set some variable and return.
Your main display function (which should be driven by a timer function that calls
glutPostRedisplay) should check this variable. If it is set, then it should be firing your firework and looping for some duration.During that time, your keypress handler should simply ignore any further pressing of the spacebar. Once the display function finishes with the firework, then it can respond to further key presses.