git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/cross-platform/metasprites/src/metasprites.c

      1 //
      2 // Cross-platform metasprites example.
      3 //
      4 // This examples demonstrates the following features in GBDK's metasprite support.
      5 //
      6 // * Drawing a metasprite with optional X/Y flipping using hardware flipping
      7 // * Drawing a metasprite with optional X/Y flipping using duplicated tiles
      8 // * Drawing a metasprite with different sub-palettes
      9 //
     10 // The flipping of tiles is handled by setting X/Y flip bits on those targets that
     11 // support it in hardware.
     12 //
     13 // For targets that don't have hardware support for flipping, a fallback is provided by
     14 // uploading duplicate tiles flipped in X and Y, and using the base_tile parameter of 
     15 // move_metasprite_* to draw the flipped tiles.
     16 // 
     17 // For targets that support sprite sub-palettes (GBC and NES), the base_props parameter
     18 // of move_metasprite_* is used to switch between 4 different sub-palettes.
     19 // Note that SMS and GG do NOT support sub-palettes for sprites, and this example
     20 // will display the same colors when built for these targets.
     21 //
     22 // Joypad buttons:
     23 // 
     24 // cursor -> Moves metasprite position in X/Y
     25 // A      -> Rotates the metasprite through X/Y flip states, and then through sub-palettes
     26 // B      -> Animates the metasprite
     27 //
     28 
     29 #include <gbdk/platform.h>
     30 #include <gbdk/metasprites.h>
     31 
     32 #include <stdint.h>
     33 
     34 // During build, png2asset metasprite conversion will write output to: obj/<platform ext>/res/
     35 //  Makefile adds part of that path as an include when compiling. Example: -Iobj/gb
     36 #include <res/sprite.h>
     37 
     38 // Constants for tile dimensions
     39 #define TILE_WIDTH          8
     40 #define TILE_HEIGHT         8
     41 #define NUM_BYTES_PER_TILE  16
     42 
     43 const uint8_t pattern[] = {0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,0x01};
     44 
     45 #define ACC_X 1
     46 #define ACC_Y 2
     47 
     48 // The metasprite will be built starting with hardware sprite zero (the first)
     49 #define SPR_NUM_START 0
     50 
     51 // Metasprite tiles are loaded into VRAM starting at tile number 0 
     52 #define TILE_NUM_START 0
     53 
     54 // sprite coords
     55 int16_t PosX, PosY;
     56 int16_t SpdX, SpdY;
     57 uint8_t PosF;
     58 uint8_t idx, rot;
     59 
     60 uint8_t flipped_data[NUM_BYTES_PER_TILE];
     61 
     62 size_t num_tiles;
     63 
     64 uint8_t joyp = 0, old_joyp = 0;
     65 
     66 #define KEY_INPUT (old_joyp = joyp, joyp = joypad())
     67 #define KEY_DOWN(KEY) (joyp & (KEY))
     68 #define KEY_PRESSED(KEY) ((joyp ^ old_joyp) & joyp & (KEY))
     69 
     70 // Table for fast reversing of bits in a byte - used for flipping in X
     71 const uint8_t reverse_bits[256] = {
     72     0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
     73     0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
     74     0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
     75     0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
     76     0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
     77     0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
     78     0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
     79     0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
     80     0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
     81     0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
     82     0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
     83     0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
     84     0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
     85     0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
     86     0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
     87     0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF
     88 };
     89 
     90 // Helper function to flip tile in X/Y
     91 // Note this assumes 2BPP tile in GB format, where bitplanes are interleaved.
     92 // Currently all platforms use GB format for 2BPP tile data storage, irrespective
     93 // of what their native tile format is, as set_sprite_data handles the conversion.
     94 void set_tile(uint8_t tile_idx, uint8_t* data, uint8_t flip_x, uint8_t flip_y)
     95 {
     96     size_t i;
     97     for(i = 0; i < TILE_HEIGHT; i++)
     98     {
     99         size_t y = flip_y ? (TILE_HEIGHT-1-i) : i; 
    100         flipped_data[2*i] = flip_x ? reverse_bits[data[2*y]] : data[2*y];
    101         flipped_data[2*i+1] = flip_x ? reverse_bits[data[2*y+1]] : data[2*y+1];
    102     }
    103     set_sprite_data(tile_idx, 1, flipped_data);
    104 }
    105 
    106 uint8_t get_tile_offset(uint8_t flipx, uint8_t flipy)
    107 {
    108     flipx; flipy; // suppress compiler warnings
    109     uint8_t offset = 0;
    110 #if !HARDWARE_SPRITE_CAN_FLIP_Y
    111     offset += flipy ? num_tiles : 0;
    112 #endif
    113 #if !HARDWARE_SPRITE_CAN_FLIP_X
    114     offset <<= 1;
    115     offset += flipx ? num_tiles : 0;
    116 #endif
    117     return offset;
    118 }
    119 
    120 // Load metasprite tile data into VRAM, one tile at a time.
    121 // For each tile, create a duplicate flipped in X and/or Y
    122 // That's placed at tile index = get_tile_offset(flipX?, flipY?)
    123 void load_and_duplicate_sprite_tile_data(void)
    124 {
    125     size_t i;
    126     num_tiles = sizeof(sprite_tiles) >> 4;
    127     for(i = 0; i < num_tiles; i++)
    128     {
    129         set_tile(i + get_tile_offset(0, 0), sprite_tiles + (i << 4), 0, 0);
    130 #if !HARDWARE_SPRITE_CAN_FLIP_X
    131         set_tile(i + get_tile_offset(1, 0), sprite_tiles + (i << 4), 1, 0);
    132 #endif
    133 #if !HARDWARE_SPRITE_CAN_FLIP_Y
    134         set_tile(i + get_tile_offset(0, 1), sprite_tiles + (i << 4), 0, 1);
    135 #endif
    136 #if !HARDWARE_SPRITE_CAN_FLIP_X && !HARDWARE_SPRITE_CAN_FLIP_Y
    137         set_tile(i + get_tile_offset(1, 1), sprite_tiles + (i << 4), 1, 1);
    138 #endif
    139     }
    140 }
    141 
    142 const palette_color_t gray_pal[4] = {   RGB8(255,255,255),
    143                                         RGB8(170,170,170),
    144                                         RGB8(85,85,85),
    145                                         RGB8(0,0,0) };
    146 const palette_color_t pink_pal[4] = {   RGB8(255,255,255),
    147                                         RGB8(255,0,255),
    148                                         RGB8(170,0,170),
    149                                         RGB8(85,0,85) };
    150 const palette_color_t cyan_pal[4] = {   RGB8(255,255,255),
    151                                         RGB8(85,255,255),
    152                                         RGB8(0,170,170),
    153                                         RGB8(0,85,85) };
    154 const palette_color_t green_pal[4] = {  RGB8(255,255,255),
    155                                         RGB8(170,255,170),
    156                                         RGB8(0,170,0),
    157                                         RGB8(0,85,0) };
    158 
    159 // Main function
    160 void main(void) {
    161     DISPLAY_OFF;
    162 
    163 #if defined(GAMEBOY)
    164     cgb_compatibility();
    165     set_sprite_palette(OAMF_CGB_PAL0, 1, gray_pal);
    166     set_sprite_palette(OAMF_CGB_PAL1, 1, pink_pal);
    167     set_sprite_palette(OAMF_CGB_PAL2, 1, cyan_pal);
    168     set_sprite_palette(OAMF_CGB_PAL3, 1, green_pal);
    169 #elif defined(NINTENDO_NES)
    170     set_sprite_palette(0, 1, gray_pal);
    171     set_sprite_palette(1, 1, pink_pal);
    172     set_sprite_palette(2, 1, cyan_pal);
    173     set_sprite_palette(3, 1, green_pal);
    174 #endif
    175 
    176     // Fill the screen background with a single tile pattern
    177     fill_bkg_rect(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT, 0);
    178 
    179     // Set tile data for background
    180     set_bkg_data(0, 1, pattern);
    181 
    182     // Load (and flip) sprite tile data
    183     load_and_duplicate_sprite_tile_data();
    184 
    185     // Show bkg and sprites
    186     SHOW_BKG; SHOW_SPRITES;
    187 
    188     // Check what size hardware sprite the metasprite is using (from sprite.h)
    189     #if sprite_TILE_H == 16
    190         SPRITES_8x16;
    191     #else
    192         SPRITES_8x8;
    193     #endif
    194     DISPLAY_ON;
    195 
    196     // Set initial position to the center of the screen, zero out speed
    197     PosX = (DEVICE_SCREEN_PX_WIDTH / 2) << 4;
    198     PosY = (DEVICE_SCREEN_PX_HEIGHT / 2) << 4;
    199     SpdX = SpdY = 0;
    200 
    201     idx = 0; rot = 0;
    202 
    203     while(TRUE) {        
    204         // Poll joypads
    205         KEY_INPUT;
    206         
    207         PosF = 0;
    208         // Game object
    209         if (KEY_DOWN(J_UP)) {
    210             SpdY -= 2;
    211             if (SpdY < -32) SpdY = -32;
    212             PosF |= ACC_Y;
    213         } else if (KEY_DOWN(J_DOWN)) {
    214             SpdY += 2;
    215             if (SpdY > 32) SpdY = 32;
    216             PosF |= ACC_Y;
    217         }
    218 
    219         if (KEY_DOWN(J_LEFT)) {
    220             SpdX -= 2;
    221             if (SpdX < -32) SpdX = -32;
    222             PosF |= ACC_X;
    223         } else if (KEY_DOWN(J_RIGHT)) {
    224             SpdX += 2;
    225             if (SpdX > 32) SpdX = 32;
    226             PosF |= ACC_X;
    227         }
    228 
    229         // Press B button to cycle through metasprite animations
    230         if (KEY_PRESSED(J_B)) {
    231             idx++; if (idx >= (sizeof(sprite_metasprites) >> 1)) idx = 0;
    232         }
    233 
    234         // Press A button to cycle metasprite through Normal/Flip-Y/Flip-XY/Flip-X and sub-pals
    235         if (KEY_PRESSED(J_A)) {
    236             rot++; rot &= 0xF;
    237         }
    238 
    239         PosX += SpdX, PosY += SpdY; 
    240 
    241         uint8_t hiwater = SPR_NUM_START;
    242 
    243         // NOTE: In a real game it would be better to only call the move_metasprite..()
    244         //       functions if something changed (such as movement or rotation). That
    245         //       reduces CPU usage on frames that don't need updates.
    246         //
    247         // In this example they are called every frame to simplify the example code
    248 
    249         // If not hidden the move and apply rotation to the metasprite
    250         uint8_t subpal = rot >> 2;
    251         switch (rot & 0x3) {
    252             case 1:
    253                 hiwater += move_metasprite_flipy( sprite_metasprites[idx],
    254                                                   TILE_NUM_START + get_tile_offset(0, 1),
    255                                                   subpal,
    256                                                   hiwater,
    257                                                   DEVICE_SPRITE_PX_OFFSET_X + (PosX >> 4),
    258                                                   DEVICE_SPRITE_PX_OFFSET_Y + (PosY >> 4));
    259                 break;
    260             case 2:
    261                 hiwater += move_metasprite_flipxy(sprite_metasprites[idx],
    262                                                   TILE_NUM_START + get_tile_offset(1, 1),
    263                                                   subpal,
    264                                                   hiwater,
    265                                                   DEVICE_SPRITE_PX_OFFSET_X + (PosX >> 4),
    266                                                   DEVICE_SPRITE_PX_OFFSET_Y + (PosY >> 4));
    267                 break;
    268             case 3:
    269                 hiwater += move_metasprite_flipx( sprite_metasprites[idx],
    270                                                   TILE_NUM_START + get_tile_offset(1, 0),
    271                                                   subpal,
    272                                                   hiwater,
    273                                                   DEVICE_SPRITE_PX_OFFSET_X + (PosX >> 4),
    274                                                   DEVICE_SPRITE_PX_OFFSET_Y + (PosY >> 4));
    275                 break;
    276             default:
    277                 hiwater += move_metasprite_ex(    sprite_metasprites[idx],
    278                                                   TILE_NUM_START + get_tile_offset(0, 0),
    279                                                   subpal,
    280                                                   hiwater,
    281                                                   DEVICE_SPRITE_PX_OFFSET_X + (PosX >> 4),
    282                                                   DEVICE_SPRITE_PX_OFFSET_Y + (PosY >> 4));
    283                 break;
    284         }
    285 
    286         // Hide rest of the hardware sprites, because amount of sprites differ between animation frames.
    287         hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES);        
    288 
    289         // Y Axis: update velocity (reduce speed) if no U/D button pressed
    290         if (!(PosF & ACC_Y)) {
    291             if (SpdY != 0) {
    292                 if (SpdY > 0) SpdY--;
    293                 else SpdY ++;
    294             }
    295         }
    296 
    297         // X Axis: update velocity (reduce speed) if no L/R button pressed
    298         if (!(PosF & ACC_X)) {
    299             if (SpdX != 0) {
    300                 if (SpdX > 0) SpdX--;
    301                 else SpdX ++;
    302             }
    303         }
    304 
    305         // Wait for VBlank to slow down everything, and reduce CPU power use on 
    306         // handheld systems.
    307         vsync();
    308     }
    309 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.