Last active
December 12, 2020 22:24
-
-
Save kantoniak/acd2499da2a5cc304b12e83e824a9435 to your computer and use it in GitHub Desktop.
std::chrono example - simple game loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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