Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
time.c

Using Banjo's time functions for elapsed time tracking and sleeping.

Using Banjo's time functions for elapsed time tracking and sleeping.This demonstrates bj_run_time() for measuring elapsed time and bj_sleep_ms() for pausing execution. These are essential for frame timing, animations, and timed events.

It is a plain console program with its own loop rather than an App (bj_run_app) program: there is no window or event handling to drive, so the bj_sleep_ms() that paces this loop is exactly what such a program should use.

#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/system.h>
#include <banjo/time.h>
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
// BJ_NO_SYSTEM means we don't need video or audio - just basic functions
// like time and logging work without any subsystem.
if (!bj_begin(BJ_NO_SYSTEM, 0)) {
return 1;
}
// Run for about 3 seconds, printing the elapsed time each iteration.
double elapsed = 0.0;
while (elapsed < 3.0) {
// bj_run_time() returns the number of seconds (as a double) since the
// program started. Useful for animations, timeouts, and delta time.
elapsed = bj_run_time();
bj_trace("- %lf", elapsed);
// bj_sleep_ms() pauses execution for the given number of milliseconds,
// which paces this loop and keeps it off the CPU between prints.
}
bj_end();
return 0;
}
#define bj_trace(...)
Log a message using the BJ_LOG_TRACE level.
Definition log.h:113
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_NO_SYSTEM
Definition system.h:79
void bj_sleep_ms(int milliseconds)
Suspend the current thread for at least the given number of milliseconds.
double bj_run_time(void)
Gets the current time in seconds since Banjo initialisation.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for system interactions.
int main(void)
Header file for time manipulation utilities.