diff --git a/src/tmx.c b/src/tmx.c index 4fa6a5e..d4c0024 100644 --- a/src/tmx.c +++ b/src/tmx.c @@ -72,6 +72,35 @@ tmx_tile* tmx_get_tile(tmx_map *map, unsigned int gid) { return NULL; } +void tmx_update_animation(tmx_map *map, int delta_time_msec) { + + tmx_tileset_list *ts_list = map->ts_head; + + while (ts_list != NULL) { + tmx_tileset *ts = ts_list->tileset; + + for(int i = 0; i < ts->tilecount; i++) { + tmx_tile *tile = &ts->tiles[i]; + + if(!tile->animation) + continue; + + tmx_anim_frame *cur_frame = &tile->animation[tile->current_animation_frame]; + + tile->animation_timer += delta_time_msec; + + if(tile->animation_timer <= cur_frame->duration) + continue; + + tile->animation_timer -= cur_frame->duration; + + tile->current_animation_frame = (tile->current_animation_frame + 1) % tile->animation_len; + } + + ts_list = ts_list->next; + } +} + static tmx_layer* _tmx_find_layer_by_id(tmx_layer *ly_head, int id) { tmx_layer *res; do { diff --git a/src/tmx.h b/src/tmx.h index 0d47243..45a005e 100644 --- a/src/tmx.h +++ b/src/tmx.h @@ -117,6 +117,8 @@ struct _tmx_tile { /* */ tmx_object *collision; unsigned int animation_len; + unsigned int current_animation_frame; + int animation_timer; tmx_anim_frame *animation; char *type; @@ -284,6 +286,8 @@ TMXEXPORT void tmx_map_free(tmx_map *map); Returns the tile associated with this gid, returns NULL if it fails */ TMXEXPORT tmx_tile* tmx_get_tile(tmx_map *map, unsigned int gid); +TMXEXPORT void tmx_update_animation(tmx_map *map, int delta_time_msec); + /* Find functions, iterate on the datastructure, you should probably cache the result */ /* Finds a layer by its id, returns NULL if not found or an error occurred */ TMXEXPORT tmx_layer* tmx_find_layer_by_id(const tmx_map *map, int id); diff --git a/src/tmx_xml.c b/src/tmx_xml.c index 8d58083..69c69eb 100644 --- a/src/tmx_xml.c +++ b/src/tmx_xml.c @@ -749,6 +749,9 @@ static int parse_tile(xmlTextReaderPtr reader, tmx_tileset *tileset, tmx_resourc if (!strcmp(name, "frame")) { res->animation = parse_animation(reader, 0, &(res->animation_len)); if (!(res->animation)) return 0; + + res->current_animation_frame = 0; + res->animation_timer = 0; } /* else: ignore */ } while (xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT ||