I'm not even sure how to word my problem, but the problem is that I was told that by limiting the framerate, the program would be able to run on any computer the same, just maybe a bit laggy but still the same. the way the youtube video explained it, he had a sprite move a certain distance multiplied by the elapsed time between the last frame. all of that made sense. But now I'm having a problem trying to implement that same idea into events. The problem for me is that sometimes the paddle skips when going up, and I really don't know why. I have tried this several times with almost the same results. Is there a simple fix for this? I'm using W for up, which is using the limited framerate to move, where as S is down, and it moves without a limited framerate. Here's my code:
#include <iostream>
#include <sfml/graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(480, 480), "Pong");
sf::Event event;
sf::RectangleShape Paddle1(sf::Vector2f(40, 100));
Paddle1.setOrigin(20, 50);
Paddle1.setPosition(50, 240);
Paddle1.setFillColor(sf::Color::Red);
Paddle1.setOutlineThickness(-1);
Paddle1.setOutlineColor(sf::Color::Black);
window.setFramerateLimit(60);
while(window.isOpen())
{
sf::Clock clock;
sf::Time time = clock.getElapsedTime();
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
Paddle1.move(0, -0.5 * time.asMicroseconds());
std::cout << Paddle1.getPosition().y << std::endl;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
Paddle1.move(0, 10);
std::cout << Paddle1.getPosition().y << std::endl;
}
clock.restart().asMicroseconds();
}//ends polling
window.clear(sf::Color::White);
////////////////////////
window.draw(Paddle1);
////////////////////////
window.display();
}//ends game loop
return 0;
}