Event handling using the callback pattern.
Event handling using the callback pattern.This demonstrates registering callback functions that are automatically invoked when events occur. Callbacks provide cleaner, more declarative event handling compared to polling, especially for simple input handling.
#define BJ_AUTOMAIN_CALLBACKS
typedef struct {
size_t cursor;
size_t button;
size_t key;
size_t enter;
bj_info(
"Cursor event, window %p, (%d,%d)",
(
void*)p_window, e->
x, e->
y
);
}
bj_info(
"Button event, window %p, button %d, %s, (%d,%d)",
);
}
(void)p_window;
const char* action_str = "pressed";
}
bj_info(
"Key 0x%04X (%s) Scancode 0x%04X (with no mods) was %s",
);
}
}
bj_info(
"Enter event, window %p, %s, (%d,%d)",
(void*)p_window,
e->
enter ?
"entered" :
"left",
);
}
int bj_app_begin(
void** user_data,
int argc,
char* argv[]) {
(void)argc; (void)argv;
return bj_callback_exit_error;
}
*user_data = counter;
return bj_callback_continue;
}
(void)user_data;
? bj_callback_exit_success
: bj_callback_continue;
}
bj_info(
"Total events: %ld cursor, %ld button, %ld key, %ld enter",
);
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_audio_play_note_data data
Definition audio_pcm.c:22
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)
Definition event_callbacks.c:39
void cursor_callback(bj_window *p_window, const bj_cursor_event *e, void *data)
Definition event_callbacks.c:31
void enter_callback(bj_window *p_window, const bj_enter_event *e, void *data)
Definition event_callbacks.c:68
void key_callback(bj_window *p_window, const bj_key_event *e, void *data)
Definition event_callbacks.c:49
size_t button
Definition event_callbacks.c:22
size_t enter
Definition event_callbacks.c:24
size_t cursor
Definition event_callbacks.c:21
size_t key
Definition event_callbacks.c:23
Definition event_callbacks.c:20
struct bj_window bj_window
Definition api.h:296
int y
Cursor y position.
Definition event.h:320
int y
Cursor y position.
Definition event.h:311
int x
Cursor x position.
Definition event.h:327
int y
Cursor y position.
Definition event.h:328
int scancode
Scancode (layout-independent)
Definition event.h:338
int button
Button identifier (e.g., BJ_BUTTON_LEFT)
Definition event.h:330
bj_bool enter
BJ_TRUE if entering window, BJ_FALSE if leaving.
Definition event.h:312
enum bj_event_action action
Action (press/release/repeat)
Definition event.h:339
enum bj_event_action action
Action (press/release)
Definition event.h:329
int x
Cursor x position.
Definition event.h:310
enum bj_key key
Key identifier.
Definition event.h:337
int x
Cursor x position.
Definition event.h:319
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:83
@ BJ_PRESS
The key or button was pressed.
Definition event.h:299
@ BJ_RELEASE
The key or button was released.
Definition event.h:298
Represent a mouse cursor movement event.
Definition event.h:318
Represent a mouse enter or leave event.
Definition event.h:309
Represent a keyboard key event.
Definition event.h:336
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:105
void * bj_calloc(size_t size)
Allocate size bytes of zero-initialized memory.
void bj_free(void *memory)
Free a previously allocated memory block.
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
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
void bj_set_window_should_close(struct bj_window *window)
Flag a given window to be closed.
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 time manipulation utilities.
Header file for bj_window type.