Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
bitmap_blit_colorkey.c
Go to the documentation of this file.
1
12
13#include <banjo/app.h>
14#include <banjo/main.h>
15#include <banjo/bitmap.h>
16#include <banjo/draw.h>
17#include <banjo/event.h>
18#include <banjo/log.h>
19#include <banjo/memory.h>
20#include <banjo/system.h>
21#include <banjo/window.h>
22
23#define WINDOW_W 800
24#define WINDOW_H 600
25
28
29// Static scene: just copy the pre-composed image to the framebuffer
30// whenever banjo asks for a repaint.
31static void on_draw(
32 struct bj_window* w,
33 struct bj_render_target* target,
34 const struct bj_rect* dirty,
35 void* user_data
36) {
37 (void)w; (void)dirty; (void)user_data;
39}
40
41static void* setup(struct bj_app* app, void* init_data) {
42 (void)init_data;
43
44 // Create an off-screen rendering target.
46
47 // Draw a checkerboard pattern as the background. This makes it easy to see
48 // which pixels are transparent after we blit sprites with colour keys.
49 for (int y = 0; y < WINDOW_H; y += 40) {
50 for (int x = 0; x < WINDOW_W; x += 40) {
51 uint32_t color = ((x/40 + y/40) % 2 == 0)
52 ? bj_make_bitmap_pixel(bmp_rendering, 0xD0, 0xD0, 0xD0)
53 : bj_make_bitmap_pixel(bmp_rendering, 0xA0, 0xA0, 0xA0);
54
57 &(bj_rect){.x = x, .y = y, .w = 40, .h = 40},
58 color
59 );
60 }
61 }
62
63 // Load a sprite sheet. Sprite sheets typically have a solid background colour
64 // (black, magenta, etc.) that should be treated as transparent.
65 bj_bitmap* sprite_sheet = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/gabe-idle-run.bmp", 0);
66
67 // Define which colour should be transparent. This sprite sheet uses black
68 // (RGB: 0, 0, 0). Convert to the sprite's native pixel format.
69 uint32_t black_key = bj_make_bitmap_pixel(sprite_sheet, 0x00, 0x00, 0x00);
70
71 // Enable colour key transparency. Any pixel matching black_key will be
72 // skipped during blitting, making it transparent. This is how classic
73 // sprite rendering works - much simpler than alpha channels.
74 bj_set_bitmap_color(sprite_sheet, black_key, BJ_BITMAP_COLORKEY);
75
76 // Now when we blit from the sprite sheet, pixels matching the colour key
77 // won't be copied, creating transparent sprites. Each frame is 24x24 pixels.
78
79 // Blit idle animation frames (row 0 of the sprite sheet).
80 for (int i = 0; i < 4; i++) {
81 bj_blit(
82 sprite_sheet,
83 &(bj_rect){.x = i * 24, .y = 0, .w = 24, .h = 24},
85 &(bj_rect){.x = 100 + i * 60, .y = 100},
87 );
88 }
89
90 // Blit run animation frames (row 1 of the sprite sheet).
91 for (int i = 0; i < 6; i++) {
92 bj_blit(
93 sprite_sheet,
94 &(bj_rect){.x = i * 24, .y = 24, .w = 24, .h = 24},
96 &(bj_rect){.x = 50 + i * 60, .y = 200},
98 );
99 }
100
101 // Colour key transparency also works with scaled blitting. Here we draw
102 // sprites at 4x scale (24x24 → 96x96).
104 sprite_sheet,
105 &(bj_rect){.x = 0, .y = 0, .w = 24, .h = 24},
107 &(bj_rect){.x = 250, .y = 350, .w = 96, .h = 96},
109 );
110
112 sprite_sheet,
113 &(bj_rect){.x = 24, .y = 24, .w = 24, .h = 24},
115 &(bj_rect){.x = 450, .y = 350, .w = 96, .h = 96},
117 );
118
119 bj_destroy_bitmap(sprite_sheet);
120
121 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
122 bj_quit_app(app, 1);
123 return 0;
124 }
125
126 window = bj_bind_window("Color Key Transparency Demo", 0, 0, WINDOW_W, WINDOW_H, 0, 0);
127
131 return 0;
132}
133
134static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
135 (void)tick;
136 (void)user_data;
138
140 bj_quit_app(app, 0);
141 }
142}
143
144static void teardown(struct bj_app* app, void* user_data) {
145 (void)user_data;
148 bj_end();
149}
150
151int main(int argc, char* argv[]) {
152 (void)argc; (void)argv;
153 return bj_run_app(setup, step, 0, teardown, 0);
154}
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_bitmap * bmp_rendering
Definition bitmap_blit.c:25
#define WINDOW_W
Definition bitmap_blit.c:21
bj_window * window
Definition bitmap_blit.c:24
#define WINDOW_H
Definition bitmap_blit.c:22
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: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.
bj_bool bj_blit(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)
Bitmap blitting operation from a source to a destination bitmap.
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_set_bitmap_color(struct bj_bitmap *bitmap, uint32_t color, uint8_t roles)
Sets one or more colour properties of a 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
@ BJ_BITMAP_COLORKEY
Transparency key for blitting (auto-enables)
Definition bitmap.h:119
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_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_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_PIXEL_MODE_BGR24
24bpp BGR
Definition pixel.h:96
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.
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.