all collision information is stored with sprites, support multiple sprites

This commit is contained in:
Akbar Rahman 2022-06-10 23:07:14 +01:00
parent 776cf354f8
commit 0615ab5073
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
9 changed files with 147 additions and 55 deletions

74
res/sprite_metadata.c Normal file
View File

@ -0,0 +1,74 @@
#include <gb/gb.h>
#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},
}
}
};

3
res/sprite_metadata.h Normal file
View File

@ -0,0 +1,3 @@
#include "../src/sprite.h"
extern Sprite sprites_info[];

View File

@ -10,7 +10,7 @@
Compression : None. Compression : None.
Counter : None. Counter : None.
Tile size : 8 x 8 Tile size : 8 x 8
Tiles : 0 to 11 Tiles : 0 to 13
Palette colors : None. Palette colors : None.
SGB Palette : None. SGB Palette : None.
@ -22,8 +22,6 @@
*/ */
#include "../src/sprite.h"
/* Start of tile array. */ /* Start of tile array. */
unsigned char sprites[] = unsigned char sprites[] =
{ {
@ -50,23 +48,11 @@ unsigned char sprites[] =
0x43,0xC3,0xDB,0xDB,0x3C,0x3C,0x7E,0x7E, 0x43,0xC3,0xDB,0xDB,0x3C,0x3C,0x7E,0x7E,
0x7E,0x7E,0x3C,0x3C,0xDB,0xDB,0x43,0xC3, 0x7E,0x7E,0x3C,0x3C,0xDB,0xDB,0x43,0xC3,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 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 */ /* End of SPRITES.C */

Binary file not shown.

View File

@ -10,7 +10,7 @@
Compression : None. Compression : None.
Counter : None. Counter : None.
Tile size : 8 x 8 Tile size : 8 x 8
Tiles : 0 to 11 Tiles : 0 to 13
Palette colors : None. Palette colors : None.
SGB Palette : None. SGB Palette : None.
@ -22,12 +22,10 @@
*/ */
#include "../src/sprite.h"
/* Bank of tiles. */ /* Bank of tiles. */
#define spritesBank 0 #define spritesBank 0
/* Start of tile array. */ /* Start of tile array. */
extern unsigned char sprites[]; extern unsigned char sprites[];
extern SpriteCorners sprite_offsets[];
/* End of SPRITES.H */ /* End of SPRITES.H */

View File

@ -24,6 +24,15 @@
#define SC_IBL 0b0010 #define SC_IBL 0b0010
#define SC_IBR 0b0001 #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: /* Sprite Collision point diagram:
* x TL LT x * x TL LT x
* ------------ * ------------

View File

@ -1,24 +1,25 @@
#include <gb/gb.h> #include <gb/gb.h>
#include <gb/drawing.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../res/tiles.h" #include "../res/tiles.h"
#include "../res/map.h" #include "../res/map.h"
#include"../res/sprites.h" #include"../res/sprites.h"
#include"../res/sprite_metadata.h"
#include "./flags.h" #include "./flags.h"
#include "./constants.h" #include "./constants.h"
#include "./sprite.h" #include "./sprite.h"
#include "./vec.h" #include "./vec.h"
#define SPRITE_NO 11 #define NO_SCREEN_SPRITES 2
void init_gfx() void init_gfx()
{ {
// Load Background tiles and then map // Load Background tiles and then map
set_bkg_data(0, 23, tiles); set_bkg_data(0, 23, tiles);
set_bkg_tiles(0, 0, map.width, map.height, map.data); set_bkg_tiles(0, 0, map.width, map.height, map.data);
set_sprite_data(0, 14, sprites);
set_sprite_data(0, 12, sprites);
set_sprite_tile(0, SPRITE_NO);
set_sprite_prop(0, 0);
// Turn the background map on to make it visible // Turn the background map on to make it visible
SHOW_BKG; SHOW_BKG;
@ -27,26 +28,35 @@ void init_gfx()
void main(void) void main(void)
{ {
unsigned int fc = 0;
struct Sprites sprite;
init_gfx(); init_gfx();
sprite.pos.x = 72; unsigned int i, jp, fc = 0;
sprite.pos.y = 36; struct Sprites screen_sprites[NO_SCREEN_SPRITES];
sprite.vel.x = 0;
sprite.vel.y = 0;
sprite.acc.x = 0; memcpy(&(screen_sprites[0]), &(sprites_info[0]), sizeof(Sprite));
sprite.acc.y = 0; memcpy(&(screen_sprites[1]), &(sprites_info[0]), sizeof(Sprite));
sprite.collision_offset = sprite_offsets[SPRITE_NO]; screen_sprites[0].has_joypad = 1;
move_sprite(0, sprite.pos.x, sprite.pos.y); 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 // Loop forever
while(1) { while(1) {
fc++; fc++;
sprite_iter_frame(&sprite, &map, joypad(), &fc); jp = joypad();
move_sprite(0, sprite.pos.x, sprite.pos.y); 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 // Done processing, yield CPU and wait for start of next frame
wait_vbl_done(); wait_vbl_done();

View File

@ -17,7 +17,13 @@ typedef struct SpriteCorners {
typedef struct Sprites { typedef struct Sprites {
int frames_since_last_dash; int frames_since_last_dash;
int frames_since_last_jump; 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; UVec pos;
Vec vel; Vec vel;
Vec acc; Vec acc;
@ -25,7 +31,7 @@ typedef struct Sprites {
int get_tile_index_by_coord(unsigned int x, unsigned int y); 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); 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_internal_collision(Map *map, Sprite *sprite);
unsigned int sprite_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.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itl.x;
sc->itl.y = sprite->pos.y - sprite->collision_offset.itl.y - 8; sc->itl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itl.y - 8;
sc->itr.x = sprite->pos.x - sprite->collision_offset.itr.x; sc->itr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].itr.x;
sc->itr.y = sprite->pos.y - sprite->collision_offset.itr.y - 8; sc->itr.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].itr.y - 8;
sc->ibl.x = sprite->pos.x - sprite->collision_offset.ibl.x; sc->ibl.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibl.x;
sc->ibl.y = sprite->pos.y - sprite->collision_offset.ibl.y - 8; sc->ibl.y = sprite->pos.y - sprite->collision_offset[sprite->bitmap_index].ibl.y - 8;
sc->ibr.x = sprite->pos.x - sprite->collision_offset.ibr.x; sc->ibr.x = sprite->pos.x - sprite->collision_offset[sprite->bitmap_index].ibr.x;
sc->ibr.y = sprite->pos.y - sprite->collision_offset.ibr.y - 8; 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; unsigned int rv = 0;
SpriteCorners sc; 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.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; 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; unsigned int rv = 0;
SpriteCorners sc; SpriteCorners sc;
getSpriteCorners(sprite, &sc); get_sprite_corners(sprite, &sc);
// check if corners are in a non 0 tile // 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; if (map->data[get_tile_index_by_coord(sc.itl.x, sc.itl.y-1)]) rv = rv | SC_TL;

View File

@ -14,7 +14,13 @@ typedef struct SpriteCorners {
typedef struct Sprites { typedef struct Sprites {
int frames_since_last_dash; int frames_since_last_dash;
int frames_since_last_jump; 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; UVec pos;
Vec vel; Vec vel;
Vec acc; Vec acc;