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

Creating and managing windows with event handling.

Creating and managing windows with event handling.This demonstrates window creation, event processing, and proper cleanup. Every graphical Banjo application needs a window and must process events to remain responsive.

#include <banjo/app.h>
#include <banjo/main.h>
#include <banjo/event.h>
#include <banjo/log.h>
#include <banjo/system.h>
#include <banjo/window.h>
static void* setup(struct bj_app* app, void* init_data) {
(void)init_data;
bj_quit_app(app, 1);
return 0;
}
// Create an OS window with bj_bind_window().
// Parameters: title, x position, y position, width, height, flags
// The last parameter (flags) can specify fullscreen, borderless, etc.
// Returns a window handle that must be destroyed with bj_unbind_window().
window = bj_bind_window("Simple Banjo Window", 100, 100, 800, 600, 0, 0);
// Set up a keyboard callback. bj_close_on_escape is a built-in helper that
// requests window closure when ESC is pressed.
return 0;
}
static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
(void)tick;
(void)user_data;
// Process OS events (mouse, keyboard, window resize, close button, etc).
// This must be called every frame or the window will freeze and become
// unresponsive. Events are either handled by callbacks or can be polled.
// Check if the window should close. This is set when the user clicks the
// X button, or when a callback (like bj_close_on_escape) requests it.
bj_quit_app(app, 0);
}
}
static void teardown(struct bj_app* app, void* user_data) {
(void)user_data;
// Destroy the window and free OS resources. Always call this before bj_end().
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
bj_window * window
Definition bitmap_blit.c:24
Sytem event management API.
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
struct bj_window bj_window
Definition api.h:354
void bj_dispatch_events(void)
Poll and dispatch all pending events.
void bj_close_on_escape(struct bj_window *window, const struct bj_key_event *event, void *user_data)
Handle the ESC key to close a window.
bj_key_callback_fn bj_set_key_callback(bj_key_callback_fn callback, void *user_data)
Set the global callback for keyboard key events.
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
struct bj_window * bj_bind_window(const char *title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags, struct bj_error **error)
Create a new struct bj_window with the specified attributes.
bj_bool bj_should_close_window(struct bj_window *window)
Get the close flag state of a window.
void bj_unbind_window(struct bj_window *window)
Deletes a struct bj_window object and releases associated memory.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for system interactions.
Header file for bj_window type.