I was just writing, "I just copied and pasted your code** and it works fine without any stutter" when I just noticed that it started to stutter! :o
I believe your problem is due to using vertical synchronisation. Although I'm not sure exactly, it could be due to the fact that it's synchronising at a low frame rate (missing the refresh so waiting for the next one - half the refresh rate).
I just tried limiting the frame rate* (I tried 30 and 60). Although it's (obviously) not pretty at 30, both 30 and 60 still seems to have 'jumps' so it might be your interpolation calculations.
* Remember that v-sync must be turned off when limiting the frame rate.
** your accumulator variable is not being initialised so I set it to zero at initialisation:
float accumulator = 0.f, delta = 1.0f / 125.0f;
Is this causing stutter somehow?
Probably not but it depends onthe kind of stutter you are experiencing. Not processing events each frame could cause your operating system to whine, which may in turn decide to give you less processing time. Probably not though.
I was just writing, "I just copied and pasted your code** and it works fine without any stutter" when I just noticed that it started to stutter! :o
I believe your problem is due to using vertical synchronisation. Although I'm not sure exactly, it could be due to the fact that it's synchronising at a low frame rate (missing the refresh so waiting for the next one - half the refresh rate).
I just tried limiting the frame rate* (I tried 30 and 60). Although it's (obviously) not pretty at 30, both 30 and 60 still seems to have 'jumps' so it might be your interpolation calculations.
* Remember that v-sync must be turned off when limiting the frame rate.
** your accumulator variable is not being initialised so I set it to zero at initialisation:
float accumulator = 0.f, delta = 1.0f / 125.0f;
Is this causing stutter somehow?
Probably not but it depends onthe kind of stutter you are experiencing. Not processing events each frame could cause your operating system to whine, which may in turn decide to give you less processing time. Probably not though.
"* Remember that v-sync must be turned off when limiting the frame rate."
Should I remove all the v-sync option from my game then? I am not limiting the frame rate, I limit the tickrate.
You can simulate the stutter effect by simply click & holding the window for a moment, so line snaps. Just do it. Is there a fix for this, it really bugs me... How can I make my game loop perfect?
About org_pos, I use rect.getPosition() and setPosition inside my update code so I think I have to do it this way.
This one runs so smooth but that's probably because it renders 6k times a second.
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(1280, 720), "Stutter");
sf::Event event;
sf::Clock clock;
float accumulator = 0.0f, delta = 1.0f / 125.0f;
//window.setVerticalSyncEnabled(true);
sf::RectangleShape rect(sf::Vector2f(5.0f, 720.0f));
float pos = 0.0f;
while(window.isOpen()) {
accumulator += clock.restart().asSeconds();
//while(accumulator > delta) {
accumulator -= delta;
// Poll Events
while(window.pollEvent(event)) if(event.type == sf::Event::Closed) window.close();
// Update
if((pos += 20.0f*delta) > 1280.0f) { pos = 0.0f; }
//}
// Set position to interpolated position
rect.setPosition(pos, 0.0f);
// Render
window.clear();
window.draw(rect);
window.display();
}
return 0;
}
Might sound stupid, but what happens if you're sleeping for 1 millisecond at the end of your update loop? Does that change anything?
Also which graphics card are you running and what's your actual FPS? Do FPS change when the stuttering happens or is it stable? How about the number of update per second in these situations?
Wow... Having sf::sleep(sf::milliseconds(1));
after the .display() helps to make it smooth like I open the program, line is shaky for like 0.3-0.4 seconds, then it becomes smooth and keeps being smooth. Yet it shakes when I shake my mouse :D (1000 Hz polling rate mouse and I don't have this issue in CS:GO)
Nvidia GTX 770 and 144 Hz monitor I have so I can see the smallest difference.
Fraps says 144 FPS, when I shake the mouse, line shakes and FPS becomes 143.
Edit: It is so weird that I removed sleep(1) and it is still smooth now. Sometimes it stutters, sometimes it does not. Very confusing.