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/common.h | 67 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 22 deletions(-) (limited to 'src/common.h') 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 -- cgit v1.2.1