Hello. I'm relatively new to SFML, but I've dabbled around with Allegro and game engines for awhile. I'm trying to create a basic fixed time-step loop for a game in SFML. I based the loop off a tutorial for Allegro. You can see the tutorial here:
http://fixbyproximity.com/2011/08/2d-game-dev-part-4-1-timing-our-game-loop-2/ . The sourcecode is below the video if you're interested.
Anyways, I want the game to act independent of FPS. I wrote a simple program below. I want the circle to take 5 seconds to go from the left side to the right side of the screen (It moves 1 pixel every 1/60th of a second).
#include <SFML\Graphics.hpp>
using namespace sf;
int main()
{
const float FPS = 60.0f; //The desired FPS. (The number of updates each second).
bool redraw = true; //Do I redraw everything on the screen?
RenderWindow window(VideoMode(300, 300, 32), "Hello");
Clock clock;
CircleShape circle(10.0f);
circle.setOrigin(10.0f, 10.0f);
circle.setPosition(0, 150.0f);
Event ev;
while (window.isOpen())
{
//Wait until 1/60th of a second has passed, then update everything.
if (clock.getElapsedTime().asSeconds() >= 1.0f / FPS)
{
redraw = true; //We're ready to redraw everything
circle.move(1.0f, 0);
clock.restart();
}
else //Sleep until next 1/60th of a second comes around
{
Time sleepTime = seconds((1.0f / FPS) - clock.getElapsedTime().asSeconds());
sleep(sleepTime);
}
//Handle input
while (window.pollEvent(ev))
{
if (ev.type == Event::Closed) window.close();
}
//Draw stuff if ready
if (redraw)
{
redraw = false;
window.clear(Color(0, 0, 0));
window.draw(circle);
window.display();
}
}
return 0;
}
Fraps reports 58-59 fps, and the circle takes 5 seconds to do its thing. If I remove the if(redraw) condition, Fraps reports around 110 fps, and the circle still takes 5 seconds (which is what I want).
My questions is: is the above code a good way to approach this? I'm just worried that I'll discover some flaw halfway into a game and have to change everything.