Banjo API 0.0.1
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() for pausing execution. These are essential for frame timing, animations, and timed events.

#define BJ_AUTOMAIN_CALLBACKS
#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/system.h>
#include <banjo/time.h>
int bj_app_begin(void** user_data, int argc, char* argv[]) {
(void)user_data; (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.
return bj_callback_exit_error;
}
return bj_callback_continue;
}
int bj_app_iterate(void* user_data) {
(void)user_data;
// bj_run_time() returns the number of seconds (as a double) since the
// program started. Useful for animations, timeouts, and delta time
// calculations.
double elapsed = bj_run_time();
bj_trace("- %lf", elapsed);
// bj_sleep() pauses execution for the specified number of milliseconds.
// This prevents busy-waiting and reduces CPU usage. Here we sleep for
// 300ms between iterations.
bj_sleep(300);
// Exit after approximately 3 seconds have elapsed.
return elapsed >= 3.0 ? bj_callback_exit_success : bj_callback_continue;
}
int bj_app_end(void* user_data, int status) {
(void)user_data;
bj_end();
return status;
}
int bj_app_begin(void **user_data, int argc, char *argv[])
Definition audio_pcm.c:25
int bj_app_iterate(void *user_data)
Definition audio_pcm.c:60
int bj_app_end(void *user_data, int status)
Definition audio_pcm.c:84
#define bj_trace(...)
Log a message using the BJ_LOG_TRACE level.
Definition log.h:77
bj_bool bj_begin(int systems, struct bj_error **error)
Initializes the system.
void bj_end(void)
De-initializes the system.
@ BJ_NO_SYSTEM
Definition system.h:18
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
double bj_run_time(void)
Gets the current time in seconds since Banjo initialization.
Logging utility functions.
Portable main substitution and application callback facilities.
Header file for system interactions.
Header file for time manipulation utilities.