I have a cloth simulation program where the user can manipulate a cloth with their mouse, and I was noticing a strange delay in the invocation of MouseMoved events after clicking with the mouse. This delay would occur no matter how long I waited to move my mouse after the initial click (as long as the mouse button was held). After some investigation, I found that when I turned off v-sync this issue disappeared. I have included a very simple program that recreates this behavior on my machine:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
// -- toggle on/off the line below
window.setVerticalSyncEnabled(true);
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::MouseMoved) {
shape.setFillColor(sf::Color::Blue);
std::cout << "moved" << std::endl;
}
else if (event.type == sf::Event::MouseButtonPressed) {
shape.setFillColor(sf::Color::Red);
std::cout << "PRESSED" << std::endl;
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
While v-sync is on, I can see the delay by clicking with the mouse, waiting with the mouse button held, and then moving the mouse with the button still held. If the event is MouseButtonPressed the circle becomes red, and if the event is MouseMoved the circle becomes blue (also printed to console during each event). I can see from this that, even in this small program, there is a short delay. In comparison, if you do not turn on v-sync, you can see that the color change and console output are as immediate as one would expect.
In my more complicated program, this delay is much more noticeable. My simulation's FPS doesn't drop during the delay, which makes me think something is affecting how the mouse events are added to the event queue.
I am on a mid-2014 Macbook Pro running MacOS 10.12 and SFML 2.5.1. I kind of suspect the likely cause is my outdated OS, but thought I might as well post this to see if anyone can reproduce what I'm seeing.
I tried doing some timing tests without adding too much overhead, so what I have is a little less than optimal but I was able to get some semi-consistent numbers from it.
v-sync disabled:
t_diff: 0.011346s
t_diff: 1e-06s
t_diff: 0.012167s
t_diff: 0.026526s
t_diff: 1e-06s
v-sync enabled:
t_diff: 0.11651s
t_diff: 0.083314s
t_diff: 0.081267s
t_diff: 0.083336s
t_diff: 0.083301s
the additions to the code are as follows:
...
// -- variable used for testing event state
bool last_event_click = false;
sf::Clock clock;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::MouseMoved) {
shape.setFillColor(sf::Color::Blue);
// std::cout << "moved" << std::endl;
if(last_event_click) {
float t = clock.restart().asSeconds();
std::cout << "t_diff: " << t << "s" << std::endl;
last_event_click = false;
}
}
else if (event.type == sf::Event::MouseButtonPressed) {
shape.setFillColor(sf::Color::Red);
// std::cout << "PRESSED" << std::endl;
last_event_click = true;
clock.restart();
}
}
...