Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
Drawing
Collaboration diagram for Drawing:

Functions

void bj_draw_line (struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, uint32_t pixel)
void bj_draw_rectangle (struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
void bj_draw_filled_rectangle (struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
void bj_draw_triangle (struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
void bj_draw_filled_triangle (struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
void bj_draw_circle (struct bj_bitmap *bitmap, int cx, int cy, int radius, uint32_t color)
void bj_draw_filled_circle (struct bj_bitmap *bitmap, int cx, int cy, int radius, uint32_t color)
void bj_draw_polyline (struct bj_bitmap *bitmap, size_t count, const int *x, const int *y, bj_bool loop, uint32_t color)

Detailed Description

Draw lines, shapes, and outlines into a bitmap.

These functions draw simple geometric shapes into a bj_bitmap. pixels, lines, rectangles, triangles, circles, and polylines (a series of connected line segments). Each function writes directly into the destination bitmap's pixels; there's no draw queue or scene graph. The pattern is:

  1. Get a bitmap to draw into (commonly the window's framebuffer via bj_render_target_bitmap).
  2. Pick a colour: build a uint32_t packed in the bitmap's pixel format with bj_make_bitmap_pixel.
  3. Call as many drawing functions as you need; they modify the bitmap in place.
  4. Push the bitmap to the screen with bj_invalidate_window.
static void on_draw(struct bj_window* w, struct bj_render_target* t,
const struct bj_rect* dirty, void* user_data) {
uint32_t red = bj_make_bitmap_pixel(fb, 0xFF, 0x00, 0x00);
bj_draw_filled_circle(fb, 320, 240, 100, red);
bj_draw_rectangle(fb, &(bj_rect){.x=10,.y=10,.w=64,.h=64}, red);
}
/* in setup: bj_set_draw_callback(window, on_draw, NULL);
bj_invalidate_window(window); *‍/
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
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
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_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_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a 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.
Axis-aligned rectangle: a top-left corner plus a width and height.
Definition rect.h:33
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.

Outlined or filled

Rectangles, triangles, and circles each come in two flavours: bj_draw_rectangle / bj_draw_filled_rectangle and so on. "Outlined" draws just the edge; "filled" colours every pixel inside. Lines and polylines are obviously line-only.

Bounds are your responsibility

Warning
These functions don't check that the coordinates you pass are inside the bitmap. If you ask to draw a circle at (10000, 10000) on a 640×480 bitmap, you'll corrupt memory or crash. The trade-off is speed: the drawing loop is one less conditional per pixel. If your code generates coordinates that might land outside the bitmap, clamp them yourself before calling.
See also
bj_clear_bitmap to wipe the destination first, drawing_2d.c for every primitive in one program, drawing_text.c for text rendering on top of these primitives.

Function Documentation

◆ bj_draw_circle()

void bj_draw_circle ( struct bj_bitmap * bitmap,
int cx,
int cy,
int radius,
uint32_t color )

Draw the outline of a circle onto a bitmap.

Uses the midpoint circle algorithm (integer arithmetic).

Parameters
bitmapTarget bitmap (must not be NULL).
cxX-coordinate of circle centre (pixels).
cyY-coordinate of circle centre (pixels).
radiusCircle radius in pixels (>= 0).
colorPixel colour in 0xAARRGGBB format.

◆ bj_draw_filled_circle()

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.

Fills all pixels within radius distance from (cx, cy).

Parameters
bitmapTarget bitmap (must not be NULL).
cxX-coordinate of circle centre (pixels).
cyY-coordinate of circle centre (pixels).
radiusCircle radius in pixels (>= 0).
colorPixel colour in 0xAARRGGBB format.
Examples
drawing_2d.c, interpolation.c, physics_kinematics.c, physics_particle.c, pong.c, and start.c.

Referenced by draw(), draw(), draw(), draw(), on_draw(), and on_draw().

◆ bj_draw_filled_rectangle()

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.

The function draws an filled rectangle by filling all pixels within area.

Parameters
bitmapThe bitmap object.
areaThe rectangle to draw.
pixelThe line pixel value.
Memory Safety

This function does not perform any bound checking on the pixel coordinates. It is up to you to ensure the coordinates lie between [0, struct bj_bitmap->width * struct bj_bitmap->height]. Writing outside of these bounds will result in undefined behaviour or corrupted memory access.

Examples
bitmap_blit_colorkey.c, drawing_2d.c, pong.c, and random_distribution.c.

Referenced by draw(), draw(), and setup().

◆ bj_draw_filled_triangle()

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.

Parameters
bitmapThe bitmap object.
x0The X coordinate of the first triangle vertex.
y0The Y coordinate of the first triangle vertex.
x1The X coordinate of the second triangle vertex.
y1The Y coordinate of the second triangle vertex.
x2The X coordinate of the third triangle vertex.
y2The Y coordinate of the third triangle vertex.
colorThe fill colour.
Memory Safety

This function does not perform any bound checking on the pixel coordinates. It is up to you to ensure the coordinates lie between [0, struct bj_bitmap->width * struct bj_bitmap->height]. Writing outside of these bounds will result in undefined behaviour or corrupted memory access.

Examples
drawing_2d.c.

Referenced by draw().

◆ bj_draw_line()

void bj_draw_line ( struct bj_bitmap * bitmap,
int x0,
int y0,
int x1,
int y1,
uint32_t pixel )

Draws a line of pixels in the given bitmap.

The line is drawn for each pixel between p0 and p1.

Parameters
bitmapThe bitmap object.
x0The X coordinate of the first point in the line.
y0The Y coordinate of the first point in the line.
x1The X coordinate of the second point in the line.
y1The Y coordinate of the second point in the line.
pixelThe line pixel value.
Memory Safety

This function does not perform any bound checking on the pixel coordinates. It is up to you to ensure the coordinates lie between [0, struct bj_bitmap->width * struct bj_bitmap->height]. Writing outside of these bounds will result in undefined behaviour or corrupted memory access.

Examples
random_distribution.c.

Referenced by draw().

◆ bj_draw_polyline()

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.

Draws line segments between successive vertex pairs (x[i], y[i]) -> (x[i+1], y[i+1]). If loop != 0 and count >= 2, an extra segment connects the last vertex to the first.

Parameters
bitmapTarget bitmap.
xPointer to array of x coordinates (length >= count).
yPointer to array of y coordinates (length >= count).
countNumber of vertices. Segments drawn for i = 0..count-2.
colorPixel colour in 0xAARRGGBB format.
loopNonzero to close the polyline.
Examples
drawing_2d.c.

Referenced by draw().

◆ bj_draw_rectangle()

void bj_draw_rectangle ( struct bj_bitmap * bitmap,
const struct bj_rect * area,
uint32_t pixel )

Draws a rectangle in the given bitmap.

The function draws an rectangle outline using 4 consecutive calls to bj_draw_line.

Parameters
bitmapThe bitmap object.
areaThe rectangle to draw.
pixelThe line pixel value.
Memory Safety

This function does not perform any bound checking on the pixel coordinates. It is up to you to ensure the coordinates lie between [0, struct bj_bitmap->width * struct bj_bitmap->height]. Writing outside of these bounds will result in undefined behaviour or corrupted memory access.

Examples
drawing_2d.c, random_distribution.c, and start.c.

Referenced by draw(), and on_draw().

◆ bj_draw_triangle()

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.

Parameters
bitmapThe bitmap object.
x0The X coordinate of the first triangle vertex.
y0The Y coordinate of the first triangle vertex.
x1The X coordinate of the second triangle vertex.
y1The Y coordinate of the second triangle vertex.
x2The X coordinate of the third triangle vertex.
y2The Y coordinate of the third triangle vertex.
colorThe line colour.
Memory Safety

This function does not perform any bound checking on the pixel coordinates. It is up to you to ensure the coordinates lie between [0, struct bj_bitmap->width * struct bj_bitmap->height]. Writing outside of these bounds will result in undefined behaviour or corrupted memory access.

Examples
drawing_2d.c.

Referenced by draw().