Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
start.c
Go to the documentation of this file.
1
176
177#include <banjo/app.h>
178#include <banjo/main.h>
179#include <banjo/bitmap.h>
180#include <banjo/draw.h>
181#include <banjo/event.h>
182#include <banjo/system.h>
183#include <banjo/time.h>
184#include <banjo/window.h>
185
187
188static void on_draw(
189 struct bj_window* w,
190 struct bj_render_target* target,
191 const struct bj_rect* dirty,
192 void* user_data
193) {
194 (void)w; (void)dirty; (void)user_data;
195 bj_bitmap* bmp = bj_render_target_bitmap(target);
196 bj_clear_bitmap(bmp);
197
198 const uint32_t red = bj_pack_bitmap_rgb(bmp, 0xFF, 0x00, 0x00);
199 const uint32_t cyan = bj_pack_bitmap_rgb(bmp, 0x00, 0xFF, 0xFF);
200
201 bj_draw_filled_circle(bmp, 320, 240, 100, red);
202 bj_rect rect = {200, 120, 240, 240};
203 bj_draw_rectangle(bmp, &rect, cyan);
204}
205
206static void* setup(struct bj_app* app, void* init_data) {
207 (void)init_data;
208
209 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
210 bj_quit_app(app, 1);
211 return 0;
212 }
213
214 window = bj_create_window("My First Banjo App", 100, 100, 640, 480, 0, 0);
218 return 0;
219}
220
221static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
222 (void)tick;
223 (void)user_data;
224
226
228 bj_quit_app(app, 0);
229 }
230}
231
232static void teardown(struct bj_app* app, void* user_data) {
233 (void)app; (void)user_data;
234
236 bj_end();
237}
238
239int main(int argc, char* argv[]) {
240 (void)argc; (void)argv;
241 return bj_run_app(setup, step, 0, teardown, 0);
242}
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
static void * setup(struct bj_app *app, void *init_data)
Definition audio_pcm.c:107
Header file for Bitmap type.
static void on_draw(struct bj_window *w, struct bj_render_target *target, const struct bj_rect *dirty, void *user_data)
Definition bitmap_blit.c:32
bj_window * window
Definition bitmap_blit.c:24
Header file for Bitmap drawing functions.
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:107
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
uint32_t bj_pack_bitmap_rgb(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Pack an (R, G, B) triple into the bitmap's native pixel format.
struct bj_render_target bj_render_target
Definition api.h:369
struct bj_bitmap bj_bitmap
Definition api.h:340
struct bj_window bj_window
Definition api.h:384
void bj_draw_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a rectangle in the given bitmap.
void bj_draw_filled_circle(struct bj_bitmap *bitmap, int cx, int cy, int radius, uint32_t color)
Draw a filled circle onto a bitmap.
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.
Axis-aligned rectangle: a top-left corner plus a width and height.
Definition rect.h:33
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
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
Definition window.h:513
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_draw_callback(struct bj_window *window, bj_window_draw_fn fn, void *user_data)
Register the redraw callback for window.
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.
void bj_destroy_window(struct bj_window *window)
Destroy a window and release its resources.
Portable main() replacement with platform-aware entry shim.
Header file for system interactions.
int main(void)
Header file for time manipulation utilities.
Header file for bj_window type.