From 0615ab5073ec64998b6d0a4dead1ff03206c6bbb Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Fri, 10 Jun 2022 23:07:14 +0100 Subject: [PATCH] all collision information is stored with sprites, support multiple sprites --- res/sprite_metadata.c | 74 ++++++++++++++++++++++++++++++++++++++++++ res/sprite_metadata.h | 3 ++ res/sprites.c | 26 ++++----------- res/sprites.gbr | Bin 10069 -> 10069 bytes res/sprites.h | 4 +-- src/flags.h | 9 +++++ src/main.c | 46 ++++++++++++++++---------- src/sprite.c | 32 ++++++++++-------- src/sprite.h | 8 ++++- 9 files changed, 147 insertions(+), 55 deletions(-) create mode 100644 res/sprite_metadata.c create mode 100644 res/sprite_metadata.h diff --git a/res/sprite_metadata.c b/res/sprite_metadata.c new file mode 100644 index 0000000..47fecf4 --- /dev/null +++ b/res/sprite_metadata.c @@ -0,0 +1,74 @@ +#include +#include "../src/sprite.h" +#include "../src/flags.h" + +Sprite sprites_info[] = { + { + .frames_since_last_dash = 0, + .frames_since_last_jump = 0, + .name = "arrow", + .has_diag_sprites = 0, + .bitmap_index = SI_LEFT, + .has_joypad = 0, + .pos = { .x = 0, .y = 0}, + .vel = { .x = 0, .y = 0}, + .acc = { .x = 0, .y = 0}, + + .bitmap_indexes[SI_UP] = 12, .bitmap_prop[SI_UP] = 0, + .collision_offset[SI_UP] = { + .itl = { .x = 3, .y = 8}, + .itr = { .x = 1, .y = 8}, + .ibl = { .x = 3, .y = 1}, + .ibr = { .x = 1, .y = 1}, + }, + .bitmap_indexes[SI_DOWN] = 12, .bitmap_prop[SI_DOWN] = S_FLIPY, + .collision_offset[SI_DOWN] = { + .itl = { .x = 8, .y = 8}, + .itr = { .x = 5, .y = 8}, + .ibl = { .x = 8, .y = 1}, + .ibr = { .x = 5, .y = 1}, + }, + .bitmap_indexes[SI_LEFT] = 11, .bitmap_prop[SI_LEFT] = 0, + .collision_offset[SI_LEFT] = { + .itl = { .x = 8, .y = 3}, + .itr = { .x = 1, .y = 3}, + .ibl = { .x = 8, .y = 1}, + .ibr = { .x = 1, .y = 1}, + }, + .bitmap_indexes[SI_RIGHT] = 11, .bitmap_prop[SI_RIGHT] = S_FLIPX, + .collision_offset[SI_RIGHT] = { + .itl = { .x = 8, .y = 3}, + .itr = { .x = 1, .y = 3}, + .ibl = { .x = 8, .y = 1}, + .ibr = { .x = 1, .y = 1}, + }, + .bitmap_indexes[SI_UP_RIGHT] = 13, .bitmap_prop[SI_UP_RIGHT] = 0, + .collision_offset[SI_UP_RIGHT] = { + .itl = { .x = 7, .y = 8}, + .itr = { .x = 1, .y = 8}, + .ibl = { .x = 7, .y = 2}, + .ibr = { .x = 1, .y = 2}, + }, + .bitmap_indexes[SI_UP_LEFT] = 13, .bitmap_prop[SI_UP_LEFT] = S_FLIPX, + .collision_offset[SI_UP_LEFT] = { + .itl = { .x = 8, .y = 8}, + .itr = { .x = 2, .y = 8}, + .ibl = { .x = 8, .y = 2}, + .ibr = { .x = 2, .y = 2}, + }, + .bitmap_indexes[SI_DOWN_LEFT] = 13, .bitmap_prop[SI_DOWN_LEFT] = S_FLIPX | S_FLIPY, + .collision_offset[SI_DOWN_LEFT] = { + .itl = { .x = 8, .y = 7}, + .itr = { .x = 2, .y = 7}, + .ibl = { .x = 8, .y = 1}, + .ibr = { .x = 2, .y = 1}, + }, + .bitmap_indexes[SI_DOWN_RIGHT] = 13, .bitmap_prop[SI_DOWN_RIGHT] = S_FLIPY, + .collision_offset[SI_DOWN_RIGHT] = { + .itl = { .x = 7, .y = 7}, + .itr = { .x = 1, .y = 7}, + .ibl = { .x = 7, .y = 1}, + .ibr = { .x = 1, .y = 1}, + } + } +}; diff --git a/res/sprite_metadata.h b/res/sprite_metadata.h new file mode 100644 index 0000000..84d6d32 --- /dev/null +++ b/res/sprite_metadata.h @@ -0,0 +1,3 @@ +#include "../src/sprite.h" + +extern Sprite sprites_info[]; diff --git a/res/sprites.c b/res/sprites.c index d32c5a4..0b8b68a 100644 --- a/res/sprites.c +++ b/res/sprites.c @@ -10,7 +10,7 @@ Compression : None. Counter : None. Tile size : 8 x 8 - Tiles : 0 to 11 + Tiles : 0 to 13 Palette colors : None. SGB Palette : None. @@ -22,8 +22,6 @@ */ -#include "../src/sprite.h" - /* Start of tile array. */ unsigned char sprites[] = { @@ -50,23 +48,11 @@ unsigned char sprites[] = 0x43,0xC3,0xDB,0xDB,0x3C,0x3C,0x7E,0x7E, 0x7E,0x7E,0x3C,0x3C,0xDB,0xDB,0x43,0xC3, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x43,0x38,0xC6,0x00,0x43 + 0x00,0x00,0x00,0x43,0x38,0xC6,0x00,0x43, + 0x00,0x02,0x00,0x07,0x02,0x00,0x02,0x00, + 0x02,0x00,0x00,0x02,0x00,0x07,0x00,0x05, + 0x00,0x03,0x02,0x01,0x04,0x00,0x08,0x00, + 0x00,0x30,0x00,0x50,0x00,0x20,0x00,0x00 }; - -// define the collision box of each sprite in the tileset -SpriteCorners sprite_offsets[] = { - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 8}, .itr = { .x = 1, .y = 8}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, }, - { .itl = { .x = 8, .y = 3}, .itr = { .x = 1, .y = 3}, .ibl = { .x = 8, .y = 1}, .ibr = { .x = 1, .y = 1}, } -}; /* End of SPRITES.C */ diff --git a/res/sprites.gbr b/res/sprites.gbr index de0a94ff2e2c1019fa37a5e51a6d3d951ac252e0..e6ab671aef6c8b17768518e998847055ec713e6a 100644 GIT binary patch delta 155 zcmccWchzr#9vcHg%xfQbppV1)1)&}lSvObl=Vhz3RoAF2&PLCr*{ e-F%R_SB8%ns8<-M3#f>Zfobz0g>JTq1snj62@LiC delta 55 zcmccWchzr#9@}I_Mg|6kjV5i(o6T8jWhOhY)bcSiFfj-NMS(Iv+0BO(y4g4~Qj@ct H@+&w1hh7Z~ diff --git a/res/sprites.h b/res/sprites.h index 49f5dda..d57e0f0 100644 --- a/res/sprites.h +++ b/res/sprites.h @@ -10,7 +10,7 @@ Compression : None. Counter : None. Tile size : 8 x 8 - Tiles : 0 to 11 + Tiles : 0 to 13 Palette colors : None. SGB Palette : None. @@ -22,12 +22,10 @@ */ -#include "../src/sprite.h" /* Bank of tiles. */ #define spritesBank 0 /* Start of tile array. */ extern unsigned char sprites[]; -extern SpriteCorners sprite_offsets[]; /* End of SPRITES.H */ diff --git a/src/flags.h b/src/flags.h index 585ee16..228b1b0 100644 --- a/src/flags.h +++ b/src/flags.h @@ -24,6 +24,15 @@ #define SC_IBL 0b0010 #define SC_IBR 0b0001 +#define SI_UP 0 +#define SI_DOWN 1 +#define SI_LEFT 2 +#define SI_RIGHT 3 +#define SI_UP_RIGHT 4 +#define SI_DOWN_RIGHT 5 +#define SI_UP_LEFT 6 +#define SI_DOWN_LEFT 7 + /* Sprite Collision point diagram: * x TL LT x * ------------ diff --git a/src/main.c b/src/main.c index 5d03e40..3115be6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,24 +1,25 @@ #include +#include #include +#include +#include #include "../res/tiles.h" #include "../res/map.h" #include"../res/sprites.h" +#include"../res/sprite_metadata.h" #include "./flags.h" #include "./constants.h" #include "./sprite.h" #include "./vec.h" -#define SPRITE_NO 11 +#define NO_SCREEN_SPRITES 2 void init_gfx() { // Load Background tiles and then map set_bkg_data(0, 23, tiles); set_bkg_tiles(0, 0, map.width, map.height, map.data); - - set_sprite_data(0, 12, sprites); - set_sprite_tile(0, SPRITE_NO); - set_sprite_prop(0, 0); + set_sprite_data(0, 14, sprites); // Turn the background map on to make it visible SHOW_BKG; @@ -27,26 +28,35 @@ void init_gfx() void main(void) { - unsigned int fc = 0; - struct Sprites sprite; - init_gfx(); - sprite.pos.x = 72; - sprite.pos.y = 36; - sprite.vel.x = 0; - sprite.vel.y = 0; - sprite.acc.x = 0; - sprite.acc.y = 0; - sprite.collision_offset = sprite_offsets[SPRITE_NO]; - move_sprite(0, sprite.pos.x, sprite.pos.y); + unsigned int i, jp, fc = 0; + struct Sprites screen_sprites[NO_SCREEN_SPRITES]; + + + memcpy(&(screen_sprites[0]), &(sprites_info[0]), sizeof(Sprite)); + memcpy(&(screen_sprites[1]), &(sprites_info[0]), sizeof(Sprite)); + screen_sprites[0].has_joypad = 1; + screen_sprites[1].bitmap_index = SI_UP; + + for (i = 0; i < NO_SCREEN_SPRITES; i++) { + set_sprite_tile(i, screen_sprites[i].bitmap_indexes[screen_sprites[i].bitmap_index]); + set_sprite_prop(i, screen_sprites[i].bitmap_prop[screen_sprites[i].bitmap_index]); + screen_sprites[i].pos.x = 72+(8*i); + screen_sprites[i].pos.y = 36+(8*i); + move_sprite(i, screen_sprites[i].pos.x, screen_sprites[i].pos.y); + } // Loop forever while(1) { fc++; - sprite_iter_frame(&sprite, &map, joypad(), &fc); - move_sprite(0, sprite.pos.x, sprite.pos.y); + jp = joypad(); + for (i = 0; i < NO_SCREEN_SPRITES; i++) { + sprite_iter_frame(&(screen_sprites[i]), &map, screen_sprites[i].has_joypad ? jp : 0, &fc); + move_sprite(i, screen_sprites[i].pos.x, screen_sprites[i].pos.y); + //gprintf("x: %d y: %d\n", sprites[i].pos.x, sprites[i].pos.y); + } // Done processing, yield CPU and wait for start of next frame wait_vbl_done(); diff --git a/src/sprite.c b/src/sprite.c index 504dd46..b0c48b8 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -17,7 +17,13 @@ typedef struct SpriteCorners { typedef struct Sprites { int frames_since_last_dash; int frames_since_last_jump; - SpriteCorners collision_offset; + int bitmap_index; + int bitmap_indexes[8]; + int bitmap_prop[8]; + int has_diag_sprites; + int has_joypad; + char name[20]; + SpriteCorners collision_offset[8]; UVec pos; Vec vel; Vec acc; @@ -25,7 +31,7 @@ typedef struct Sprites { int get_tile_index_by_coord(unsigned int x, unsigned int y); -void getSpriteCorners(Sprite *sprite, SpriteCorners *r); +void get_sprite_corners(Sprite *sprite, SpriteCorners *r); void sprite_iter_frame(Sprite *sprite, Map *map, int joypad, unsigned int *fc); unsigned int sprite_internal_collision(Map *map, Sprite *sprite); unsigned int sprite_collision(Map *map, Sprite *sprite); @@ -37,16 +43,16 @@ int get_tile_index_by_coord(unsigned int x, unsigned int y) } -void getSpriteCorners(Sprite *sprite, SpriteCorners *sc) +void get_sprite_corners(Sprite *sprite, SpriteCorners *sc) { - sc->itl.x = sprite->pos.x - sprite->collision_offset.itl.x; - sc->itl.y = sprite->pos.y - sprite->collision_offset.itl.y - 8; - sc->itr.x = sprite->pos.x - sprite->collision_offset.itr.x; - sc->itr.y = sprite->pos.y - sprite->collision_offset.itr.y - 8; - sc->ibl.x = sprite->pos.x - sprite->collision_offset.ibl.x; - sc->ibl.y = sprite->pos.y - sprite->collision_offset.ibl.y - 8; - sc->ibr.x = sprite->pos.x - sprite->collision_offset.ibr.x; - sc->ibr.y = sprite->pos.y - sprite->collision_offset.ibr.y - 8; + sc->itl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itl.x; + sc->itl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itl.y - 8; + sc->itr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itr.x; + sc->itr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itr.y - 8; + sc->ibl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibl.x; + sc->ibl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibl.y - 8; + sc->ibr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibr.x; + sc->ibr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibr.y - 8; } @@ -149,7 +155,7 @@ unsigned int sprite_internal_collision(Map *map, Sprite *sprite) { unsigned int rv = 0; SpriteCorners sc; - getSpriteCorners(sprite, &sc); + get_sprite_corners(sprite, &sc); if (map->data[get_tile_index_by_coord(sc.itl.x, sc.itl.y)]) rv = rv | SC_ITL; if (map->data[get_tile_index_by_coord(sc.itr.x, sc.itr.y)]) rv = rv | SC_ITR; @@ -165,7 +171,7 @@ unsigned int sprite_collision(Map *map, Sprite *sprite) { unsigned int rv = 0; SpriteCorners sc; - getSpriteCorners(sprite, &sc); + get_sprite_corners(sprite, &sc); // check if corners are in a non 0 tile if (map->data[get_tile_index_by_coord(sc.itl.x, sc.itl.y-1)]) rv = rv | SC_TL; diff --git a/src/sprite.h b/src/sprite.h index 7285059..c37790e 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -14,7 +14,13 @@ typedef struct SpriteCorners { typedef struct Sprites { int frames_since_last_dash; int frames_since_last_jump; - SpriteCorners collision_offset; + int bitmap_index; + int bitmap_indexes[8]; + int bitmap_prop[8]; + int has_diag_sprites; + int has_joypad; + char name[20]; + SpriteCorners collision_offset[8]; UVec pos; Vec vel; Vec acc;