mirror of
https://gitlab.com/dosowisko.net/libsuperderpy.git
synced 2025-02-01 11:06:44 +01:00
implement delays in timeline manager
This commit is contained in:
parent
db056c3a2a
commit
efaa1803ea
3 changed files with 97 additions and 18 deletions
|
@ -160,8 +160,9 @@ void Level_Load(struct Game *game) {
|
|||
TM_Init(game);
|
||||
TM_AddAction(&napis, NULL);
|
||||
TM_AddAction(&napis, NULL);
|
||||
TM_AddDelay(2000);
|
||||
TM_AddAction(&napis, NULL);
|
||||
TM_AddBackgroundAction(&napis2, NULL, 0);
|
||||
TM_AddBackgroundAction(&napis2, NULL, 1125);
|
||||
TM_AddAction(&wyjscie, NULL);
|
||||
TM_AddBackgroundAction(&FadeIn, NULL, 0);
|
||||
}
|
||||
|
|
103
src/timeline.c
103
src/timeline.c
|
@ -19,7 +19,7 @@
|
|||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
#include <allegro5/allegro.h>
|
||||
#include <stdio.h>
|
||||
#include "main.h"
|
||||
#include "timeline.h"
|
||||
|
||||
unsigned int lastid;
|
||||
|
@ -28,6 +28,7 @@ struct TM_Action *queue, *background;
|
|||
bool paused;
|
||||
|
||||
void TM_Init(struct Game* g) {
|
||||
PrintConsole(g, "Timeline Manager: init");
|
||||
game = g;
|
||||
lastid = 0;
|
||||
queue = NULL;
|
||||
|
@ -44,6 +45,7 @@ void TM_Process() {
|
|||
if (*queue->function) {
|
||||
queue->active = true;
|
||||
if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) {
|
||||
PrintConsole(game, "Timeline Manager: destroy action (queue) %d", queue->id);
|
||||
queue->active=false;
|
||||
(*queue->function)(game, queue, TM_ACTIONSTATE_DESTROY);
|
||||
struct TM_Action *tmp = queue;
|
||||
|
@ -51,7 +53,14 @@ void TM_Process() {
|
|||
free(tmp);
|
||||
}
|
||||
} else {
|
||||
// TODO: handle delay
|
||||
// delay
|
||||
if (queue->active) {
|
||||
struct TM_Action *tmp = queue;
|
||||
queue = queue->next;
|
||||
free(tmp);
|
||||
} else {
|
||||
al_start_timer(queue->timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
// process all elements from background marked as active
|
||||
|
@ -62,6 +71,7 @@ void TM_Process() {
|
|||
if (*pom->function) {
|
||||
if ((*pom->function)(game, pom, TM_ACTIONSTATE_RUNNING)) {
|
||||
pom->active=false;
|
||||
PrintConsole(game, "Timeline Manager: destroy action (background) %d", pom->id);
|
||||
(*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
||||
if (tmp) {
|
||||
tmp->next = pom->next;
|
||||
|
@ -70,7 +80,12 @@ void TM_Process() {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: handle delay
|
||||
// delay
|
||||
if (tmp) {
|
||||
tmp->next = pom->next;
|
||||
} else {
|
||||
background = pom->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((!tmp) || (tmp->next==pom)) {
|
||||
|
@ -87,13 +102,40 @@ void TM_Process() {
|
|||
}
|
||||
|
||||
void TM_HandleEvent(ALLEGRO_EVENT *ev) {
|
||||
if (ev->type != ALLEGRO_EVENT_TIMER) return;
|
||||
if (!game) return;
|
||||
if (paused) return;
|
||||
// TODO: find proper action and mark it as active
|
||||
|
||||
if (queue) {
|
||||
if (ev->timer.source == queue->timer) {
|
||||
PrintConsole(game, "Timeline Manager: event matched (queue) %d", queue->id);
|
||||
queue->active=true;
|
||||
al_destroy_timer(queue->timer);
|
||||
queue->timer = NULL;
|
||||
if (queue->function) {
|
||||
PrintConsole(game, "Timeline Manager: init action (queue) %d", queue->id);
|
||||
(*queue->function)(game, queue, TM_ACTIONSTATE_INIT);
|
||||
} else {
|
||||
PrintConsole(game, "Timeline Manager: delay reached (queue) %d", queue->id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
struct TM_Action *pom = background;
|
||||
while (pom) {
|
||||
if (ev->timer.source == pom->timer) {
|
||||
PrintConsole(game, "Timeline Manager: event matched (background) %d", pom->id);
|
||||
pom->active=true;
|
||||
al_destroy_timer(pom->timer);
|
||||
pom->timer = NULL;
|
||||
PrintConsole(game, "Timeline Manager: delay reached, init action (background) %d", pom->id);
|
||||
(*pom->function)(game, pom, TM_ACTIONSTATE_INIT);
|
||||
return;
|
||||
}
|
||||
pom=pom->next;
|
||||
}
|
||||
}
|
||||
|
||||
void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args) {
|
||||
struct TM_Action* 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;
|
||||
|
@ -110,10 +152,15 @@ void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionSt
|
|||
action->timer = NULL;
|
||||
action->active = false;
|
||||
action->delay = 0;
|
||||
action->id = ++lastid;
|
||||
if (action->function) {
|
||||
PrintConsole(game, "Timeline Manager: init action (queue) %d", action->id);
|
||||
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
|
||||
}
|
||||
return action;
|
||||
}
|
||||
|
||||
void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay) {
|
||||
void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, float delay) {
|
||||
struct TM_Action *action = malloc(sizeof(struct TM_Action));
|
||||
if (background) {
|
||||
struct TM_Action *pom = background;
|
||||
|
@ -127,34 +174,64 @@ void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum T
|
|||
action->next = NULL;
|
||||
action->function = func;
|
||||
action->arguments = args;
|
||||
action->timer = NULL; // TODO
|
||||
action->delay = delay;
|
||||
action->id = ++lastid;
|
||||
if (delay) {
|
||||
action->active = false;
|
||||
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);
|
||||
} else {
|
||||
action->timer = NULL;
|
||||
action->active = true;
|
||||
PrintConsole(game, "Timeline Manager: init action (background) %d", action->id);
|
||||
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
|
||||
}
|
||||
}
|
||||
|
||||
void TM_AddDelay(int delay) {
|
||||
TM_AddAction(NULL, NULL);
|
||||
void TM_AddDelay(float delay) {
|
||||
/*int *tmp;
|
||||
tmp = malloc(sizeof(int));
|
||||
*tmp = delay;
|
||||
TM_AddAction(NULL, TM_AddToArgs(NULL, tmp));*/
|
||||
struct TM_Action* tmp = TM_AddAction(NULL, NULL);
|
||||
PrintConsole(game, "Timeline Manager: adding delay %f ms %d", delay, tmp->id);
|
||||
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));
|
||||
}
|
||||
|
||||
void TM_Pause(bool pause) {
|
||||
paused = pause;
|
||||
// TODO: pause timers
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void TM_Destroy() {
|
||||
if (!game) return;
|
||||
PrintConsole(game, "Timeline Manager: destroy");
|
||||
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);
|
||||
else {
|
||||
// TODO: handle delay
|
||||
if (pom->timer) al_destroy_timer(pom->timer);
|
||||
}
|
||||
} else {
|
||||
TM_DestroyArgs(pom->arguments);
|
||||
|
@ -177,7 +254,7 @@ void TM_Destroy() {
|
|||
if (pom->active) {
|
||||
if (*pom->function) (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
||||
else {
|
||||
// TODO: handle delay
|
||||
if (pom->timer) al_destroy_timer(pom->timer);
|
||||
}
|
||||
} else {
|
||||
TM_DestroyArgs(pom->arguments);
|
||||
|
|
|
@ -40,15 +40,16 @@ struct TM_Action {
|
|||
bool active;
|
||||
int delay;
|
||||
struct TM_Action *next;
|
||||
unsigned int id;
|
||||
//*prev;
|
||||
};
|
||||
|
||||
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*, 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);
|
||||
struct TM_Action* 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, float delay);
|
||||
void TM_AddDelay(float delay);
|
||||
void TM_Pause(bool pause);
|
||||
void TM_Destroy();
|
||||
struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, void* arg);
|
||||
|
|
Loading…
Reference in a new issue