gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit b5af10fc660c5cc55e86c659fce96de2ed00f98c parent 611058b85d2f1627093c1951c6171d8e6203e1a9 Author: bbbbbr <bbbbbr@users.noreply.github.com> Date: Sat, 10 Dec 2022 10:56:38 -0800 Merge pull request #465 from michel-iwaniec/cross_platform_metasprite_flipxy_fixes Code Review changes to cross-platform/metasprites Diffstat:
| M | gbdk-lib/examples/cross-platform/metasprites/src/metasprites.c | 95 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
1 file changed, 53 insertions(+), 42 deletions(-)
diff --git a/gbdk-lib/examples/cross-platform/metasprites/src/metasprites.c b/gbdk-lib/examples/cross-platform/metasprites/src/metasprites.c @@ -24,6 +24,7 @@ // cursor -> Moves metasprite position in X/Y // A -> Rotates the metasprite through X/Y flip states, and then through sub-palettes // B -> Animates the metasprite +// #include <stdint.h> @@ -36,10 +37,12 @@ // Makefile adds part of that path as an include when compiling. Example: -Iobj/gb #include <res/sprite.h> +// Constants for tile dimensions +#define TILE_WIDTH 8 +#define TILE_HEIGHT 8 +#define NUM_BYTES_PER_TILE 16 -const unsigned char pattern[] = {0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,0x01}; - -joypads_t joypads; +const uint8_t pattern[] = {0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,0x01}; #define ACC_X 1 #define ACC_Y 2 @@ -57,12 +60,12 @@ uint8_t PosF; uint8_t hide, jitter; uint8_t idx, rot; -unsigned char flipped_data[16]; +uint8_t flipped_data[NUM_BYTES_PER_TILE]; size_t num_tiles; // Table for fast reversing of bits in a byte - used for flipping in X -UBYTE reverse_bits[256] = { +const uint8_t reverse_bits[256] = { 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0, 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8, 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4, @@ -81,13 +84,16 @@ UBYTE reverse_bits[256] = { 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF }; -// Helper function to flip tile -void set_tile(UBYTE tile_idx, UBYTE* data, UBYTE flip_x, UBYTE flip_y) +// Helper function to flip tile in X/Y +// Note this assumes 2BPP tile in GB format, where bitplanes are interleaved. +// Currently all platforms use GB format for 2BPP tile data storage, irrespective +// of what their native tile format is, as set_sprite_data handles the conversion. +void set_tile(uint8_t tile_idx, uint8_t* data, uint8_t flip_x, uint8_t flip_y) { size_t i; - for(i = 0; i < 8; i++) + for(i = 0; i < TILE_HEIGHT; i++) { - size_t y = flip_y ? (7-i) : i; + size_t y = flip_y ? (TILE_HEIGHT-1-i) : i; flipped_data[2*i] = flip_x ? reverse_bits[data[2*y]] : data[2*y]; flipped_data[2*i+1] = flip_x ? reverse_bits[data[2*y+1]] : data[2*y+1]; } @@ -108,6 +114,28 @@ uint8_t get_tile_offset(uint8_t flipx, uint8_t flipy) return offset; } +// Load metasprite tile data into VRAM, one tile at a time. +// For each tile, create a duplicate flipped in X and/or Y +// That's placed at tile index = get_tile_offset(flipX?, flipY?) +void load_and_duplicate_sprite_tile_data() +{ + size_t i; + num_tiles = sizeof(sprite_tiles) >> 4; + for(i = 0; i < num_tiles; i++) + { + set_tile(i + get_tile_offset(0, 0), sprite_tiles + (i << 4), 0, 0); +#if !HARDWARE_SPRITE_CAN_FLIP_X + set_tile(i + get_tile_offset(1, 0), sprite_tiles + (i << 4), 1, 0); +#endif +#if !HARDWARE_SPRITE_CAN_FLIP_Y + set_tile(i + get_tile_offset(0, 1), sprite_tiles + (i << 4), 0, 1); +#endif +#if !HARDWARE_SPRITE_CAN_FLIP_X && !HARDWARE_SPRITE_CAN_FLIP_Y + set_tile(i + get_tile_offset(1, 1), sprite_tiles + (i << 4), 1, 1); +#endif + } +} + const palette_color_t gray_pal[4] = { RGB8(255,255,255), RGB8(170,170,170), RGB8(85,85,85), @@ -125,9 +153,8 @@ const palette_color_t green_pal[4] = { RGB8(255,255,255), RGB8(0,170,0), RGB8(0,85,0) }; -// main funxction +// Main function void main(void) { - size_t i; DISPLAY_OFF; #if defined(GAMEBOY) @@ -145,26 +172,13 @@ void main(void) { // Fill the screen background with a single tile pattern fill_bkg_rect(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT, 0); - // set tile data for background + // Set tile data for background set_bkg_data(0, 1, pattern); - // Load metasprite tile data into VRAM, one tile at a time - num_tiles = sizeof(sprite_tiles) >> 4; - for(i = 0; i < num_tiles; i++) - { - set_tile(i + get_tile_offset(0, 0), sprite_tiles + (i << 4), 0, 0); -#if !HARDWARE_SPRITE_CAN_FLIP_X - set_tile(i + get_tile_offset(1, 0), sprite_tiles + (i << 4), 1, 0); -#endif -#if !HARDWARE_SPRITE_CAN_FLIP_Y - set_tile(i + get_tile_offset(0, 1), sprite_tiles + (i << 4), 0, 1); -#endif -#if !HARDWARE_SPRITE_CAN_FLIP_X && !HARDWARE_SPRITE_CAN_FLIP_Y - set_tile(i + get_tile_offset(1, 1), sprite_tiles + (i << 4), 1, 1); -#endif - } + // Load (and flip) sprite tile data + load_and_duplicate_sprite_tile_data(); - // show bkg and sprites + // Show bkg and sprites SHOW_BKG; SHOW_SPRITES; // Check what size hardware sprite the metasprite is using (from sprite.h) @@ -175,9 +189,6 @@ void main(void) { #endif DISPLAY_ON; - // init 1 joypad - joypad_init(1, &joypads); - // Set initial position, zero out speed PosX = PosY = 96 << 4; SpdX = SpdY = 0; @@ -185,39 +196,39 @@ void main(void) { hide = 0; jitter = 0; idx = 0; rot = 0; while(1) { - // poll joypads - joypad_ex(&joypads); + // Poll joypads + uint8_t joyp = joypad(); PosF = 0; - // game object - if (joypads.joy0 & J_UP) { + // Game object + if (joyp & J_UP) { SpdY -= 2; if (SpdY < -32) SpdY = -32; PosF |= ACC_Y; - } else if (joypads.joy0 & J_DOWN) { + } else if (joyp & J_DOWN) { SpdY += 2; if (SpdY > 32) SpdY = 32; PosF |= ACC_Y; } - if (joypads.joy0 & J_LEFT) { + if (joyp & J_LEFT) { SpdX -= 2; if (SpdX < -32) SpdX = -32; PosF |= ACC_X; - } else if (joypads.joy0 & J_RIGHT) { + } else if (joyp & J_RIGHT) { SpdX += 2; if (SpdX > 32) SpdX = 32; PosF |= ACC_X; } // Press B button to cycle through metasprite animations - if ((joypads.joy0 & J_B) && (!jitter)) { + if ((joyp & J_B) && (!jitter)) { idx++; if (idx >= (sizeof(sprite_metasprites) >> 1)) idx = 0; jitter = 10; } // Press A button to cycle metasprite through Normal/Flip-Y/Flip-XY/Flip-X and sub-pals - if ((joypads.joy0 & J_A) && (!jitter)) { + if ((joyp & J_A) && (!jitter)) { rot++; rot &= 0xF; jitter = 20; } @@ -296,8 +307,8 @@ void main(void) { } } - // wait for VBlank to slow down everything and reduce cpu use when idle + // Wait for VBlank to slow down everything, and reduce CPU power use on + // handheld systems. vsync(); } } -
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.