diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-03-26 09:34:18 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-03-26 09:34:18 (EDT) |
commit | 6b0df99a9d55385e11272f13bdfd51022d92be19 (patch) | |
tree | 9fbeddb5e2471dd6a757689aa3a592cff4fd618c | |
parent | bc8de0954b04eab4aa3b810fb5913b0500aae923 (diff) | |
download | dodge-balls-6b0df99a9d55385e11272f13bdfd51022d92be19.zip dodge-balls-6b0df99a9d55385e11272f13bdfd51022d92be19.tar.gz dodge-balls-6b0df99a9d55385e11272f13bdfd51022d92be19.tar.bz2 |
tileset: Add collision functions
And fix collision bit field setting.
-rw-r--r-- | src/tileset.c | 30 | ||||
-rw-r--r-- | src/tileset.h | 4 |
2 files changed, 32 insertions, 2 deletions
diff --git a/src/tileset.c b/src/tileset.c index d8b3c03..c41f4ff 100644 --- a/src/tileset.c +++ b/src/tileset.c @@ -179,7 +179,7 @@ _db_tsx_property_start(void *pv, const char *name, const char **attr) col = SDL_FALSE; /* Shut up, GCC. */ db_xml_get_bool_attr(p, attr, "value", &col, 1); if (col) { - tileset->b_col &= (1 << tileset->cur_tile); + tileset->b_col |= (1 << tileset->cur_tile); } db_dbg(" Ball collision: %s", (col ? "true" : "false")); @@ -195,7 +195,7 @@ _db_tsx_property_start(void *pv, const char *name, const char **attr) col = SDL_FALSE; /* Shut up, GCC. */ db_xml_get_bool_attr(p, attr, "value", &col, 1); if (col) { - tileset->p_col &= (1 << tileset->cur_tile); + tileset->p_col |= (1 << tileset->cur_tile); } db_dbg(" Player collision: %s", (col ? "true" : "false")); @@ -428,6 +428,32 @@ db_tileset_new(const char *game_id, const char *file, int firstgid, return tileset; } +SDL_bool +db_tile_player_collides(struct db_tileset *tileset, int gid) +{ + for (; tileset != NULL; tileset = tileset->next) { + if (gid >= tileset->firstgid && + gid < tileset->firstgid + tileset->tc) { + gid -= tileset->firstgid; + return tileset->p_col & 1 << gid; + } + } + return SDL_FALSE; +} + +SDL_bool +db_tile_ball_collides(struct db_tileset *tileset, int gid) +{ + for (; tileset != NULL; tileset = tileset->next) { + if (gid >= tileset->firstgid && + gid < tileset->firstgid + tileset->tc) { + gid -= tileset->firstgid; + return tileset->b_col & 1 << gid; + } + } + return SDL_FALSE; +} + static SDL_Texture * _db_tileset_texture(struct db_tileset *tileset, SDL_Renderer *renderer) { diff --git a/src/tileset.h b/src/tileset.h index 652e1a3..e34117e 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -27,6 +27,10 @@ struct db_tileset; struct db_tileset *db_tileset_new(const char *game_id, const char *file, int firstgid, int tilewidth, int tileheight, struct db_tileset *prev); +SDL_bool db_tile_player_collides(struct db_tileset *tileset, int gid) + __attribute__((__pure__)); +SDL_bool db_tile_ball_collides(struct db_tileset *tileset, int gid) + __attribute__((__pure__)); int db_tile_render(struct db_tileset *tileset, SDL_Renderer *renderer, int gid, SDL_Rect *dstrect); |