I have been walking through the Arkanoid in 160 lines tutorial (I don't believe knowledge of the tutorial is necessary to answer the question). I just finished the second one and started to notice my version is exhibiting some undesirable behavior. This occurs when I pass the ball and paddle function time deltas.
What happens is the first visual the player sees is further along then I would like to be. I have attached two screenshots. In the first one the ball is near the center of the screen and this is the appropriate behavior. However, in the second one the ball has already knocked out two bricks and traveled quite a bit of distance before it was ever drawn.
What I have noticed is my initial draws take a very long time compared to the following ones on the next iteration of the loop. Here is a code snippet to show what I am doing. The points of interest are where I begin timing (comment 'Start time') and finish timing ('End time').
while (window.isOpen())
{
auto startFrame(std::chrono::high_resolution_clock::now()); // Start time
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
ball.update();
paddle.update();
testCollision(paddle, ball);
for (auto &brick : bricks)
{
testCollision(brick, ball);
}
bricks.erase(std::remove_if(begin(bricks), end(bricks),
[](const Brick &brick){ return brick.mDestroyed; }),
end(bricks));
window.clear(sf::Color::Black);
window.draw(ball.mShape);
window.draw(paddle.mShape);
for (const auto &brick : bricks)
{
window.draw(brick.mShape);
}
window.display();
auto endFrame(std::chrono::high_resolution_clock::now()); // End time
// Frame duration
auto frameTime(std::chrono::duration_cast<std::chrono::duration<FrameTime, std::milli>>
(endFrame - startFrame).count());
}
The value of 'frametime' on the initial time through the loop takes around 400-700 milliseconds. This value will be passed to the update functions and on the next draw the ball will make a huge jump and that's the undesirable behavior I am seeing. However, after this initial frame each frame then takes around 15 milliseconds and this is very expected (as it matches the tutorial).
Also, to be clear I moved around the time points and it was clear that it was the draws were taking up the bulk of the time. I believe I understand what is going on, but I am surprised. I want to ask the following questions. Should the initial draw be significantly slower than the following ones? Also, what explains the difference in Romeo's (author of tutorial) game behavior and mine? I don't think such a simple example would exasperate the fact I may be using slower computer.