Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
template_callbacks.c

Loop-based program structure using Banjo's application API.

Loop-based program structure using Banjo's application API.This is the canonical Banjo program shape: a struct bj_app populated with setup, step, and teardown function pointers, passed to bj_run_app which drives the lifecycle. The same source compiles and runs the same way on Linux, macOS, Windows, and Emscripten.

#include <banjo/app.h>
#include <banjo/main.h>
#include <banjo/log.h>
#include <banjo/system.h>
// Called once at program startup. Initialize your resources here.
static void* setup(struct bj_app* app, void* init_data) {
(void)init_data;
// Initialize Banjo subsystems. Same as in template.c.
bj_quit_app(app, 1);
return 0;
}
return 0;
}
// Called repeatedly in a loop. Your main logic goes here. Call bj_quit_app
// to exit the loop and proceed to teardown.
static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
(void)tick;
(void)user_data;
bj_info("Hello Banjo!");
// Exit after one iteration for this simple example. In a real program,
// you'd keep iterating until the user closes the window or some exit
// condition is met.
bj_quit_app(app, 0);
}
// Called once after the loop ends. Cleanup your resources here.
static void teardown(struct bj_app* app, void* user_data) {
(void)user_data;
// Shutdown Banjo and release resources.
bj_end();
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
return bj_run_app(setup, step, 0, teardown, 0);
}
Application lifecycle: callback-driven setup, step, and teardown.
int main(int argc, char *argv[])
Definition audio_pcm.c:177
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
Definition audio_pcm.c:144
static void teardown(struct bj_app *app, void *user_data)
Definition audio_pcm.c:170
static void * setup(struct bj_app *app, void *init_data)
Definition audio_pcm.c:107
int bj_run_app(bj_app_setup_fn setup, bj_app_step_fn step, bj_app_fixed_step_fn fixed_step, bj_app_teardown_fn teardown, void *init_data)
Drive the application lifecycle.
void bj_quit_app(struct bj_app *app, int exit_code)
Signal the given application to exit on the next iteration.
Timing snapshot handed to the step and fixed-step callbacks.
Definition app.h:106
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:81
@ BJ_AUDIO_SYSTEM
Definition system.h:80
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for system interactions.