Banjo API 0.0.1
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.

#define BJ_AUTOMAIN_CALLBACKS
#include <banjo/event.h>
#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/system.h>
#include <banjo/window.h>
int bj_app_begin(void** user_data, int argc, char* argv[]) {
(void)user_data; (void)argc; (void)argv;
return bj_callback_exit_error;
}
// 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 bj_callback_continue;
}
int bj_app_iterate(void* user_data) {
(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 returns true when the user clicks
// the X button, or when a callback (like bj_close_on_escape) requests it.
? bj_callback_exit_success
: bj_callback_continue;
}
int bj_app_end(void* user_data, int status) {
(void)user_data;
// Destroy the window and free OS resources. Always call this before bj_end().
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
bj_window * window
Definition bitmap_blit.c:24
Sytem event management API.
struct bj_window bj_window
Definition api.h:296
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)
Initializes the system.
void bj_end(void)
De-initializes the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:20
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 substitution and application callback facilities.
Header file for system interactions.
Header file for bj_window type.