Steward
分享是一種喜悅、更是一種幸福
掌機 - Game Boy Color - C/C++ - Sprite
參考資訊:
https://bgb.bircd.org/
https://github.com/mrombout/gbdk_playground
http://gbdk.sourceforge.net/doc/html/book01.html
GBDK裡面的Sprite指的就是Object Attribute Memory(OAM),而Tile指的則是Character RAM(單位是8x8 Pixels)
Tile設定
1 | void set_sprite_data ( UBYTE first_tile, UBYTE nb_tiles, unsigned char * data ); |
第一個參數是Tile位置(0~255)
第二個參數是需要複製多少個Tile像素(0代表256個),每個Tile像素是8x8(16 Bytes)
第三個參數是Tile來源位置
Title指定
1 | void set_sprite_tile ( UBYTE nb, UBYTE tile); |
第一個參數是OAM的索引(0~39)
第二個參數是Tile位置(0~255)
Palette設定
1 | void set_sprite_palette ( UINT8 first_palette, UINT8 nb_palettes, UINT16 *rgb_data) NONBANKED ; |
第一個參數是Palette目標位置(0~7)
第二個參數是需要複製多少個Palette(1~8)
第三個參數是Palette來源位置
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <gb/gb.h> #include <gb/cgb.h> unsigned char sprite[] = { 0x7e, // .xxxxxx. 0x7e, // .xxxxxx. 0x99, // x..xx..x 0x99, // x..xx..x 0x81, // x......x 0x81, // x......x 0xa5, // x.x..x.x 0xa5, // x.x..x.x 0x81, // x......x 0x81, // x......x 0xdb, // xx.xx.xx 0xdb, // xx.xx.xx 0xc3, // xx....xx 0xc3, // xx....xx 0x3c, // ..xxxx.. 0x3c // ..xxxx.. }; unsigned short palette[] = {0, RGB_RED , RGB_GREEN , RGB_BLUE }; void main ( void ) { SPRITES_8x8 ; set_sprite_data (0, 1, sprite); set_sprite_tile (0, 0); set_sprite_palette (0, 1, palette); move_sprite (0, 50, 50); SHOW_SPRITES ; } |
完成