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

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

Text rendering with built-in fonts, ANSI colors, 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 colors, printf-style formatting, and various transparency modes.

#define BJ_AUTOMAIN_CALLBACKS
#include <banjo/bitmap.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) {
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
// colors. Use \x1B[<code>m for colors 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 colors 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");
}
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
Sytem event management API.
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_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 color and transparent background.
@ BJ_MASK_BG_OPAQUE
Opaque band: mix(background, foreground, mask)
Definition bitmap.h:628
@ BJ_MASK_BG_REV_TRANSPARENT
Carved: mix(destination, background, 1-mask)
Definition bitmap.h:629
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_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.
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.