1
General / Trying to implement DeWITTER's game loop.
« on: January 22, 2013, 02:22:14 pm »
Hello. I'm trying to get the game loop from http://www.koonsolo.com/news/dewitters-gameloop/ to work on my project. The code works fine, however the interpolated object will stutter slightly while the non-interpolated object runs very smooth. I can't seem to figure out why and I was wondering if anyone could help me out with this.
Here's my code:
As you can probably see the top "###" stutters a bit while the bottom "###" runs very smoothly.
Here's my code:
#include <SFML/Graphics.hpp>
#include <iostream>
const int TICKS_PER_SECOND = 25;
const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 5;
//StateManager TargetState;
int main()
{
sf::RenderWindow Window(sf::VideoMode(800,600,32), "Test", !sf::Style::Resize | sf::Style::Close);
// Game loop variables
sf::Clock ticker;
sf::Int32 next_game_tick = ticker.getElapsedTime().asMilliseconds();
int loops;
float interpolation;
//TargetState.changeState(MenuState::Instance(&Window));
while(Window.isOpen()) {
//TargetState.input();
// Update the game
loops = 0;
while(ticker.getElapsedTime().asMilliseconds() > next_game_tick && loops < MAX_FRAMESKIP) {
//TargetState.update();
next_game_tick += SKIP_TICKS;
loops++;
}
interpolation = float(ticker.getElapsedTime().asMilliseconds() + SKIP_TICKS - next_game_tick)
/ float (SKIP_TICKS);
Window.clear();
//TargetState.draw(interpolation);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text test("###", font, 250);
sf::Text test2("###", font, 250);
static float lex = 0.0;
test.setPosition(lex + interpolation, 0);
test2.setPosition(lex , 200);
lex += 0.05;
Window.draw(test);
Window.draw(test2);
Window.display();
}
return 0;
}
#include <iostream>
const int TICKS_PER_SECOND = 25;
const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 5;
//StateManager TargetState;
int main()
{
sf::RenderWindow Window(sf::VideoMode(800,600,32), "Test", !sf::Style::Resize | sf::Style::Close);
// Game loop variables
sf::Clock ticker;
sf::Int32 next_game_tick = ticker.getElapsedTime().asMilliseconds();
int loops;
float interpolation;
//TargetState.changeState(MenuState::Instance(&Window));
while(Window.isOpen()) {
//TargetState.input();
// Update the game
loops = 0;
while(ticker.getElapsedTime().asMilliseconds() > next_game_tick && loops < MAX_FRAMESKIP) {
//TargetState.update();
next_game_tick += SKIP_TICKS;
loops++;
}
interpolation = float(ticker.getElapsedTime().asMilliseconds() + SKIP_TICKS - next_game_tick)
/ float (SKIP_TICKS);
Window.clear();
//TargetState.draw(interpolation);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text test("###", font, 250);
sf::Text test2("###", font, 250);
static float lex = 0.0;
test.setPosition(lex + interpolation, 0);
test2.setPosition(lex , 200);
lex += 0.05;
Window.draw(test);
Window.draw(test2);
Window.display();
}
return 0;
}
As you can probably see the top "###" stutters a bit while the bottom "###" runs very smoothly.