0
\$\begingroup\$

The problem is that the mouse and keyboard events can be triggered before the main loop(in which the program starts to render and update content) takes place. In the program if the moues button is pressed, there will be a sound effect. And the code looks like this

void main()
{
// Here I initialize everything including parsing model files, 
// so when the window appears it's initially plain white(loading files and not 
// drawing stuff). I set up the callback functions with glfwSetKeyCallBack() 
// and glfwSetMouseButtonCallBack(). 
// The next line is the main loop

 while (!glfwWindowShouldClose(window))
 {
 // Here I draw all the content and this is supposed to be only the place that the events  
 // can be triggered
 }

glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}

When I debug the program, the window will appear and if then I click the mouse, the sound effect will appear when the program starts drawing stuff on the window. But I want the callback functions to process events only when window enters the main loop. It should not receive or process events during the initialization part. Is there a way to prevent the callback functions to receive or process events before the main loop?

\$\endgroup\$
1
\$\begingroup\$

To my knowledge of GLFW documentation and internals, events should only be dispatched in response to calls to PollEvents/WaitEvents. GLFW2 used to PollEvents in SwapBuffers, but that is gone in GLFW3.

In any way, there's two possible solutions to your problem:

  • Register your callbacks in the main loop, predicated on a boolean to ensure you do it once.
  • Register your callbacks early, but test a boolean in the callbacks to see if the program is ready or not. You may save up event information if you want to avoid losing it, or just discard events until ready.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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