Tutorial: Drawing 2D primitives onto the framebuffer.
Banjo can run with framebuffers in different pixel modes (XRGB8888, RGB565, indexed palettes…). A literal 0x00FF0000 is red in XRGB8888 but means something else in any other mode. The bj_make_bitmap_pixel call asks the bitmap to pack the value itself, so the same drawing code works regardless of mode. The Pixel Definition topic explains the encoding in more detail.
The setup callback opens a window, registers a draw callback, and installs the ESC-to-quit shortcut. Same shape as start.c; the interesting part is what comes next.
The draw callback is fired by the backend whenever the window has pending damage. Since nothing in this scene animates, setup renders the scene a single time into a fixed-size off-screen bitmap (the canvas), and the draw callback only scales that canvas onto the framebuffer. The callback then runs on each uncover / resize / system damage event, rescaling the same canvas to the window's current size. A real game would invalidate on every step iteration; we don't.
The backend takes care of pushing the result to the window after the callback returns. To request the next paint (e.g. on animation), call bj_invalidate_window from your step.
void* user_data
);
for (size_t x = 10; x < 490; ++x) {
if (x % 7 == 0) {
}
}
int poly_x[] = { 100, 95, 95, 100, 100, 95, 75, 75, 95, 120, 140, 140, 120, 115, 115, 120, 120, 115, };
int poly_y[] = { 20, 25, 50, 55, 100, 100, 120, 145, 165, 165, 145, 120, 100, 100, 55, 50, 25, 20, };
int verts[][2] = {
{330, 270}, {270, 210}, {210, 270}, {210, 150}, {390, 210}, {450, 270},
{450, 150}, {180, 330}, {270, 390}, {390, 390}, {480, 330}, {330, 450},
{300, 480}, {360, 480},
};
size_t tris[13][3] = {
{0, 1, 2}, {0, 2, 3}, {0, 4, 5}, {0, 1, 4}, {4, 6, 5}, {2, 8, 7},
{0, 8, 2}, {0, 5, 9}, {9, 5, 10}, {8, 9, 11}, {8, 11, 12},
{9, 13, 11}, {11, 12, 13},
};
for (size_t t = 0; t < 13; ++t) {
verts[tris[t][0]][0], verts[tris[t][0]][1],
verts[tris[t][1]][0], verts[tris[t][1]][1],
verts[tris[t][2]][0], verts[tris[t][2]][1],
color_white
);
}
bj_rect board = {.w = 10, .h = 10,};
for(size_t y = 0 ; y < 8 ; ++y) {
for(size_t x = 0 ; x < 8 ; ++x) {
board.
x = 200 + x * board.
w;
board.
y = 50 + y * board.
h;
if((x ^ y) & 1) {
}
}
}
&(
bj_rect) {.x = 200, .y = 50, .w = 80, .h = 80,},
color_cyan
);
for (int r = 80; r > 0; r -= 20) {
}
}
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
}
static void*
setup(
struct bj_app* app,
void* init_data) {
(void)init_data;
return 0;
}
return 0;
}
return 0;
}
static void step(
struct bj_app* app,
struct bj_tick_info tick,
void* user_data) {
(void)tick;
(void)user_data;
}
}
static void teardown(
struct bj_app* app,
void* user_data) {
(void)user_data;
}
int main(
int argc,
char* argv[]) {
(void)argc; (void)argv;
}
Application lifecycle: callback-driven setup, step, and teardown.
int main(int argc, char *argv[])
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
static void teardown(struct bj_app *app, void *user_data)
static void * setup(struct bj_app *app, void *init_data)
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)
Header file for Bitmap drawing functions.
static bj_bitmap * canvas
void draw(bj_bitmap *bmp)
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.
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
bj_bool bj_blit_stretched(const struct bj_bitmap *src, const struct bj_rect *src_area, struct bj_bitmap *dst, const struct bj_rect *dst_area, enum bj_blit_op op)
Stretched bitmap blitting (nearest neighbor).
uint32_t bj_make_bitmap_pixel(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel colour, given its RGB composition.
struct bj_bitmap * bj_create_bitmap(size_t width, size_t height, enum bj_pixel_mode mode, size_t stride)
Creates a new struct bj_bitmap with the specified width and height.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel colour at given coordinate.
void bj_destroy_bitmap(struct bj_bitmap *bitmap)
Deletes a struct bj_bitmap object and releases associated memory.
@ BJ_BLIT_OP_COPY
Copy source to destination (fast path when formats match)
struct bj_render_target bj_render_target
struct bj_bitmap bj_bitmap
struct bj_window bj_window
#define BJ_TRUE
Boolean true value (1).
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_polyline(struct bj_bitmap *bitmap, size_t count, const int *x, const int *y, bj_bool loop, uint32_t color)
Draw a polyline from C-style coordinate arrays.
void bj_draw_triangle(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
Draws the edges of a triangle given its 3 corners.
void bj_draw_filled_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a filled 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_draw_filled_triangle(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
Draws a filled triangle given its 3 corners.
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.
uint16_t w
Width in pixels.
uint16_t h
Height in pixels.
int16_t y
Y coordinate of the top-left corner (pixels, can be negative).
int16_t x
X coordinate of the top-left corner (pixels, can be negative).
Axis-aligned rectangle: a top-left corner plus a width and height.
@ BJ_PIXEL_MODE_XRGB8888
32bpp RGB
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
void bj_set_draw_callback(struct bj_window *window, bj_window_draw_fn fn, void *user_data)
Register the redraw callback for window.
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
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.
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.
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.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for general pixel manipulation facilities.
Header file for system interactions.
Header file for bj_window type.