From e6fa44514e600a1f02839dfe8054afcd57c24b0a Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Wed, 26 Sep 2012 03:08:58 +0200 Subject: [PATCH] refactored every gamestate for logic spearation except first level --- src/about.c | 47 +++++------ src/about.h | 1 + src/disclaimer.c | 22 +----- src/intro.c | 74 ++++++----------- src/intro.h | 1 + src/level.c | 10 ++- src/level.h | 3 +- src/loading.c | 36 ++++++--- src/main.c | 202 ++++++++++++++++++++++++++--------------------- src/main.h | 8 +- src/map.c | 24 ++---- src/map.h | 1 + src/menu.c | 37 ++------- src/menu.h | 1 + src/moonwalk.c | 62 +++++---------- src/moonwalk.h | 1 + 16 files changed, 231 insertions(+), 299 deletions(-) diff --git a/src/about.c b/src/about.c index a33731c..ef1e60d 100644 --- a/src/about.c +++ b/src/about.c @@ -21,17 +21,26 @@ #include #include "about.h" +void About_Logic(struct Game *game) { + if (al_get_sample_instance_position(game->about.music)<700000) { return; } + if (game->about.fadeloop>=0) { + if (game->about.fadeloop==0) PrintConsole(game, "Fade in"); + game->about.fadeloop+=5; + if (game->about.fadeloop>=256) { + al_destroy_bitmap(game->about.fade_bitmap); + game->about.fadeloop=-1; + } + return; + } + game->about.x+=0.00025; +} + void About_Draw(struct Game *game) { /*PrintConsole(game, "%d", al_get_sample_instance_position(game->about.music));*/ if (al_get_sample_instance_position(game->about.music)<700000) { al_clear_to_color(al_map_rgba(0,0,0,0)); return; } if (game->about.fadeloop>=0) { if (game->about.fadeloop==0) PrintConsole(game, "Fade in"); al_draw_tinted_bitmap(game->about.fade_bitmap,al_map_rgba_f(game->about.fadeloop/255.0,game->about.fadeloop/255.0,game->about.fadeloop/255.0,1),0,0,0); - game->about.fadeloop+=tps(game, 300); - if (game->about.fadeloop>=256) { - al_destroy_bitmap(game->about.fade_bitmap); - game->about.fadeloop=-1; - } return; } @@ -43,7 +52,6 @@ void About_Draw(struct Game *game) { subbitmap = al_create_sub_bitmap(game->about.text_bitmap, 0, x*al_get_bitmap_height(game->about.text_bitmap), al_get_bitmap_width(game->about.text_bitmap), game->viewportHeight); al_draw_rotated_bitmap(subbitmap, al_get_bitmap_width(subbitmap)/2.0, al_get_bitmap_height(subbitmap)/2.0, game->viewportWidth*0.5+al_get_bitmap_width(subbitmap)/2.0, game->viewportHeight*0.1+al_get_bitmap_height(subbitmap)/2.0, -0.11, 0); al_destroy_bitmap(subbitmap); - game->about.x+=tps(game, 60*0.00025); if (game->about.x>1) { UnloadGameState(game); game->loadstate = GAMESTATE_MENU; @@ -184,7 +192,7 @@ void About_Preload(struct Game *game, void (*progress)(struct Game*, float)) { PROGRESS; game->about.fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); - + al_set_target_bitmap(game->about.fade_bitmap); al_draw_bitmap(game->about.image, 0, 0, 0); al_draw_bitmap(game->about.letter, game->viewportWidth*0.3, -game->viewportHeight*0.1, 0); @@ -198,29 +206,12 @@ void About_Preload(struct Game *game, void (*progress)(struct Game*, float)) { } void About_Unload(struct Game *game) { - if (game->about.x<0) game->about.x=0; - ALLEGRO_EVENT ev; - game->about.fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); - al_set_target_bitmap(game->about.fade_bitmap); - al_draw_bitmap(game->about.image,0,0,0); - al_draw_bitmap(game->about.letter, game->viewportWidth*0.3, -game->viewportHeight*0.1, 0); - - ALLEGRO_BITMAP* subbitmap; - subbitmap = al_create_sub_bitmap(game->about.text_bitmap, 0, game->about.x*al_get_bitmap_height(game->about.text_bitmap), al_get_bitmap_width(game->about.text_bitmap), game->viewportHeight); - al_draw_rotated_bitmap(subbitmap, al_get_bitmap_width(subbitmap)/2.0, al_get_bitmap_height(subbitmap)/2.0, game->viewportWidth*0.5+al_get_bitmap_width(subbitmap)/2.0, game->viewportHeight*0.1+al_get_bitmap_height(subbitmap)/2.0, -0.11, 0); - al_destroy_bitmap(subbitmap); - al_set_target_bitmap(al_get_backbuffer(game->display)); - float fadeloop; - if (game->about.fadeloop!=0) - for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->about.fade_bitmap, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0); - DrawConsole(game); - al_flip_display(); - } + if (game->about.fadeloop!=0) { + FadeGameState(game, false); + } al_destroy_bitmap(game->about.image); al_destroy_bitmap(game->about.letter); - al_destroy_bitmap(game->about.fade_bitmap); + if (game->about.fadeloop>=0) al_destroy_bitmap(game->about.fade_bitmap); al_destroy_bitmap(game->about.text_bitmap); al_destroy_sample_instance(game->about.music); al_destroy_sample(game->about.sample); diff --git a/src/about.h b/src/about.h index 2d93b32..893b4e8 100644 --- a/src/about.h +++ b/src/about.h @@ -21,6 +21,7 @@ #include "main.h" void About_Draw(struct Game *game); +void About_Logic(struct Game *game); void About_Preload(struct Game *game, void (*progress)(struct Game*, float)); void About_Unload(struct Game *game); void About_Load(struct Game *game); diff --git a/src/disclaimer.c b/src/disclaimer.c index 80d455d..b10143c 100644 --- a/src/disclaimer.c +++ b/src/disclaimer.c @@ -33,16 +33,7 @@ void Disclaimer_Draw(struct Game *game) { } void Disclaimer_Load(struct Game *game) { - ALLEGRO_EVENT ev; - float fadeloop; - for (fadeloop=255; fadeloop>0; fadeloop-=tps(game, 600)) { - al_wait_for_event(game->event_queue, &ev); - Disclaimer_Draw(game); - al_draw_filled_rectangle(0,0,game->viewportWidth,game->viewportHeight,al_map_rgba(0,0,0,fadeloop)); - DrawConsole(game); - al_flip_display(); - } - Disclaimer_Draw(game); + FadeGameState(game, true); } int Disclaimer_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { @@ -62,18 +53,9 @@ void Disclaimer_Preload(struct Game *game, void (*progress)(struct Game*, float) } void Disclaimer_Unload(struct Game *game) { - ALLEGRO_EVENT ev; - float fadeloop; - for (fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)) { - al_wait_for_event(game->event_queue, &ev); - Disclaimer_Draw(game); - al_draw_filled_rectangle(0,0,game->viewportWidth,game->viewportHeight,al_map_rgba(0,0,0,fadeloop)); - DrawConsole(game); - al_flip_display(); - } + FadeGameState(game, false); if (!game->menu.loaded) { al_destroy_font(game->menu.font); al_destroy_font(game->menu.font_selected); } - al_clear_to_color(al_map_rgb(0,0,0)); } diff --git a/src/intro.c b/src/intro.c index a608e54..a17e7a9 100644 --- a/src/intro.c +++ b/src/intro.c @@ -43,8 +43,6 @@ void AnimPage(struct Game *game, int page, ALLEGRO_COLOR tint) { if (page==4) { amount1=5; amount2=3; anim*=2; } if (page<5) al_draw_tinted_bitmap_region(game->intro.animsprites[page],tint,game->viewportWidth*0.3125*(int)fmod(anim,amount1),game->viewportHeight*0.63*(((int)(anim/amount1))%amount2),game->viewportWidth*0.3125, game->viewportHeight*0.63,offset+game->viewportWidth*1.08, game->viewportHeight*0.18,0); - - game->intro.anim += tps(game, 2); } void FillPage(struct Game *game, int page) { @@ -142,6 +140,20 @@ void FillPage(struct Game *game, int page) { al_destroy_bitmap(second); } +void Intro_Logic(struct Game *game) { + game->intro.anim += 1.0/20.0; + if (game->intro.in_animation) { + int old = game->intro.position%game->viewportWidth; + game->intro.position -= 10; + if (game->intro.position%game->viewportWidth>old) { + game->intro.in_animation = false; + FillPage(game, game->intro.page+1); + PrintConsole(game, "Animation finished."); + al_set_audio_stream_playing(game->intro.audiostream, true); + } + } +} + void Intro_Draw(struct Game *game) { al_clear_to_color(al_map_rgb(0,0,0)); if (game->intro.in_animation) { @@ -155,44 +167,21 @@ void Intro_Draw(struct Game *game) { al_draw_text_with_shadow(game->intro.font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight*0.90, ALLEGRO_ALIGN_CENTRE, "Press any key to continue or escape to skip..."); al_draw_bitmap(game->intro.frame, 0, 0, 0); /*PrintConsole(game, "drawing");*/ - if (game->intro.in_animation) { - /*PrintConsole(game, "animating");*/ - int old = game->intro.position%game->viewportWidth; - if (tps(game, 600)) game->intro.position -= tps(game, 600); - else game->intro.position -= 1; - /*PrintConsole(game, "%d", game->intro.position%game->viewportWidth);*/ - if (game->intro.position%game->viewportWidth>old) { - /*DrawConsole(game); - al_flip_display();*/ - game->intro.in_animation = false; - FillPage(game, game->intro.page+1); - PrintConsole(game, "Animation finished."); - al_set_audio_stream_playing(game->intro.audiostream, true); - } - else if (game->intro.position<=-4*game->viewportWidth) { - PrintConsole(game, "This was the last page."); - UnloadGameState(game); - game->loadstate = GAMESTATE_MAP; - PrintConsole(game, "Chainloading GAMESTATE_MAP..."); - LoadGameState(game); - } + if (game->intro.in_animation && game->intro.position<=-4*game->viewportWidth) { + PrintConsole(game, "This was the last page."); + game->intro.in_animation = false; + game->intro.page--; + UnloadGameState(game); + game->loadstate = GAMESTATE_MAP; + PrintConsole(game, "Chainloading GAMESTATE_MAP..."); + LoadGameState(game); } } void Intro_Load(struct Game *game) { al_play_sample_instance(game->intro.music); - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->intro.table,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0); - AnimPage(game, game->intro.page+1, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1)); - al_draw_tinted_bitmap(game->intro.frame,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0); - DrawConsole(game); - al_flip_display(); - } + FadeGameState(game, true); al_set_audio_stream_playing(game->intro.audiostream, true); - Intro_Draw(game); } int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { @@ -269,26 +258,11 @@ void Intro_Preload(struct Game *game, void (*progress)(struct Game*, float)) { } void Intro_Unload(struct Game *game) { + FadeGameState(game, false); if (game->intro.audiostream) { al_set_audio_stream_playing(game->intro.audiostream, false); al_destroy_audio_stream(game->intro.audiostream); } - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - if (game->intro.in_animation) { - al_draw_tinted_bitmap(game->intro.table, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), -1*game->viewportWidth + (cos(((-1*((game->intro.position)%game->viewportWidth))/(float)game->viewportWidth)*(ALLEGRO_PI))/2.0)*game->viewportWidth + game->viewportWidth/2.0, 0, 0); - AnimPage(game, game->intro.page, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1)); - } - else { - al_draw_tinted_bitmap(game->intro.table, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0); - AnimPage(game, game->intro.page+1, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1)); - } - al_draw_tinted_bitmap(game->intro.frame,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0); - DrawConsole(game); - al_flip_display(); - } al_destroy_bitmap(game->intro.frame); al_destroy_bitmap(game->intro.table); int i; diff --git a/src/intro.h b/src/intro.h index 6f1a1e0..a8a433e 100644 --- a/src/intro.h +++ b/src/intro.h @@ -21,6 +21,7 @@ #include "main.h" void Intro_Draw(struct Game *game); +void Intro_Logic(struct Game *game); void Intro_Preload(struct Game *game, void (*progress)(struct Game*, float)); void Intro_Unload(struct Game *game); void Intro_Load(struct Game *game); diff --git a/src/level.c b/src/level.c index 5bc6c13..f2f79f3 100644 --- a/src/level.c +++ b/src/level.c @@ -99,6 +99,10 @@ void Level_Passed(struct Game *game) { } } +void Level_Logic(struct Game *game) { + if (game->level.current_level!=1) Moonwalk_Logic(game); +} + void Level_Draw(struct Game *game) { if (!al_get_sample_instance_playing(game->level.music) && (game->loadstate==GAMESTATE_LEVEL)) { al_set_sample_instance_playing(game->level.music, true); @@ -380,7 +384,7 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { return 0; } -void Level_ProcessLogic(struct Game *game, ALLEGRO_EVENT *ev) { +void Level_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev) { if (game->level.handle_input) { if ((ev->type==ALLEGRO_EVENT_KEY_UP) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) { game->level.speed_modifier = 1; @@ -427,12 +431,12 @@ void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)) { void Level_Unload(struct Game *game) { Pause_Unload_Real(game); - al_destroy_sample_instance(game->level.music); - al_destroy_sample(game->level.sample); if (game->level.current_level!=1) Moonwalk_Unload(game); else { TM_Destroy(); } + al_destroy_sample_instance(game->level.music); + al_destroy_sample(game->level.sample); struct Obstacle *t = game->level.obstacles; if (t) { while (t->next) { diff --git a/src/level.h b/src/level.h index 87f7826..76a5110 100644 --- a/src/level.h +++ b/src/level.h @@ -23,10 +23,11 @@ void SelectDerpySpritesheet(struct Game *game, char* name); void Level_Passed(struct Game *game); void Level_Draw(struct Game *game); +void Level_Logic(struct Game *game); void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)); void Level_Unload(struct Game *game); void Level_Load(struct Game *game); -void Level_ProcessLogic(struct Game *game, ALLEGRO_EVENT *ev); +void Level_ProcessEvent(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, void (*progress)(struct Game*, float)); diff --git a/src/loading.c b/src/loading.c index 3fd8a3d..39d501a 100644 --- a/src/loading.c +++ b/src/loading.c @@ -31,13 +31,18 @@ void Progress(struct Game *game, float p) { } void Loading_Draw(struct Game *game) { - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){ + float fadeloop=0; + while (fadeloop<256) { + ALLEGRO_EVENT ev; al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->loading.loading_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0); - DrawConsole(game); - al_flip_display(); + if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->timer)) { + fadeloop+=10; + } + if (al_is_event_queue_empty(game->event_queue)) { + al_draw_tinted_bitmap(game->loading.loading_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0); + DrawConsole(game); + al_flip_display(); + } } al_draw_bitmap(game->loading.loading_bitmap,0,0,0); @@ -49,12 +54,21 @@ void Loading_Draw(struct Game *game) { al_wait_for_vsync(); al_start_timer(game->timer); - for(fadeloop=255; fadeloop>0; fadeloop-=tps(game, 600)){ + fadeloop=0; + while (fadeloop<256) { + ALLEGRO_EVENT ev; al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->loading.loading_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0); - al_draw_filled_rectangle(0, game->viewportHeight*0.985, game->viewportWidth, game->viewportHeight, al_map_rgba(fadeloop,fadeloop,fadeloop,255)); - DrawConsole(game); - al_flip_display(); + if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->timer)) { + fadeloop+=10; + } + if (al_is_event_queue_empty(game->event_queue)) { + al_draw_bitmap(game->loading.loading_bitmap,0,0,0); + al_draw_filled_rectangle(0, game->viewportHeight*0.985, game->viewportWidth, game->viewportHeight, al_map_rgba(255,255,255,255)); + al_draw_filled_rectangle(0, 0, game->viewportWidth, game->viewportHeight, al_map_rgba_f(0,0,0,fadeloop/255.0)); + + DrawConsole(game); + al_flip_display(); + } } al_clear_to_color(al_map_rgb(0,0,0)); DrawConsole(game); diff --git a/src/main.c b/src/main.c index 781aea5..98b9961 100644 --- a/src/main.c +++ b/src/main.c @@ -59,6 +59,9 @@ /*! \brief Macro for drawing active gamestate. */ #define DRAW_STATE(state, name) case state:\ name ## _Draw(game); break; +/*! \brief Macro for invocing logic function of active gamestate. */ +#define LOGIC_STATE(state, name) case state:\ + name ## _Logic(game); break; double old_time = 0, fps; int frames_done = 0; @@ -149,13 +152,13 @@ void PreloadGameState(struct Game *game, void (*progress)(struct Game*, float)) } switch (game->loadstate) { PRELOAD_STATE(GAMESTATE_MENU, Menu) - PRELOAD_STATE(GAMESTATE_LOADING, Loading) - PRELOAD_STATE(GAMESTATE_ABOUT, About) - PRELOAD_STATE(GAMESTATE_INTRO, Intro) - PRELOAD_STATE(GAMESTATE_MAP, Map) - PRELOAD_STATE(GAMESTATE_LEVEL, Level) - PRELOAD_STATE(GAMESTATE_DISCLAIMER, Disclaimer) - default: + PRELOAD_STATE(GAMESTATE_LOADING, Loading) + PRELOAD_STATE(GAMESTATE_ABOUT, About) + PRELOAD_STATE(GAMESTATE_INTRO, Intro) + PRELOAD_STATE(GAMESTATE_MAP, Map) + PRELOAD_STATE(GAMESTATE_LEVEL, Level) + PRELOAD_STATE(GAMESTATE_DISCLAIMER, Disclaimer) + default: PrintConsole(game, "ERROR: Attempted to preload unknown gamestate %d!", game->loadstate); break; } @@ -171,61 +174,107 @@ void UnloadGameState(struct Game *game) { PrintConsole(game, "Just stopping GAMESTATE_MENU..."); Menu_Stop(game); } break; - UNLOAD_STATE(GAMESTATE_PAUSE, Pause) - UNLOAD_STATE(GAMESTATE_LOADING, Loading) - UNLOAD_STATE(GAMESTATE_ABOUT, About) - UNLOAD_STATE(GAMESTATE_INTRO, Intro) - UNLOAD_STATE(GAMESTATE_MAP, Map) - UNLOAD_STATE(GAMESTATE_LEVEL, Level) - UNLOAD_STATE(GAMESTATE_DISCLAIMER, Disclaimer) - default: - PrintConsole(game, "ERROR: Attempted to unload unknown gamestate %d!", game->gamestate); + UNLOAD_STATE(GAMESTATE_PAUSE, Pause) + UNLOAD_STATE(GAMESTATE_LOADING, Loading) + UNLOAD_STATE(GAMESTATE_ABOUT, About) + UNLOAD_STATE(GAMESTATE_INTRO, Intro) + UNLOAD_STATE(GAMESTATE_MAP, Map) + UNLOAD_STATE(GAMESTATE_LEVEL, Level) + UNLOAD_STATE(GAMESTATE_DISCLAIMER, Disclaimer) + default: + PrintConsole(game, "ERROR: Attempted to unload unknown gamestate %d!", game->gamestate); break; } PrintConsole(game, "finished"); } void LoadGameState(struct Game *game) { - switch (game->loadstate) { + game->gamestate = game->loadstate; + game->loadstate = -1; + switch (game->gamestate) { LOAD_STATE(GAMESTATE_MENU, Menu) - LOAD_STATE(GAMESTATE_LOADING, Loading) - LOAD_STATE(GAMESTATE_ABOUT, About) - LOAD_STATE(GAMESTATE_INTRO, Intro) - LOAD_STATE(GAMESTATE_MAP, Map) - LOAD_STATE(GAMESTATE_LEVEL, Level) - LOAD_STATE(GAMESTATE_DISCLAIMER, Disclaimer) - default: + LOAD_STATE(GAMESTATE_LOADING, Loading) + LOAD_STATE(GAMESTATE_ABOUT, About) + LOAD_STATE(GAMESTATE_INTRO, Intro) + LOAD_STATE(GAMESTATE_MAP, Map) + LOAD_STATE(GAMESTATE_LEVEL, Level) + LOAD_STATE(GAMESTATE_DISCLAIMER, Disclaimer) + default: PrintConsole(game, "ERROR: Attempted to load unknown gamestate %d!", game->loadstate); } PrintConsole(game, "finished"); - game->gamestate = game->loadstate; - game->loadstate = -1; } void DrawGameState(struct Game *game) { switch (game->gamestate) { DRAW_STATE(GAMESTATE_MENU, Menu) - DRAW_STATE(GAMESTATE_PAUSE, Pause) - DRAW_STATE(GAMESTATE_LOADING, Loading) - DRAW_STATE(GAMESTATE_ABOUT, About) - DRAW_STATE(GAMESTATE_INTRO, Intro) - DRAW_STATE(GAMESTATE_MAP, Map) - DRAW_STATE(GAMESTATE_LEVEL, Level) - DRAW_STATE(GAMESTATE_DISCLAIMER, Disclaimer) - default: + DRAW_STATE(GAMESTATE_PAUSE, Pause) + DRAW_STATE(GAMESTATE_LOADING, Loading) + DRAW_STATE(GAMESTATE_ABOUT, About) + DRAW_STATE(GAMESTATE_INTRO, Intro) + DRAW_STATE(GAMESTATE_MAP, Map) + DRAW_STATE(GAMESTATE_LEVEL, Level) + DRAW_STATE(GAMESTATE_DISCLAIMER, Disclaimer) + default: game->showconsole = true; - al_clear_to_color(al_map_rgb(0,0,0)); - PrintConsole(game, "ERROR: Unknown gamestate %d reached! (5 sec sleep)", game->gamestate); - DrawConsole(game); - al_flip_display(); - al_rest(5.0); - PrintConsole(game, "Returning to menu..."); - game->gamestate = GAMESTATE_LOADING; - game->loadstate = GAMESTATE_MENU; - break; + al_clear_to_color(al_map_rgb(0,0,0)); + PrintConsole(game, "ERROR: Unknown gamestate %d reached! (5 sec sleep)", game->gamestate); + DrawConsole(game); + al_flip_display(); + al_rest(5.0); + PrintConsole(game, "Returning to menu..."); + game->gamestate = GAMESTATE_LOADING; + game->loadstate = GAMESTATE_MENU; + break; } } +void LogicGameState(struct Game *game) { + switch (game->gamestate) { + LOGIC_STATE(GAMESTATE_ABOUT, About) + LOGIC_STATE(GAMESTATE_MENU, Menu) + LOGIC_STATE(GAMESTATE_MAP, Map) + LOGIC_STATE(GAMESTATE_INTRO, Intro) + LOGIC_STATE(GAMESTATE_LEVEL, Level) + default: + // not every gamestate needs to have logic function + break; + } +} + +void FadeGameState(struct Game *game, bool in) { + ALLEGRO_BITMAP* bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); + al_set_target_bitmap(bitmap); + al_clear_to_color(al_map_rgb(0,0,0)); + al_set_target_bitmap(al_get_backbuffer(game->display)); + float fadeloop; + if (in) { + fadeloop = 255; + } else { + fadeloop = 0; + } + while ((in && fadeloop>=0) || (!in && fadeloop<255)) { + ALLEGRO_EVENT ev; + al_wait_for_event(game->event_queue, &ev); + if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->timer)) { + LogicGameState(game); + if (in) { + fadeloop-=10; + } else { + fadeloop+=10; + } + } + if (al_is_event_queue_empty(game->event_queue)) { + DrawGameState(game); + al_draw_tinted_bitmap(bitmap,al_map_rgba_f(1,1,1,fadeloop/255.0),0,0,0); + DrawConsole(game); + al_flip_display(); + } + } + al_destroy_bitmap(bitmap); + DrawGameState(game); +} + /*! \brief Scales bitmap using software linear filtering method to current target. */ void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height) { if ((al_get_bitmap_width(source)==width) && (al_get_bitmap_height(source)==height)) { @@ -299,8 +348,7 @@ ALLEGRO_BITMAP* LoadScaledBitmap(char* filename, int width, int height) { } float tps(struct Game *game, float t) { - if (game->fps>0) return t/game->fps; - else return t/fps; + return t/60; } void SetupViewport(struct Game *game) { @@ -373,15 +421,12 @@ int main(int argc, char **argv){ InitConfig(); - bool redraw = true; - struct Game game; game.fullscreen = atoi(GetConfigOptionDefault("SuperDerpy", "fullscreen", "1")); game.music = atoi(GetConfigOptionDefault("SuperDerpy", "music", "7")); game.voice = atoi(GetConfigOptionDefault("SuperDerpy", "voice", "10")); game.fx = atoi(GetConfigOptionDefault("SuperDerpy", "fx", "10")); - game.fps = atoi(GetConfigOptionDefault("SuperDerpy", "fps", "0")); game.debug = atoi(GetConfigOptionDefault("SuperDerpy", "debug", "0")); game.width = atoi(GetConfigOptionDefault("SuperDerpy", "width", "800")); if (game.width<320) game.width=320; @@ -481,28 +526,14 @@ int main(int argc, char **argv){ game.showconsole = game.debug; - ALLEGRO_DISPLAY_MODE mode; - al_get_display_mode(0, &mode); - if (mode.refresh_rate < game.fps) { - if (atoi(GetConfigOptionDefault("SuperDerpy", "lower_fps_to_refresh_rate", "0"))) { - PrintConsole(&game, "Refresh rate %d lower than FPS %d, lowering", mode.refresh_rate, game.fps); - game.fps = mode.refresh_rate; - } else { - PrintConsole(&game, "Refresh rate %d lower than FPS %d, NOT lowering due to config", mode.refresh_rate, game.fps); - } - } else if (game.fps == 0) game.fps = mode.refresh_rate; - if (game.fps>600) game.fps = 600; - al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); - if (game.fps>0) game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(game.fps)); - else game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(600)); + game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(60)); // logic timer if(!game.timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } al_register_event_source(game.event_queue, al_get_timer_event_source(game.timer)); - al_wait_for_vsync(); al_start_timer(game.timer); setlocale(LC_NUMERIC, "C"); /* FIXME? */ @@ -529,28 +560,20 @@ int main(int argc, char **argv){ while(1) { ALLEGRO_EVENT ev; - bool event = false; - if (game.fps<0) { - redraw = true; - event=al_get_next_event(game.event_queue, &ev); - } else { - al_wait_for_event(game.event_queue, &ev); - event=true; - } - if (!event) {} - else if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game.timer)) { - redraw = true; + al_wait_for_event(game.event_queue, &ev); + if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game.timer)) { + LogicGameState(&game); } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { /*PrintConsole(&game, "KEYCODE: %s", al_keycode_to_name(ev.keyboard.keycode));*/ -#ifdef ALLEGRO_MACOSX + #ifdef ALLEGRO_MACOSX if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == 104)) { -#else + #else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_TILDE)) { -#endif + #endif game.showconsole = !game.showconsole; } else if ((game.debug) && (ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_F1)) { @@ -570,14 +593,14 @@ int main(int argc, char **argv){ al_destroy_path(path); } KEYDOWN_STATE(GAMESTATE_PAUSE, Pause) - KEYDOWN_STATE(GAMESTATE_MENU, Menu) - KEYDOWN_STATE(GAMESTATE_LOADING, Loading) - KEYDOWN_STATE(GAMESTATE_ABOUT, About) - KEYDOWN_STATE(GAMESTATE_INTRO, Intro) - KEYDOWN_STATE(GAMESTATE_MAP, Map) - KEYDOWN_STATE(GAMESTATE_LEVEL, Level) - KEYDOWN_STATE(GAMESTATE_DISCLAIMER, Disclaimer) - else { + KEYDOWN_STATE(GAMESTATE_MENU, Menu) + KEYDOWN_STATE(GAMESTATE_LOADING, Loading) + KEYDOWN_STATE(GAMESTATE_ABOUT, About) + KEYDOWN_STATE(GAMESTATE_INTRO, Intro) + KEYDOWN_STATE(GAMESTATE_MAP, Map) + KEYDOWN_STATE(GAMESTATE_LEVEL, Level) + KEYDOWN_STATE(GAMESTATE_DISCLAIMER, Disclaimer) + else { game.showconsole = true; PrintConsole(&game, "ERROR: Keystroke in unknown (%d) gamestate! (5 sec sleep)", game.gamestate); DrawConsole(&game); @@ -587,14 +610,11 @@ int main(int argc, char **argv){ game.gamestate = GAMESTATE_LOADING; game.loadstate = GAMESTATE_MENU; } - } else { - if (game.gamestate == GAMESTATE_LEVEL) { - Level_ProcessLogic(&game, &ev); - } + } else if (game.gamestate == GAMESTATE_LEVEL) { + Level_ProcessEvent(&game, &ev); } - if(redraw && al_is_event_queue_empty(game.event_queue)) { - redraw = false; + if (al_is_event_queue_empty(game.event_queue)) { DrawGameState(&game); DrawConsole(&game); al_flip_display(); diff --git a/src/main.h b/src/main.h index edcfeff..1df8903 100644 --- a/src/main.h +++ b/src/main.h @@ -95,7 +95,6 @@ struct Spritesheet { /*! \brief Resources used by moonwalk level placeholder. */ struct Moonwalk { - ALLEGRO_BITMAP *fade_bitmap; /*!< Bitmap used on fade-in and fade-out animations. */ ALLEGRO_BITMAP *image; /*!< Background texture. */ int derpy_frame; /*!< Current frame of Derpy animation. */ int derpy_frame_tmp; /*!< Counter used to slow down Derpy animation. */ @@ -173,7 +172,6 @@ enum menustate_enum { /*! \brief Resources used by Menu state. */ struct Menu { - ALLEGRO_BITMAP *menu_fade_bitmap; /*!< Screenshot bitmap used for fades. */ ALLEGRO_BITMAP *image; /*!< Bitmap with lower portion of menu landscape. */ ALLEGRO_BITMAP *cloud; /*!< Bitmap with bigger cloud. */ ALLEGRO_BITMAP *cloud2; /*!< Bitmap with small cloud. */ @@ -340,6 +338,12 @@ float tps(struct Game *game, float t); /*! \brief Draws frame from current gamestate. */ void DrawGameState(struct Game *game); +/*! \brief Processes logic of current gamestate. */ +void LogicGameState(struct Game *game); + +/*! \brief Displays fade in or fade out animation on current gamestate. */ +void FadeGameState(struct Game *game, bool in); + /*! \brief Load shared resources. */ int Shared_Load(struct Game *game); diff --git a/src/map.c b/src/map.c index 0eaa3f9..7573d99 100644 --- a/src/map.c +++ b/src/map.c @@ -53,20 +53,15 @@ void Map_Draw(struct Game *game) { break; } al_draw_scaled_bitmap(game->map.arrow, 0, 0, al_get_bitmap_width(game->map.arrow), al_get_bitmap_height(game->map.arrow), game->viewportWidth*x, game->viewportHeight*y + ((sin(game->map.arrowpos)+0.5)/20.0)*game->viewportHeight, game->viewportWidth*0.1, game->viewportHeight*0.16, 0); - game->map.arrowpos += tps(game, 0.1*60); +} + +void Map_Logic(struct Game *game) { + game->map.arrowpos += 0.1; } void Map_Load(struct Game *game) { al_play_sample_instance(game->map.music); - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->map.map,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0); - DrawConsole(game); - al_flip_display(); - } - Map_Draw(game); + FadeGameState(game, true); } int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { @@ -148,14 +143,7 @@ void Map_Preload(struct Game *game, void (*progress)(struct Game*, float)) { } void Map_Unload(struct Game *game) { - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->map.map, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0); - DrawConsole(game); - al_flip_display(); - } + FadeGameState(game, false); al_destroy_bitmap(game->map.map); al_destroy_bitmap(game->map.map_bg); al_destroy_bitmap(game->map.highlight); diff --git a/src/map.h b/src/map.h index 793136f..0b851f2 100644 --- a/src/map.h +++ b/src/map.h @@ -21,6 +21,7 @@ #include "main.h" void Map_Draw(struct Game *game); +void Map_Logic(struct Game *game); void Map_Preload(struct Game *game, void (*progress)(struct Game*, float)); void Map_Unload(struct Game *game); void Map_Load(struct Game *game); diff --git a/src/menu.c b/src/menu.c index 2d669e1..f01c9b8 100644 --- a/src/menu.c +++ b/src/menu.c @@ -170,9 +170,11 @@ void Menu_Draw(struct Game *game) { al_draw_text_with_shadow(game->menu.font_subtitle, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.275, ALLEGRO_ALIGN_CENTRE, "Muffin Attack"); DrawMenuState(game); +} - game->menu.cloud_position-=tps(game, 0.1*60); - game->menu.cloud2_position-=tps(game, 0.025*60); +void Menu_Logic(struct Game *game) { + game->menu.cloud_position-=0.1; + game->menu.cloud2_position-=0.025; if (game->menu.cloud_position<-80) { game->menu.cloud_position=100; PrintConsole(game, "cloud_position"); } if (game->menu.cloud2_position<0) { game->menu.cloud2_position=100; PrintConsole(game, "cloud2_position"); } } @@ -285,22 +287,9 @@ void Menu_Preload(struct Game *game, void (*progress)(struct Game*, float)) { } void Menu_Stop(struct Game* game) { - game->menu.menu_fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); - al_set_target_bitmap(game->menu.menu_fade_bitmap); - al_clear_to_color(al_map_rgb(0,0,0)); - al_set_target_bitmap(al_get_backbuffer(game->display)); - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - Menu_Draw(game); - al_draw_tinted_bitmap(game->menu.menu_fade_bitmap,al_map_rgba_f(1,1,1,fadeloop/255.0),0,0,0); - DrawConsole(game); - al_flip_display(); - } + FadeGameState(game, false); al_stop_sample_instance(game->menu.music); al_stop_sample_instance(game->menu.rain_sound); - al_destroy_bitmap(game->menu.menu_fade_bitmap); } void ChangeMenuState(struct Game *game, enum menustate_enum state) { @@ -347,21 +336,7 @@ void Menu_Load(struct Game *game) { al_play_sample_instance(game->menu.music); al_play_sample_instance(game->menu.rain_sound); - game->menu.menu_fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); - al_set_target_bitmap(game->menu.menu_fade_bitmap); - al_clear_to_color(al_map_rgb(0,0,0)); - al_set_target_bitmap(al_get_backbuffer(game->display)); - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - Menu_Draw(game); - al_draw_tinted_bitmap(game->menu.menu_fade_bitmap,al_map_rgba_f(1,1,1,fadeloop/255.0),0,0,0); - DrawConsole(game); - al_flip_display(); - } - al_destroy_bitmap(game->menu.menu_fade_bitmap); - Menu_Draw(game); + FadeGameState(game, true); } int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { diff --git a/src/menu.h b/src/menu.h index 25feedb..0b28562 100644 --- a/src/menu.h +++ b/src/menu.h @@ -22,6 +22,7 @@ void DrawMenuState(struct Game *game); void Menu_Draw(struct Game *game); +void Menu_Logic(struct Game *game); void Menu_Preload(struct Game *game, void (*progress)(struct Game*, float)); void Menu_Stop(struct Game *game); void Menu_Unload(struct Game *game); diff --git a/src/moonwalk.c b/src/moonwalk.c index 87f033b..c3ee12d 100644 --- a/src/moonwalk.c +++ b/src/moonwalk.c @@ -23,14 +23,26 @@ #include "level.h" #include "moonwalk.h" +void Moonwalk_Logic(struct Game *game) { + game->level.moonwalk.derpy_pos=game->level.moonwalk.derpy_pos+0.00092; + game->level.moonwalk.derpy_frame_tmp++; + if (game->level.moonwalk.derpy_frame_tmp%3==0) { + if (game->level.moonwalk.derpy_frame_tmp%5==0) game->level.moonwalk.derpy_frame++; + if (game->level.moonwalk.derpy_frame_tmp%22==21) game->level.moonwalk.derpy_frame--; + game->level.moonwalk.derpy_frame++; + if (game->level.moonwalk.derpy_frame>=24) game->level.moonwalk.derpy_frame=0; + } +} + void Moonwalk_Draw(struct Game *game) { al_set_target_bitmap(game->level.derpy); al_clear_to_color(al_map_rgba(0,0,0,0)); al_draw_bitmap_region(*(game->level.derpy_sheet),al_get_bitmap_width(game->level.derpy)*(game->level.moonwalk.derpy_frame%6),al_get_bitmap_height(game->level.derpy)*(game->level.moonwalk.derpy_frame/6),al_get_bitmap_width(game->level.derpy), al_get_bitmap_height(game->level.derpy),0,0,0); al_set_target_bitmap(al_get_backbuffer(game->display)); - game->level.moonwalk.derpy_pos=game->level.moonwalk.derpy_pos+tps(game, 60*0.00092); - if (game->level.moonwalk.derpy_pos>1) { UnloadGameState(game); + if (game->level.moonwalk.derpy_pos>1) { + game->level.moonwalk.derpy_pos=-1; + UnloadGameState(game); Level_Passed(game); if (game->level.current_level<6) { game->gamestate = GAMESTATE_LOADING; @@ -39,16 +51,7 @@ void Moonwalk_Draw(struct Game *game) { game->gamestate = GAMESTATE_LOADING; game->loadstate = GAMESTATE_ABOUT; } - return; } - int i; - for (i = 0; i < tps(game, 60); i++ ) { - game->level.moonwalk.derpy_frame_tmp++; - if (game->level.moonwalk.derpy_frame_tmp%3==0) { - if (game->level.moonwalk.derpy_frame_tmp%5==0) game->level.moonwalk.derpy_frame++; - if (game->level.moonwalk.derpy_frame_tmp%22==21) game->level.moonwalk.derpy_frame--; - game->level.moonwalk.derpy_frame++; - if (game->level.moonwalk.derpy_frame>=24) game->level.moonwalk.derpy_frame=0; - } + return; } al_draw_scaled_bitmap(game->level.moonwalk.image,0,0,al_get_bitmap_width(game->level.moonwalk.image),al_get_bitmap_height(game->level.moonwalk.image),0,0,game->viewportWidth, game->viewportHeight,0); al_draw_bitmap(game->level.derpy, game->level.moonwalk.derpy_pos*game->viewportWidth, game->viewportHeight*0.95-al_get_bitmap_height(game->level.derpy), ALLEGRO_FLIP_HORIZONTAL); @@ -62,16 +65,7 @@ void Moonwalk_Load(struct Game *game) { game->level.moonwalk.derpy_frame_tmp = 0; game->level.moonwalk.derpy_pos = -0.2; al_play_sample_instance(game->level.music); - ALLEGRO_EVENT ev; - float fadeloop; - for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->level.moonwalk.fade_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0); - DrawConsole(game); - al_flip_display(); - } - al_destroy_bitmap(game->level.moonwalk.fade_bitmap); - Moonwalk_Draw(game); + FadeGameState(game, true); } int Moonwalk_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { @@ -83,12 +77,7 @@ void Moonwalk_PreloadBitmaps(struct Game *game) { /*game->level.derpy_sheet = LoadScaledBitmap("levels/derpcycle.png", game->viewportWidth*0.1953125*6, game->viewportHeight*0.25*4);*/ game->level.derpy = al_create_bitmap(game->viewportWidth*0.1953125, game->viewportHeight*0.25); - - game->level.moonwalk.fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); - al_set_target_bitmap(game->level.moonwalk.fade_bitmap); - al_draw_bitmap(game->level.moonwalk.image,0,0,0); - al_draw_textf(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/2.2, ALLEGRO_ALIGN_CENTRE, "Level %d: Not implemented yet!", game->level.current_level); - al_draw_text(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/1.8, ALLEGRO_ALIGN_CENTRE, "Have some moonwalk instead."); + al_set_target_bitmap(al_get_backbuffer(game->display)); } @@ -110,20 +99,5 @@ void Moonwalk_UnloadBitmaps(struct Game *game) { } void Moonwalk_Unload(struct Game *game) { - ALLEGRO_EVENT ev; - game->level.moonwalk.fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); - al_set_target_bitmap(game->level.moonwalk.fade_bitmap); - al_draw_bitmap(game->level.moonwalk.image,0,0,0); - al_draw_bitmap(game->level.derpy, game->level.moonwalk.derpy_pos*game->viewportWidth, game->viewportHeight*0.95-al_get_bitmap_height(game->level.derpy), ALLEGRO_FLIP_HORIZONTAL); - al_draw_textf(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/2.2, ALLEGRO_ALIGN_CENTRE, "Level %d: Not implemented yet!", game->level.current_level); - al_draw_text(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/1.8, ALLEGRO_ALIGN_CENTRE, "Have some moonwalk instead."); - al_set_target_bitmap(al_get_backbuffer(game->display)); - float fadeloop; - for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){ - al_wait_for_event(game->event_queue, &ev); - al_draw_tinted_bitmap(game->level.moonwalk.fade_bitmap, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0); - DrawConsole(game); - al_flip_display(); - } - al_destroy_bitmap(game->level.moonwalk.fade_bitmap); + FadeGameState(game, false); } diff --git a/src/moonwalk.h b/src/moonwalk.h index 3f91a15..c3ed961 100644 --- a/src/moonwalk.h +++ b/src/moonwalk.h @@ -21,6 +21,7 @@ #include "main.h" void Moonwalk_Draw(struct Game *game); +void Moonwalk_Logic(struct Game *game); void Moonwalk_Preload(struct Game *game); void Moonwalk_Unload(struct Game *game); void Moonwalk_Load(struct Game *game);