Hello, I have a strange stuttering issue with this code.
#include "Core/MathUtility.h"
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 800), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
sf::Clock clock;
sf::Time elapsed = sf::Time::Zero;
sf::Vector2f pos;
sf::Vector2f oldPos;
while (window.isOpen())
{
elapsed += clock.restart();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
while (elapsed >= sf::seconds(1 / 60.f))
{
std::cout << "tick\n";
elapsed -= sf::seconds(1 / 60.f);
oldPos = pos;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
pos.x -= 5.f;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
pos.x += 5.f;
}
float t = elapsed.asSeconds() / sf::seconds(1 / 60.f).asSeconds();
window.clear();
sf::Vector2f renderPos = lerp(oldPos, pos, t);
std::cout << "renderPos: " << renderPos.x << "\n";
shape.setPosition(pos);
window.draw(shape);
window.display();
}
return 0;
}
I think the stutter comes from random high frametimes.
dt: 0.000104
renderPos: 298.681
dt: 0.038244
tick
tick
tick
renderPos: 310.154
dt: 0.000183
renderPos: 310.209
dt: 0.000158
renderPos: 310.257
dt: 0.037992
tick
tick
renderPos: 321.655
dt: 0.000206
renderPos: 321.716
dt: 0.000212
renderPos: 321.78
I'm using sfml 2.4.0, windows 10, i7 6700k/gtx 1070.
Sometimes if I reboot my computer, the stutter goes away.
What I don't understand is that stutter never seems to occur on my laptop with integrated graphics.