mirror of
https://gitlab.com/dosowisko.net/libsuperderpy.git
synced 2025-02-01 19:16: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_Init(game);
|
||||||
TM_AddAction(&napis, NULL);
|
TM_AddAction(&napis, NULL);
|
||||||
TM_AddAction(&napis, NULL);
|
TM_AddAction(&napis, NULL);
|
||||||
|
TM_AddDelay(2000);
|
||||||
TM_AddAction(&napis, NULL);
|
TM_AddAction(&napis, NULL);
|
||||||
TM_AddBackgroundAction(&napis2, NULL, 0);
|
TM_AddBackgroundAction(&napis2, NULL, 1125);
|
||||||
TM_AddAction(&wyjscie, NULL);
|
TM_AddAction(&wyjscie, NULL);
|
||||||
TM_AddBackgroundAction(&FadeIn, NULL, 0);
|
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.
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
#include <allegro5/allegro.h>
|
#include <allegro5/allegro.h>
|
||||||
#include <stdio.h>
|
#include "main.h"
|
||||||
#include "timeline.h"
|
#include "timeline.h"
|
||||||
|
|
||||||
unsigned int lastid;
|
unsigned int lastid;
|
||||||
|
@ -28,6 +28,7 @@ struct TM_Action *queue, *background;
|
||||||
bool paused;
|
bool paused;
|
||||||
|
|
||||||
void TM_Init(struct Game* g) {
|
void TM_Init(struct Game* g) {
|
||||||
|
PrintConsole(g, "Timeline Manager: init");
|
||||||
game = g;
|
game = g;
|
||||||
lastid = 0;
|
lastid = 0;
|
||||||
queue = NULL;
|
queue = NULL;
|
||||||
|
@ -44,6 +45,7 @@ void TM_Process() {
|
||||||
if (*queue->function) {
|
if (*queue->function) {
|
||||||
queue->active = true;
|
queue->active = true;
|
||||||
if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) {
|
if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) {
|
||||||
|
PrintConsole(game, "Timeline Manager: destroy action (queue) %d", queue->id);
|
||||||
queue->active=false;
|
queue->active=false;
|
||||||
(*queue->function)(game, queue, TM_ACTIONSTATE_DESTROY);
|
(*queue->function)(game, queue, TM_ACTIONSTATE_DESTROY);
|
||||||
struct TM_Action *tmp = queue;
|
struct TM_Action *tmp = queue;
|
||||||
|
@ -51,7 +53,14 @@ void TM_Process() {
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
// process all elements from background marked as active
|
||||||
|
@ -62,6 +71,7 @@ void TM_Process() {
|
||||||
if (*pom->function) {
|
if (*pom->function) {
|
||||||
if ((*pom->function)(game, pom, TM_ACTIONSTATE_RUNNING)) {
|
if ((*pom->function)(game, pom, TM_ACTIONSTATE_RUNNING)) {
|
||||||
pom->active=false;
|
pom->active=false;
|
||||||
|
PrintConsole(game, "Timeline Manager: destroy action (background) %d", pom->id);
|
||||||
(*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
(*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
tmp->next = pom->next;
|
tmp->next = pom->next;
|
||||||
|
@ -70,7 +80,12 @@ void TM_Process() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: handle delay
|
// delay
|
||||||
|
if (tmp) {
|
||||||
|
tmp->next = pom->next;
|
||||||
|
} else {
|
||||||
|
background = pom->next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((!tmp) || (tmp->next==pom)) {
|
if ((!tmp) || (tmp->next==pom)) {
|
||||||
|
@ -87,13 +102,40 @@ void TM_Process() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM_HandleEvent(ALLEGRO_EVENT *ev) {
|
void TM_HandleEvent(ALLEGRO_EVENT *ev) {
|
||||||
|
if (ev->type != ALLEGRO_EVENT_TIMER) return;
|
||||||
if (!game) return;
|
if (!game) return;
|
||||||
if (paused) 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));
|
struct TM_Action *action = malloc(sizeof(struct TM_Action));
|
||||||
if (queue) {
|
if (queue) {
|
||||||
struct TM_Action *pom = 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->timer = NULL;
|
||||||
action->active = false;
|
action->active = false;
|
||||||
action->delay = 0;
|
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);
|
(*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));
|
struct TM_Action *action = malloc(sizeof(struct TM_Action));
|
||||||
if (background) {
|
if (background) {
|
||||||
struct TM_Action *pom = 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->next = NULL;
|
||||||
action->function = func;
|
action->function = func;
|
||||||
action->arguments = args;
|
action->arguments = args;
|
||||||
action->timer = NULL; // TODO
|
|
||||||
action->delay = delay;
|
action->delay = delay;
|
||||||
|
action->id = ++lastid;
|
||||||
if (delay) {
|
if (delay) {
|
||||||
action->active = false;
|
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 {
|
} else {
|
||||||
|
action->timer = NULL;
|
||||||
action->active = true;
|
action->active = true;
|
||||||
|
PrintConsole(game, "Timeline Manager: init action (background) %d", action->id);
|
||||||
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
|
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM_AddDelay(int delay) {
|
void TM_AddDelay(float delay) {
|
||||||
TM_AddAction(NULL, NULL);
|
/*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) {
|
void TM_Pause(bool pause) {
|
||||||
paused = 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() {
|
void TM_Destroy() {
|
||||||
if (!game) return;
|
if (!game) return;
|
||||||
|
PrintConsole(game, "Timeline Manager: destroy");
|
||||||
struct TM_Action *tmp, *tmp2, *pom = queue;
|
struct TM_Action *tmp, *tmp2, *pom = queue;
|
||||||
tmp = NULL;
|
tmp = NULL;
|
||||||
while (pom!=NULL) {
|
while (pom!=NULL) {
|
||||||
if (pom->active) {
|
if (pom->active) {
|
||||||
if (*pom->function) (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
if (*pom->function) (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
||||||
else {
|
else {
|
||||||
// TODO: handle delay
|
if (pom->timer) al_destroy_timer(pom->timer);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TM_DestroyArgs(pom->arguments);
|
TM_DestroyArgs(pom->arguments);
|
||||||
|
@ -177,7 +254,7 @@ void TM_Destroy() {
|
||||||
if (pom->active) {
|
if (pom->active) {
|
||||||
if (*pom->function) (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
if (*pom->function) (*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
|
||||||
else {
|
else {
|
||||||
// TODO: handle delay
|
if (pom->timer) al_destroy_timer(pom->timer);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TM_DestroyArgs(pom->arguments);
|
TM_DestroyArgs(pom->arguments);
|
||||||
|
|
|
@ -40,15 +40,16 @@ struct TM_Action {
|
||||||
bool active;
|
bool active;
|
||||||
int delay;
|
int delay;
|
||||||
struct TM_Action *next;
|
struct TM_Action *next;
|
||||||
|
unsigned int id;
|
||||||
//*prev;
|
//*prev;
|
||||||
};
|
};
|
||||||
|
|
||||||
void TM_Init(struct Game* game);
|
void TM_Init(struct Game* game);
|
||||||
void TM_Process();
|
void TM_Process();
|
||||||
void TM_HandleEvent(ALLEGRO_EVENT *ev);
|
void TM_HandleEvent(ALLEGRO_EVENT *ev);
|
||||||
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);
|
||||||
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);
|
||||||
void TM_AddDelay(int delay);
|
void TM_AddDelay(float delay);
|
||||||
void TM_Pause(bool pause);
|
void TM_Pause(bool pause);
|
||||||
void TM_Destroy();
|
void TM_Destroy();
|
||||||
struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, void* arg);
|
struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, void* arg);
|
||||||
|
|
Loading…
Reference in a new issue