Hi! I've been working on overall design of a game loop and I've gotten most of information from here:
http://gameprogrammingpatterns.com/game-loop.html
I'm using SFML, so I have a question and also code I need help with. First with the question:
The last (and from his words usually the best) type of game loop is "Fixed update time step, variable rendering." I get the fixed update time step, but I have no idea how to do the variable rendering. If someone could give me a super rough example of this in SFML or something similar it would be a godsend. Because I have no idea how to render something at half speed.
Onto my problem! I've been trying to create the "Variable time step", and I think I have done so, but there's a problem: my program is very unstable when it comes to draw time and fps. With about 1.2k sprites on the screen it will jump to 13 ms per draw frame then suddenly jump to 40-100ms. Is there any way to make it not do that? You can see my "info" I'm getting these numbers with:
http://i.imgur.com/UEJnXgc.png
My graphics card (GTX 760) is around 42% load @ 1600 squares on the screen so it'd definitely not because I'm close to the max my system can handle. It has to be because of something else.
Here is the loop code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
void App::MainLoop()
{
sf::Clock fpsClock;
sf::Clock printClock;
sf::Clock updateClock;
sf::Clock timeTakenClock;
sf::Time previous, lag, current, elapsed, update = sf::seconds(1.f / 80);
sf::Time frameTime;
while (window->isOpen())
{
//get current fps
frameTime = fpsClock.restart();
fps = sf::seconds(1.f) / frameTime;
//fixed time step
current = updateClock.getElapsedTime();
elapsed = current - previous;
previous = current;
lag += elapsed;
//get input time
timeTakenClock.restart();
Input();
inputTime = timeTakenClock.getElapsedTime();
timeTakenClock.restart();
while (lag >= update)
{
Update();
lag -= update;
}
updateTime = timeTakenClock.getElapsedTime();
timeTakenClock.restart();
Draw();
drawTime = timeTakenClock.getElapsedTime();
if (printClock.getElapsedTime() > sf::seconds(1))
{
int totalShapes = 0;
for (auto it = trees.begin(); it != trees.end(); it++)
{
totalShapes += (*it)->getSize();
}
system("cls");
std::cout << "fps: " << fps << " | " << sys.timeToString(frameTime) << " frame time" << std::endl
<< "input time: " << sys.timeToString(inputTime) << std::endl
<< "update time: " << sys.timeToString(updateTime) << std::endl
<< "draw time: " << sys.timeToString(drawTime) << std::endl
<< "object count: " << totalShapes << std::endl;
printClock.restart();
}
}
}
|
Thanks a bunch for the read and answering just one of my questions would be really helpful. I doubt you need to see the rest of the program: it's literally just an incomplete (more or les bad) QuadTree with a vector of rectangles. In fact, here's the update part. Nothing special:
1 2 3 4 5 6 7 8
|
void QuadTree::Update()
{
for (auto it = shapes.begin(); it != shapes.end(); it++)
{
(*it)->rotate(1);
//(*it)->move(sf::Vector2f(.1, 0));
}
}
|
edit: so as i increase the update speed to higher numbers, the game seems to run a lot better but crashes harder. i can get to around 800 objects without shuddering with an update speed of 1/400th of a second but after it starts to shudder it can never stop. is there a sweet spot i should put it at? obviously not everybody's computer is the same..