Ported over the 'fix your timestep' game loop with the nifty C++11 chrono stuff

master
Mitchell Hansen 8 years ago
parent 030dbc5997
commit 07885a2b86

Binary file not shown.

1
.gitignore vendored

@ -0,0 +1 @@
.DS_Store

Binary file not shown.

@ -1,26 +1,65 @@
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <chrono>
#include <random>
const int WINDOW_X = 600;
const int WINDOW_Y = 800;
float elap_time(){
static std::chrono::time_point<std::chrono::system_clock> start;
static bool started = false;
if (!started){
start = std::chrono::system_clock::now();
started = true;
}
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_time = now - start;
return elapsed_time.count();
}
int main() {
elap_time();
std::mt19937 rng(time(NULL));
std::uniform_int_distribution<int> rgen(100, 400);
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML");
float step_size = 0.005f;
double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0;
sf::RenderWindow window(sf::VideoMode(300, 300), "SFML");
sf::CircleShape shape(10.0f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed){
if (event.type == sf::Event::Closed)
window.close();
}
elapsed_time = elap_time();
delta_time = elapsed_time - current_time;
current_time = elapsed_time;
if (delta_time > 0.02f)
delta_time = 0.02f;
accumulator_time += delta_time;
while ((accumulator_time - step_size) >= step_size) {
accumulator_time -= step_size;
// Update(step_size);
}
window.clear();
window.draw(shape);
window.clear(sf::Color::Black);
window.display();
}
}
return 0;
}

Loading…
Cancel
Save