summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/bitfont.inl.c2
-rwxr-xr-xexamples/ui.c159
2 files changed, 96 insertions, 65 deletions
diff --git a/examples/bitfont.inl.c b/examples/bitfont.inl.c
index 16ddabd..860cbbe 100644
--- a/examples/bitfont.inl.c
+++ b/examples/bitfont.inl.c
@@ -1 +1 @@
-0x1f111111f111111f, 0x111f111111f11111, 0x11111f111111f111, 0xf111111f111111f1, 0x11f111111f111111, 0x1111f111111f1111, 0x111111f111111f11, 0x1f111111f111111f, 0x111f111111f11111, 0x11111f111111f111, 0xf111111f111111f1, 0x11f111111f111111, 0x1111f111111f1111, 0x111111f111111f11, 0x1f01011110000000, 0x111f111111f11111, 0x11111f111111f111, 0xf111111f111111f1, 0x11f111111f111111, 0x1111f12000001111, 0x111111f010000011, 0x1f111111f111111f, 0x111f111111f11111, 0x11111f111111f111, 0xf111111f111111f1, 0x11f111111f111111, 0x1111f111111f1111, 0x111111f111111f11, 0x1f111111f111111f, 0x111f111111f11111, 0x11111f111111f111, 0xf0999f99111111f1, 0x11f111111f111111, 0x1111f111111f1111, 0x111111f111111f11, 0x1f111111f111111f, 0x111f069861611111, 0x11111f111111f111, 0xf111111f111111f1, 0x11f111111f111111, 0x1111f111111f1111, 0x111111f111111f11, 0x1f069f860111111f, 0x111f111111f11111, 0x11111f061f960111, 0x111111f111111f1, 0x11f111111f072302, 0x1111f02111111111, 0x699960111111f11, 0x50111111f111111f, 0x111f111111f01113, 0x11111f111111f111, 0xf111111f111111f1, 0x11f111111f111111, 0x1111f111111f1111, 0x111111f111111f11,
+0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5501011110000000, 0x63b65f55f5000000, 0x9536564a4f2526ba, 0x1211111200000117, 0x2000002772122222, 0x700012000000027, 0x1122244010000000, 0x7007222300755570, 0x7550074247007124, 0x7571600347170044, 0x75757002224700, 0x2000100100034757, 0x7070004212401200, 0x202452012421000, 0x97099f99661dbd96, 0x9997069119607997, 0x11171f0f1171f079, 0x70999f99069d1960, 0x3590254447072222, 0x9fff907111110995, 0x6999960999db909, 0x9786999960117997, 0x2227069861609979, 0x2755550699999022, 0x9099669906ff9990, 0x1130f1248f022469, 0x2222344222113111, 0xf000000000005232, 0x10069f8600000021, 0x9e80069196007997, 0x227260061f9600e9, 0x99711068e99600, 0x5501223020072302, 0xfff9002111110553, 0x699960099997009, 0x5008e99e00179970, 0x2270068616001113, 0x2255500e99990022, 0x99699006ff9900, 0x2240f168f0012699, 0x2422111111114221, 0x5a12,
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 <math.h>
#include <assert.h>
-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();
}