Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
drawing_text.c
Go to the documentation of this file.
1
10
11#include <banjo/app.h>
12#include <banjo/main.h>
13#include <banjo/bitmap.h>
14#include <banjo/event.h>
15#include <banjo/log.h>
16#include <banjo/system.h>
17#include <banjo/window.h>
18
20void draw(bj_bitmap* bmp) {
21
22 uint32_t white = bj_make_bitmap_pixel(bmp,255,255,255);
23 uint32_t black = bj_make_bitmap_pixel(bmp,0,0,0);
24 uint32_t light_grey = bj_make_bitmap_pixel(bmp,220,220,220);
25 uint32_t cyan = bj_make_bitmap_pixel(bmp,0,200,200);
26
27 // bj_draw_text() renders text with ANSI escape code support for inline
28 // colours. Use \x1B[<code>m for colours and \x1B[0m to reset to default.
29 // Parameters: bitmap, x, y, size, foreground_color, text
30 bj_draw_text(bmp, 20, 20, 18, white, "\x1B[31mRED\x1B[0m normal");
31 bj_draw_text(bmp, 20, 52, 18, white, "\x1B[94mBrightBlue\x1B[0m + default");
32 bj_draw_text(bmp, 20, 84, 18, white, "\x1B[38;2;255;128;0mTruecolor Orange\x1B[0m");
33
34 // bj_draw_textf() works like printf, supporting all standard format
35 // specifiers. This is the easiest way to display dynamic values like
36 // scores, health, coordinates, etc.
37 bj_draw_textf(bmp, 20, 200, 18, white, "Hello, %s!", "world");
38 bj_draw_textf(bmp, 20, 230, 18, white, "Score: %d Lives: %u", -123, 3);
39 bj_draw_textf(bmp, 20, 260, 18, white, "Zero-pad: %08u Left: %-8u|", 42, 42);
40 bj_draw_textf(bmp, 20, 290, 18, white, "Name: %.5s Pi≈%.3f (note: if %%f not supported, skip)", "Banjo", 3.14159);
41 bj_draw_textf(bmp, 20, 320, 18, white, "HEX: 0x%08X oct: %o ptr: %p", 0xDEADBEEF, 0755, (void*)bmp);
42 bj_draw_textf(bmp, 20, 350, 18, white, "Width(*)=%*u Prec(*)=%. *u", 6, 123, 4, 123);
43
44 // Length modifiers (ll, l, h, hh) are supported for different integer sizes.
45 unsigned long long big = 18446744073709551615ull;
46 bj_draw_textf(bmp, 20, 380, 18, white, "ll: %llu l: %ld h: %hd hh: %hhu",
47 big, (long)-123456, (short)1234, (unsigned char)255);
48
49 // bj_blit_text() provides more control with separate foreground and
50 // background colours and transparency modes:
51 // BJ_MASK_BG_OPAQUE - solid background rectangle
52 // BJ_MASK_BG_REV_TRANSPARENT - background shows through foreground
53 bj_blit_text(bmp, 20, 116, 14, black, light_grey, BJ_MASK_BG_OPAQUE, "OPAQUE band (FG black)");
54 bj_blit_text(bmp, 20, 150, 32, black, cyan, BJ_MASK_BG_REV_TRANSPARENT, "CARVED cyan");
55}
56
57static void on_draw(
58 struct bj_window* w,
59 struct bj_render_target* target,
60 const struct bj_rect* dirty,
61 void* user_data
62) {
63 (void)w; (void)dirty; (void)user_data;
65}
66
67static void* setup(struct bj_app* app, void* init_data) {
68 (void)init_data;
69
70 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
71 bj_quit_app(app, 1);
72 return 0;
73 }
74
75 window = bj_bind_window("Simple Text", 100, 100, 500, 500, 0, 0);
76
80 return 0;
81}
82
83static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
84 (void)tick;
85 (void)user_data;
87
89 bj_quit_app(app, 0);
90 }
91}
92
93static void teardown(struct bj_app* app, void* user_data) {
94 (void)user_data;
96 bj_end();
97}
98
99int main(int argc, char* argv[]) {
100 (void)argc; (void)argv;
101 return bj_run_app(setup, step, 0, teardown, 0);
102}
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.