From 495a816647304d5858347b3c030a6ffab0262dc4 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sat, 10 Aug 2024 16:15:16 +0200 Subject: Update bit font --- examples/ui.c | 159 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 95 insertions(+), 64 deletions(-) (limited to 'examples/ui.c') diff --git a/examples/ui.c b/examples/ui.c index 3bf8435..b66521c 100755 --- a/examples/ui.c +++ b/examples/ui.c @@ -172,9 +172,10 @@ Platform platform = {0}; #include #include -i64 char_num_bits_x = 4; -i64 char_num_bits_y = 7; -u64 bitfont[] = { +#define CHAR_NUM_BITS_X 4 +#define CHAR_NUM_BITS_Y 7 + +u64 bitfont[] = { #include "bitfont.inl.c" }; @@ -182,35 +183,61 @@ void print_text(u32 color, i32 x, i32 y, f64 width, f64 height, i64 num_chars, c assert(text != NULL); f64 dx = x; + f64 dy = y; i64 w = (i64) floor(width + .5); for (i64 n = 0; n < num_chars; ++n) { + if (text[n] == '\r') { + dx = x; + continue; + } + + if (text[n] == '\n') { + dx = x; + dy += (height * (CHAR_NUM_BITS_Y + 1)) / CHAR_NUM_BITS_Y; + continue; + } + i64 x0 = (i64) floor(dx + .5); - i64 y1 = (i64) floor(y + height + .5); + i64 y0 = (i64) floor(dy + .5); + i64 y1 = (i64) floor(dy + height + .5); i64 num_cols = 0; for (i64 i = x0; i < x0 + w; ++i, ++num_cols) { b8 empty_col = 1; - for (i64 j = y; j < y1; ++j) { - if (i < 0 || i >= platform.frame_width || j < 0 || j >= platform.frame_height) - continue; - i64 char_x = ((i - x0) * (char_num_bits_x + 1)) / w; - if (char_x >= char_num_bits_x) - continue; - i64 char_y = ((j - y ) * char_num_bits_y ) / (y1 - y ); - i64 bit_index = text[n] * char_num_bits_x * char_num_bits_y + char_y * char_num_bits_x + char_x; + i64 char_x = ((i - x0) * (CHAR_NUM_BITS_X + 1)) / w; + if (char_x >= CHAR_NUM_BITS_X) + break; + + for (i64 j = y0; j < y1; ++j) { + i64 char_y = ((j - y0) * CHAR_NUM_BITS_Y) / (y1 - y0); + i64 bit_index = text[n] * CHAR_NUM_BITS_X * CHAR_NUM_BITS_Y + char_y * CHAR_NUM_BITS_X + char_x; i64 qword_index = bit_index / 64; if (qword_index >= (i64) (sizeof bitfont / sizeof *bitfont)) continue; u64 mask = 1ull << (bit_index % 64); if (!!(bitfont[qword_index] & mask)) { - platform.pixels[j * platform.frame_width + i] = color; empty_col = 0; + if (i < 0 || i >= platform.frame_width || j < 0 || j >= platform.frame_height) + continue; + platform.pixels[j * platform.frame_width + i] = color; } } + if (empty_col && char_x + 1 < CHAR_NUM_BITS_X) + for (i64 j = y; j < y1; ++j) { + i64 char_y = ((j - y) * CHAR_NUM_BITS_Y) / (y1 - y); + i64 bit_index = text[n] * CHAR_NUM_BITS_X * CHAR_NUM_BITS_Y + char_y * CHAR_NUM_BITS_X + char_x + 1; + i64 qword_index = bit_index / 64; + if (qword_index >= (i64) (sizeof bitfont / sizeof *bitfont)) + continue; + u64 mask = 1ull << (bit_index % 64); + if (!!(bitfont[qword_index] & mask)) + empty_col = 0; + } + if (empty_col) break; } @@ -218,7 +245,7 @@ void print_text(u32 color, i32 x, i32 y, f64 width, f64 height, i64 num_chars, c if (text[n] == ' ') dx += width; else - dx += num_cols + width / char_num_bits_x; + dx += num_cols + width / CHAR_NUM_BITS_X; } } @@ -238,64 +265,64 @@ i32 main(i32 argc, c8 **argv) { " " " " " " - " ! xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx xxxx" - " ! x x x x x x x x x x x x " - " ! x x x x x x x x x x x x " - " ! x x x x x x x x x x x x " - " x x x x x x x x x x x x " - " ! x x x x x x x x x x , x . x " - " x x x x x x x x x x , x x " - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "x x x x x x x x x x x x x x x x " - "x x x x x x x x x x x x x x x x " - "x x x x x x x x x x x x x x x x " - "x x x x x x x x x x x x x x x x " - "x x x x x x x x x x x x x x x x " - "x x x x x x x x x x x x x x x x " - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxH Hxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "x x x x x x x x H Hx x x x x x x " - "x x x x x x x x HHHHx x x x x x x " - "x x x x x x x x H Hx x x x x x x " - "x x x x x x x x H Hx x x x x x x " - "x x x x x x x x H Hx x x x x x x " - "x x x x x x x x x x x x x x x " - "xxxxxxxxxxxx SS xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "x x x S x x x x x x x x x x x x " - "x x x SS x x x x x x x x x x x x " - "x x x Sx x x x x x x x x x x x " - "x x x S Sx x x x x x x x x x x x " - "x x x SS x x x x x x x x x x x x " - "x x x x x x x x x x x x x x x " - "xxxx xxxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxl xxxxxxxx " - "x aa x x x ee x x x i x x l x x oo " - "x ax x x e ex x x x x l x x o o" - "x aaaax x x eeeex x x ii x x l x x o o" - "x a ax x x e x x x i x x l x x o o" - "x aa x x x ee x x x iii x x l x x oo " - "x x x x x x x x x x x " - "xxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "x x r r x x x x x x x x x x x x x " - "x x rr x x x x x x x x x x x x x " - "x x r x x x x x x x x x x x x x " - "x x r x x x x x x x x x x x x x " - "x x r x x x x x x x x x x x x x " - "x x x x x x x x x x x x x x x " + " ! ' ' $$ % && ' ( ) * / " + " ! ' ' # # $$ $% % & & ' ( ) *** / " + " ! ####$$ % && ( ) *** + / " + " ! # # $$ %%%%&& ( ) * +++ --- / " + " # # $ $ % & & ( ) + / " + " ! ####$$ $ % %& & ( ) , . / " + " # # $$ % &&& ( ) , / " + " ? " + "000 11 222 333 4 4 555 66 777 888 999 < > ? ? " + "0 0 1 2 3 4 4 5 6 7 8 8 9 9 : ; < === > ? " + "0 0 1 2 3 444 555 666 7 888 999 < > ? " + "0 0 1 2 3 4 5 6 6 7 8 8 9 < === > " + "000 111 222 333 4 55 666 7 888 99 : ; < > ? " + " ; " + " @@ AA BBB CC DDD EEEEFFFF GG H HIII JJJ K KL M MN N OO " + "@ @A AB BC CD DE F G GH H I J K K L MMMMNN NO O" + "@ @@A ABBB C D DEEE FFF G HHHH I J KK L MMMMN NNO O" + "@@ @AAAAB BC D DE F G GGH H I J K K L MMMMN NO O" + "@ @@A AB BC CD DE F G GH H I J J K KL M MN NO O" + "@ A ABBB CC DDD EEEEF GG H HIII J K KLLL M MN N OO " + " @@ " + "PPP QQ RRR SS TTT U UV V W WX XY YZZZZ[[ x ]] ^ " + "P PQ QR RS T U UV V W WX XY Y Z[ x ] ^ ^ " + "P PQ QR R SS T U UV V W W XX YY Z [ x ] " + "PPP Q QRRR S T U UV V WWWW XX Y Z [ x ] " + "P Q QR RS S T U UVVV WWWWX X Y Z [ x ] " + "P QQ R R SS T UU V WW X X Y ZZZZ[ x ] " + " Q [[ x ]] ____" + "` l " + " ` aa b cc d ee ff gg h i j k k l m mnnn oo " + " abbb c c ddde e f g gh k k l mmmmn no o" + " aaaab bc d deeeefff g ghhh ii jj kk l mmmmn no o" + " a ab bc cd de f gggh h i j k k l mmmmn no o" + " aa bbb cc ddd ee f gh hiii j k k l m mn n oo " + " gg j " + " { | } ~ ~ " + "ppp qqqr r ss ttt u uv v w wx xy yzzzz { | } ~ ~ " + "p pq qrr s t u uv v w wx xy y z { | } " + "p pq qr ss t u uv v wwww xx yy zz { | } " + "ppp qqqr s t u u v wwwwx x y z { | } " + "p qr ss t uuu v ww x xy zzzz { | } " + " { | } " ; - u64 bits[(16 * 8 * 7 * 4) / 64] = {0}; + u64 bits[(16 * 8 * CHAR_NUM_BITS_X * CHAR_NUM_BITS_Y) / 64] = {0}; for (i64 j = 0; j < 8; ++j) for (i64 i = 0; i < 16; ++i) - for (i64 y = 0; y < char_num_bits_y; ++y) - for (i64 x = 0; x < char_num_bits_x; ++x) { + for (i64 y = 0; y < CHAR_NUM_BITS_Y; ++y) + for (i64 x = 0; x < CHAR_NUM_BITS_X; ++x) { i64 num_char = j * 16 + i; - i64 num_bit = y * char_num_bits_x + x; - i64 bit_index = num_char * char_num_bits_x * char_num_bits_y + num_bit; + i64 num_bit = y * CHAR_NUM_BITS_X + x; + i64 bit_index = num_char * CHAR_NUM_BITS_X * CHAR_NUM_BITS_Y + num_bit; i64 qword_index = bit_index / 64; u64 mask = 1ull << (bit_index % 64); - i64 dx = i * char_num_bits_x + x; - i64 dy = j * char_num_bits_y + y; - i64 index = dy * char_num_bits_x * 16 + dx; + i64 dx = i * CHAR_NUM_BITS_X + x; + i64 dy = j * CHAR_NUM_BITS_Y + y; + i64 index = dy * CHAR_NUM_BITS_X * 16 + dx; if (index < (i64) (sizeof data / sizeof *data) && data[index] != ' ') bits[qword_index] |= mask; } @@ -331,6 +358,10 @@ i32 main(i32 argc, c8 **argv) { print_text(color, 80, 80, 60., 100., 14, "Hello, Sailor!"); + c8 chars[] = "\"#$%&'()*+,-./\n0123456789:;<=>?@\nABCDEFGHIJKLMNOPQRSTUVWXYZ\n[\\]^_`\nabcdefghijklmnopqrstuvwxyz\n{|}~"; + + print_text(0x7fffff, 80, 200, 60., 100., sizeof chars - 1, chars); + p_render_frame(); } -- cgit v1.2.3