Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
window.c
Go to the documentation of this file.
1
10
11#include <banjo/app.h>
12#include <banjo/main.h>
13#include <banjo/event.h>
14#include <banjo/log.h>
15#include <banjo/system.h>
16#include <banjo/window.h>
17
19
20static void* setup(struct bj_app* app, void* init_data) {
21 (void)init_data;
22
23 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
24 bj_quit_app(app, 1);
25 return 0;
26 }
27
28 // Create an OS window with bj_bind_window().
29 // Parameters: title, x position, y position, width, height, flags
30 // The last parameter (flags) can specify fullscreen, borderless, etc.
31 // Returns a window handle that must be destroyed with bj_unbind_window().
32 window = bj_bind_window("Simple Banjo Window", 100, 100, 800, 600, 0, 0);
33
34 // Set up a keyboard callback. bj_close_on_escape is a built-in helper that
35 // requests window closure when ESC is pressed.
37 return 0;
38}
39
40static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
41 (void)tick;
42 (void)user_data;
43
44 // Process OS events (mouse, keyboard, window resize, close button, etc).
45 // This must be called every frame or the window will freeze and become
46 // unresponsive. Events are either handled by callbacks or can be polled.
48
49 // Check if the window should close. This is set when the user clicks the
50 // X button, or when a callback (like bj_close_on_escape) requests it.
52 bj_quit_app(app, 0);
53 }
54}
55
56static void teardown(struct bj_app* app, void* user_data) {
57 (void)user_data;
58
59 // Destroy the window and free OS resources. Always call this before bj_end().
61 bj_end();
62}
63
64int main(int argc, char* argv[]) {
65 (void)argc; (void)argv;
66 return bj_run_app(setup, step, 0, teardown, 0);
67}
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
static void * setup(struct bj_app *app, void *init_data)
Definition audio_pcm.c:107
bj_window * window
Definition bitmap_blit.c:24
Sytem event management API.
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
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)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:81
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() replacement with platform-aware entry shim.
Header file for system interactions.
Header file for bj_window type.