2012-04-06 17:28:38 +02:00
|
|
|
/*! \file timeline.c
|
|
|
|
* \brief Timeline Manager framework code.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
#include <allegro5/allegro.h>
|
2012-04-07 23:03:34 +02:00
|
|
|
#include "main.h"
|
2012-04-06 17:28:38 +02:00
|
|
|
#include "timeline.h"
|
|
|
|
|
2012-04-06 23:32:15 +02:00
|
|
|
unsigned int lastid;
|
|
|
|
struct Game* game = NULL;
|
|
|
|
struct TM_Action *queue, *background;
|
|
|
|
bool paused;
|
2012-04-06 17:28:38 +02:00
|
|
|
|
2012-04-06 23:32:15 +02:00
|
|
|
void TM_Init(struct Game* g) {
|
2012-04-07 23:03:34 +02:00
|
|
|
PrintConsole(g, "Timeline Manager: init");
|
2012-04-06 23:32:15 +02:00
|
|
|
game = g;
|
|
|
|
lastid = 0;
|
|
|
|
queue = NULL;
|
|
|
|
background = NULL;
|
|
|
|
paused = false;
|
2012-04-06 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-06 23:32:15 +02:00
|
|
|
void TM_Process() {
|
|
|
|
if (!game) return;
|
|
|
|
if (paused) return;
|
|
|
|
// process first element from queue
|
|
|
|
// if returns true, then delete it
|
|
|
|
if (queue) {
|
|
|
|
if (*queue->function) {
|
2012-04-07 18:32:47 +02:00
|
|
|
queue->active = true;
|
2012-04-07 13:04:55 +02:00
|
|
|
if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) {
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: queue: destroy action (%d - %s)", queue->id, queue->name);
|
2012-04-07 18:32:47 +02:00
|
|
|
queue->active=false;
|
2012-04-07 13:04:55 +02:00
|
|
|
(*queue->function)(game, queue, TM_ACTIONSTATE_DESTROY);
|
2012-04-06 23:32:15 +02:00
|
|
|
struct TM_Action *tmp = queue;
|
|
|
|
queue = queue->next;
|
2012-04-09 17:17:16 +02:00
|
|
|
free(tmp->name);
|
2012-04-06 23:32:15 +02:00
|
|
|
free(tmp);
|
2012-04-09 17:17:16 +02:00
|
|
|
if (queue) PrintConsole(game, "Timeline Manager: queue: run action (%d - %s)", queue->id, queue->name);
|
2012-04-06 23:32:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-04-07 23:03:34 +02:00
|
|
|
// delay
|
|
|
|
if (queue->active) {
|
|
|
|
struct TM_Action *tmp = queue;
|
|
|
|
queue = queue->next;
|
2012-04-09 17:17:16 +02:00
|
|
|
free(tmp->name);
|
2012-04-07 23:03:34 +02:00
|
|
|
free(tmp);
|
2012-04-09 17:17:16 +02:00
|
|
|
if (queue) PrintConsole(game, "Timeline Manager: queue: run action (%d - %s)", queue->id, queue->name);
|
2012-04-07 23:03:34 +02:00
|
|
|
} else {
|
|
|
|
al_start_timer(queue->timer);
|
|
|
|
}
|
2012-04-06 23:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// process all elements from background marked as active
|
|
|
|
struct TM_Action *tmp, *tmp2, *pom = background;
|
|
|
|
tmp = NULL;
|
|
|
|
while (pom!=NULL) {
|
|
|
|
if (pom->active) {
|
|
|
|
if (*pom->function) {
|
2012-04-07 13:04:55 +02:00
|
|
|
if ((*pom->function)(game, pom, TM_ACTIONSTATE_RUNNING)) {
|
2012-04-07 18:32:47 +02:00
|
|
|
pom->active=false;
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: background: destroy action (%d - %s)", pom->id, pom->name);
|
2012-04-07 13:04:55 +02:00
|
|
|
(*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
2012-04-06 23:32:15 +02:00
|
|
|
if (tmp) {
|
|
|
|
tmp->next = pom->next;
|
|
|
|
} else {
|
|
|
|
background = pom->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-04-07 23:03:34 +02:00
|
|
|
// delay
|
|
|
|
if (tmp) {
|
|
|
|
tmp->next = pom->next;
|
|
|
|
} else {
|
|
|
|
background = pom->next;
|
|
|
|
}
|
2012-04-06 23:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((!tmp) || (tmp->next==pom)) {
|
|
|
|
tmp = pom;
|
|
|
|
pom = pom->next;
|
|
|
|
} else {
|
2012-04-09 17:17:16 +02:00
|
|
|
free(pom->name);
|
2012-04-06 23:32:15 +02:00
|
|
|
free(pom);
|
|
|
|
tmp2 = tmp;
|
|
|
|
if (!tmp) pom=background->next;
|
|
|
|
else pom=tmp->next;
|
|
|
|
tmp = tmp2;
|
|
|
|
}
|
|
|
|
}
|
2012-04-06 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-06 23:32:15 +02:00
|
|
|
void TM_HandleEvent(ALLEGRO_EVENT *ev) {
|
2012-04-07 23:03:34 +02:00
|
|
|
if (ev->type != ALLEGRO_EVENT_TIMER) return;
|
2012-04-06 23:32:15 +02:00
|
|
|
if (!game) return;
|
|
|
|
if (paused) return;
|
2012-04-07 23:03:34 +02:00
|
|
|
if (queue) {
|
|
|
|
if (ev->timer.source == queue->timer) {
|
|
|
|
queue->active=true;
|
|
|
|
al_destroy_timer(queue->timer);
|
|
|
|
queue->timer = NULL;
|
|
|
|
if (queue->function) {
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: queue: init action (%d - %s)", queue->id, queue->name);
|
2012-04-07 23:03:34 +02:00
|
|
|
(*queue->function)(game, queue, TM_ACTIONSTATE_INIT);
|
|
|
|
} else {
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: queue: delay reached (%d - %s)", queue->id, queue->name);
|
2012-04-07 23:03:34 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct TM_Action *pom = background;
|
|
|
|
while (pom) {
|
|
|
|
if (ev->timer.source == pom->timer) {
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: background: delay reached, init and run action (%d - %s)", pom->id, pom->name);
|
2012-04-07 23:03:34 +02:00
|
|
|
pom->active=true;
|
|
|
|
al_destroy_timer(pom->timer);
|
|
|
|
pom->timer = NULL;
|
|
|
|
(*pom->function)(game, pom, TM_ACTIONSTATE_INIT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pom=pom->next;
|
|
|
|
}
|
2012-04-06 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-09 17:17:16 +02:00
|
|
|
struct TM_Action* TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, char* name) {
|
2012-04-06 23:32:15 +02:00
|
|
|
struct TM_Action *action = malloc(sizeof(struct TM_Action));
|
|
|
|
if (queue) {
|
|
|
|
struct TM_Action *pom = queue;
|
|
|
|
while (pom->next!=NULL) {
|
|
|
|
pom=pom->next;
|
|
|
|
}
|
|
|
|
pom->next = action;
|
|
|
|
} else {
|
|
|
|
queue = action;
|
|
|
|
}
|
|
|
|
action->next = NULL;
|
|
|
|
action->function = func;
|
|
|
|
action->arguments = args;
|
2012-04-09 17:17:16 +02:00
|
|
|
action->name = malloc((strlen(name)+1)*sizeof(char));
|
|
|
|
strcpy(action->name, name);
|
2012-04-06 23:32:15 +02:00
|
|
|
action->timer = NULL;
|
|
|
|
action->active = false;
|
|
|
|
action->delay = 0;
|
2012-04-07 23:03:34 +02:00
|
|
|
action->id = ++lastid;
|
|
|
|
if (action->function) {
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: queue: init action (%d - %s)", action->id, action->name);
|
2012-04-07 23:03:34 +02:00
|
|
|
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
|
|
|
|
}
|
|
|
|
return action;
|
2012-04-06 23:32:15 +02:00
|
|
|
}
|
|
|
|
|
2012-04-09 17:17:16 +02:00
|
|
|
struct TM_Action* TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, float delay, char* name) {
|
2012-04-06 23:32:15 +02:00
|
|
|
struct TM_Action *action = malloc(sizeof(struct TM_Action));
|
|
|
|
if (background) {
|
|
|
|
struct TM_Action *pom = background;
|
|
|
|
while (pom->next!=NULL) {
|
|
|
|
pom=pom->next;
|
|
|
|
}
|
|
|
|
pom->next = action;
|
|
|
|
} else {
|
|
|
|
background = action;
|
|
|
|
}
|
|
|
|
action->next = NULL;
|
|
|
|
action->function = func;
|
|
|
|
action->arguments = args;
|
2012-04-09 17:17:16 +02:00
|
|
|
action->name = malloc((strlen(name)+1)*sizeof(char));
|
|
|
|
strcpy(action->name, name);
|
2012-04-07 18:32:47 +02:00
|
|
|
action->delay = delay;
|
2012-04-07 23:03:34 +02:00
|
|
|
action->id = ++lastid;
|
2012-04-07 20:32:47 +02:00
|
|
|
if (delay) {
|
|
|
|
action->active = false;
|
2012-04-07 23:03:34 +02:00
|
|
|
action->timer = al_create_timer(delay/1000.0);
|
|
|
|
al_register_event_source(game->event_queue, al_get_timer_event_source(action->timer));
|
|
|
|
al_start_timer(action->timer);
|
2012-04-07 20:32:47 +02:00
|
|
|
} else {
|
2012-04-07 23:03:34 +02:00
|
|
|
action->timer = NULL;
|
2012-04-07 20:32:47 +02:00
|
|
|
action->active = true;
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: background: init and run action (%d - %s)", action->id, action->name);
|
2012-04-07 20:32:47 +02:00
|
|
|
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
|
|
|
|
}
|
2012-04-08 23:25:14 +02:00
|
|
|
return action;
|
2012-04-06 23:32:15 +02:00
|
|
|
}
|
|
|
|
|
2012-04-09 12:55:04 +02:00
|
|
|
bool runinbackground(struct Game* game, struct TM_Action* action, enum TM_ActionState state) {
|
|
|
|
if (state != TM_ACTIONSTATE_RUNNING) return false;
|
|
|
|
float* delay = (float*) action->arguments->next->value;
|
2012-04-09 17:17:16 +02:00
|
|
|
char* name = (char*) action->arguments->next->next->value;
|
|
|
|
TM_AddBackgroundAction(action->arguments->value, action->arguments->next->next->next, *delay, name);
|
|
|
|
free(name);
|
2012-04-09 12:55:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-09 17:17:16 +02:00
|
|
|
struct TM_Action* TM_AddQueuedBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, float delay, char* name) {
|
2012-04-09 12:55:04 +02:00
|
|
|
struct TM_Arguments* arguments = TM_AddToArgs(NULL, (void*) func);
|
|
|
|
arguments = TM_AddToArgs(arguments, malloc(sizeof(float)));
|
2012-04-09 17:17:16 +02:00
|
|
|
arguments = TM_AddToArgs(arguments, NULL);
|
2012-04-09 12:55:04 +02:00
|
|
|
*(float*)(arguments->next->value) = delay;
|
2012-04-09 17:17:16 +02:00
|
|
|
arguments->next->next->value = malloc((strlen(name)+1)*sizeof(char));
|
|
|
|
strcpy(arguments->next->next->value, name);
|
|
|
|
|
|
|
|
arguments->next->next->next = args;
|
|
|
|
return TM_AddAction(*runinbackground, arguments, "TM_BackgroundAction");
|
2012-04-09 12:55:04 +02:00
|
|
|
}
|
|
|
|
|
2012-04-07 23:03:34 +02:00
|
|
|
void TM_AddDelay(float delay) {
|
|
|
|
/*int *tmp;
|
|
|
|
tmp = malloc(sizeof(int));
|
|
|
|
*tmp = delay;
|
|
|
|
TM_AddAction(NULL, TM_AddToArgs(NULL, tmp));*/
|
2012-04-09 17:18:41 +02:00
|
|
|
struct TM_Action* tmp = TM_AddAction(NULL, NULL, "TM_Delay");
|
2012-04-09 17:17:16 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: queue: adding delay %f ms (%d)", delay, tmp->id);
|
2012-04-07 23:03:34 +02:00
|
|
|
tmp->delay = delay;
|
|
|
|
tmp->timer = al_create_timer(delay/1000.0);
|
|
|
|
al_register_event_source(game->event_queue, al_get_timer_event_source(tmp->timer));
|
2012-04-06 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TM_Pause(bool pause) {
|
2012-04-06 23:32:15 +02:00
|
|
|
paused = pause;
|
2012-04-07 23:03:34 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: pause %d", pause);
|
|
|
|
if (queue) {
|
|
|
|
if (queue->timer) {
|
|
|
|
if (pause) {
|
|
|
|
al_stop_timer(queue->timer);
|
|
|
|
} else if (!queue->active) al_start_timer(queue->timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct TM_Action* tmp = background;
|
|
|
|
while (tmp) {
|
|
|
|
if (tmp->timer) {
|
|
|
|
if (pause) {
|
|
|
|
al_stop_timer(tmp->timer);
|
|
|
|
} else if (!tmp->active) al_start_timer(tmp->timer);
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
2012-04-06 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TM_Destroy() {
|
2012-04-07 18:32:47 +02:00
|
|
|
if (!game) return;
|
2012-04-07 23:03:34 +02:00
|
|
|
PrintConsole(game, "Timeline Manager: destroy");
|
2012-04-07 18:32:47 +02:00
|
|
|
struct TM_Action *tmp, *tmp2, *pom = queue;
|
|
|
|
tmp = NULL;
|
|
|
|
while (pom!=NULL) {
|
|
|
|
if (pom->active) {
|
2012-04-07 18:41:39 +02:00
|
|
|
if (*pom->function) (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
|
|
|
else {
|
2012-04-07 23:03:34 +02:00
|
|
|
if (pom->timer) al_destroy_timer(pom->timer);
|
2012-04-07 18:32:47 +02:00
|
|
|
}
|
2012-04-07 18:41:39 +02:00
|
|
|
} else {
|
|
|
|
TM_DestroyArgs(pom->arguments);
|
|
|
|
pom->arguments = NULL;
|
2012-04-07 18:32:47 +02:00
|
|
|
}
|
|
|
|
if ((!tmp) || (tmp->next==pom)) {
|
|
|
|
tmp = pom;
|
|
|
|
pom = pom->next;
|
|
|
|
} else {
|
2012-04-09 17:17:16 +02:00
|
|
|
free(pom->name);
|
2012-04-07 18:32:47 +02:00
|
|
|
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) {
|
2012-04-07 18:41:39 +02:00
|
|
|
if (*pom->function) (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
|
|
|
else {
|
2012-04-07 23:03:34 +02:00
|
|
|
if (pom->timer) al_destroy_timer(pom->timer);
|
2012-04-07 18:32:47 +02:00
|
|
|
}
|
2012-04-07 18:41:39 +02:00
|
|
|
} else {
|
|
|
|
TM_DestroyArgs(pom->arguments);
|
|
|
|
pom->arguments = NULL;
|
2012-04-07 18:32:47 +02:00
|
|
|
}
|
|
|
|
if ((!tmp) || (tmp->next==pom)) {
|
|
|
|
tmp = pom;
|
|
|
|
pom = pom->next;
|
|
|
|
} else {
|
2012-04-09 17:17:16 +02:00
|
|
|
free(pom->name);
|
2012-04-07 18:32:47 +02:00
|
|
|
free(pom);
|
|
|
|
tmp2 = tmp;
|
|
|
|
if (!tmp) pom=background->next;
|
|
|
|
else pom=tmp->next;
|
|
|
|
tmp = tmp2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-06 23:32:15 +02:00
|
|
|
game = NULL;
|
2012-04-06 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-06 23:32:15 +02:00
|
|
|
struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, void* arg) {
|
2012-04-08 23:25:14 +02:00
|
|
|
struct TM_Arguments* tmp = args;
|
2012-04-07 20:32:47 +02:00
|
|
|
if (!args) {
|
|
|
|
tmp = malloc(sizeof(struct TM_Arguments));
|
|
|
|
tmp->value = arg;
|
|
|
|
tmp->next = NULL;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
while (tmp->next) {
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
tmp->next = malloc(sizeof(struct TM_Arguments));
|
|
|
|
tmp->next->value = arg;
|
|
|
|
tmp->next->next = NULL;
|
|
|
|
return args;
|
2012-04-06 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TM_DestroyArgs(struct TM_Arguments* args) {
|
2012-04-07 18:32:47 +02:00
|
|
|
struct TM_Arguments *pom;
|
|
|
|
while (args) {
|
|
|
|
pom = args->next;
|
|
|
|
free(args);
|
|
|
|
args = pom;
|
|
|
|
}
|
|
|
|
}
|
2012-04-08 23:25:14 +02:00
|
|
|
|
|
|
|
bool TM_Initialized() {
|
|
|
|
if (game) return true;
|
|
|
|
return false;
|
|
|
|
}
|