timeline: expose the delta value to actions

This commit is contained in:
Sebastian Krzyszkowiak 2018-06-27 19:00:48 +02:00
parent 016c482154
commit 1c66f219d8
2 changed files with 13 additions and 8 deletions

View file

@ -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) { SYMBOL_EXPORT void TM_Process(struct Timeline* timeline, double delta) {
// TODO: handle delta
/* process first element from queue /* process first element from queue
if returns true, delete it if returns true, delete it
and repeat for the next one */ and repeat for the next one */
bool next = true; bool next = true;
while (next) { while (next) {
if (timeline->queue) { if (timeline->queue) {
timeline->queue->delta = delta;
if (*timeline->queue->function) { if (*timeline->queue->function) {
if (!timeline->queue->active) { if (!timeline->queue->active) {
PrintConsole(timeline->game, "Timeline Manager[%s]: queue: run action (%d - %s)", timeline->name, timeline->queue->id, timeline->queue->name); 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; tmp = NULL;
while (pom != NULL) { while (pom != NULL) {
bool destroy = false; bool destroy = false;
pom->delta = delta;
if (pom->active) { if (pom->active) {
if (*pom->function) { if (*pom->function) {
if ((*pom->function)(timeline->game, pom, TM_ACTIONSTATE_RUNNING)) { 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)); struct TM_Action* action = malloc(sizeof(struct TM_Action));
if (timeline->queue) { if (timeline->queue) {
struct TM_Action* pom = 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; 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)); struct TM_Action* action = malloc(sizeof(struct TM_Action));
if (timeline->background) { if (timeline->background) {
struct TM_Action* pom = 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; 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(int, del, delay);
TM_WrapArg(bool, used, false); TM_WrapArg(bool, used, false);
struct TM_Arguments* arguments = TM_AddToArgs(NULL, 6, (void*)func, del, strdup(name), (void*)timeline, args, used); struct TM_Arguments* arguments = TM_AddToArgs(NULL, 6, (void*)func, del, strdup(name), (void*)timeline, args, used);

View file

@ -38,6 +38,9 @@ enum TM_ActionState {
TM_ACTIONSTATE_RESUME TM_ACTIONSTATE_RESUME
}; };
struct TM_Action;
typedef bool TM_ActionCallback(struct Game*, struct TM_Action*, enum TM_ActionState);
/*! \brief Timeline structure. */ /*! \brief Timeline structure. */
struct Timeline { struct Timeline {
struct TM_Action* queue; /*!< Main timeline queue. */ struct TM_Action* queue; /*!< Main timeline queue. */
@ -55,11 +58,12 @@ struct TM_Arguments {
/*! \brief Timeline action. */ /*! \brief Timeline action. */
struct TM_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. */ struct TM_Arguments* arguments; /*!< Arguments of the action. */
ALLEGRO_TIMER* timer; /*!< Delay timer. */ ALLEGRO_TIMER* timer; /*!< Delay timer. */
bool active; /*!< If false, then this action is waiting for it's delay to finish. */ 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. */ 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. */ struct TM_Action* next; /*!< Pointer to next action in queue. */
unsigned int id; /*!< ID of the action. */ unsigned int id; /*!< ID of the action. */
char* name; /*!< "User friendly" name of the action. */ char* name; /*!< "User friendly" name of the action. */
@ -78,11 +82,11 @@ void TM_Resume(struct Timeline*);
/*! \brief Handle timer events. */ /*! \brief Handle timer events. */
void TM_HandleEvent(struct Timeline*, ALLEGRO_EVENT* ev); void TM_HandleEvent(struct Timeline*, ALLEGRO_EVENT* ev);
/*! \brief Add new action to main queue. */ /*! \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. */ /*! \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. */ /*! \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. */ /*! \brief Add delay to main queue. */
void TM_AddDelay(struct Timeline*, int delay); void TM_AddDelay(struct Timeline*, int delay);
/*! \brief Remove all actions from main queue. */ /*! \brief Remove all actions from main queue. */