Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated tile support. WIP! #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/tmx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/tmx.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ struct _tmx_tile { /* <tile> */
tmx_object *collision;

unsigned int animation_len;
unsigned int current_animation_frame;
int animation_timer;
tmx_anim_frame *animation;

char *type;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/tmx_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down