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

Comprehensive demonstration of 2D primitive drawing functions.

Comprehensive demonstration of 2D primitive drawing functions.Banjo provides functions to draw pixels, lines, rectangles, circles, and triangles to bitmaps. This example showcases all major drawing primitives and demonstrates the difference between filled and outlined shapes.

#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/renderer.h>
#include <banjo/system.h>
#include <banjo/time.h>
#include <banjo/window.h>
void draw(bj_bitmap* bmp) {
// Clear the bitmap to black before drawing.
// Create colors in the bitmap's native pixel format. Always use
// bj_make_bitmap_pixel() rather than hardcoding values, as the format
// varies by platform and configuration.
const uint32_t color_red = bj_make_bitmap_pixel(bmp, 0xFF, 0x00, 0x00);
const uint32_t color_cyan = bj_make_bitmap_pixel(bmp, 0x7F, 0xFF, 0xD4);
const uint32_t color_white = bj_make_bitmap_pixel(bmp, 0xFF, 0xFF, 0xFF);
// Draw individual pixels. bj_put_pixel() sets a single pixel at (x, y).
// This is the most basic drawing operation.
for (size_t x = 10; x < 490; ++x) {
if (x % 7 == 0) {
bj_put_pixel(bmp, x, 10, color_red);
}
}
// Draw a polyline (connected line segments). The last parameter (BJ_TRUE)
// closes the shape by connecting the last point back to the first.
// This draws a banjo outline.
int poly_x[] = { 100, 95, 95, 100, 100, 95, 75, 75, 95, 120, 140, 140, 120, 115, 115, 120, 120, 115, };
int poly_y[] = { 20, 25, 50, 55, 100, 100, 120, 145, 165, 165, 145, 120, 100, 100, 55, 50, 25, 20, };
bj_draw_polyline(bmp, 18, poly_x, poly_y, BJ_TRUE, color_cyan);
// Draw outlined triangles by indexing into a vertex array. This technique
// is common in graphics programming and efficiently reuses vertex data.
// Here we draw 13 triangles forming a fox shape.
int verts[][2] = {
{330, 270}, {270, 210}, {210, 270}, {210, 150}, {390, 210}, {450, 270},
{450, 150}, {180, 330}, {270, 390}, {390, 390}, {480, 330}, {330, 450},
{300, 480}, {360, 480},
};
size_t tris[13][3] = {
{0, 1, 2}, {0, 2, 3}, {0, 4, 5}, {0, 1, 4}, {4, 6, 5}, {2, 8, 7},
{0, 8, 2}, {0, 5, 9}, {9, 5, 10}, {8, 9, 11}, {8, 11, 12},
{9, 13, 11}, {11, 12, 13},
};
for (size_t t = 0; t < 13; ++t) {
verts[tris[t][0]][0], verts[tris[t][0]][1],
verts[tris[t][1]][0], verts[tris[t][1]][1],
verts[tris[t][2]][0], verts[tris[t][2]][1],
color_white
);
}
// Draw a checkerboard pattern using filled rectangles. The bj_rect
// structure defines a rectangle's position and size.
bj_rect board = {.w = 10, .h = 10,};
for(size_t y = 0 ; y < 8 ; ++y) {
for(size_t x = 0 ; x < 8 ; ++x) {
board.x = 200 + x * board.w;
board.y = 50 + y * board.h;
// XOR determines checkerboard pattern (alternating squares).
if((x ^ y) & 1) {
bj_draw_filled_rectangle(bmp, &board, color_red);
}
}
}
// Draw an outline around the entire checkerboard. Note the difference
// between bj_draw_rectangle (outline) and bj_draw_filled_rectangle (solid).
&(bj_rect) {.x = 200, .y = 50, .w = 80, .h = 80,},
color_cyan
);
// Draw concentric circles with alternating colors. bj_draw_filled_circle()
// takes center (x, y), radius, and color.
for (int r = 80; r > 0; r -= 20) {
bj_draw_filled_circle(bmp, 100, 400, r, (r/20) % 2 ? color_red : color_white);
}
// Draw filled triangles for a simple mountain scene. Filled triangles are
// drawn with bj_draw_filled_triangle(), taking three vertices and a color.
const uint32_t color_dark_gray = bj_make_bitmap_pixel(bmp, 0x50, 0x50, 0x50);
const uint32_t color_gray = bj_make_bitmap_pixel(bmp, 0x80, 0x80, 0x80);
const uint32_t color_light_gray = bj_make_bitmap_pixel(bmp, 0xB0, 0xB0, 0xB0);
bj_draw_filled_triangle(bmp, 250, 400, 200, 480, 300, 480, color_gray);
bj_draw_filled_triangle(bmp, 300, 420, 250, 480, 350, 480, color_dark_gray);
bj_draw_filled_triangle(bmp, 350, 390, 300, 480, 400, 480, color_light_gray);
}
int bj_app_begin(void** user_data, int argc, char* argv[]) {
(void)user_data; (void)argc; (void)argv;
return bj_callback_exit_error;
}
window = bj_bind_window("Simple Text", 100, 100, 500, 500, 0, 0);
return bj_callback_continue;
}
int bj_app_iterate(void* user_data) {
(void)user_data;
bj_sleep(300);
? 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
bj_window * window
Definition bitmap_blit.c:24
Header file for Bitmap drawing functions.
Sytem event management API.
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear color.
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.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel color at given coordinate.
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
#define BJ_TRUE
Boolean true value (1).
Definition api.h:218
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_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.
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.
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_draw_filled_circle(struct bj_bitmap *bitmap, int cx, int cy, int radius, uint32_t color)
Draw a filled circle onto a bitmap.
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.
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.
uint16_t w
The width of the rectangle.
Definition rect.h:22
uint16_t h
The height of the rectangle.
Definition rect.h:23
int16_t y
The y-coordinate of the rectangle's top-left corner.
Definition rect.h:21
int16_t x
The x-coordinate of the rectangle's top-left corner.
Definition rect.h:20
Represents a rectangle with position and dimensions.
Definition rect.h:19
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.
static void draw()
Definition physics_kinematics.c:136
Rendering backend interface.
Header file for system interactions.
Header file for time manipulation utilities.
Header file for bj_window type.