Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
event_polling.c
Go to the documentation of this file.
1
17
18#include <banjo/app.h>
19#include <banjo/main.h>
20#include <banjo/error.h>
21#include <banjo/event.h>
22#include <banjo/log.h>
23#include <banjo/memory.h>
24#include <banjo/system.h>
25#include <banjo/window.h>
26
28
29typedef struct {
30 size_t cursor;
31 size_t button;
32 size_t key;
33 size_t enter;
34 size_t resize;
36
37// Resize is not a queued event type, so even a polling program reacts to it
38// through a callback. Registered per-window with bj_set_resize_callback, it
39// receives the new width and height directly.
40static void resize_callback(bj_window* p_window, int width, int height, void* data) {
41 event_counter* counter = (event_counter*)data;
42 ++counter->resize;
43 bj_info("Resize event, window %p, %dx%d", (void*)p_window, width, height);
44}
45
46static void* setup(struct bj_app* app, void* init_data) {
47 (void)init_data;
48 event_counter* counter = bj_calloc(sizeof(event_counter));
49
50 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
51 bj_free(counter);
52 bj_quit_app(app, 1);
53 return 0;
54 }
55
56 window = bj_bind_window("Event Polling", 100, 100, 800, 600, BJ_WINDOW_FLAG_RESIZABLE, 0);
58 return counter;
59}
60
61static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
62 (void)tick;
63 event_counter* counter = (event_counter*)user_data;
64 bj_event e;
65
66 // Poll events from the queue. bj_poll_events() fills the event structure
67 // and returns true if an event was available, false when the queue is empty.
68 // Process all queued events in a loop before continuing with your frame logic.
69 while (bj_poll_events(&e)) {
70 // Each event has a type field that determines which union member is valid.
71 switch (e.type) {
72 case BJ_EVENT_ENTER:
73 // Mouse cursor entering or leaving the window.
74 ++counter->enter;
75 bj_info("Enter event, window %p, %s, (%d,%d)",
76 window,
77 e.as.enter.enter ? "entered" : "left",
78 e.as.enter.x, e.as.enter.y
79 );
80 break;
81
82 case BJ_EVENT_CURSOR:
83 // Mouse movement within the window.
84 ++counter->cursor;
85 bj_info("Cursor event, window %p, (%d,%d)",
86 window, e.as.cursor.x, e.as.cursor.y
87 );
88 break;
89
90 case BJ_EVENT_KEY:
91 // Keyboard input. Action can be PRESS, RELEASE, or REPEAT.
92 ++counter->key;
93
94 const char* action_str = "pressed";
95 if (e.as.key.action != BJ_PRESS) {
96 action_str = e.as.key.action == BJ_RELEASE ? "released" : "repeated";
97 }
98
99 bj_info("Key 0x%04X (%s) Scancode 0x%04X (with no mods) was %s",
100 e.as.key.key, bj_key_name(e.as.key.key), e.as.key.scancode, action_str
101 );
102
103 // With polling, you manually check for keys like ESC and handle
104 // them explicitly, unlike with callbacks where you register handlers.
105 if (e.as.key.key == BJ_KEY_ESCAPE) {
107 }
108 break;
109
110 case BJ_EVENT_BUTTON:
111 // Mouse button clicks. Button index and position are provided.
112 ++counter->button;
113 bj_info("Button event, window %p, button %d, %s, (%d,%d)",
114 (void*)window, e.as.button.button,
115 e.as.button.action == BJ_PRESS ? "pressed" : "released",
116 e.as.button.x, e.as.button.y
117 );
118 break;
119 }
120 }
121
123 bj_quit_app(app, 0);
124 }
125}
126
127static void teardown(struct bj_app* app, void* user_data) {
128 (void)app;
129 event_counter* counter = (event_counter*)user_data;
130 bj_info("Total events: %ld cursor, %ld button, %ld key, %ld enter, %ld resize",
131 counter->cursor, counter->button, counter->key, counter->enter, counter->resize
132 );
133
135 bj_end();
136 bj_free(counter);
137}
138
139int main(int argc, char* argv[]) {
140 (void)argc; (void)argv;
141 return bj_run_app(setup, step, 0, teardown, 0);
142}
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
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 resize_callback(bj_window *p_window, int width, int height, 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:106
struct bj_window bj_window
Definition api.h:354
union bj_event::@102271004006361263344115053024230242311221140216 as
enum bj_event_type type
Type of event.
Definition event.h:455
const char * bj_key_name(int key)
Get the string name of a key.
bj_bool bj_poll_events(struct bj_event *event)
Poll the next pending event from the system queue.
@ BJ_KEY_ESCAPE
Esc key.
Definition event.h:176
@ BJ_EVENT_BUTTON
Mouse button.
Definition event.h:442
@ BJ_EVENT_KEY
Keyboard key.
Definition event.h:441
@ BJ_EVENT_ENTER
Mouse enter/leave.
Definition event.h:439
@ BJ_EVENT_CURSOR
Mouse move.
Definition event.h:440
@ BJ_PRESS
The key or button was pressed.
Definition event.h:392
@ BJ_RELEASE
The key or button was released.
Definition event.h:391
Represent a generic window-related 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
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.
void bj_set_resize_callback(struct bj_window *window, bj_window_resize_fn fn, void *user_data)
Register the resize callback for window.
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.
@ BJ_WINDOW_FLAG_RESIZABLE
User may resize the window.
Definition window.h:190
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
All memory-related functions, including custom allocators.
Header file for system interactions.
Header file for bj_window type.