Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
drawing_text.c

Text rendering with built-in fonts, ANSI colours, and printf-style formatting.

Text rendering with built-in fonts, ANSI colours, and printf-style formatting.Banjo includes bitmap text rendering with a built-in font. No external font files are needed. Text can be rendered with ANSI escape codes for inline colours, printf-style formatting, and various transparency modes.

#include <banjo/app.h>
#include <banjo/main.h>
#include <banjo/bitmap.h>
#include <banjo/event.h>
#include <banjo/log.h>
#include <banjo/system.h>
#include <banjo/window.h>
void draw(bj_bitmap* bmp) {
uint32_t white = bj_make_bitmap_pixel(bmp,255,255,255);
uint32_t black = bj_make_bitmap_pixel(bmp,0,0,0);
uint32_t light_grey = bj_make_bitmap_pixel(bmp,220,220,220);
uint32_t cyan = bj_make_bitmap_pixel(bmp,0,200,200);
// bj_draw_text() renders text with ANSI escape code support for inline
// colours. Use \x1B[<code>m for colours and \x1B[0m to reset to default.
// Parameters: bitmap, x, y, size, foreground_color, text
bj_draw_text(bmp, 20, 20, 18, white, "\x1B[31mRED\x1B[0m normal");
bj_draw_text(bmp, 20, 52, 18, white, "\x1B[94mBrightBlue\x1B[0m + default");
bj_draw_text(bmp, 20, 84, 18, white, "\x1B[38;2;255;128;0mTruecolor Orange\x1B[0m");
// bj_draw_textf() works like printf, supporting all standard format
// specifiers. This is the easiest way to display dynamic values like
// scores, health, coordinates, etc.
bj_draw_textf(bmp, 20, 200, 18, white, "Hello, %s!", "world");
bj_draw_textf(bmp, 20, 230, 18, white, "Score: %d Lives: %u", -123, 3);
bj_draw_textf(bmp, 20, 260, 18, white, "Zero-pad: %08u Left: %-8u|", 42, 42);
bj_draw_textf(bmp, 20, 290, 18, white, "Name: %.5s Pi≈%.3f (note: if %%f not supported, skip)", "Banjo", 3.14159);
bj_draw_textf(bmp, 20, 320, 18, white, "HEX: 0x%08X oct: %o ptr: %p", 0xDEADBEEF, 0755, (void*)bmp);
bj_draw_textf(bmp, 20, 350, 18, white, "Width(*)=%*u Prec(*)=%. *u", 6, 123, 4, 123);
// Length modifiers (ll, l, h, hh) are supported for different integer sizes.
unsigned long long big = 18446744073709551615ull;
bj_draw_textf(bmp, 20, 380, 18, white, "ll: %llu l: %ld h: %hd hh: %hhu",
big, (long)-123456, (short)1234, (unsigned char)255);
// bj_blit_text() provides more control with separate foreground and
// background colours and transparency modes:
// BJ_MASK_BG_OPAQUE - solid background rectangle
// BJ_MASK_BG_REV_TRANSPARENT - background shows through foreground
bj_blit_text(bmp, 20, 116, 14, black, light_grey, BJ_MASK_BG_OPAQUE, "OPAQUE band (FG black)");
bj_blit_text(bmp, 20, 150, 32, black, cyan, BJ_MASK_BG_REV_TRANSPARENT, "CARVED cyan");
}
static void on_draw(
struct bj_window* w,
struct bj_render_target* target,
const struct bj_rect* dirty,
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
}
static void* setup(struct bj_app* app, void* init_data) {
(void)init_data;
bj_quit_app(app, 1);
return 0;
}
window = bj_bind_window("Simple Text", 100, 100, 500, 500, 0, 0);
return 0;
}
static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
(void)tick;
(void)user_data;
bj_quit_app(app, 0);
}
}
static void teardown(struct bj_app* app, void* user_data) {
(void)user_data;
bj_end();
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
return bj_run_app(setup, step, 0, teardown, 0);
}
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_window * window
Definition bitmap_blit.c:24
void draw(bj_bitmap *bmp)
Definition drawing_2d.c:133
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
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.
void bj_blit_text(struct 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.
void bj_draw_textf(struct bj_bitmap *bitmap, int x, int y, unsigned height, uint32_t fg_native, const char *fmt,...)
Prints formatted text into a bitmap, similar to printf.
void bj_draw_text(struct bj_bitmap *dst, int x, int y, unsigned height, uint32_t fg_native, const char *text)
Prints text using the default foreground colour and transparent background.
@ BJ_MASK_BG_OPAQUE
Opaque band: mix(background, foreground, mask)
Definition bitmap.h:672
@ BJ_MASK_BG_REV_TRANSPARENT
Carved: mix(destination, background, 1-mask)
Definition bitmap.h:673
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_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_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.
Header file for system interactions.
Header file for bj_window type.