diff --git a/src/level.c b/src/level.c index ae3f1ea..a6bb571 100644 --- a/src/level.c +++ b/src/level.c @@ -25,8 +25,6 @@ #include "level.h" #include "timeline.h" -int x = 0, x2 = 0; - void Level_Passed(struct Game *game) { if (game->level.current_level<6) { int available = atoi(GetConfigOptionDefault("MuffinAttack", "level", "1")); @@ -51,24 +49,48 @@ void Level_Draw(struct Game *game) { } } -bool napis(struct Game *game, struct TM_Action *lol) { - al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), x, 20, 0, "wat"); - x+=10; - if (x>1000) { x=0; return true; } +bool napis(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (!action->arguments) { + action->arguments = malloc(sizeof(struct TM_Arguments)); + action->arguments->value = malloc(sizeof(int)); + action->arguments->next = NULL; + } + int* tmp; + tmp = (int*)action->arguments->value; + if (state == TM_ACTIONSTATE_INIT) { + *tmp = 0; + } else if (state == TM_ACTIONSTATE_RUNNING) { + al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), *tmp, 20, 0, "wat"); + *tmp+=10; + if (*tmp>1000) { *tmp=0; return true; } + } return false; } -bool napis2(struct Game *game, struct TM_Action *lol) { - al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), x2, 100, 0, "lol"); - x2+=5; - if (x2>1000) { x2=0; return true; } +bool napis2(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (!action->arguments) { + action->arguments = malloc(sizeof(struct TM_Arguments)); + action->arguments->value = malloc(sizeof(int)); + action->arguments->next = NULL; + } + int* tmp; + tmp = (int*)action->arguments->value; + if (state == TM_ACTIONSTATE_INIT) { + *tmp = 0; + } else if (state == TM_ACTIONSTATE_RUNNING) { + al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), *tmp, 100, 0, "lol"); + *tmp+=5; + if (*tmp>1000) { *tmp=0; return true; } + } return false; } -bool wyjscie(struct Game *game, struct TM_Action *lol) { - Level_Passed(game); - game->gamestate = GAMESTATE_LOADING; - game->loadstate = GAMESTATE_MAP; +bool wyjscie(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state == TM_ACTIONSTATE_DESTROY) { + Level_Passed(game); + game->gamestate = GAMESTATE_LOADING; + game->loadstate = GAMESTATE_MAP; + } return true; } diff --git a/src/timeline.c b/src/timeline.c index 16c4604..cbc2c9b 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -42,9 +42,11 @@ void TM_Process() { // if returns true, then delete it if (queue) { if (*queue->function) { - if ((*queue->function)(game, queue)) { + if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) { + (*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 { @@ -57,7 +59,8 @@ void TM_Process() { while (pom!=NULL) { if (pom->active) { if (*pom->function) { - if ((*pom->function)(game, pom)) { + if ((*pom->function)(game, pom, TM_ACTIONSTATE_RUNNING)) { + (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY); if (tmp) { tmp->next = pom->next; } else { @@ -72,6 +75,7 @@ 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 +92,7 @@ void TM_HandleEvent(ALLEGRO_EVENT *ev) { } -void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args) { +void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args) { struct TM_Action *action = malloc(sizeof(struct TM_Action)); if (queue) { struct TM_Action *pom = queue; @@ -105,9 +109,10 @@ void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Argum action->timer = NULL; action->active = false; action->delay = 0; + (*action->function)(game, action, TM_ACTIONSTATE_INIT); } -void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args, int delay) { +void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay) { struct TM_Action *action = malloc(sizeof(struct TM_Action)); if (background) { struct TM_Action *pom = background; @@ -124,6 +129,7 @@ void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struc action->timer = NULL; action->active = true; action->delay = 0; + (*action->function)(game, action, TM_ACTIONSTATE_INIT); } void TM_AddDelay(int delay) { diff --git a/src/timeline.h b/src/timeline.h index f49a47e..7846231 100644 --- a/src/timeline.h +++ b/src/timeline.h @@ -21,6 +21,12 @@ #include "main.h" +enum TM_ActionState { + TM_ACTIONSTATE_INIT, + TM_ACTIONSTATE_RUNNING, + TM_ACTIONSTATE_DESTROY +}; + struct TM_Arguments { void *value; struct TM_Arguments *next; @@ -28,7 +34,7 @@ struct TM_Arguments { }; struct TM_Action { - bool (*function)(struct Game*, struct TM_Action*); + bool (*function)(struct Game*, struct TM_Action*, enum TM_ActionState); struct TM_Arguments *arguments; ALLEGRO_TIMER *timer; bool active; @@ -40,8 +46,8 @@ struct TM_Action { void TM_Init(struct Game* game); void TM_Process(); void TM_HandleEvent(ALLEGRO_EVENT *ev); -void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args); -void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args, int delay); +void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args); +void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay); void TM_AddDelay(int delay); void TM_Pause(bool pause); void TM_Destroy();