From 1c66f219d89ef04e71066162a38d112bb2ea0014 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Wed, 27 Jun 2018 19:00:48 +0200 Subject: [PATCH] timeline: expose the delta value to actions --- src/timeline.c | 9 +++++---- src/timeline.h | 12 ++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/timeline.c b/src/timeline.c index 83ffdf8..dd476f9 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -35,13 +35,13 @@ SYMBOL_EXPORT struct Timeline* TM_Init(struct Game* game, char* name) { } SYMBOL_EXPORT void TM_Process(struct Timeline* timeline, double delta) { - // TODO: handle delta /* process first element from queue if returns true, delete it and repeat for the next one */ bool next = true; while (next) { if (timeline->queue) { + timeline->queue->delta = delta; if (*timeline->queue->function) { if (!timeline->queue->active) { PrintConsole(timeline->game, "Timeline Manager[%s]: queue: run action (%d - %s)", timeline->name, timeline->queue->id, timeline->queue->name); @@ -84,6 +84,7 @@ SYMBOL_EXPORT void TM_Process(struct Timeline* timeline, double delta) { tmp = NULL; while (pom != NULL) { bool destroy = false; + pom->delta = delta; if (pom->active) { if (*pom->function) { if ((*pom->function)(timeline->game, pom, TM_ACTIONSTATE_RUNNING)) { @@ -217,7 +218,7 @@ SYMBOL_EXPORT void TM_HandleEvent(struct Timeline* timeline, ALLEGRO_EVENT* ev) } } -SYMBOL_EXPORT struct TM_Action* TM_AddAction(struct Timeline* timeline, bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, char* name) { +SYMBOL_EXPORT struct TM_Action* TM_AddAction(struct Timeline* timeline, TM_ActionCallback* func, struct TM_Arguments* args, char* name) { struct TM_Action* action = malloc(sizeof(struct TM_Action)); if (timeline->queue) { struct TM_Action* pom = timeline->queue; @@ -243,7 +244,7 @@ SYMBOL_EXPORT struct TM_Action* TM_AddAction(struct Timeline* timeline, bool (*f return action; } -SYMBOL_EXPORT struct TM_Action* TM_AddBackgroundAction(struct Timeline* timeline, bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name) { +SYMBOL_EXPORT struct TM_Action* TM_AddBackgroundAction(struct Timeline* timeline, TM_ActionCallback* func, struct TM_Arguments* args, int delay, char* name) { struct TM_Action* action = malloc(sizeof(struct TM_Action)); if (timeline->background) { struct TM_Action* pom = timeline->background; @@ -300,7 +301,7 @@ static bool runinbackground(struct Game* game, struct TM_Action* action, enum TM return true; } -SYMBOL_EXPORT struct TM_Action* TM_AddQueuedBackgroundAction(struct Timeline* timeline, bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name) { +SYMBOL_EXPORT struct TM_Action* TM_AddQueuedBackgroundAction(struct Timeline* timeline, TM_ActionCallback* func, struct TM_Arguments* args, int delay, char* name) { TM_WrapArg(int, del, delay); TM_WrapArg(bool, used, false); struct TM_Arguments* arguments = TM_AddToArgs(NULL, 6, (void*)func, del, strdup(name), (void*)timeline, args, used); diff --git a/src/timeline.h b/src/timeline.h index 0111f68..251d930 100644 --- a/src/timeline.h +++ b/src/timeline.h @@ -38,6 +38,9 @@ enum TM_ActionState { TM_ACTIONSTATE_RESUME }; +struct TM_Action; +typedef bool TM_ActionCallback(struct Game*, struct TM_Action*, enum TM_ActionState); + /*! \brief Timeline structure. */ struct Timeline { struct TM_Action* queue; /*!< Main timeline queue. */ @@ -55,11 +58,12 @@ struct TM_Arguments { /*! \brief Timeline action. */ struct TM_Action { - bool (*function)(struct Game*, struct TM_Action*, enum TM_ActionState); /*!< Function callback of the action. */ + TM_ActionCallback* function; /*!< Function callback of the action. */ struct TM_Arguments* arguments; /*!< Arguments of the action. */ ALLEGRO_TIMER* timer; /*!< Delay timer. */ bool active; /*!< If false, then this action is waiting for it's delay to finish. */ int delay; /*!< Number of miliseconds to delay before action is started. */ + double delta; /*!< Number of miliseconds since the last TM_Process invocation. */ struct TM_Action* next; /*!< Pointer to next action in queue. */ unsigned int id; /*!< ID of the action. */ char* name; /*!< "User friendly" name of the action. */ @@ -78,11 +82,11 @@ void TM_Resume(struct Timeline*); /*! \brief Handle timer events. */ void TM_HandleEvent(struct Timeline*, ALLEGRO_EVENT* ev); /*! \brief Add new action to main queue. */ -struct TM_Action* TM_AddAction(struct Timeline*, bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, char* name); +struct TM_Action* TM_AddAction(struct Timeline*, TM_ActionCallback* func, struct TM_Arguments* args, char* name); /*! \brief Add new action to background queue. */ -struct TM_Action* TM_AddBackgroundAction(struct Timeline*, bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name); +struct TM_Action* TM_AddBackgroundAction(struct Timeline*, TM_ActionCallback* func, struct TM_Arguments* args, int delay, char* name); /*! \brief Add new action to main queue, which adds specified action into background queue. */ -struct TM_Action* TM_AddQueuedBackgroundAction(struct Timeline*, bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name); +struct TM_Action* TM_AddQueuedBackgroundAction(struct Timeline*, TM_ActionCallback* func, struct TM_Arguments* args, int delay, char* name); /*! \brief Add delay to main queue. */ void TM_AddDelay(struct Timeline*, int delay); /*! \brief Remove all actions from main queue. */