1
Graphics / Re: Sprites stutter/shake inconsistently while moving the view
« on: March 21, 2020, 03:59:30 pm »
Wow, thank you so much that was definitely my issue.
I made a few changes based on your comments.
I changed my update code to always use the delta time UNLESS the delta is larger than my Fixed Update Time. In which case I will loop using the Fixed Update Time until the delta time is less, then I continue to update using the remainder of the delta. I simulated a GPU hiccup by adding a sf::sleep of half a second.
I also added full screen mode to ensure VSYNC was working, which I believe it was as my graphics card displays the FPS for the window as 60 both in windowed and full screen.
It now runs nice and smooth
I apologize about the code formatting, I had pasted directly from visual studio but I guess the tabbing got messed up.
Here is my updated code:
Thanks again!
I made a few changes based on your comments.
I changed my update code to always use the delta time UNLESS the delta is larger than my Fixed Update Time. In which case I will loop using the Fixed Update Time until the delta time is less, then I continue to update using the remainder of the delta. I simulated a GPU hiccup by adding a sf::sleep of half a second.
I also added full screen mode to ensure VSYNC was working, which I believe it was as my graphics card displays the FPS for the window as 60 both in windowed and full screen.
It now runs nice and smooth
I apologize about the code formatting, I had pasted directly from visual studio but I guess the tabbing got messed up.
Here is my updated code:
#include <SFML/Graphics.hpp>
#include <time.h>
#include <iostream>
namespace
{
static const float PPU = 16.0f;
const float MOVE_SPEED = 64.0f;
const sf::Vector2f VIEW_SIZE{ 320.0f, 180.0f };
const float FIXED_DELTA_TIME = 1.0f / 30.0f; // Fixed Time Update rate.
}
void update(sf::Transformable& player, float delta)
{
sf::Vector2f velocity;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
velocity.y = -MOVE_SPEED;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
velocity.x = -MOVE_SPEED;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
velocity.y = MOVE_SPEED;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
velocity.x = MOVE_SPEED;
if ((velocity.y > 0.0f || velocity.y < 0.0f) && (velocity.x > 0.0f || velocity.x < 0.0f))
velocity *= sin(45.0f);
player.move(velocity * delta);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Shiver", sf::Style::Default);
// Tried with V-SYNC true and Framerate limit off.
window.setVerticalSyncEnabled(true);
//window.setFramerateLimit(60);
sf::View view(sf::Vector2f(0.0f, 0.0f), VIEW_SIZE);
sf::Clock clock;
srand(time(NULL));
sf::Texture treeTexture;
if (!treeTexture.loadFromFile("Assets/evergreen-tree.png"))
return 0;
sf::Sprite player;
player.setTexture(treeTexture);
player.setTextureRect(sf::IntRect(0, 0, 48, 48));
std::vector<sf::Sprite> trees;
for (unsigned int i = 0; i < 100; ++i)
{
sf::Sprite tree;
tree.setTexture(treeTexture);
tree.setTextureRect(sf::IntRect(0, 0, 48, 48));
tree.setPosition(rand() % 10 * i, rand() % 10 * i);
trees.push_back(tree);
}
unsigned int fixedUpdateCount = 0;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.type == sf::Event::Resized)
{
auto width = VIEW_SIZE.y / event.size.height * event.size.width;
view.setSize(width, VIEW_SIZE.y);
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::F11)
{
auto mode = sf::VideoMode::getDesktopMode();
window.create(sf::VideoMode(mode.width, mode.height), "Shiver", sf::Style::Fullscreen);
window.setVerticalSyncEnabled(true);
auto width = VIEW_SIZE.y / mode.height * mode.width;
view.setSize(width, VIEW_SIZE.y);
}
// Added a sleep to simulate a stutter in the render time.
else if (event.key.code == sf::Keyboard::Space)
{
sf::sleep(sf::seconds(0.5f));
}
}
}
auto delta = clock.restart().asSeconds();
// If our delta time is greater than our fixed update time
// We update at the fixed rate until we catch up.
while (delta > FIXED_DELTA_TIME)
{
update(player, FIXED_DELTA_TIME);
delta -= FIXED_DELTA_TIME;
++fixedUpdateCount;
}
update(player, delta);
if (fixedUpdateCount > 0)
{
std::cout << "Fixed Update: " << fixedUpdateCount << std::endl;
fixedUpdateCount = 0;
}
view.setCenter(player.getPosition());
window.clear(sf::Color(150, 150, 150));
window.setView(view);
for (auto& tree : trees)
{
window.draw(tree);
}
window.draw(player);
window.display();
}
return 0;
}
#include <time.h>
#include <iostream>
namespace
{
static const float PPU = 16.0f;
const float MOVE_SPEED = 64.0f;
const sf::Vector2f VIEW_SIZE{ 320.0f, 180.0f };
const float FIXED_DELTA_TIME = 1.0f / 30.0f; // Fixed Time Update rate.
}
void update(sf::Transformable& player, float delta)
{
sf::Vector2f velocity;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
velocity.y = -MOVE_SPEED;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
velocity.x = -MOVE_SPEED;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
velocity.y = MOVE_SPEED;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
velocity.x = MOVE_SPEED;
if ((velocity.y > 0.0f || velocity.y < 0.0f) && (velocity.x > 0.0f || velocity.x < 0.0f))
velocity *= sin(45.0f);
player.move(velocity * delta);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Shiver", sf::Style::Default);
// Tried with V-SYNC true and Framerate limit off.
window.setVerticalSyncEnabled(true);
//window.setFramerateLimit(60);
sf::View view(sf::Vector2f(0.0f, 0.0f), VIEW_SIZE);
sf::Clock clock;
srand(time(NULL));
sf::Texture treeTexture;
if (!treeTexture.loadFromFile("Assets/evergreen-tree.png"))
return 0;
sf::Sprite player;
player.setTexture(treeTexture);
player.setTextureRect(sf::IntRect(0, 0, 48, 48));
std::vector<sf::Sprite> trees;
for (unsigned int i = 0; i < 100; ++i)
{
sf::Sprite tree;
tree.setTexture(treeTexture);
tree.setTextureRect(sf::IntRect(0, 0, 48, 48));
tree.setPosition(rand() % 10 * i, rand() % 10 * i);
trees.push_back(tree);
}
unsigned int fixedUpdateCount = 0;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.type == sf::Event::Resized)
{
auto width = VIEW_SIZE.y / event.size.height * event.size.width;
view.setSize(width, VIEW_SIZE.y);
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::F11)
{
auto mode = sf::VideoMode::getDesktopMode();
window.create(sf::VideoMode(mode.width, mode.height), "Shiver", sf::Style::Fullscreen);
window.setVerticalSyncEnabled(true);
auto width = VIEW_SIZE.y / mode.height * mode.width;
view.setSize(width, VIEW_SIZE.y);
}
// Added a sleep to simulate a stutter in the render time.
else if (event.key.code == sf::Keyboard::Space)
{
sf::sleep(sf::seconds(0.5f));
}
}
}
auto delta = clock.restart().asSeconds();
// If our delta time is greater than our fixed update time
// We update at the fixed rate until we catch up.
while (delta > FIXED_DELTA_TIME)
{
update(player, FIXED_DELTA_TIME);
delta -= FIXED_DELTA_TIME;
++fixedUpdateCount;
}
update(player, delta);
if (fixedUpdateCount > 0)
{
std::cout << "Fixed Update: " << fixedUpdateCount << std::endl;
fixedUpdateCount = 0;
}
view.setCenter(player.getPosition());
window.clear(sf::Color(150, 150, 150));
window.setView(view);
for (auto& tree : trees)
{
window.draw(tree);
}
window.draw(player);
window.display();
}
return 0;
}
Thanks again!