Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
stopwatch.c
Go to the documentation of this file.
1
15
16#include <banjo/log.h>
17#include <banjo/main.h>
18#include <banjo/system.h>
19#include <banjo/time.h>
20
21int main(int argc, char* argv[]) {
22 (void)argc; (void)argv;
23
24 if (!bj_begin(BJ_NO_SYSTEM, 0)) {
25 return 1;
26 }
27
28 // A stopwatch must be zero-initialised before use.
29 bj_stopwatch stopwatch = {0};
30
31 // Run for about 3 seconds, measuring the time around each iteration.
32 double elapsed = 0.0;
33 while (elapsed < 3.0) {
34 // bj_step_stopwatch records a new step and returns the seconds since
35 // the previous one - the delta time. This is crucial for
36 // frame-independent movement and animation. For example:
37 // position += velocity * delay;
38 // ensures objects move at constant speed regardless of loop rate.
39 // The first call starts the stopwatch and returns 0.
40 double delay = bj_step_stopwatch(&stopwatch);
41
42 // Total time elapsed since the stopwatch started (first step call).
43 elapsed = bj_stopwatch_total(&stopwatch);
44
45 bj_trace("Elapsed: %.3lf s | Delay: %.3lf s", elapsed, delay);
46
47 bj_sleep_ms(300);
48 }
49
50 bj_end();
51 return 0;
52}
#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
double bj_step_stopwatch(struct bj_stopwatch *stopwatch)
Record a step and return the seconds since the previous step.
void bj_sleep_ms(int milliseconds)
Suspend the current thread for at least the given number of milliseconds.
double bj_stopwatch_total(const struct bj_stopwatch *stopwatch)
Return the seconds elapsed since the stopwatch was last reset.
Structure representing a simple stopwatch.
Definition time.h:153
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.