From 74d6b52636ecdd560995f22dff5b29a59fc62d5d Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 22 Apr 2021 17:34:38 +0200 Subject: Test background scrolling with input control --- src/main.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 8 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 0af41f8..4dd2554 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include "shorthand.h" #include "bd-font.c" #include "gba-buttons.c" +#include "background-tiles.c" // // Memory sections. @@ -46,10 +47,34 @@ #define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D // Registers to control of BG layers. -#define BG_CTRL_0 *((vu16*)(0x04000008 + 0x0000)) -#define BG_CTRL_1 *((vu16*)(0x04000008 + 0x0002)) -#define BG_CTRL_2 *((vu16*)(0x04000008 + 0x0004)) -#define BG_CTRL_3 *((vu16*)(0x04000008 + 0x0006)) +#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)) + +// 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_CHARBLOCK(N) ((N) << 2) +#define BG_MOSAIC (1 << 6) +#define BG_HIGH_COLOR (1 << 7) +#define BG_SCREENBLOCK(N) ((N) << 8) +#define BG_AFFINE (1 << 0xD) +#define BG_SIZE(N) ((N) << 0xE) + +// BG registers for horizontal displacement. +#define BG_H_SCROLL_0 *((vu16*)(0x04000010 + 0x0004 * 0)) +#define BG_H_SCROLL_1 *((vu16*)(0x04000010 + 0x0004 * 1)) +#define BG_H_SCROLL_2 *((vu16*)(0x04000010 + 0x0004 * 2)) +#define BG_H_SCROLL_3 *((vu16*)(0x04000010 + 0x0004 * 3)) + +// BG registers for vertical displacement. +#define BG_V_SCROLL_0 *((vu16*)(0x04000012 + 0x0004 * 0)) +#define BG_V_SCROLL_1 *((vu16*)(0x04000012 + 0x0004 * 1)) +#define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2)) +#define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3)) // Screen settings. #define SCREEN_WIDTH 240 @@ -69,9 +94,11 @@ typedef struct Tile { } Tile; typedef Tile TileBlock[512]; - #define TILE_MEM ((TileBlock*) MEM_VRAM) +typedef u16 ScreenBlock[1024]; +#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM) + // 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: // @@ -524,9 +551,6 @@ load_packed_sprite_data(u32 *sprite_data, size_t n_tiles, size_t n_frames) { } int main(void) { - // Configure the display in mode 0 to show OBJs, where tile memory is - // sequential. - DISP_CTRL = DISP_MODE_0 | DISP_ENABLE_SPRITES | DISP_BG_2; // Add colors to the sprite color palette. Tiles with color number 0 are // treated as transparent. @@ -543,6 +567,21 @@ int main(void) { sprite_tile_counter = 0; sprite_memory = &TILE_MEM[4][sprite_tile_counter]; + // Load background palette. + memcpy(&PAL_BUFFER_BG[0], &bg_palette, 512); + memcpy(&TILE_MEM[0][0], bg_data, 3168); + memcpy(&SCREENBLOCK_MEM[30][0], bg_map, 2048); + + // Configure BG0 to use 4bpp, 64x32 tile map in charblock 0 and screenblock + // 31. + BG_CTRL_0 = BG_CHARBLOCK(0) | BG_SCREENBLOCK(30) | BG_SIZE(0); + BG_H_SCROLL_0 = 0; + BG_V_SCROLL_0 = 0; + + // 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; + // Initialize the A/B button sprites. int buttons_x = SCREEN_WIDTH / 2; int buttons_y = SCREEN_HEIGHT / 2; @@ -635,6 +674,8 @@ int main(void) { OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start; int frame_counter = 0; + int x = 0; + int y = 0; while(true) { wait_vsync(); poll_keys(); @@ -784,6 +825,19 @@ int main(void) { } } + if (key_hold(KEY_DOWN)) { + y += 3; + } + if (key_hold(KEY_UP)) { + y -= 3; + } + if (key_hold(KEY_LEFT)) { + x -= 3; + } + if (key_hold(KEY_RIGHT)) { + x += 3; + } + OBJ_ATTR_2(btn_b.id) = sprites[btn_b.id].tile_start + animation_states[btn_b.state]->tile_offsets[btn_b.frame]; OBJ_ATTR_2(btn_a.id) = sprites[btn_a.id].tile_start + animation_states[btn_a.state]->tile_offsets[btn_a.frame]; OBJ_ATTR_2(btn_up.id) = sprites[btn_up.id].tile_start + animation_states[btn_up.state]->tile_offsets[btn_up.frame]; @@ -793,6 +847,8 @@ int main(void) { OBJ_ATTR_2(btn_l.id) = sprites[btn_l.id].tile_start + animation_states[btn_l.state]->tile_offsets[btn_l.frame]; OBJ_ATTR_2(btn_r.id) = sprites[btn_r.id].tile_start + animation_states[btn_r.state]->tile_offsets[btn_r.frame]; frame_counter++; + BG_H_SCROLL_0 = x; + BG_V_SCROLL_0 = y; }; return 0; -- cgit v1.2.1