metasprites.c (3782B)
1#include <gbdk/platform.h> 2#include <gbdk/metasprites.h> 3 4#include <stdint.h> 5 6// During build, png2asset metasprite conversion will write output to: obj/<platform ext>/res/ 7// Makefile adds part of that path as an include when compiling. Example: -Iobj/gb 8#include <res/sprite.h> 9 10 11const unsigned char pattern[] = {0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,0x01}; 12 13joypads_t joypads; 14 15#define ACC_X 1 16#define ACC_Y 2 17 18// The metasprite will be built starting with hardware sprite zero (the first) 19#define SPR_NUM_START 0 20 21// Metasprite tiles are loaded into VRAM starting at tile number 0 22#define TILE_NUM_START 0 23 24// sprite coords 25uint16_t PosX, PosY; 26int16_t SpdX, SpdY; 27uint8_t PosF; 28uint8_t hide, jitter; 29uint8_t idx; 30 31// main funxction 32void main(void) { 33 DISPLAY_OFF; 34 35 // Fill the screen background with a single tile pattern 36 fill_bkg_rect(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT, 0); 37 38 // set tile data for background 39 set_bkg_data(0, 1, pattern); 40 41 // Load metasprite tile data into VRAM 42 set_sprite_data(TILE_NUM_START, sizeof(sprite_tiles) >> 4, sprite_tiles); 43 44 // show bkg and sprites 45 SHOW_BKG; SHOW_SPRITES; 46 47 // Check what size hardware sprite the metasprite is using (from sprite.h) 48 #if sprite_TILE_H == 16 49 SPRITES_8x16; 50 #else 51 SPRITES_8x8; 52 #endif 53 DISPLAY_ON; 54 55 // init 1 joypad 56 joypad_init(1, &joypads); 57 58 // Set initial position, zero out speed 59 PosX = PosY = 96 << 4; 60 SpdX = SpdY = 0; 61 62 hide = 0; jitter = 0; idx = 0; 63 64 while(1) { 65 // poll joypads 66 joypad_ex(&joypads); 67 68 PosF = 0; 69 // game object 70 if (joypads.joy0 & J_UP) { 71 SpdY -= 2; 72 if (SpdY < -32) SpdY = -32; 73 PosF |= ACC_Y; 74 } else if (joypads.joy0 & J_DOWN) { 75 SpdY += 2; 76 if (SpdY > 32) SpdY = 32; 77 PosF |= ACC_Y; 78 } 79 80 if (joypads.joy0 & J_LEFT) { 81 SpdX -= 2; 82 if (SpdX < -32) SpdX = -32; 83 PosF |= ACC_X; 84 } else if (joypads.joy0 & J_RIGHT) { 85 SpdX += 2; 86 if (SpdX > 32) SpdX = 32; 87 PosF |= ACC_X; 88 } 89 90 91 // Press A button to show/hide metasprite 92 if ((joypads.joy0 & J_A) && (!jitter)) { 93 hide = (!hide); 94 jitter = 20; 95 } 96 97 // Press B button to cycle through metasprite animations 98 if ((joypads.joy0 & J_B) && (!jitter) && (!hide)) { 99 idx++; if (idx >= (sizeof(sprite_metasprites) >> 1)) idx = 0; 100 jitter = 10; 101 } 102 103 // Hide/Animate/Flip input throttling 104 if (jitter) jitter--; 105 106 PosX += SpdX, PosY += SpdY; 107 108 uint8_t hiwater = 0; 109 110 // Hide the metasprite or move it 111 if (hide) 112 hide_metasprite(sprite_metasprites[idx], SPR_NUM_START); 113 else 114 hiwater = move_metasprite(sprite_metasprites[idx], TILE_NUM_START, SPR_NUM_START, (PosX >> 4), (PosY >> 4)); 115 116 // Hide rest of the hardware sprites, because amount of sprites differ between animation frames. 117 hide_sprites_range(hiwater, MAX_HARDWARE_SPRITES); 118 119 // Y Axis: update velocity (reduce speed) if no U/D button pressed 120 if (!(PosF & ACC_Y)) { 121 if (SpdY != 0) { 122 if (SpdY < 0) SpdY++; 123 else SpdY --; 124 } 125 } 126 127 // X Axis: update velocity (reduce speed) if no L/R button pressed 128 if (!(PosF & ACC_X)) { 129 if (SpdX != 0) { 130 if (SpdX < 0) SpdX++; 131 else SpdX --; 132 } 133 } 134 135 // wait for VBlank to slow down everything and reduce cpu use when idle 136 wait_vbl_done(); 137 } 138} 139