Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
event_callbacks.c
Go to the documentation of this file.
1
102
103#include <banjo/app.h>
104#include <banjo/main.h>
105#include <banjo/error.h>
106#include <banjo/event.h>
107#include <banjo/log.h>
108#include <banjo/memory.h>
109#include <banjo/system.h>
110#include <banjo/window.h>
111
113
114typedef struct {
115 size_t cursor;
116 size_t button;
117 size_t key;
118 size_t enter;
119 size_t resize;
121
122// Callback functions follow a standard signature:
123// (bj_window* window, const event_type* event, void* user_data)
124// The user_data parameter is whatever you passed during registration.
125
126void cursor_callback(bj_window* p_window, const bj_cursor_event* e, void* data) {
127 event_counter* counter = (event_counter*)data;
128 ++counter->cursor;
129 bj_info("Cursor event, window %p, (%d,%d)",
130 (void*)p_window, e->x, e->y
131 );
132}
133
134void button_callback(bj_window* p_window, const bj_button_event* e, void* data) {
135 event_counter* counter = (event_counter*)data;
136 ++counter->button;
137 bj_info("Button event, window %p, button %d, %s, (%d,%d)",
138 (void*)p_window, e->button,
139 e->action == BJ_EVENT_ACTION_PRESS ? "pressed" : "released",
140 e->x, e->y
141 );
142}
143
144void key_callback(bj_window* p_window, const bj_key_event* e, void* data) {
145 event_counter* counter = (event_counter*)data;
146 ++counter->key;
147 (void)p_window;
148
149 const char* action_str = "pressed";
150 if(e->action != BJ_EVENT_ACTION_PRESS) {
151 action_str = e->action == BJ_EVENT_ACTION_RELEASE ? "released" : "repeated";
152 }
153
154 bj_info("Key 0x%04X (%s) Scancode 0x%04X (with no mods) was %s",
155 e->key, bj_key_name(e->key), e->scancode, action_str
156 );
157
158 if(e->key == BJ_KEY_ESCAPE) {
160 }
161}
162
163void enter_callback(bj_window* p_window, const bj_enter_event* e, void* data) {
164 event_counter* counter = (event_counter*)data;
165 ++counter->enter;
166 bj_info("Enter event, window %p, %s, (%d,%d)",
167 (void*)p_window,
168 e->enter ? "entered" : "left",
169 e->x, e->y
170 );
171}
172
173// The resize callback is the odd one out: it is per-window and receives
174// the new size directly rather than an event struct. Banjo has already
175// resized the framebuffer by the time this fires; here we just count and
176// log it. A real program would recompute layout or rescale here.
177void resize_callback(bj_window* p_window, int width, int height, void* data) {
178 event_counter* counter = (event_counter*)data;
179 ++counter->resize;
180 bj_info("Resize event, window %p, %dx%d", (void*)p_window, width, height);
181}
182
183static void* setup(struct bj_app* app, void* init_data) {
184 (void)init_data;
186
187 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
188 bj_free(counter);
189 bj_quit_app(app, 1);
190 return 0;
191 }
192
193 // Opt the window into user resizing; it is fixed-size otherwise.
194 window = bj_create_window("Event Callbacks", 100, 100, 800, 600,
196
197 // Register callbacks for each event type. When bj_dispatch_events() runs,
198 // these functions will be called automatically for each matching event.
199 // The second parameter (counter) is passed to the callback as user_data.
204 // Resize is registered per-window, not globally like the input callbacks.
206 return counter;
207}
208
209static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
210 (void)tick;
211 (void)user_data;
212
213 // bj_dispatch_events() processes all queued events and invokes the
214 // registered callbacks. Unlike polling where you manually check each event,
215 // callbacks are automatically invoked for you.
217
219 bj_quit_app(app, 0);
220 }
221}
222
223static void teardown(struct bj_app* app, void* user_data) {
224 (void)app;
225 event_counter* counter = (event_counter*)user_data;
226 bj_info("Total events: %ld cursor, %ld button, %ld key, %ld enter, %ld resize",
227 counter->cursor, counter->button, counter->key, counter->enter, counter->resize
228 );
229
231 bj_end();
232 bj_free(counter);
233}
234
235int main(int argc, char* argv[]) {
236 (void)argc; (void)argv;
237 return bj_run_app(setup, step, 0, teardown, 0);
238}
Application lifecycle: callback-driven setup, step, and teardown.
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
Definition audio_pcm.c:142
static void teardown(struct bj_app *app, void *user_data)
Definition audio_pcm.c:168
bj_audio_play_note_data data
Definition audio_pcm.c:104
static void * setup(struct bj_app *app, void *init_data)
Definition audio_pcm.c:107
bj_window * window
Definition bitmap_blit.c:24
Recoverable error handling.
Sytem event management API.
void button_callback(bj_window *p_window, const bj_button_event *e, void *data)
void cursor_callback(bj_window *p_window, const bj_cursor_event *e, void *data)
void resize_callback(bj_window *p_window, int width, int height, void *data)
void enter_callback(bj_window *p_window, const bj_enter_event *e, void *data)
void key_callback(bj_window *p_window, const bj_key_event *e, void *data)
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:107
struct bj_window bj_window
Definition api.h:384
int y
Cursor y position.
Definition event.h:437
int y
Cursor y position.
Definition event.h:428
int x
Cursor x position.
Definition event.h:444
int y
Cursor y position.
Definition event.h:445
int scancode
Scancode (layout-independent)
Definition event.h:455
int button
Button identifier (e.g., BJ_BUTTON_LEFT)
Definition event.h:447
bj_bool enter
BJ_TRUE if entering window, BJ_FALSE if leaving.
Definition event.h:429
enum bj_event_action action
Action (press/release/repeat)
Definition event.h:456
enum bj_event_action action
Action (press/release)
Definition event.h:446
int x
Cursor x position.
Definition event.h:427
enum bj_key key
Key identifier.
Definition event.h:454
int x
Cursor x position.
Definition event.h:436
const char * bj_key_name(int key)
Get the string name of a key.
void bj_dispatch_events(void)
Poll and dispatch all pending events.
bj_cursor_callback_fn bj_set_cursor_callback(bj_cursor_callback_fn callback, void *user_data)
Set the global callback for cursor events.
bj_enter_callback_fn bj_set_enter_callback(bj_enter_callback_fn callback, void *user_data)
Set the global callback for mouse enter/leave events.
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_button_callback_fn bj_set_button_callback(bj_button_callback_fn callback, void *user_data)
Set the global callback for mouse button events.
@ BJ_KEY_ESCAPE
Esc key.
Definition event.h:177
@ BJ_EVENT_ACTION_PRESS
The key or button was pressed.
Definition event.h:395
@ BJ_EVENT_ACTION_RELEASE
The key or button was released.
Definition event.h:394
Represent a mouse button event.
Definition event.h:443
Represent a mouse cursor movement event.
Definition event.h:435
Represent a mouse enter or leave event.
Definition event.h:426
Represent a keyboard key event.
Definition event.h:453
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
void * bj_calloc(size_t size)
Allocate size bytes of zero-initialised memory.
void bj_free(void *memory)
Free a previously allocated memory block.
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_bool bj_window_should_close(struct bj_window *window)
Report whether a window's close flag is set.
struct bj_window * bj_create_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 window with the given attributes.
void bj_set_window_should_close(struct bj_window *window)
Flag a window to be closed.
void bj_set_window_resize_callback(struct bj_window *window, bj_window_resize_fn fn, void *user_data)
Register the resize callback for window.
void bj_destroy_window(struct bj_window *window)
Destroy a window and release its resources.
@ BJ_WINDOW_FLAG_RESIZABLE
User may resize the window.
Definition window.h:196
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
All memory-related functions, including custom allocators.
Header file for system interactions.
int main(void)
Header file for bj_window type.