#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
static const int FRAME_LIMIT = 120;
bool WINDOW_FOCUS = true;
const sf::Time TICK_INTERVAL = sf::seconds(1.0f / FRAME_LIMIT);
int main()
{
// Create the main rendering window
sf::RenderWindow window(sf::VideoMode(768, 768), "SFML works!");
sf::RectangleShape Rectangle;
Rectangle.setSize(sf::Vector2f(300, 300));
Rectangle.setPosition(150, 150);
Rectangle.setFillColor(sf::Color::Blue);
Rectangle.setOutlineThickness(1.0f);
Rectangle.setOutlineColor(sf::Color::White);
sf::Clock clock;
sf::Time accumulatedTime;
clock.restart();
//Yes, I've tried commenting out the following line. Nothing changed
window.setFramerateLimit(FRAME_LIMIT);
//LOOP UNTIL YOU DONT WANT IT TO
while (window.isOpen())
{
//WINDOW EVENTS (ignore this)
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//TIME CALCULATIONS
accumulatedTime += clock.restart();//.asMilliseconds();
//PHYSICS CODE
while(accumulatedTime >= TICK_INTERVAL)
{
accumulatedTime -= TICK_INTERVAL;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
// left key is pressed: move our character
Rectangle.move(-0.1f, 0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
Rectangle.move(0.1f, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
// left key is pressed: move our character
Rectangle.move(0, -0.1f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
Rectangle.move(0, 0.1f);
}
}
//DRAWING
window.clear();
window.draw(Rectangle);
window.display();
}
return EXIT_SUCCESS;
}
With this code, the rectangle's movements are very choppy. I really don't know what I'm doing wrong. I tried the other way (multiplying rectangle move value by deltaTime) and the movement was awesome! Smooth! Pleasant to look at! However, multiplying by deltaTime all the time is tedious and annoying.
I would love some help.