Banjo API 0.0.1
C99 game development API
|
Topics | |
Drawing | |
Pixel Definition | |
Shaders |
Typedefs | |
typedef struct bj_bitmap_t | bj_bitmap |
Enumerations | |
enum | bj_blit_op { BJ_BLIT_OP_COPY = 0 , BJ_BLIT_OP_XOR , BJ_BLIT_OP_OR , BJ_BLIT_OP_AND , BJ_BLIT_OP_ADD_SAT , BJ_BLIT_OP_SUB_SAT } |
enum | bj_mask_bg_mode { BJ_MASK_BG_TRANSPARENT = 0 , BJ_MASK_BG_OPAQUE , BJ_MASK_BG_REV_TRANSPARENT } |
Functions | |
bj_bitmap * | bj_allocate_bitmap (void) |
bj_bitmap * | bj_create_bitmap (size_t width, size_t height, bj_pixel_mode mode, size_t stride) |
void | bj_destroy_bitmap (bj_bitmap *p_bitmap) |
bj_bitmap * | bj_create_bitmap_from_file (const char *p_path, bj_error **p_error) |
bj_bitmap * | bj_create_bitmap_from_pixels (void *p_pixels, size_t width, size_t height, bj_pixel_mode mode, size_t stride) |
bj_bitmap * | bj_copy_bitmap (const bj_bitmap *p_bitmap) |
bj_bitmap * | bj_convert_bitmap (const bj_bitmap *p_bitmap, bj_pixel_mode mode) |
bj_bitmap * | bj_init_bitmap (bj_bitmap *p_bitmap, void *p_pixels, size_t width, size_t height, bj_pixel_mode mode, size_t stride) |
void | bj_reset_bitmap (bj_bitmap *p_bitmap) |
void * | bj_bitmap_pixels (bj_bitmap *p_bitmap) |
size_t | bj_bitmap_width (const bj_bitmap *p_bitmap) |
size_t | bj_bitmap_height (const bj_bitmap *p_bitmap) |
int | bj_bitmap_mode (bj_bitmap *p_bitmap) |
size_t | bj_bitmap_stride (bj_bitmap *p_bitmap) |
void | bj_make_bitmap_rgb (const bj_bitmap *p_bitmap, size_t x, size_t y, uint8_t *p_red, uint8_t *p_green, uint8_t *p_blue) |
uint32_t | bj_make_bitmap_pixel (bj_bitmap *p_bitmap, uint8_t red, uint8_t green, uint8_t blue) |
void | bj_put_pixel (bj_bitmap *p_bitmap, size_t x, size_t y, uint32_t value) |
void | bj_clear_bitmap (bj_bitmap *p_bitmap) |
void | bj_set_bitmap_colorkey (bj_bitmap *p_bitmap, bj_bool enabled, uint32_t key_value) |
uint32_t | bj_bitmap_pixel (const bj_bitmap *p_bitmap, size_t x, size_t y) |
void | bj_set_bitmap_clear_color (bj_bitmap *p_bitmap, uint32_t clear_color) |
bj_bool | bj_blit (const bj_bitmap *src, const bj_rect *src_area, bj_bitmap *dst, const bj_rect *dst_area, bj_blit_op op) |
bj_bool | bj_blit_stretched (const bj_bitmap *src, const bj_rect *src_area, bj_bitmap *dst, const bj_rect *dst_area, bj_blit_op op) |
bj_bool | bj_blit_mask (const bj_bitmap *mask, const bj_rect *mask_area, bj_bitmap *dst, const bj_rect *dst_area, uint32_t fg_native, uint32_t bg_native, bj_mask_bg_mode mode) |
bj_bool | bj_blit_mask_stretched (const bj_bitmap *mask, const bj_rect *mask_area, bj_bitmap *dst, const bj_rect *dst_area, uint32_t fg_native, uint32_t bg_native, bj_mask_bg_mode mode) |
void | bj_draw_text (bj_bitmap *dst, int x, int y, unsigned height, uint32_t fg_native, const char *text) |
void | bj_draw_textf (bj_bitmap *p_bitmap, int x, int y, unsigned height, uint32_t fg_native, const char *fmt,...) |
void | bj_blit_text (bj_bitmap *dst, int x, int y, unsigned height, uint32_t fg_native, uint32_t bg_native, bj_mask_bg_mode mode, const char *text) |
Matrix of pixels.
2D pixel buffers and blit utilities. Create, destroy, load from files, convert pixel modes, access pixels, and render via blit, masked blit, stretched blit, and text drawing. Integrates with bj_pixel_mode, bj_rect, and window framebuffers.
Typical usage:
enum bj_blit_op |
Raster operation (ROP) to apply during blitting.
These operations define how source pixels combine with destination pixels during bj_blit and bj_blit_stretched. Some operations are optimized on specific formats (e.g., 32bpp).
enum bj_mask_bg_mode |
Mask background mode for masked blits (glyph/text rendering).
bj_bitmap * bj_allocate_bitmap | ( | void | ) |
Allocate a new bitmap object.
size_t bj_bitmap_height | ( | const bj_bitmap * | p_bitmap | ) |
Get the height of the given bitmap.
p_bitmap | The bitmap object. |
int bj_bitmap_mode | ( | bj_bitmap * | p_bitmap | ) |
Get the pixel mode of the given bitmap.
p_bitmap | The bitmap object. |
uint32_t bj_bitmap_pixel | ( | const bj_bitmap * | p_bitmap, |
size_t | x, | ||
size_t | y ) |
Gets the color of a bitmap pixel, given its coordinates.
p_bitmap | The bitmap object. |
x | The X coordinate of the pixel. |
y | The Y coordinate of the pixel. |
This function does not perform any bound checking on the pixel coordinates. It is up to you to ensure the coordinates lie between [0, bj_bitmap->width * bj_bitmap->height]. Writing outside of these bounds will result in undefined behavior or corrupted memory access.
void * bj_bitmap_pixels | ( | bj_bitmap * | p_bitmap | ) |
Get the underlying pixels data for direct access.
p_bitmap | The bitmap object. |
size_t bj_bitmap_stride | ( | bj_bitmap * | p_bitmap | ) |
Get the number of bytes in a row of pixel data, including the padding.
p_bitmap | The bitmap object. |
size_t bj_bitmap_width | ( | const bj_bitmap * | p_bitmap | ) |
Get the width of the given bitmap.
p_bitmap | The bitmap object. |
bj_bool bj_blit | ( | const bj_bitmap * | src, |
const bj_rect * | src_area, | ||
bj_bitmap * | dst, | ||
const bj_rect * | dst_area, | ||
bj_blit_op | op ) |
Bitmap blitting operation from a source to a destination bitmap.
src | The source bitmap. |
src_area | Optional area to copy from in the source bitmap (0 = full source). |
dst | The destination bitmap. |
dst_area | Optional area to copy to in the destination bitmap (0 = same size at {0,0}). |
op | The raster operation to apply (see bj_blit_op). |
If src_area is 0, the entire source is copied. If dst_area is 0, the destination area defaults to {.x=0,.y=0,.w=src_area.w,.h=src_area.h}.
The blit is automatically clipped to the destination bounds. If clipping occurs, the source area is adjusted accordingly to preserve pixel mapping.
If the source bitmap has color keying enabled via bj_set_bitmap_colorkey, any source pixel equal to the key value is skipped.
Sub-byte formats (1/4/8bpp) are supported but may be slower due to bit packing.
bj_bool bj_blit_mask | ( | const bj_bitmap * | mask, |
const bj_rect * | mask_area, | ||
bj_bitmap * | dst, | ||
const bj_rect * | dst_area, | ||
uint32_t | fg_native, | ||
uint32_t | bg_native, | ||
bj_mask_bg_mode | mode ) |
Masked blit (non-stretched).
The mask must be 8bpp (coverage 0..255).
mask | The 8bpp mask bitmap (0 = fully transparent, 255 = fully opaque). |
mask_area | Optional area in the mask to use (0 = full mask). |
dst | The destination bitmap. |
dst_area | Optional destination area (0 = place at {0,0} with mask_area size). |
fg_native | Foreground color packed in the destination's native format. |
bg_native | Background color packed in the destination's native format. |
mode | The background mode (see bj_mask_bg_mode). |
The mask must be 8 bits per pixel. Destination can be any supported pixel mode; colors must be provided in destination-native format (see bj_make_bitmap_pixel).
The destination area is clipped to the destination bounds. The mask area is clipped to the mask bounds. Both rectangles must have identical sizes.
bj_bool bj_blit_mask_stretched | ( | const bj_bitmap * | mask, |
const bj_rect * | mask_area, | ||
bj_bitmap * | dst, | ||
const bj_rect * | dst_area, | ||
uint32_t | fg_native, | ||
uint32_t | bg_native, | ||
bj_mask_bg_mode | mode ) |
Masked blit with stretching (nearest neighbor).
The mask must be 8bpp.
mask | The 8bpp mask bitmap (0..255 coverage). |
mask_area | Optional area in the mask (0 = full mask). |
dst | The destination bitmap. |
dst_area | Optional destination area (0 = full destination). |
fg_native | Foreground color packed for destination. |
bg_native | Background color packed for destination. |
mode | The background mode (see bj_mask_bg_mode). |
The destination area is clipped to the destination bounds. The source mask area is proportionally adjusted so that the visible sub-rectangle of the stretched glyph corresponds to the same sub-rectangle of the source. This avoids visual “wrap-around” artifacts when partially off-screen.
Mask values are interpreted as linear coverage (0..255). Alpha blending uses integer arithmetic: mix(dst, src, a) → (dst*(255-a) + src*a)/255.
bj_bool bj_blit_stretched | ( | const bj_bitmap * | src, |
const bj_rect * | src_area, | ||
bj_bitmap * | dst, | ||
const bj_rect * | dst_area, | ||
bj_blit_op | op ) |
Stretched bitmap blitting (nearest neighbor).
src | The source bitmap. |
src_area | Optional area to copy from in the source bitmap (0 = full source). |
dst | The destination bitmap. |
dst_area | Optional area to copy to in the destination bitmap (0 = full destination). |
op | The raster operation to apply (see bj_blit_op). |
If source and destination rectangles have the same size, this function delegates to bj_blit and uses the same fast paths.
The destination rectangle is clipped to the destination bounds, and the source rectangle is proportionally adjusted to ensure visual consistency.
Color keying on the source bitmap is honored (see bj_set_bitmap_colorkey).
void bj_blit_text | ( | bj_bitmap * | dst, |
int | x, | ||
int | y, | ||
unsigned | height, | ||
uint32_t | fg_native, | ||
uint32_t | bg_native, | ||
bj_mask_bg_mode | mode, | ||
const char * | text ) |
Prints text with explicit foreground/background and background mode.
dst | The destination bitmap. |
x | X coordinate of the baseline origin. |
y | Y coordinate of the baseline origin. |
height | Target font height in pixels. |
fg_native | Foreground color in destination-native format. |
bg_native | Background color in destination-native format. |
mode | Background mode (see bj_mask_bg_mode). |
text | UTF-8/ASCII string with optional ANSI SGR sequences. |
The string may embed standard SGR sequences following ‘ESC ’['`:
Unsupported or malformed sequences are ignored gracefully.
Each glyph is pre-clipped to the destination bounds. The source mask sub-rectangle is adjusted proportionally so edge glyphs render correctly without wrap-around artifacts.
The glyph mask atlas is cached per destination bitmap. Rendering uses nearest-neighbor scaling and an inner loop with integer blending.
void bj_clear_bitmap | ( | bj_bitmap * | p_bitmap | ) |
Fills the entire bitmap with the clear color.
p_bitmap | The bitmap object to reset. |
The clear color can be set with bj_set_bitmap_clear_color. This function effectively fills all the pixels of the bitmap with the clear color.
bj_bitmap * bj_convert_bitmap | ( | const bj_bitmap * | p_bitmap, |
bj_pixel_mode | mode ) |
Creates a new bj_bitmap by converting p_bitmap.
p_bitmap | The source bitmap. |
mode | The new pixel mode. |
The new bitmap is provided by creating a new empty bitmap matching source's width and height, but using mode as pixel mode. The pixels are then converted to fill-in the new buffer.
Returns bj_copy_bitmap(p_bitmap) if mode == bj_bitmap_mode(p_bitmap).
Returns 0 if mode is BJ_PIXEL_MODE_UNKNOWN or an unsupported value.
The caller is responsible from releasing the bitmap using bj_destroy_bitmap.
If p_bitmap was initially created using bj_create_bitmap_from_pixels, the "weak" property of the bitmap is not maintained in the new bitmap and the caller is not responsible for manually releasing its pixel data.
Creates a new bj_bitmap by copying p_bitmap.
p_bitmap | The source bitmap. |
The new bitmap will have exactly the same properties than the source bitmap.
The caller is responsible from releasing the bitmap using bj_destroy_bitmap.
If p_source was initially created using bj_create_bitmap_from_pixels, the "weak" property of the bitmap is not maintained in the new bitmap and the caller is not responsible for manually releasing its pixel data.
bj_bitmap * bj_create_bitmap | ( | size_t | width, |
size_t | height, | ||
bj_pixel_mode | mode, | ||
size_t | stride ) |
Creates a new bj_bitmap with the specified width and height.
width | Width of the bitmap. |
height | Height of the bitmap. |
mode | The pixel mode. |
stride | The suggested bitmap stride. |
The stride corresponds to the size in bytes of a row. If the value is less than the required stride, the actual minimum stride is used. Set it to 0 to automatically compute the stride.
Creates a new bitmap by loading from a file.
p_path | Path to the bitmap file. |
p_error | Pointer to an error object to store any errors encountered during loading. |
The new object must be deleted using bj_destroy_bitmap.
Banjo supports the reading or 1, 4, 8, 24 and 32 bits per pixels images. Reading RLE encoding for 4 and 8 bpp is also supported.
According to the file compression (MSDN) and the pixel byte size, the created Bitmap will have one of the following pixel mode:
Bits Per Pixel | Compression | bj_pixel_mode |
---|---|---|
1 | BI_RGB | BJ_PIXEL_MODE_INDEXED_1 |
4 | BI_RGB | BJ_PIXEL_MODE_INDEXED_4 |
4 | BJ_RLE4 | BJ_PIXEL_MODE_INDEXED_4 |
8 | BI_RGB | BJ_PIXEL_MODE_INDEXED_8 |
8 | BI_RLE8 | BJ_PIXEL_MODE_INDEXED_8 |
16 | BI_RGB | BJ_PIXEL_MODE_XRGB1555 |
16 | BI_BITFIELDS | Depends on bit fields |
24 | BI_RGB | BJ_PIXEL_MODE_BGR24 |
32 | BI_RGB | BJ_PIXEL_MODE_XRGB8888 |
32 | BI_BITFIELDS | Depends on bit fields |
Banjo does not support all bitfield configuration. The following table shows our supported bit fields:
Bits Per Pixel | Red Mask | Green Mask | Blue Mask | bj_pixel_mode |
---|---|---|---|---|
16 | 0x0000F800 | 0x000007E0 | 0x0000001F | BJ_PIXEL_MODE_RGB565 |
32 | 0x00FF0000 | 0x0000FF00 | 0x000000FF | BJ_PIXEL_MODE_XRGB8888 |
Any other configuration leads to either BJ_PIXEL_MODE_UNKNOWN or a failure in loading the file.
Once loaded, the pixel mode can be retrieved using bj_bitmap_mode.
While reading indexed Bitmap works, 1, 4 and 8bpp images are automatically converted to 24bpp images for now.
bj_bitmap * bj_create_bitmap_from_pixels | ( | void * | p_pixels, |
size_t | width, | ||
size_t | height, | ||
bj_pixel_mode | mode, | ||
size_t | stride ) |
Creates a new bj_bitmap with the specified width and height.
Contrary to bj_create_bitmap, the pixel data is explicitely provided by the caller through p_pixels. The caller is reponsible for ensuring the allocated pixel data matches width, height, mode and stride.
You can use bj_compute_bitmap_stride with width and mode to retrieve the most suitable value for stride. It's also possible to set stride to 0 and let Banjo compute it automatically.
p_pixels | A pre-allocated array of pixels |
width | Width of the bitmap. |
height | Height of the bitmap. |
mode | The pixel mode. |
stride | The suggested bitmap stride. |
Returns 0 if p_pixels is 0.
The caller is responsible for the memory management of p_pixels. bj_bitmap will not modify it and it will not be released upon calling bj_destroy_bitmap.
The caller is responsible for releasing the bitmap using bj_destroy_bitmap.
void bj_destroy_bitmap | ( | bj_bitmap * | p_bitmap | ) |
void bj_draw_text | ( | bj_bitmap * | dst, |
int | x, | ||
int | y, | ||
unsigned | height, | ||
uint32_t | fg_native, | ||
const char * | text ) |
Prints text using the default foreground color and transparent background.
dst | The destination bitmap. |
x | X coordinate of the baseline origin. |
y | Y coordinate of the baseline origin. |
height | Target font height in pixels (glyphs are scaled from the internal font cell size to this height). |
fg_native | Foreground color in destination-native format. |
text | UTF-8/ASCII string with optional ANSI SGR sequences (see below). |
The text supports a subset of ANSI SGR sequences introduced by ESC (\x1B):
The function uses the internal monochrome font mask and performs a masked blit in transparent background mode (foreground over destination).
Text is clipped to the destination bounds. Out-of-range glyphs are skipped.
void bj_draw_textf | ( | bj_bitmap * | p_bitmap, |
int | x, | ||
int | y, | ||
unsigned | height, | ||
uint32_t | fg_native, | ||
const char * | fmt, | ||
... ) |
Prints formatted text into a bitmap, similar to printf.
This function formats the full string using vsnprintf into a temporary buffer (heap-allocated), then renders it with bj_draw_text.
p_bitmap | The destination bitmap. |
x | The X coordinate (top-left) where the text begins. |
y | The Y coordinate (top-left) where the text begins. |
height | The pixel height of the rendered font. |
fg_native | Foreground color in destination-native format. |
fmt | The printf-style format string. |
... | Additional arguments corresponding to the format string. |
bj_bitmap * bj_init_bitmap | ( | bj_bitmap * | p_bitmap, |
void * | p_pixels, | ||
size_t | width, | ||
size_t | height, | ||
bj_pixel_mode | mode, | ||
size_t | stride ) |
Initializes a new bj_bitmap with the specified width and height.
p_bitmap | The bitmap object. |
p_pixels | The pixel buffer data. |
width | Width of the bitmap. |
height | Height of the bitmap. |
mode | The pixel mode. |
stride | The suggested bitmap stride. |
If the pixel buffer provided by p_pixels is 0, the buffer will allocate
The stride corresponds to the size in bytes of a row. If the value is less than the required stride, the actual minimum stride is used.
uint32_t bj_make_bitmap_pixel | ( | bj_bitmap * | p_bitmap, |
uint8_t | red, | ||
uint8_t | green, | ||
uint8_t | blue ) |
Returns an opaque value representing a pixel color, given its RGB composition.
p_bitmap | The bitmap object. |
red | The red component of the color |
green | The green component of the color |
blue | The blue component of the color |
void bj_make_bitmap_rgb | ( | const bj_bitmap * | p_bitmap, |
size_t | x, | ||
size_t | y, | ||
uint8_t * | p_red, | ||
uint8_t * | p_green, | ||
uint8_t * | p_blue ) |
Gets the RGB value of a pixel given its 32-bits representation.
p_bitmap | The bitmap object |
x | The X coordinate of the pixel. |
y | The Y coordinate of the pixel. |
p_red | A location to the red component |
p_green | A location to the green component |
p_blue | A location to the blue component |
void bj_put_pixel | ( | bj_bitmap * | p_bitmap, |
size_t | x, | ||
size_t | y, | ||
uint32_t | value ) |
Change the pixel color at given coordinate.
p_bitmap | The bitmap object. |
x | The X coordinate of the pixel. |
y | The Y coordinate of the pixel. |
value | The pixel value to put. |
void bj_reset_bitmap | ( | bj_bitmap * | p_bitmap | ) |
void bj_set_bitmap_clear_color | ( | bj_bitmap * | p_bitmap, |
uint32_t | clear_color ) |
Sets the color used for clearing the bitmap.
p_bitmap | The target bitmap. |
clear_color | The new clear color. |
Enables or disables color key transparency for blitting.
p_bitmap | The target bitmap. |
enabled | Whether the color key should be enabled. |
key_value | The pixel value (in bitmap's native format) considered transparent. |
When color keying is enabled on the source bitmap, blitters skip any source pixel equal to key_value.