Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
load_bmp.c

Tutorial: Load a BMP file from disk, query its size, and blit it to the window framebuffer.

Tutorial: Load a BMP file from disk, query its size, and blit it to the window framebuffer.This tutorial introduces the bitmap loader and the blit operation: the two pieces you need to move pre-made artwork from disk into your window. Banjo includes a self-contained BMP reader, so there is no external image library to install.

What you'll learn

Prerequisites

You've worked through start.c (window + draw callback) and understand what a framebuffer is. The drawing-primitive tutorial (drawing_2d.c) is helpful but not required.

Walkthrough

1. Load before initialising video

The bitmap is loaded before bj_begin. That order is deliberate: bj_create_bitmap_from_file doesn't depend on the video subsystem (it only allocates memory and reads from disk), so you can load assets up-front and bail out if something is missing before paying for video initialisation. The BANJO_ASSETS_DIR macro expands to the path of the bundled assets directory at build time, so the example is location-independent.

2. Size the window to the asset

Once the bitmap is loaded, bj_bitmap_width and bj_bitmap_height tell you the source size. The tutorial scales up by 10× so the sprite sheet is large enough to read; for a real game you'd usually keep 1:1.

3. bj_blit_stretched does the scaling

bj_blit_stretched copies the source bitmap into the destination (here, the window's framebuffer) and stretches as needed to fit. Passing NULL for either rectangle means "use the entire bitmap". The last argument is the blit operation; BJ_BLIT_OP_COPY is a straight pixel copy with no blending. The companion bj_blit (no stretch) is faster when you don't need scaling.

4. Clean up in the right order

Window → bj_end → loaded bitmaps. Destroying the bitmap last guarantees that no Banjo subsystem still references it when bj_destroy_bitmap returns its memory.

What's next

#include <banjo/app.h>
#include <banjo/main.h>
#include <banjo/bitmap.h>
#include <banjo/event.h>
#include <banjo/log.h>
#include <banjo/memory.h>
#include <banjo/system.h>
#include <banjo/time.h>
#include <banjo/window.h>
// Static image: just stretch-blit the loaded sprite sheet to the
// framebuffer whenever banjo asks for a paint. The user's model (the
// loaded bitmap) persists across calls.
static void on_draw(
struct bj_window* w,
struct bj_render_target* target,
const struct bj_rect* dirty,
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
}
static void* setup(struct bj_app* app, void* init_data) {
(void)init_data;
// Load a BMP file from disk. BANJO_ASSETS_DIR is a macro pointing to the
// assets directory. The second parameter is an optional error pointer.
// Returns NULL on failure (file not found, invalid format, etc.).
bmp_sprite_sheet = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/gabe-idle-run.bmp", 0);
bj_quit_app(app, 1);
return 0;
}
// Create a window sized to the loaded bitmap. bj_bitmap_width() and
// bj_bitmap_height() query the dimensions. We scale by 10 for visibility
// since the sprite sheet is small.
window = bj_bind_window("sprite sheet - Banjo", 0, 0,
0, 0
);
return 0;
}
static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
(void)tick;
(void)user_data;
bj_quit_app(app, 0);
}
}
static void teardown(struct bj_app* app, void* user_data) {
(void)user_data;
bj_end();
// Always destroy loaded bitmaps to free memory. Do this after bj_end()
// to ensure the bitmap isn't referenced by any Banjo subsystems.
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
return bj_run_app(setup, step, 0, teardown, 0);
}
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
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
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
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).
struct bj_bitmap * bj_create_bitmap_from_file(const char *path, struct bj_error **error)
Creates a new bitmap by loading from a file.
size_t bj_bitmap_height(const struct bj_bitmap *bitmap)
Get the height of the given bitmap.
size_t bj_bitmap_width(const struct bj_bitmap *bitmap)
Get the width of the given bitmap.
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)
Definition bitmap.h:93
struct bj_render_target bj_render_target
Definition api.h:346
struct bj_bitmap bj_bitmap
Definition api.h:328
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.
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
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.
Definition window.h:470
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_bitmap * bmp_sprite_sheet
Definition load_bmp.c:81
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 time manipulation utilities.
Header file for bj_window type.