Can event polling happen more frequently than frame rate?

Hi, I’ve always thought the event polling works independently from the update/draw methods.
But I just found out the event polling happens every frame.

Here’s the simple test code.

static double lastElapsedTime = 0.0;

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y){

    cout << "TIME :" << ofGetElapsedTimeMillis() - lastElapsedTime << endl;
    lastElapsedTime = ofGetElapsedTimeMillis();
}

When I run it and move the mouse cursor, the TIME prints around 16~17ms when the frame rate is set to 60.
And If I change the frame rate to 20, the TIME prints around 50ms. (1000/20)

So my guess is that the event polling rate is same as the frame rate.

If this is correct, Is it possible to separate the event polling from the drawing?
I personally would like to make the event polling to happen more frequently.

Is there an easy way to do this in OF?
Thank you in advance!

Hi! On which platform are you working on? I seem to remember that Windows tends to make that kind of stuff.

I’m working on macOS but I would like to know a cross-platform solution if possible.

Hmm, ok, not what i’ve thought then. Were are you printing the values of TIME? For what I can get from your snippet, it seems to be printing to the console (maybe it does that on each update()?). Maybe you can try for example to save to an array and then print that array, to see if the time is the same. I believe that the event polling is not necessarily tied to the framerate, at least not outside of Windows.

Does ofSetVerticalSync(false) do this? That disconnects the CPU with the GPU refresh cycles so maybe it does the same for the events too, but I’m not too sure. However, side effects like tearing on the picture happen with it.

I wasn’t aware of how event polling happens, honestly. But you could try to see if this helps.

Yes, OF polls the events once per frame changing that is not trivial but you can do so by creating a separate ofMainLoop and polling it manually

The main problem is that each opengl context has to run always on the same thread and that some glfw functions can only be called on the main thread only, among them the polling of events.

The way to do it is to create one thread for the opengl context, run the application from there and then in the main thread run the event poll at whatever frequency you want by sleeping the thread for a certain amount of time and then polling, for example by using ofTimer.

when you use several threads global variables can be problematic depending on how things are layed out and you might need to use a renderer explicitly

There’s an example on how to use a renderer explicitly here:

In any case unless you want this for audio or something else that runs faster than the main thread it’s usually not very useful since you would get all the events anyway.

Also glfw sends the time of each event which we discard but we should probably keep to have more fine grained events without needing to poll more frequently

2 Likes

Thank you for your answer. It is really helpful. :slight_smile:

by the way, a kind of hackish solution is to just call glfwPollEvents() from time to time in update. you will get varying frequencies of update but if you don’t care much about that then it’s a really easy way to poll the events more frequently

1 Like