As I said many many times before, I'm very new with game programming.
I'v read on several occasions that using delta time when calculating movement is the right way to begin.
To do that I'll need to get the delta time from the sf::Clock
object.
Though I'm not sure I'm placing it right, so I thought I'd ask the forums:
void Game::run()
{
initialize();
sf::Image image;
image.loadFromFile("data/graphics/items/coin.png");
image.createMaskFromColor(sf::Color::Magenta);
sf::Texture texture;
texture.loadFromImage(image);
sf::Sprite sprite(texture);
sprite.setPosition(10,10);
sf::Event appEvent;
sf::Clock Clock;
while(app.isOpen())
{
/* Get the deltatime of the last frame */
const float dt = Clock.restart().asSeconds();
while(app.pollEvent(appEvent))
{
switch(appEvent.type)
{
case sf::Event::Closed:
app.close();
break;
default:
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
sprite.move(60 * dt, 0);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
sprite.move(-60 * dt, 0);
app.clear(sf::Color(0,0,0));
/* Update the active screen and render the objects */
/*
screenStack.top()->Update(dt);
screenStack.top()->Draw(app, dt);
*/
app.draw(sprite);
app.display();
}
}
Is this the right placement of the "Clock.restart()" function? Also i'v heard of a "better" way by using "fixed timestep" but haven't found much on the subject.
That and I'm getting some random distortion when moving around the screen, but I'v heard SFML use double buffering as default is this true or not? If yes, then what is causing the flickering/distortions?
Kind regards Moonkis