Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
bitmap_blit_colorkey.c

Demonstration of color key transparency for sprite blitting.

Demonstration of color key transparency for sprite blitting.This example shows how to use bj_set_bitmap_color to make specific pixel values transparent during blitting. This is commonly used for sprite rendering where a background color (like black or magenta) should be treated as transparent. The example uses the Gabe sprite sheet with black (RGB: 0, 0, 0) as the color key.

#define BJ_AUTOMAIN_CALLBACKS
#include <banjo/bitmap.h>
#include <banjo/draw.h>
#include <banjo/event.h>
#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/memory.h>
#include <banjo/renderer.h>
#include <banjo/system.h>
#include <banjo/time.h>
#include <banjo/window.h>
#define WINDOW_W 800
#define WINDOW_H 600
int bj_app_begin(void** user_data, int argc, char* argv[]) {
(void)user_data; (void)argc; (void)argv;
// Create an off-screen rendering target.
// Draw a checkerboard pattern as the background. This makes it easy to see
// which pixels are transparent after we blit sprites with color keys.
for (int y = 0; y < WINDOW_H; y += 40) {
for (int x = 0; x < WINDOW_W; x += 40) {
uint32_t color = ((x/40 + y/40) % 2 == 0)
? bj_make_bitmap_pixel(bmp_rendering, 0xD0, 0xD0, 0xD0)
: bj_make_bitmap_pixel(bmp_rendering, 0xA0, 0xA0, 0xA0);
&(bj_rect){.x = x, .y = y, .w = 40, .h = 40},
color
);
}
}
// Load a sprite sheet. Sprite sheets typically have a solid background color
// (black, magenta, etc.) that should be treated as transparent.
bj_bitmap* sprite_sheet = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/gabe-idle-run.bmp", 0);
// Define which color should be transparent. This sprite sheet uses black
// (RGB: 0, 0, 0). Convert to the sprite's native pixel format.
uint32_t black_key = bj_make_bitmap_pixel(sprite_sheet, 0x00, 0x00, 0x00);
// Enable color key transparency. Any pixel matching black_key will be
// skipped during blitting, making it transparent. This is how classic
// sprite rendering works - much simpler than alpha channels.
bj_set_bitmap_color(sprite_sheet, black_key, BJ_BITMAP_COLORKEY);
// Now when we blit from the sprite sheet, pixels matching the color key
// won't be copied, creating transparent sprites. Each frame is 24x24 pixels.
// Blit idle animation frames (row 0 of the sprite sheet).
for (int i = 0; i < 4; i++) {
sprite_sheet,
&(bj_rect){.x = i * 24, .y = 0, .w = 24, .h = 24},
&(bj_rect){.x = 100 + i * 60, .y = 100},
);
}
// Blit run animation frames (row 1 of the sprite sheet).
for (int i = 0; i < 6; i++) {
sprite_sheet,
&(bj_rect){.x = i * 24, .y = 24, .w = 24, .h = 24},
&(bj_rect){.x = 50 + i * 60, .y = 200},
);
}
// Color key transparency also works with scaled blitting. Here we draw
// sprites at 4x scale (24x24 → 96x96).
sprite_sheet,
&(bj_rect){.x = 0, .y = 0, .w = 24, .h = 24},
&(bj_rect){.x = 250, .y = 350, .w = 96, .h = 96},
);
sprite_sheet,
&(bj_rect){.x = 24, .y = 24, .w = 24, .h = 24},
&(bj_rect){.x = 450, .y = 350, .w = 96, .h = 96},
);
bj_destroy_bitmap(sprite_sheet);
return bj_callback_exit_error;
}
window = bj_bind_window("Color Key Transparency Demo", 0, 0, WINDOW_W, WINDOW_H, 0, 0);
return bj_callback_continue;
}
int bj_app_iterate(void* user_data) {
(void)user_data;
bj_sleep(30);
? bj_callback_exit_success
: bj_callback_continue;
}
int bj_app_end(void* user_data, int status) {
(void)user_data;
bj_end();
return status;
}
int bj_app_begin(void **user_data, int argc, char *argv[])
Definition audio_pcm.c:25
int bj_app_iterate(void *user_data)
Definition audio_pcm.c:60
int bj_app_end(void *user_data, int status)
Definition audio_pcm.c:84
Header file for Bitmap type.
bj_renderer * renderer
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.
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 color, 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 color 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:49
@ BJ_BITMAP_COLORKEY
Transparency key for blitting (auto-enables)
Definition bitmap.h:75
struct bj_bitmap bj_bitmap
Definition api.h:270
struct bj_renderer bj_renderer
Definition api.h:288
struct bj_window bj_window
Definition api.h:296
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.
Represents a rectangle with position and dimensions.
Definition rect.h:19
@ BJ_PIXEL_MODE_BGR24
24bpp BGR
Definition pixel.h:26
void bj_present(struct bj_renderer *renderer, struct bj_window *window)
Present the framebuffer to a window.
struct bj_renderer * bj_create_renderer(enum bj_renderer_type type, struct bj_error **error)
Create a new renderer instance.
struct bj_bitmap * bj_get_framebuffer(struct bj_renderer *renderer)
Get the renderer's framebuffer.
bj_bool bj_renderer_configure(struct bj_renderer *renderer, struct bj_window *window, struct bj_error **error)
Configure a renderer for a specific window.
void bj_destroy_renderer(struct bj_renderer *renderer)
Destroy a renderer and free associated resources.
@ BJ_RENDERER_TYPE_SOFTWARE
Software (CPU-based) renderer.
Definition renderer.h:35
bj_bool bj_begin(int systems, struct bj_error **error)
Initializes the system.
void bj_end(void)
De-initializes the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:20
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
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 substitution and application callback facilities.
All memory-related functions, including custom allocators.
Rendering backend interface.
bj_bitmap * bmp_rendering
Definition sprite_animation.c:36
Header file for system interactions.
Header file for time manipulation utilities.
Header file for bj_window type.