Banjo API
1.0.0-rc.2
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
21
int
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
// Update the stopwatch once per iteration to record the current time.
35
// The first call starts the stopwatch.
36
bj_step_stopwatch
(&stopwatch);
37
38
// Total time elapsed since the stopwatch started (first step call).
39
elapsed =
bj_stopwatch_elapsed
(&stopwatch);
40
41
// Delta time - time since the previous step. This is crucial for
42
// frame-independent movement and animation. For example:
43
// position += velocity * delay;
44
// ensures objects move at constant speed regardless of loop rate.
45
double
delay =
bj_stopwatch_delay
(&stopwatch);
46
47
bj_trace
(
"Elapsed: %.3lf s | Delay: %.3lf s"
, elapsed, delay);
48
49
bj_sleep
(300);
50
}
51
52
bj_end
();
53
return
0;
54
}
main
int main(int argc, char *argv[])
Definition
audio_pcm.c:177
bj_trace
#define bj_trace(...)
Log a message using the BJ_LOG_TRACE level.
Definition
log.h:113
bj_begin
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
bj_end
void bj_end(void)
De-initialises the system.
BJ_NO_SYSTEM
@ BJ_NO_SYSTEM
Definition
system.h:79
bj_sleep
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
bj_stopwatch_delay
double bj_stopwatch_delay(const struct bj_stopwatch *stopwatch)
Returns the time in seconds since the last step.
bj_step_stopwatch
void bj_step_stopwatch(struct bj_stopwatch *stopwatch)
Records a step/checkpoint in time.
bj_stopwatch_elapsed
double bj_stopwatch_elapsed(const struct bj_stopwatch *stopwatch)
Returns the elapsed time in seconds since the stopwatch was reset.
bj_stopwatch
Structure representing a simple stopwatch.
Definition
time.h:150
log.h
Logging utility functions.
main.h
Portable main() replacement with platform-aware entry shim.
system.h
Header file for system interactions.
time.h
Header file for time manipulation utilities.
examples
stopwatch.c
Generated on
for Banjo API by
1.14.0