Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
std::chrono example - simple game loop
#include <chrono>
#include <iostream>
using namespace std::literals::chrono_literals;
void input() {
}
void update() {
// Do something to kill the time
long long some_number = 1799997;
for (unsigned i = 0; i<1000000; i++) {
some_number *= some_number + some_number;
}
}
void render() {
}
int main() {
bool is_running = true;
unsigned int frame = 0;
auto last_timepoint = std::chrono::steady_clock::now();
while (is_running) {
std::cout << "Loop:" << std::endl;
input();
auto current_timepoint = std::chrono::steady_clock::now();
while (current_timepoint - last_timepoint < 16ms) { // No more than 60 FPS
std::cout << "> updating..." << std::endl;
update();
current_timepoint = std::chrono::steady_clock::now();
}
last_timepoint = current_timepoint;
render();
frame++;
if (3 == frame) {
is_running = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.