From cff6de73c37298ea8eb29c160bde586ef760163e Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 7 Apr 2012 18:32:47 +0200 Subject: [PATCH] implement timeline destroying --- src/level.c | 5 ++++ src/timeline.c | 79 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/level.c b/src/level.c index 43028d5..0aeb900 100644 --- a/src/level.c +++ b/src/level.c @@ -73,6 +73,7 @@ bool FadeIn(struct Game *game, struct TM_Action *action, enum TM_ActionState sta } else { al_destroy_bitmap(fade_bitmap); free(fadeloop); + TM_DestroyArgs(action->arguments); } return false; } @@ -104,6 +105,7 @@ bool FadeOut(struct Game *game, struct TM_Action *action, enum TM_ActionState st Level_Unload(game); game->gamestate = GAMESTATE_LOADING; game->loadstate = GAMESTATE_MAP; + TM_DestroyArgs(action->arguments); } return false; } @@ -122,6 +124,8 @@ bool napis2(struct Game *game, struct TM_Action *action, enum TM_ActionState sta al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), *tmp, 100, 0, "lol"); *tmp+=5; if (*tmp>=al_get_display_width(game->display)) { *tmp=0; return true; } + } else { + TM_DestroyArgs(action->arguments); } return false; } @@ -142,6 +146,7 @@ bool napis(struct Game *game, struct TM_Action *action, enum TM_ActionState stat if (*tmp>=al_get_display_width(game->display)) { *tmp=0; return true; } } else { TM_AddBackgroundAction(&napis2, NULL, 0); + TM_DestroyArgs(action->arguments); } return false; } diff --git a/src/timeline.c b/src/timeline.c index cbc2c9b..5b77f14 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -42,11 +42,12 @@ void TM_Process() { // if returns true, then delete it if (queue) { if (*queue->function) { + queue->active = true; if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) { + queue->active=false; (*queue->function)(game, queue, TM_ACTIONSTATE_DESTROY); struct TM_Action *tmp = queue; queue = queue->next; - if (tmp->arguments) free(tmp->arguments); // TODO: cascade destroy free(tmp); } } else { @@ -60,6 +61,7 @@ void TM_Process() { if (pom->active) { if (*pom->function) { if ((*pom->function)(game, pom, TM_ACTIONSTATE_RUNNING)) { + pom->active=false; (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY); if (tmp) { tmp->next = pom->next; @@ -75,7 +77,6 @@ void TM_Process() { tmp = pom; pom = pom->next; } else { - if (pom->arguments) free(pom->arguments); // TODO: cascade destroy free(pom); tmp2 = tmp; if (!tmp) pom=background->next; @@ -88,7 +89,7 @@ void TM_Process() { void TM_HandleEvent(ALLEGRO_EVENT *ev) { if (!game) return; if (paused) return; - // TODO: mark action as active + // TODO: find proper action and mark it as active } @@ -126,10 +127,10 @@ void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum T action->next = NULL; action->function = func; action->arguments = args; - action->timer = NULL; - action->active = true; - action->delay = 0; - (*action->function)(game, action, TM_ACTIONSTATE_INIT); + action->timer = NULL; // TODO + action->active = true; // TODO: false here, true when delay + action->delay = delay; + (*action->function)(game, action, TM_ACTIONSTATE_INIT); // TODO: move to TM_HandleEvent } void TM_AddDelay(int delay) { @@ -142,15 +143,75 @@ void TM_Pause(bool pause) { } void TM_Destroy() { + if (!game) return; // TODO: delete everything from queues // maybe delete all args too? + struct TM_Action *tmp, *tmp2, *pom = queue; + tmp = NULL; + while (pom!=NULL) { + if (pom->active) { + if (*pom->function) { + (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY); + if (tmp) { + tmp->next = pom->next; + } else { + background = pom->next; + } + } else { + // TODO: handle delay + } + } + if ((!tmp) || (tmp->next==pom)) { + tmp = pom; + pom = pom->next; + } else { + free(pom); + tmp2 = tmp; + if (!tmp) pom=background->next; + else pom=tmp->next; + tmp = tmp2; + } + } + tmp = NULL; + pom=background; + while (pom!=NULL) { + if (pom->active) { + if (*pom->function) { + (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY); + if (tmp) { + tmp->next = pom->next; + } else { + background = pom->next; + } + } else { + // TODO: handle delay + } + } + if ((!tmp) || (tmp->next==pom)) { + tmp = pom; + pom = pom->next; + } else { + free(pom); + tmp2 = tmp; + if (!tmp) pom=background->next; + else pom=tmp->next; + tmp = tmp2; + } + } + game = NULL; } struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, void* arg) { + // TODO return NULL; } void TM_DestroyArgs(struct TM_Arguments* args) { - -} \ No newline at end of file + struct TM_Arguments *pom; + while (args) { + pom = args->next; + free(args); + args = pom; + } +}