Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
events.c

Basic event handling.

Basic event handling.

#define BJ_AUTOMAIN_CALLBACKS
#include <banjo/error.h>
#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/system.h>
#include <banjo/time.h>
#include <banjo/window.h>
bj_window* window = 0;
void cursor_event(bj_window* p_window, int x, int y) {
bj_info("Cursor event, window %p, (%d,%d)",
(void*)p_window, x, y
);
}
void button_event(bj_window* p_window, int button, bj_event_action action, int x, int y) {
bj_info("Button event, window %p, button %d, %s, (%d,%d)",
(void*)p_window, button,
action == BJ_PRESS ? "pressed" : "released",
x, y
);
}
void key_event(bj_window* p_window, bj_event_action action, bj_key key, int scancode) {
(void)p_window;
const char* action_str = "pressed";
if(action != BJ_PRESS) {
action_str = action == BJ_RELEASE ? "released" : "repeated";
}
bj_info("Key 0x%04X (%s) Scancode 0x%04X (with no mods) was %s",
key, bj_get_key_name(key), scancode, action_str
);
if(key == BJ_KEY_ESCAPE) {
}
}
void enter_event(bj_window* p_window, bj_bool enter, int x, int y) {
bj_info("Enter event, window %p, %s, (%d,%d)",
(void*)p_window,
enter ? "entered" : "left",
x, y
);
}
int bj_app_begin(void** user_data, int argc, char* argv[]) {
(void)user_data; (void)argc; (void)argv;
bj_error* p_error = 0;
if(!bj_begin(&p_error)) {
bj_err("Error 0x%08X: %s", p_error->code, p_error->message);
return bj_callback_exit_error;
}
window = bj_window_new("Simple Banjo Window", 100, 100, 800, 600, 0);
bj_window_set_key_event(window, key_event);
bj_window_set_button_event(window, button_event);
bj_window_set_cursor_event(window, cursor_event);
bj_window_set_enter_event(window, enter_event);
return bj_callback_continue;
}
int bj_app_iterate(void* user_data) {
(void)user_data;
bj_sleep(30);
return bj_window_should_close(window)
? bj_callback_exit_success
: bj_callback_continue;
}
int bj_app_end(void* user_data, int status) {
(void)user_data;
bj_window_del(window);
bj_end(0);
return status;
}
Recoverable error handling.
uint32_t code
Error code.
Definition error.h:132
char message[BJ_ERROR_MESSAGE_MAX_LEN+1]
Optional error description.
Definition error.h:133
Error structure.
Definition error.h:131
enum bj_event_action_t bj_event_action
Event action type enumeration.
enum bj_key_t bj_key
List of possible keys on a keyboard.
const char * bj_get_key_name(int key)
Get a C-String representation of a key.
@ BJ_PRESS
The button/key is pressed.
Definition input.h:20
@ BJ_RELEASE
The button/key is released.
Definition input.h:19
@ BJ_KEY_ESCAPE
Esc key.
Definition input.h:86
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:103
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:131
void bj_end(bj_error **p_error)
De-initializes the system.
bj_bool bj_begin(bj_error **p_error)
Initializes the system.
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
bj_window_button_event_t bj_window_set_button_event(bj_window *p_window, bj_window_button_event_t p_callback)
Set the callback for button events.
bj_window * bj_window_new(const char *p_title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags)
Create a new bj_window with the specified attributes.
bj_bool bj_window_should_close(bj_window *p_window)
Get the close flag state of a window.
struct bj_window_t bj_window
Opaque typedef for the window type.
Definition window.h:18
bj_window_cursor_event_t bj_window_set_cursor_event(bj_window *p_window, bj_window_cursor_event_t p_callback)
Set the callback for cursor events.
void bj_poll_events(void)
Polls all pending events and dispatch them to callbacks.
void bj_window_set_should_close(bj_window *p_window)
Flag a given window to be closed.
void bj_window_del(bj_window *p_window)
Deletes a bj_window object and releases associated memory.
bj_window_enter_event_t bj_window_set_enter_event(bj_window *p_window, bj_window_enter_event_t p_callback)
Set the callback for enter events.
bj_window_key_event_t bj_window_set_key_event(bj_window *p_window, bj_window_key_event_t p_callback)
Set the callback for key events.
Logging utility functions.
Header file for system interactions.
Header file for time manipulation.
Header file for bj_window type.