diff --git a/src/level.c b/src/level.c index d257992..ae3f1ea 100644 --- a/src/level.c +++ b/src/level.c @@ -23,6 +23,9 @@ #include "config.h" #include "pause.h" #include "level.h" +#include "timeline.h" + +int x = 0, x2 = 0; void Level_Passed(struct Game *game) { if (game->level.current_level<6) { @@ -44,11 +47,42 @@ void Level_Draw(struct Game *game) { if (game->level.current_level!=1) Moonwalk_Draw(game); else { al_clear_to_color(al_map_rgb(0,0,0)); + TM_Process(); } } +bool napis(struct Game *game, struct TM_Action *lol) { + al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), x, 20, 0, "wat"); + x+=10; + if (x>1000) { x=0; return true; } + return false; +} + +bool napis2(struct Game *game, struct TM_Action *lol) { + al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), x2, 100, 0, "lol"); + x2+=5; + if (x2>1000) { x2=0; return true; } + return false; +} + +bool wyjscie(struct Game *game, struct TM_Action *lol) { + Level_Passed(game); + game->gamestate = GAMESTATE_LOADING; + game->loadstate = GAMESTATE_MAP; + return true; +} + void Level_Load(struct Game *game) { + al_clear_to_color(al_map_rgb(0,0,0)); if (game->level.current_level!=1) Moonwalk_Load(game); + else { + TM_Init(game); + TM_AddAction(&napis, NULL); + TM_AddAction(&napis, NULL); + TM_AddAction(&napis, NULL); + TM_AddBackgroundAction(&napis2, NULL, 0); + TM_AddAction(&wyjscie, NULL); + } } int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { @@ -61,6 +95,10 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { return 0; } +void Level_ProcessLogic(struct Game *game, ALLEGRO_EVENT *ev) { + if (game->level.current_level==1) TM_HandleEvent(ev); +} + void Level_Preload(struct Game *game) { Pause_Preload(game); if (game->level.current_level!=1) Moonwalk_Preload(game); @@ -69,7 +107,9 @@ void Level_Preload(struct Game *game) { void Level_Unload(struct Game *game) { Pause_Unload_Real(game); if (game->level.current_level!=1) Moonwalk_Unload(game); - else Level_Passed(game); + else { + TM_Destroy(); + } } void Level_UnloadBitmaps(struct Game *game) { diff --git a/src/level.h b/src/level.h index d408710..27160ed 100644 --- a/src/level.h +++ b/src/level.h @@ -25,6 +25,7 @@ void Level_Draw(struct Game *game); void Level_Preload(struct Game *game); void Level_Unload(struct Game *game); void Level_Load(struct Game *game); +void Level_ProcessLogic(struct Game *game, ALLEGRO_EVENT *ev); int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev); void Level_UnloadBitmaps(struct Game *game); void Level_PreloadBitmaps(struct Game *game); \ No newline at end of file diff --git a/src/main.c b/src/main.c index a4c0863..77cdb3c 100644 --- a/src/main.c +++ b/src/main.c @@ -391,7 +391,7 @@ int main(int argc, char **argv){ ALLEGRO_EVENT ev; al_wait_for_event(game.event_queue, &ev); - if(ev.type == ALLEGRO_EVENT_TIMER) { + if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game.timer)) { redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { @@ -418,6 +418,10 @@ int main(int argc, char **argv){ game.gamestate = GAMESTATE_LOADING; game.loadstate = GAMESTATE_MENU; } + } else { + if (game.gamestate == GAMESTATE_LEVEL) { + Level_ProcessLogic(&game, &ev); + } } if(redraw && al_is_event_queue_empty(game.event_queue)) { diff --git a/src/timeline.c b/src/timeline.c index aec2fc9..16c4604 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -19,35 +19,129 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ #include +#include #include "timeline.h" -int lastid; +unsigned int lastid; +struct Game* game = NULL; +struct TM_Action *queue, *background; +bool paused; -void TM_Init() { +void TM_Init(struct Game* g) { + game = g; + lastid = 0; + queue = NULL; + background = NULL; + paused = false; +} + +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) { + if ((*queue->function)(game, queue)) { + struct TM_Action *tmp = queue; + queue = queue->next; + free(tmp); + } + } else { + // TODO: handle delay + } + } + // 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) { + if ((*pom->function)(game, pom)) { + if (tmp) { + tmp->next = pom->next; + } else { + background = pom->next; + } + } + } else { + // TODO: handle delay + } + } + if ((!tmp) || (tmp->next==pom)) { + tmp = pom; + pom = pom->next; + } else { + free(pom); + tmp2 = tmp; + if (!tmp) pom=background->next; + else pom=tmp->next; + tmp = tmp2; + } + } +} + +void TM_HandleEvent(ALLEGRO_EVENT *ev) { + if (!game) return; + if (paused) return; + // TODO: mark action as active } -void TM_AddAction(void *func, struct TM_Arguments* args) { - +void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args) { + 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; + action->timer = NULL; + action->active = false; + action->delay = 0; } -void TM_AddBackgroundAction(void *func, struct TM_Arguments* args, int delay) { - +void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args, int delay) { + 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; + action->timer = NULL; + action->active = true; + action->delay = 0; } void TM_AddDelay(int delay) { - + TM_AddAction(NULL, NULL); } void TM_Pause(bool pause) { - + paused = pause; + // TODO: pause timers } void TM_Destroy() { - + // TODO: delete everything from queues + // maybe delete all args too? + game = NULL; } -struct TM_Arguments* TM_AddToArgs() { +struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, void* arg) { return NULL; } diff --git a/src/timeline.h b/src/timeline.h index 3202f48..f49a47e 100644 --- a/src/timeline.h +++ b/src/timeline.h @@ -19,15 +19,31 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "main.h" + struct TM_Arguments { - + void *value; + struct TM_Arguments *next; + //*prev; }; -void TM_Init(); -void TM_AddAction(void *func, struct TM_Arguments* args); -void TM_AddBackgroundAction(void *func, struct TM_Arguments* args, int delay); +struct TM_Action { + bool (*function)(struct Game*, struct TM_Action*); + struct TM_Arguments *arguments; + ALLEGRO_TIMER *timer; + bool active; + int delay; + struct TM_Action *next; + //*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*), struct TM_Arguments* args); +void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args, int delay); void TM_AddDelay(int delay); void TM_Pause(bool pause); void TM_Destroy(); -struct TM_Arguments* TM_AddToArgs(); -void TM_DestroyArgs(struct TM_Arguments* args); \ No newline at end of file +struct TM_Arguments* TM_AddToArgs(struct TM_Arguments* args, void* arg); +void TM_DestroyArgs(struct TM_Arguments* args);