From a0efde919bedfbb427d2e2c5ae34e06489930b0c Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 26 Apr 2021 20:58:00 +0200 Subject: Test text drawing on tiled backgrounds --- src/bd-font.c | 386 ++++++++++++++++++++-------------------------------------- src/common.h | 67 ++++++---- src/main.c | 107 ++++++++-------- src/sprites.h | 22 +--- src/text.h | 142 +++++++++++++++++++++ 5 files changed, 370 insertions(+), 354 deletions(-) create mode 100644 src/text.h diff --git a/src/bd-font.c b/src/bd-font.c index 6173e97..e3fb6cc 100644 --- a/src/bd-font.c +++ b/src/bd-font.c @@ -1,258 +1,130 @@ -static const u8 font[][8] = { - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x24, 0x00, 0x42, 0x3c, 0x00, 0x00}, - {0x00, 0x00, 0x24, 0x00, 0x3c, 0x42, 0x00, 0x00}, - {0x00, 0x00, 0x6c, 0x7c, 0x7c, 0x38, 0x10, 0x00}, - {0x00, 0x10, 0x38, 0x7c, 0x7c, 0x38, 0x10, 0x00}, - {0x00, 0x38, 0x38, 0x7c, 0x6c, 0x7c, 0x10, 0x38}, - {0x00, 0x10, 0x38, 0x7c, 0x7c, 0x7c, 0x10, 0x38}, - {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00}, - {0x7e, 0x7e, 0x7e, 0x66, 0x66, 0x7e, 0x7e, 0x7e}, - {0x00, 0x00, 0x18, 0x24, 0x24, 0x18, 0x00, 0x00}, - {0x7e, 0x7e, 0x66, 0x5a, 0x5a, 0x66, 0x7e, 0x7e}, - {0x00, 0x10, 0x38, 0x7c, 0x10, 0x38, 0x44, 0x38}, - {0x00, 0x38, 0x44, 0x38, 0x10, 0x10, 0x7c, 0x10}, - {0x00, 0x18, 0x1c, 0x14, 0x10, 0x10, 0x30, 0x30}, - {0x00, 0x3c, 0x24, 0x24, 0x24, 0x2c, 0x6c, 0x60}, - {0x00, 0x10, 0x44, 0x10, 0x38, 0x10, 0x44, 0x10}, - {0x20, 0x30, 0x38, 0x3c, 0x38, 0x30, 0x20, 0x00}, - {0x04, 0x0c, 0x1c, 0x3c, 0x1c, 0x0c, 0x04, 0x00}, - {0x10, 0x38, 0x7c, 0x10, 0x10, 0x7c, 0x38, 0x10}, - {0x00, 0x28, 0x28, 0x28, 0x28, 0x28, 0x00, 0x28}, - {0x00, 0x3c, 0x54, 0x54, 0x34, 0x14, 0x14, 0x14}, - {0x00, 0x1c, 0x20, 0x3c, 0x42, 0x3c, 0x04, 0x78}, - {0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00}, - {0x10, 0x38, 0x7c, 0x10, 0x7c, 0x38, 0x10, 0x7c}, - {0x10, 0x38, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x38, 0x10}, - {0x00, 0x00, 0x08, 0x0c, 0x7e, 0x0c, 0x08, 0x00}, - {0x00, 0x00, 0x10, 0x30, 0x7e, 0x30, 0x10, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x7c}, - {0x00, 0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00}, - {0x00, 0x10, 0x10, 0x38, 0x38, 0x7c, 0x7c, 0x00}, - {0x00, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10}, - {0x28, 0x28, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x28, 0x7c, 0x28, 0x7c, 0x28, 0x00}, - {0x00, 0x10, 0x38, 0x40, 0x38, 0x04, 0x78, 0x10}, - {0x00, 0x00, 0x64, 0x68, 0x10, 0x2c, 0x4c, 0x00}, - {0x00, 0x10, 0x28, 0x10, 0x2c, 0x48, 0x34, 0x00}, - {0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x10, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10}, - {0x10, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x10}, - {0x00, 0x00, 0x28, 0x10, 0x7c, 0x10, 0x28, 0x00}, - {0x00, 0x00, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x10, 0x20}, - {0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00}, - {0x00, 0x04, 0x0c, 0x18, 0x30, 0x60, 0x40, 0x00}, - {0x00, 0x00, 0x38, 0x44, 0x54, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x30, 0x50, 0x10, 0x10, 0x7c, 0x00}, - {0x00, 0x00, 0x38, 0x04, 0x38, 0x40, 0x7c, 0x00}, - {0x00, 0x00, 0x38, 0x44, 0x18, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x08, 0x18, 0x28, 0x7c, 0x08, 0x00}, - {0x00, 0x00, 0x7c, 0x40, 0x78, 0x04, 0x78, 0x00}, - {0x00, 0x00, 0x38, 0x40, 0x78, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x7c, 0x04, 0x08, 0x10, 0x20, 0x00}, - {0x00, 0x00, 0x38, 0x44, 0x38, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x38, 0x44, 0x3c, 0x04, 0x38, 0x00}, - {0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x00}, - {0x00, 0x00, 0x30, 0x30, 0x00, 0x30, 0x10, 0x20}, - {0x00, 0x08, 0x10, 0x20, 0x20, 0x10, 0x08, 0x00}, - {0x00, 0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x00}, - {0x00, 0x20, 0x10, 0x08, 0x08, 0x10, 0x20, 0x00}, - {0x00, 0x38, 0x44, 0x04, 0x08, 0x10, 0x00, 0x10}, - {0x00, 0x38, 0x4c, 0x54, 0x58, 0x40, 0x38, 0x00}, - {0x00, 0x38, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x00}, - {0x00, 0x78, 0x44, 0x78, 0x44, 0x44, 0x78, 0x00}, - {0x00, 0x38, 0x44, 0x40, 0x40, 0x44, 0x38, 0x00}, - {0x00, 0x70, 0x48, 0x44, 0x44, 0x48, 0x70, 0x00}, - {0x00, 0x7c, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00}, - {0x00, 0x7c, 0x40, 0x78, 0x40, 0x40, 0x40, 0x00}, - {0x00, 0x38, 0x44, 0x40, 0x4c, 0x44, 0x38, 0x00}, - {0x00, 0x44, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x00}, - {0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00}, - {0x00, 0x04, 0x04, 0x04, 0x44, 0x44, 0x38, 0x00}, - {0x00, 0x44, 0x48, 0x50, 0x70, 0x48, 0x44, 0x00}, - {0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7c, 0x00}, - {0x00, 0x44, 0x6c, 0x54, 0x44, 0x44, 0x44, 0x00}, - {0x00, 0x44, 0x64, 0x54, 0x4c, 0x44, 0x44, 0x00}, - {0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00}, - {0x00, 0x78, 0x44, 0x44, 0x78, 0x40, 0x40, 0x00}, - {0x00, 0x38, 0x44, 0x44, 0x54, 0x48, 0x34, 0x00}, - {0x00, 0x78, 0x44, 0x44, 0x78, 0x48, 0x44, 0x00}, - {0x00, 0x38, 0x40, 0x38, 0x04, 0x44, 0x38, 0x00}, - {0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}, - {0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00}, - {0x00, 0x44, 0x44, 0x44, 0x44, 0x28, 0x10, 0x00}, - {0x00, 0x44, 0x44, 0x54, 0x54, 0x54, 0x28, 0x00}, - {0x00, 0x44, 0x28, 0x10, 0x10, 0x28, 0x44, 0x00}, - {0x00, 0x44, 0x44, 0x28, 0x10, 0x10, 0x10, 0x00}, - {0x00, 0x7c, 0x08, 0x10, 0x20, 0x40, 0x7c, 0x00}, - {0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38}, - {0x00, 0x40, 0x60, 0x30, 0x18, 0x0c, 0x04, 0x00}, - {0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38}, - {0x10, 0x28, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00}, - {0x00, 0x20, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c, 0x00}, - {0x00, 0x40, 0x40, 0x78, 0x44, 0x44, 0x78, 0x00}, - {0x00, 0x00, 0x38, 0x44, 0x40, 0x44, 0x38, 0x00}, - {0x00, 0x04, 0x04, 0x3c, 0x44, 0x44, 0x3c, 0x00}, - {0x00, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c, 0x00}, - {0x00, 0x00, 0x3c, 0x40, 0x40, 0x78, 0x40, 0x40}, - {0x00, 0x00, 0x3c, 0x44, 0x44, 0x3c, 0x04, 0x38}, - {0x00, 0x40, 0x40, 0x78, 0x44, 0x44, 0x44, 0x00}, - {0x00, 0x10, 0x00, 0x30, 0x10, 0x10, 0x7c, 0x00}, - {0x00, 0x04, 0x00, 0x04, 0x04, 0x04, 0x44, 0x38}, - {0x00, 0x40, 0x48, 0x50, 0x60, 0x50, 0x4c, 0x00}, - {0x00, 0x30, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00}, - {0x00, 0x00, 0x68, 0x54, 0x54, 0x54, 0x54, 0x00}, - {0x00, 0x00, 0x70, 0x4c, 0x44, 0x44, 0x44, 0x00}, - {0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x78, 0x44, 0x44, 0x78, 0x40, 0x40}, - {0x00, 0x00, 0x3c, 0x44, 0x44, 0x3c, 0x04, 0x0e}, - {0x00, 0x00, 0x58, 0x64, 0x40, 0x40, 0x40, 0x00}, - {0x00, 0x00, 0x38, 0x40, 0x38, 0x04, 0x78, 0x00}, - {0x00, 0x20, 0x7c, 0x20, 0x20, 0x20, 0x18, 0x00}, - {0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x44, 0x44, 0x44, 0x28, 0x10, 0x00}, - {0x00, 0x00, 0x44, 0x44, 0x54, 0x54, 0x28, 0x00}, - {0x00, 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00}, - {0x00, 0x00, 0x44, 0x44, 0x44, 0x3c, 0x04, 0x38}, - {0x00, 0x00, 0x7c, 0x08, 0x10, 0x20, 0x7c, 0x00}, - {0x18, 0x20, 0x20, 0x40, 0x40, 0x20, 0x20, 0x18}, - {0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}, - {0x30, 0x08, 0x08, 0x04, 0x04, 0x08, 0x08, 0x30}, - {0x00, 0x00, 0x34, 0x58, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x10, 0x28, 0x44, 0x44, 0x7c, 0x00}, - {0x00, 0x38, 0x44, 0x40, 0x44, 0x38, 0x10, 0x70}, - {0x00, 0x28, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38}, - {0x08, 0x10, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c}, - {0x10, 0x28, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c}, - {0x00, 0x28, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c}, - {0x20, 0x10, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c}, - {0x10, 0x28, 0x10, 0x38, 0x04, 0x3c, 0x44, 0x3c}, - {0x00, 0x00, 0x38, 0x40, 0x44, 0x38, 0x10, 0x70}, - {0x10, 0x28, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c}, - {0x00, 0x28, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c}, - {0x20, 0x10, 0x00, 0x38, 0x44, 0x78, 0x40, 0x3c}, - {0x00, 0x28, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c}, - {0x00, 0x28, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c}, - {0x20, 0x10, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c}, - {0x00, 0x28, 0x00, 0x38, 0x44, 0x7c, 0x44, 0x44}, - {0x10, 0x28, 0x10, 0x38, 0x44, 0x7c, 0x44, 0x44}, - {0x08, 0x10, 0x00, 0x7c, 0x40, 0x78, 0x40, 0x7c}, - {0x00, 0x00, 0x68, 0x14, 0x38, 0x50, 0x2c, 0x00}, - {0x00, 0x3c, 0x50, 0x58, 0x70, 0x50, 0x5c, 0x00}, - {0x10, 0x28, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38}, - {0x00, 0x28, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38}, - {0x20, 0x10, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38}, - {0x10, 0x28, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38}, - {0x20, 0x10, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38}, - {0x00, 0x28, 0x00, 0x44, 0x44, 0x3c, 0x04, 0x38}, - {0x28, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x38}, - {0x28, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38}, - {0x00, 0x10, 0x38, 0x44, 0x40, 0x44, 0x38, 0x10}, - {0x00, 0x18, 0x24, 0x70, 0x20, 0x20, 0x7c, 0x00}, - {0x00, 0x44, 0x28, 0x10, 0x38, 0x10, 0x38, 0x10}, - {0x00, 0x1c, 0x20, 0x78, 0x20, 0x78, 0x20, 0x1c}, - {0x00, 0x0c, 0x10, 0x38, 0x10, 0x10, 0x10, 0x60}, - {0x08, 0x10, 0x00, 0x38, 0x04, 0x3c, 0x44, 0x3c}, - {0x08, 0x10, 0x00, 0x30, 0x10, 0x10, 0x10, 0x7c}, - {0x08, 0x10, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38}, - {0x08, 0x10, 0x00, 0x44, 0x44, 0x44, 0x44, 0x38}, - {0x34, 0x58, 0x00, 0x70, 0x4c, 0x44, 0x44, 0x44}, - {0x34, 0x58, 0x00, 0x44, 0x64, 0x54, 0x4c, 0x44}, - {0x00, 0x38, 0x48, 0x34, 0x00, 0x78, 0x00, 0x00}, - {0x00, 0x30, 0x48, 0x30, 0x00, 0x78, 0x00, 0x00}, - {0x10, 0x00, 0x10, 0x20, 0x40, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x00, 0x7c, 0x40, 0x40, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x7c, 0x04, 0x04, 0x00, 0x00}, - {0x60, 0x24, 0x28, 0x16, 0x22, 0x44, 0x0f, 0x00}, - {0x60, 0x24, 0x28, 0x16, 0x2a, 0x4f, 0x02, 0x00}, - {0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00}, - {0x00, 0x12, 0x24, 0x48, 0x24, 0x12, 0x00, 0x00}, - {0x00, 0x48, 0x24, 0x12, 0x24, 0x48, 0x00, 0x00}, - {0x00, 0x11, 0x00, 0x44, 0x00, 0x11, 0x00, 0x44}, - {0xaa, 0x11, 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44}, - {0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55}, - {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0x10, 0xf0, 0x10, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0xf0, 0x10, 0xf0, 0x10, 0x10, 0x10}, - {0x28, 0x28, 0x28, 0xe8, 0x28, 0x28, 0x28, 0x28}, - {0x00, 0x00, 0x00, 0xf8, 0x28, 0x28, 0x28, 0x28}, - {0x00, 0x00, 0xf0, 0x10, 0xf0, 0x10, 0x10, 0x10}, - {0x28, 0x28, 0xe8, 0x08, 0xe8, 0x28, 0x28, 0x28}, - {0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28}, - {0x00, 0x00, 0xf8, 0x08, 0xe8, 0x28, 0x28, 0x28}, - {0x28, 0x28, 0xe8, 0x08, 0xf8, 0x00, 0x00, 0x00}, - {0x28, 0x28, 0x28, 0xf8, 0x00, 0x00, 0x00, 0x00}, - {0x10, 0x10, 0xf0, 0x10, 0xf0, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0xf0, 0x10, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0x10, 0x1f, 0x00, 0x00, 0x00, 0x00}, - {0x10, 0x10, 0x10, 0xff, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0xff, 0x10, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0x10, 0x1f, 0x10, 0x10, 0x10, 0x10}, - {0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00}, - {0x10, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0x1f, 0x10, 0x1f, 0x10, 0x10, 0x10}, - {0x28, 0x28, 0x28, 0x2f, 0x28, 0x28, 0x28, 0x28}, - {0x28, 0x28, 0x2f, 0x20, 0x3f, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x3f, 0x20, 0x2f, 0x28, 0x28, 0x28}, - {0x28, 0x28, 0xef, 0x00, 0xff, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0xff, 0x00, 0xef, 0x28, 0x28, 0x28}, - {0x28, 0x28, 0x2f, 0x20, 0x2f, 0x28, 0x28, 0x28}, - {0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00}, - {0x28, 0x28, 0xef, 0x00, 0xef, 0x28, 0x28, 0x28}, - {0x10, 0x10, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00}, - {0x28, 0x28, 0x28, 0xff, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0xff, 0x00, 0xff, 0x10, 0x10, 0x10}, - {0x00, 0x00, 0x00, 0xff, 0x28, 0x28, 0x28, 0x28}, - {0x28, 0x28, 0x28, 0x3f, 0x00, 0x00, 0x00, 0x00}, - {0x10, 0x10, 0x1f, 0x10, 0x1f, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x1f, 0x10, 0x1f, 0x10, 0x10, 0x10}, - {0x00, 0x00, 0x00, 0x3f, 0x28, 0x28, 0x28, 0x28}, - {0x28, 0x28, 0x28, 0xff, 0x28, 0x28, 0x28, 0x28}, - {0x10, 0x10, 0xff, 0x10, 0xff, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0x10, 0xf0, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x1f, 0x10, 0x10, 0x10, 0x10}, - {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, - {0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff}, - {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0}, - {0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f}, - {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x34, 0x48, 0x48, 0x48, 0x34, 0x00}, - {0x00, 0x30, 0x48, 0x50, 0x48, 0x44, 0x5c, 0x40}, - {0x00, 0x7c, 0x44, 0x40, 0x40, 0x40, 0x40, 0x00}, - {0x00, 0x00, 0x7c, 0x28, 0x28, 0x28, 0x28, 0x00}, - {0x7c, 0x44, 0x20, 0x10, 0x20, 0x44, 0x7c, 0x00}, - {0x00, 0x00, 0x3c, 0x48, 0x48, 0x48, 0x30, 0x00}, - {0x00, 0x00, 0x44, 0x44, 0x44, 0x78, 0x40, 0x40}, - {0x00, 0x34, 0x58, 0x10, 0x10, 0x14, 0x08, 0x00}, - {0x38, 0x10, 0x38, 0x44, 0x44, 0x38, 0x10, 0x38}, - {0x38, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x38, 0x00}, - {0x38, 0x44, 0x44, 0x44, 0x44, 0x28, 0x6c, 0x00}, - {0x78, 0x20, 0x18, 0x24, 0x44, 0x44, 0x38, 0x00}, - {0x00, 0x00, 0x28, 0x54, 0x54, 0x28, 0x00, 0x00}, - {0x00, 0x04, 0x28, 0x54, 0x54, 0x28, 0x40, 0x00}, - {0x00, 0x00, 0x3c, 0x40, 0x38, 0x40, 0x3c, 0x00}, - {0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x40}, - {0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x7c, 0x00}, - {0x00, 0x00, 0x10, 0x38, 0x10, 0x00, 0x38, 0x00}, - {0x20, 0x10, 0x08, 0x10, 0x20, 0x00, 0x38, 0x00}, - {0x08, 0x10, 0x20, 0x10, 0x08, 0x00, 0x38, 0x00}, - {0x0c, 0x12, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}, - {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x90, 0x60}, - {0x00, 0x00, 0x10, 0x00, 0x7c, 0x00, 0x10, 0x00}, - {0x00, 0x00, 0x32, 0x4c, 0x00, 0x32, 0x4c, 0x00}, - {0x00, 0x18, 0x24, 0x18, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x38, 0x38, 0x38, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00}, - {0x00, 0x0f, 0x08, 0x08, 0x08, 0x48, 0x28, 0x18}, - {0x30, 0x2c, 0x24, 0x24, 0x00, 0x00, 0x00, 0x00}, - {0x18, 0x08, 0x10, 0x3c, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x3c, 0x3c, 0x3c, 0x3c, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} +u32 bd_font[] = { + 0x00000000, 0x00000000, 0x00002400, 0x423c0000, + 0x00002400, 0x3c420000, 0x0000363e, 0x3e1c0800, + 0x00081c3e, 0x3e1c0800, 0x001c1c3e, 0x363e081c, + 0x00081c3e, 0x3e3e081c, 0x00000018, 0x18000000, + 0x7e7e7e66, 0x667e7e7e, 0x00001824, 0x24180000, + 0x7e7e665a, 0x5a667e7e, 0x00081c3e, 0x081c221c, + 0x001c221c, 0x08083e08, 0x00183828, 0x08080c0c, + 0x003c2424, 0x24343606, 0x00082208, 0x1c082208, + 0x040c1c3c, 0x1c0c0400, 0x2030383c, 0x38302000, + 0x081c3e08, 0x083e1c08, 0x00141414, 0x14140014, + 0x003c2a2a, 0x2c282828, 0x0038043c, 0x423c201e, + 0x00000000, 0x7e000000, 0x081c3e08, 0x3e1c083e, + 0x081c3e08, 0x08080808, 0x08080808, 0x083e1c08, + 0x00001030, 0x7e301000, 0x0000080c, 0x7e0c0800, + 0x00000000, 0x0002023e, 0x00002466, 0xff662400, + 0x0008081c, 0x1c3e3e00, 0x003e3e1c, 0x1c080800, + 0x00000000, 0x00000000, 0x00080808, 0x08080008, + 0x14141400, 0x00000000, 0x0000143e, 0x143e1400, + 0x00081c02, 0x1c201e08, 0x00002616, 0x08343200, + 0x00081408, 0x34122c00, 0x08080800, 0x00000000, + 0x08040404, 0x04040408, 0x08101010, 0x10101008, + 0x00001408, 0x3e081400, 0x00000808, 0x3e080800, + 0x00000000, 0x000c0804, 0x00000000, 0x3e000000, + 0x00000000, 0x000c0c00, 0x00203018, 0x0c060200, + 0x00001c22, 0x2a221c00, 0x00000c0a, 0x08083e00, + 0x00001c20, 0x1c023e00, 0x00001c22, 0x18221c00, + 0x00001018, 0x143e1000, 0x00003e02, 0x1e201e00, + 0x00001c02, 0x1e221c00, 0x00003e20, 0x10080400, + 0x00001c22, 0x1c221c00, 0x00001c22, 0x3c201c00, + 0x00000c0c, 0x000c0c00, 0x00000c0c, 0x000c0804, + 0x00100804, 0x04081000, 0x0000003e, 0x003e0000, + 0x00040810, 0x10080400, 0x001c2220, 0x10080008, + 0x001c322a, 0x1a021c00, 0x001c2222, 0x3e222200, + 0x001e221e, 0x22221e00, 0x001c2202, 0x02221c00, + 0x000e1222, 0x22120e00, 0x003e021e, 0x02023e00, + 0x003e021e, 0x02020200, 0x001c2202, 0x32221c00, + 0x00222222, 0x3e222200, 0x003e0808, 0x08083e00, + 0x00202020, 0x22221c00, 0x0022120a, 0x0e122200, + 0x00020202, 0x02023e00, 0x0022362a, 0x22222200, + 0x0022262a, 0x32222200, 0x001c2222, 0x22221c00, + 0x001e2222, 0x1e020200, 0x001c2222, 0x2a122c00, + 0x001e2222, 0x1e122200, 0x001c021c, 0x20221c00, + 0x003e0808, 0x08080800, 0x00222222, 0x22221c00, + 0x00222222, 0x22140800, 0x0022222a, 0x2a2a1400, + 0x00221408, 0x08142200, 0x00222214, 0x08080800, + 0x003e1008, 0x04023e00, 0x1c040404, 0x0404041c, + 0x0002060c, 0x18302000, 0x1c101010, 0x1010101c, + 0x08142200, 0x00000000, 0x00000000, 0x00003e00, + 0x00040810, 0x00000000, 0x00001c20, 0x3c223c00, + 0x0002021e, 0x22221e00, 0x00001c22, 0x02221c00, + 0x0020203c, 0x22223c00, 0x00001c22, 0x1e023c00, + 0x00003c02, 0x021e0202, 0x00003c22, 0x223c201c, + 0x0002021e, 0x22222200, 0x0008000c, 0x08083e00, + 0x00200020, 0x2020221c, 0x0002120a, 0x060a3200, + 0x000c0808, 0x08083e00, 0x0000162a, 0x2a2a2a00, + 0x00000e32, 0x22222200, 0x00001c22, 0x22221c00, + 0x00001e22, 0x221e0202, 0x00003c22, 0x223c2070, + 0x00001a26, 0x02020200, 0x00001c02, 0x1c201e00, + 0x00043e04, 0x04041800, 0x00002222, 0x22221c00, + 0x00002222, 0x22140800, 0x00002222, 0x2a2a1400, + 0x00002214, 0x08142200, 0x00002222, 0x223c201c, + 0x00003e10, 0x08043e00, 0x18040402, 0x02040418, + 0x00080808, 0x08080808, 0x0c101020, 0x2010100c, + 0x00002c1a, 0x00000000, 0x00000814, 0x22223e00, + 0x001c2202, 0x221c080e, 0x00140022, 0x2222221c, + 0x1008001c, 0x221e023c, 0x0814001c, 0x203c223c, + 0x0014001c, 0x203c223c, 0x0408001c, 0x203c223c, + 0x0814081c, 0x203c223c, 0x00001c02, 0x221c080e, + 0x0814001c, 0x221e023c, 0x0014001c, 0x221e023c, + 0x0408001c, 0x221e023c, 0x0014000c, 0x0808083e, + 0x0014000c, 0x0808083e, 0x0408000c, 0x0808083e, + 0x0014001c, 0x223e2222, 0x0814081c, 0x223e2222, + 0x1008003e, 0x021e023e, 0x00001628, 0x1c0a3400, + 0x003c0a1a, 0x0e0a3a00, 0x0814001c, 0x2222221c, + 0x0014001c, 0x2222221c, 0x0408001c, 0x2222221c, + 0x08140022, 0x2222221c, 0x04080022, 0x2222221c, + 0x00140022, 0x223c201c, 0x14001c22, 0x2222221c, + 0x14002222, 0x2222221c, 0x00081c22, 0x02221c08, + 0x0018240e, 0x04043e00, 0x00221408, 0x1c081c08, + 0x0038041e, 0x041e0438, 0x0030081c, 0x08080806, + 0x1008001c, 0x203c223c, 0x1008000c, 0x0808083e, + 0x1008001c, 0x2222221c, 0x10080022, 0x2222221c, + 0x2c1a000e, 0x32222222, 0x2c1a0022, 0x262a3222, + 0x001c122c, 0x001e0000, 0x000c120c, 0x001e0000, + 0x08000804, 0x02221c00, 0x0000003e, 0x02020000, + 0x0000003e, 0x20200000, 0x06241468, 0x4422f000, + 0x06241468, 0x54f24000, 0x08000808, 0x08080800, + 0x00482412, 0x24480000, 0x00122448, 0x24120000, + 0x00880022, 0x00880022, 0x55885522, 0x55885522, + 0x55aa55aa, 0x55aa55aa, 0x08080808, 0x08080808, + 0x0808080f, 0x08080808, 0x08080f08, 0x0f080808, + 0x14141417, 0x14141414, 0x0000001f, 0x14141414, + 0x00000f08, 0x0f080808, 0x14141710, 0x17141414, + 0x14141414, 0x14141414, 0x00001f10, 0x17141414, + 0x14141710, 0x1f000000, 0x1414141f, 0x00000000, + 0x08080f08, 0x0f000000, 0x0000000f, 0x08080808, + 0x080808f8, 0x00000000, 0x080808ff, 0x00000000, + 0x000000ff, 0x08080808, 0x080808f8, 0x08080808, + 0x000000ff, 0x00000000, 0x080808ff, 0x08080808, + 0x0808f808, 0xf8080808, 0x141414f4, 0x14141414, + 0x1414f404, 0xfc000000, 0x0000fc04, 0xf4141414, + 0x1414f700, 0xff000000, 0x0000ff00, 0xf7141414, + 0x1414f404, 0xf4141414, 0x0000ff00, 0xff000000, + 0x1414f700, 0xf7141414, 0x0808ff00, 0xff000000, + 0x141414ff, 0x00000000, 0x0000ff00, 0xff080808, + 0x000000ff, 0x14141414, 0x141414fc, 0x00000000, + 0x0808f808, 0xf8000000, 0x0000f808, 0xf8080808, + 0x000000fc, 0x14141414, 0x141414ff, 0x14141414, + 0x0808ff08, 0xff080808, 0x0808080f, 0x00000000, + 0x000000f8, 0x08080808, 0xffffffff, 0xffffffff, + 0x00000000, 0xffffffff, 0x0f0f0f0f, 0x0f0f0f0f, + 0xf0f0f0f0, 0xf0f0f0f0, 0xffffffff, 0x00000000, + 0x00002c12, 0x12122c00, 0x000c120a, 0x12223a02, + 0x003e2202, 0x02020200, 0x00003e14, 0x14141400, + 0x3e220408, 0x04223e00, 0x00003c12, 0x12120c00, + 0x00002222, 0x221e0202, 0x002c1a08, 0x08281000, + 0x1c081c22, 0x221c081c, 0x1c22223e, 0x22221c00, + 0x1c222222, 0x22143600, 0x1e041824, 0x22221c00, + 0x0000142a, 0x2a140000, 0x0020142a, 0x2a140200, + 0x00003c02, 0x1c023c00, 0x001c2222, 0x22222202, + 0x00003e00, 0x3e003e00, 0x0000081c, 0x08001c00, + 0x04081008, 0x04001c00, 0x10080408, 0x10001c00, + 0x30480808, 0x08080808, 0x08080808, 0x08080906, + 0x00000800, 0x3e000800, 0x00004c32, 0x004c3200, + 0x00182418, 0x00000000, 0x00001c1c, 0x1c000000, + 0x00000018, 0x18000000, 0x00f01010, 0x10121418, + 0x0c342424, 0x00000000, 0x1810083c, 0x00000000, + 0x00003c3c, 0x3c3c0000, 0x00000000, 0x00000000, }; diff --git a/src/common.h b/src/common.h index 6047e5f..669fb6e 100644 --- a/src/common.h +++ b/src/common.h @@ -47,16 +47,10 @@ #define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D // Registers to control of BG layers. -#define BG_CTRL_0 *((vu16*)(0x04000008 + 0x0002 * 0)) -#define BG_CTRL_1 *((vu16*)(0x04000008 + 0x0002 * 1)) -#define BG_CTRL_2 *((vu16*)(0x04000008 + 0x0002 * 2)) -#define BG_CTRL_3 *((vu16*)(0x04000008 + 0x0002 * 3)) +#define BG_CTRL(N) *((vu16*)(0x04000008 + 0x0002 * (N))) // Bits to control the background. -#define BG_PRIORITY_0 0x0 -#define BG_PRIORITY_1 0x1 -#define BG_PRIORITY_2 0x2 -#define BG_PRIORITY_3 0x3 +#define BG_PRIORITY(N) ((N) & 0x3) #define BG_CHARBLOCK(N) ((N) << 2) #define BG_MOSAIC (1 << 6) #define BG_HIGH_COLOR (1 << 7) @@ -76,20 +70,6 @@ #define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2)) #define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3)) -// Screenblocks for BGs. -typedef u16 ScreenBlock[1024]; -#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM) - -// Screenblock entry bits. -#define SCREENBLOCK_ENTRY_H_FLIP (1 << 0xA) -#define SCREENBLOCK_ENTRY_V_FLIP (1 << 0xB) -#define SCREENBLOCK_ENTRY_PAL(N) ((N) << 0xC) - -size_t se_index(size_t tile_x, size_t tile_y, size_t map_width) { - size_t sbb = ((tile_x >> 5) + (tile_y >> 5) * (map_width >> 5)); - return sbb * 1024 + ((tile_x & 31) + (tile_y & 31) * 32); -} - // Screen settings. #define SCREEN_WIDTH 240 #define SCREEN_HEIGHT 160 @@ -113,6 +93,23 @@ typedef struct Tile { typedef Tile TileBlock[512]; #define TILE_MEM ((TileBlock*) MEM_VRAM) +// Screenblocks and charblocks for backgrounds. +typedef u16 ScreenBlock[1024]; +typedef Tile CharBlock[512]; +#define CHARBLOCK_MEM ((CharBlock*)MEM_VRAM) +#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM) + +// Screenblock entry bits. +#define SCREENBLOCK_ENTRY_H_FLIP (1 << 0xA) +#define SCREENBLOCK_ENTRY_V_FLIP (1 << 0xB) +#define SCREENBLOCK_ENTRY_PAL(N) ((N) << 0xC) + +size_t se_index(size_t tile_x, size_t tile_y, size_t map_width) { + size_t sbb = ((tile_x >> 5) + (tile_y >> 5) * (map_width >> 5)); + return sbb * 1024 + ((tile_x & 31) + (tile_y & 31) * 32); +} + + // We can treat the screen as a HxW matrix. With the following macro we can // write a pixel to the screen at the (x, y) position using: // @@ -293,4 +290,30 @@ key_hold(u32 key) { // Check if the given key/button is currently pressed. #define KEY_PRESSED(key) (~(KEY_INPUTS) & key) +// Back/unpack bits. +static inline u32 +unpack_1bb(u8 hex) { + const u32 conversion_u32[16] = { + 0x00000000, 0x00000001, 0x00000010, 0x00000011, + 0x00000100, 0x00000101, 0x00000110, 0x00000111, + 0x00001000, 0x00001001, 0x00001010, 0x00001011, + 0x00001100, 0x00001101, 0x00001110, 0x00001111, + }; + u8 low = hex & 0xF; + u8 high = (hex >> 4) & 0xF; + return (conversion_u32[high] << 16) | conversion_u32[low]; +} + +// Unpack N tiles packed at 1bpp. +unpack_tiles(u32 *src, u32 *dst, size_t n_tiles) { + u32 *target_src = src + n_tiles * 2; + while (src != target_src) { + *dst++ = unpack_1bb((*src >> 24) & 0xFF); + *dst++ = unpack_1bb((*src >> 16) & 0xFF); + *dst++ = unpack_1bb((*src >> 8) & 0xFF); + *dst++ = unpack_1bb(*src & 0xFF); + src++; + } +} + #endif // GBAEXP_COMMON_H diff --git a/src/main.c b/src/main.c index 793baa8..3bd5865 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "gba-buttons.c" #include "background-tiles.c" #include "sprites.h" +#include "text.h" // // Main functions. @@ -11,62 +12,25 @@ // TODO: Cleanup OBJ/OAM memory copying and access. // -#define CBB_0 0 -#define SBB_0 28 - -#define CROSS_TX 15 -#define CROSS_TY 10 - -u16 *bg0_map = SCREENBLOCK_MEM[SBB_0]; - -void -init_map() { - // initialize a background - BG_CTRL_0 = BG_CHARBLOCK(CBB_0) | BG_SCREENBLOCK(SBB_0) | BG_SIZE(3); - BG_H_SCROLL_0 = 0; - BG_V_SCROLL_0 = 0; - - // (1) create the tiles: basic tile and a cross - const Tile tiles[2] = { - {{0x11111111, 0x01111111, 0x01111111, 0x01111111, - 0x01111111, 0x01111111, 0x01111111, 0x00000001}}, - {{0x00000000, 0x00100100, 0x01100110, 0x00011000, - 0x00011000, 0x01100110, 0x00100100, 0x00000000}}, - }; - TILE_MEM[CBB_0][0] = tiles[0]; - TILE_MEM[CBB_0][1] = tiles[1]; - - // (2) create a palette - PAL_BANK_BG[0][1] = rgb15(31, 0, 0); - PAL_BANK_BG[1][1] = rgb15( 0, 31, 0); - PAL_BANK_BG[2][1] = rgb15( 0, 0, 31); - PAL_BANK_BG[3][1] = rgb15(16, 16, 16); - - // (3) Create a map: four contingent blocks of - // 0x0000, 0x1000, 0x2000, 0x3000. - u16 *pse = bg0_map; - for(int i = 0; i < 4; i++) { - for(int j = 0; j < 32 * 32; j++) { - *pse++ = SCREENBLOCK_ENTRY_PAL(i) | 0; - } - } -} int main(void) { - init_map(); // Configure the display in mode 0 to show OBJs, where tile memory is // sequential. DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0; - u32 tx, ty, se_curr, se_prev= CROSS_TY * 32 + CROSS_TX; - - bg0_map[se_prev]++; // initial position of cross - // Initialize sprite button overlay. init_sprite_pal(0, COLOR_WHITE); init_sprites(0); init_button_sprites(); + // Initialize text engine. + txt_init(0, COLOR_RED, 0); + txt_position(0, 6); + txt_printf("Strings: %s\n", "Hello World"); + txt_printf("Signed ints: %d\n", -100000); + txt_printf("Unsigned ints: %u\n", 1234567); + txt_printf("Hex values: 0x%x\n", 1024); + int frame_counter = 0; int x = 0; int y = 0; @@ -74,6 +38,9 @@ int main(void) { wait_vsync(); poll_keys(); + txt_position(0, 0); + txt_printf("Frame counter: %d\n", frame_counter); + if (key_hold(KEY_DOWN)) { y += 3; } @@ -86,19 +53,51 @@ int main(void) { if (key_hold(KEY_RIGHT)) { x += 3; } - tx = ((x >> 3) + CROSS_TX) & 0x3F; - ty = ((y >> 3) + CROSS_TY) & 0x3F; - se_curr = se_index(tx, ty, 64); - if(se_curr != se_prev) { - bg0_map[se_prev]--; - bg0_map[se_curr]++; - se_prev= se_curr; + if (key_pressed(KEY_A)) { + txt_clear_line(); + txt_printf("Pressed key: A"); + } + if (key_pressed(KEY_B)) { + txt_clear_line(); + txt_printf("Pressed key: B"); + } + if (key_pressed(KEY_L)) { + txt_clear_line(); + txt_printf("Pressed key: L"); + } + if (key_pressed(KEY_R)) { + txt_clear_line(); + txt_printf("Pressed key: R"); + } + if (key_pressed(KEY_START)) { + txt_clear_line(); + txt_printf("Pressed key: Start"); + } + if (key_pressed(KEY_SELECT)) { + txt_clear_line(); + txt_printf("Pressed key: Select"); + } + if (key_pressed(KEY_UP)) { + txt_clear_line(); + txt_printf("Pressed key: Up"); + } + if (key_pressed(KEY_DOWN)) { + txt_clear_line(); + txt_printf("Pressed key: Down"); + } + if (key_pressed(KEY_LEFT)) { + txt_clear_line(); + txt_printf("Pressed key: Left"); + } + if (key_pressed(KEY_RIGHT)) { + txt_clear_line(); + txt_printf("Pressed key: Right"); } frame_counter++; - BG_H_SCROLL_0 = x; - BG_V_SCROLL_0 = y; + // BG_H_SCROLL_0 = x; + // BG_V_SCROLL_0 = y; update_button_sprites(); }; diff --git a/src/sprites.h b/src/sprites.h index e249e95..88ca787 100644 --- a/src/sprites.h +++ b/src/sprites.h @@ -3,19 +3,6 @@ #include "common.h" -u32 -unpack_1bb(u8 hex) { - const u32 conversion_u32[16] = { - 0x00000000, 0x00000001, 0x00000010, 0x00000011, - 0x00000100, 0x00000101, 0x00000110, 0x00000111, - 0x00001000, 0x00001001, 0x00001010, 0x00001011, - 0x00001100, 0x00001101, 0x00001110, 0x00001111, - }; - u8 low = hex & 0xF; - u8 high = (hex >> 4) & 0xF; - return (conversion_u32[high] << 16) | conversion_u32[low]; -} - typedef struct Sprite { // A unique sprite identifier. size_t id; @@ -54,14 +41,7 @@ load_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) { size_t load_packed_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) { - size_t counter = 0; - for (size_t i = 0; i < 8 * n_tiles * n_frames / 4; ++i) { - u32 hex = sprite_data[i]; - sprite_memory[counter++] = unpack_1bb((hex >> 24) & 0xFF); - sprite_memory[counter++] = unpack_1bb((hex >> 16) & 0xFF); - sprite_memory[counter++] = unpack_1bb((hex >> 8) & 0xFF); - sprite_memory[counter++] = unpack_1bb((hex) & 0xFF); - } + unpack_tiles(sprite_data, sprite_memory, n_tiles * n_frames); sprite_memory += 8 * n_tiles * n_frames; Sprite sprite = { .id = sprite_counter, diff --git a/src/text.h b/src/text.h new file mode 100644 index 0000000..e38c220 --- /dev/null +++ b/src/text.h @@ -0,0 +1,142 @@ +#ifndef GBAEXP_TILES_H +#define GBAEXP_TILES_H + +#include + +#include "common.h" +#include "bd-font.c" + +typedef struct TextEngine { + // Currently working on tiled backgrounds only. The X and Y positions + // correspond to the tile X and Y starting from the top left of the screen. + // For a 240x160 screen, we have 30x20 tiles available. + size_t cursor_x; + size_t cursor_y; + // Pointer to the screenblock being used for writing to the screen. + u16 *screenblock; +} TextEngine; + +static TextEngine text_engine = {0}; + +void +txt_putc(char c) { + if (c == '\0') { + return; + } + if (c == '\n') { + text_engine.cursor_x = 0; + text_engine.cursor_y++; + } else { + text_engine.screenblock[text_engine.cursor_x + 32 * text_engine.cursor_y] = c; + text_engine.cursor_x++; + if (text_engine.cursor_x >= 30) { + text_engine.cursor_x = 0; + text_engine.cursor_y++; + } + } + if (text_engine.cursor_y >= 20) { + text_engine.cursor_y = 0; + } +} + +static inline void +txt_puts(char *msg) { + while (*msg) { + txt_putc(*msg++); + } +} + +void +txt_init(size_t bg, Color clr, size_t cb_idx) { + // The screenblock for the tile map should start after the tile memory + // (MEM_VRAM + 0x2000 for 256 characters). Since each screenblock is + // composed of 1024 screenblock entries of u16 (2 bytes), we need an + // screenblock index offset of 8192 / (1024 * 2) = 4 screen blocks. + size_t sb_idx = cb_idx * 8 + 4; + + // Set the background parameters for the text layer. + BG_CTRL(bg) = BG_CHARBLOCK(cb_idx) | BG_SCREENBLOCK(sb_idx) | BG_PRIORITY(3); + + // Load font data in video memory. Each character is unpacked into a tile of + // 8 32bit values (4bpp), meaning that for the full ASCII set of 256 + // characters, we need 8192 bytes of VRAM (8 * 4 * 256). + unpack_tiles(&bd_font, &CHARBLOCK_MEM[cb_idx], 256); + + // Load palette color. + PAL_BUFFER_BG[1] = clr; + + // Update text_engine variables. + text_engine.screenblock = SCREENBLOCK_MEM[sb_idx]; +} + +// Print text to the screen with formatting. +void +txt_printf(char *msg, ...) { + va_list arg_list; + va_start(arg_list, msg); + char c; + char prev_c; + while ((c = *msg++) != '\0') { + if (c != '%') { + txt_putc(c); + prev_c = c; + continue; + } + c = *msg++; + switch (c) { + case 's': { + txt_puts(va_arg(arg_list, char *)); + } break; + case 'd': { + char buf[32] = {0}; + int x = va_arg(arg_list, int); + sprintf(buf, "%d", x); + txt_puts(buf); + } break; + case 'u': { + char buf[32] = {0}; + unsigned int x = va_arg(arg_list, unsigned int); + sprintf(buf, "%u", x); + txt_puts(buf); + } break; + case 'x': { + char buf[32] = {0}; + unsigned int x = va_arg(arg_list, unsigned int); + sprintf(buf, "%x", x); + txt_puts(buf); + } break; + default: { + txt_putc('%'); + txt_putc(c); + } break; + } + } +} + +void +txt_clear_line(void) { + for (size_t i = 0; i < 30; ++i) { + text_engine.screenblock[i + 32 * text_engine.cursor_y] = ' '; + } + text_engine.cursor_x = 0; +} + +void +txt_clear_screen(void) { + for (size_t j = 0; j < 20; ++j) { + for (size_t i = 0; i < 30; ++i) { + text_engine.screenblock[i + 32 * j] = ' '; + } + } + text_engine.cursor_x = 0; + text_engine.cursor_y = 0; +} + +void +txt_position(size_t tile_x, size_t tile_y) { + text_engine.cursor_x = tile_x; + text_engine.cursor_y = tile_y; +} + +#endif // GBAEXP_TILES_H + -- cgit v1.2.1