From b552e242609a5d3b9debd7927aa9c1c2186ce847 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 18 Apr 2021 21:01:30 +0200 Subject: Add a bouncing animation for ASCII sprites --- src/main.c | 135 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 69 insertions(+), 66 deletions(-) diff --git a/src/main.c b/src/main.c index 17c6654..e39551c 100644 --- a/src/main.c +++ b/src/main.c @@ -442,31 +442,41 @@ copy_font_to_tile_memory(Tile *tile) { } } +static int bouncing_animation[] = { + 0, 0, 0, + 0, 0, 0, + 0, 0, 0, + 0, 0, 0, + 1, 1, 1, + 3, 3, 3, + 5, 5, 5, + 5, 5, 5, + 7, 7, 7, + 8, 8, 8, + 7, 7, 7, + 5, 5, 5, + 5, 5, 5, + 3, 3, 3, + 1, 1, 1, + 0, 0, 0, + 0, 0, 0, + 0, 0, 0, + 0, 0, 0, +}; + +typedef struct FontSprite { + int x; + int y; + int animation_state; + u32 tile_index; + u32 pal_bank; +} FontSprite; + int main(void) { // Configure the display in mode 0 to show OBJs, where tile memory is // sequential. DISP_CTRL = DISP_MODE_3 | DISP_OBJ | DISP_OBJ_1D | DISP_BG_2; - // Create two 4bpp tiles, one filled with color 1 and another with color 2. - Tile *tile_mem = &TILE_MEM[4][512]; - // size_t n_tiles = 8; - // u32 colors[] = { - // 0x11111111, - // 0x22222222, - // 0x33333333, - // 0x44444444, - // 0x55555555, - // 0x66666666, - // 0x77777777, - // 0x88888888, - // }; - // for (size_t j = 0; j < n_tiles; ++j) { - // for (size_t i = j * 8; i < 8 + 8 * j; ++i) { - // tile_mem->data[i] = colors[j]; - // } - // } - copy_font_to_tile_memory(tile_mem); - // Add colors to the sprite color palette. Tiles with color number 0 are // treated as transparent. PAL_BUFFER_SPRITES[1] = COLOR_WHITE; @@ -478,82 +488,75 @@ int main(void) { PAL_BUFFER_SPRITES[7] = COLOR_CYAN; PAL_BUFFER_SPRITES[8] = COLOR_BLUE; - int x_a = 100; - int y_a = 100; - int tile_id_a = 512; - int x_b = 50; - int y_b = 50; - int tile_id_b = 513; - // Initialize all attributes by disabling rendering. If we don't do this, // glitches may appear. for (size_t i = 0; i < 128; ++i) { OBJ_ATTR_0(i) = (1 << 9); } - OBJ_ATTR_0(0) = y_a; - OBJ_ATTR_1(0) = x_a | (1 << 14); - OBJ_ATTR_2(0) = tile_id_a; + size_t initial_tile = 512; + Tile *tile_mem = &TILE_MEM[4][initial_tile]; - OBJ_ATTR_0(1) = y_b; - OBJ_ATTR_1(1) = x_b; - OBJ_ATTR_2(1) = tile_id_b; + // Load bd-font as tiles. The full extended ASCII range (256 characters) + // will be available as an offset of the initial tile. For example, the + // uppercase A letter will be at tile index of 512 + 65. + copy_font_to_tile_memory(tile_mem); - draw_logo(); + // Initialize character sprites for all font chars. + FontSprite font_sprites[256] = {0}; + size_t x = 49; + size_t y = 0; + for (size_t i = 0; i < 256; ++i) { + font_sprites[i].x = x; + font_sprites[i].y = y; + font_sprites[i].tile_index = initial_tile + i; + font_sprites[i].animation_state = i % 56; + if ((i % 16) == 0) { + y += 16; + x = 49; + } + x += 8; + } + + // Number of characters to become sprites. + size_t n_chars = 128; + for (size_t i = 0; i < n_chars; ++i) { + OBJ_ATTR_0(i) = font_sprites[i].y; + OBJ_ATTR_1(i) = font_sprites[i].x; + OBJ_ATTR_2(i) = font_sprites[i].tile_index; + } + + // draw_logo(); int frame_counter = 0; - int active_sprite = 0; while(true) { wait_vsync(); poll_keys(); // Toggle frame counter when we press down. if (key_pressed(KEY_DOWN) || key_hold(KEY_DOWN)) { - if (active_sprite == 0) { - y_a += 3; - } else { - y_b += 3; - } } if (key_pressed(KEY_UP) || key_hold(KEY_UP)) { - if (active_sprite == 0) { - y_a -= 3; - } else { - y_b -= 3; - } } if (key_pressed(KEY_LEFT) || key_hold(KEY_LEFT)) { - if (active_sprite == 0) { - x_a -= 3; - } else { - x_b -= 3; - } } if (key_pressed(KEY_RIGHT) || key_hold(KEY_RIGHT)) { - if (active_sprite == 0) { - x_a += 3; - } else { - x_b += 3; - } } if (key_pressed(KEY_B)) { - if (active_sprite == 0) { - active_sprite = 1; - } else { - active_sprite = 0; - } } if (key_pressed(KEY_L)) { - OBJ_ATTR_2(0) = OBJ_ATTR_2(0) - 1; } if (key_pressed(KEY_R)) { - OBJ_ATTR_2(0) = OBJ_ATTR_2(0) + 1; } - OBJ_ATTR_0(0) = (OBJ_ATTR_0(0) & ~0xFF) | (y_a & 0xFF); - OBJ_ATTR_1(0) = (OBJ_ATTR_1(0) & ~0x1FF) | (x_a & 0x1FF); - OBJ_ATTR_0(1) = (OBJ_ATTR_0(1) & ~0xFF) | (y_b & 0xFF); - OBJ_ATTR_1(1) = (OBJ_ATTR_1(1) & ~0x1FF) | (x_b & 0x1FF); + for (size_t i = 0; i < 128; ++i) { + int y = font_sprites[i].y; + y -= bouncing_animation[font_sprites[i].animation_state++]; + if (font_sprites[i].animation_state > 56) { + font_sprites[i].animation_state = 0; + } + OBJ_ATTR_0(i) = (OBJ_ATTR_0(i) & ~0xFF) | (y & 0xFF); + } }; return 0; -- cgit v1.2.1