How do I "tick" my simulation every 17 milliseconds without a realtime clock?
Posted: Fri Jul 12, 2024 6:21 pm
My program's simulation runs at a fixed rate of 60 times per second (16.67 milliseconds per tick, later changed to 17 milliseconds due to a lack of floating point number support). On a system with a real-time clock, this worked perfectly fine, using the following code:
cc_tickGame() is the function which runs the simulation, while cc_util_ctms() simply returns the current time in milliseconds. game->timeLastTicked is the last time, in milliseconds, when cc_tickGame() was called.
The issue is that this code relies on the real-time clock functionality from the standard library header sys/time.h, which works fine under Unix (and other POSIX-compliant systems), but either doesn't exist or works very differently under cc65 on a 6502 system.
I tried a version of this using sleep() from cc65's unistd.h, but that just doesn't work how I need it to. Is there a better way to define a fixed tick rate for my game?
Code: Select all
while (game->isRunning) {
if (game->timeLastTicked + CC_TICKRATE < cc_util_ctms()) {
cc_tickGame(game);
//printf("[CCCore] Current tick: %d\n", game->tick);
}
}
The issue is that this code relies on the real-time clock functionality from the standard library header sys/time.h, which works fine under Unix (and other POSIX-compliant systems), but either doesn't exist or works very differently under cc65 on a 6502 system.
I tried a version of this using sleep() from cc65's unistd.h, but that just doesn't work how I need it to. Is there a better way to define a fixed tick rate for my game?