Skip to content

Instantly share code, notes, and snippets.

@kantoniak
Last active December 12, 2020 22:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kantoniak/acd2499da2a5cc304b12e83e824a9435 to your computer and use it in GitHub Desktop.
Save kantoniak/acd2499da2a5cc304b12e83e824a9435 to your computer and use it in GitHub Desktop.
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