Skip to content

vsg::Window event invalid when limited frame rate #1078

Answered by robertosfield
Brucewu998 asked this question in Q&A
Discussion options

You must be logged in to vote

Spinlocks are really not something you want in your code except for exceptional circumstances. The simplest way, and most power efficient, would be to add a sleep to the main loop:

while(viewer->advanceToNextFrame())
{
        viewer->handleEvents();
        viewer->update();
        viewer->recordAndSubmit();
        viewer->present();

        // just add a sleep 
        std::this_thread::sleep_for(std::chrono::milliseconds(40));
}

If you want to be more sophisticated you could measure how long you have left in the frame after doing all the viewer calls then sleep for the rest of the frame time you have left. You can be conservative with the sleep as vsync will cause the frame to sleep…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Brucewu998
Comment options

@robertosfield
Comment options

Answer selected by Brucewu998
@Brucewu998
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants