From b0a43696783c40b3f3e2348ea4e673036ed5e9cd Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Wed, 7 Mar 2012 22:06:19 +0100 Subject: [PATCH] introduce menustates --- TODO | 1 + src/main.h | 14 ++++++--- src/menu.c | 89 ++++++++++++++++++++++++++++++++++------------------- src/menu.h | 1 + src/pause.c | 53 ++++++++++--------------------- 5 files changed, 86 insertions(+), 72 deletions(-) diff --git a/TODO b/TODO index 345fab0..1334eb4 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,7 @@ - moar documentation - control settings - video settings +- command line options - playable levels :D diff --git a/src/main.h b/src/main.h index a5d7bd2..1bf9337 100644 --- a/src/main.h +++ b/src/main.h @@ -56,6 +56,15 @@ struct Level { double derpy_pos; /*!< Position of Derpy on screen. */ }; +/*! \brief Enum of menu states in Menu and Pause game states. */ +enum menustate_enum { + MENUSTATE_MAIN, + MENUSTATE_OPTIONS, + MENUSTATE_CONTROLS, + MENUSTATE_VIDEO, + MENUSTATE_PAUSE +}; + /*! \brief Resources used by Menu state. */ struct Menu { ALLEGRO_BITMAP *menu_fade_bitmap; @@ -83,8 +92,7 @@ struct Menu { ALLEGRO_FONT *font; ALLEGRO_FONT *font_selected; int selected; - bool options; - //bool draw_while_fading; + enum menustate_enum menustate; bool loaded; }; @@ -98,8 +106,6 @@ struct Loading { struct Pause { ALLEGRO_BITMAP *bitmap; ALLEGRO_BITMAP *derpy; - int selected; - bool options; }; /*! \brief Resources used by About state. */ diff --git a/src/menu.c b/src/menu.c index 1e29585..fbcd621 100644 --- a/src/menu.c +++ b/src/menu.c @@ -23,6 +23,54 @@ #include "config.h" #include "menu.h" +void DrawMenuState(struct Game *game) { + ALLEGRO_FONT *font; + + switch (game->menu.menustate) { + case MENUSTATE_MAIN: + font = game->menu.font; if (game->menu.selected==0) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.5, ALLEGRO_ALIGN_CENTRE, "Start game"); + font = game->menu.font; if (game->menu.selected==1) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.6, ALLEGRO_ALIGN_CENTRE, "Options"); + font = game->menu.font; if (game->menu.selected==2) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.7, ALLEGRO_ALIGN_CENTRE, "About"); + font = game->menu.font; if (game->menu.selected==3) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.8, ALLEGRO_ALIGN_CENTRE, "Exit"); + break; + case MENUSTATE_OPTIONS: + font = game->menu.font; if (game->menu.selected==0) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.5, ALLEGRO_ALIGN_CENTRE, "Control settings"); + font = game->menu.font; if (game->menu.selected==1) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.6, ALLEGRO_ALIGN_CENTRE, "Video settings"); + font = game->menu.font; if (game->menu.selected==2) font = game->menu.font_selected; + char* text; + if ((game->music) && (game->fx)) + text="Sounds: all"; + else if (game->music) + text="Sounds: music only"; + else if (game->fx) + text="Sounds: fx only"; + else + text="Sounds: none"; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.7, ALLEGRO_ALIGN_CENTRE, text); + font = game->menu.font; if (game->menu.selected==3) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.8, ALLEGRO_ALIGN_CENTRE, "Back"); + break; + case MENUSTATE_PAUSE: + font = game->menu.font; if (game->menu.selected==0) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.5, ALLEGRO_ALIGN_CENTRE, "Resume game"); + font = game->menu.font; if (game->menu.selected==1) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.6, ALLEGRO_ALIGN_CENTRE, "Return to map"); + font = game->menu.font; if (game->menu.selected==2) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.7, ALLEGRO_ALIGN_CENTRE, "Options"); + font = game->menu.font; if (game->menu.selected==3) font = game->menu.font_selected; + al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.8, ALLEGRO_ALIGN_CENTRE, "Exit"); + break; + default: + break; + } +} + void Menu_Draw(struct Game *game) { if (!game->menu.loaded) { game->gamestate=GAMESTATE_LOADING; @@ -57,29 +105,7 @@ void Menu_Draw(struct Game *game) { al_draw_text_with_shadow(game->menu.font_title, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.1, ALLEGRO_ALIGN_CENTRE, "Super Derpy"); al_draw_text_with_shadow(game->menu.font_subtitle, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.275, ALLEGRO_ALIGN_CENTRE, "Muffin Attack"); - ALLEGRO_FONT *font; - char* text; - font = game->menu.font; if (game->menu.selected==0) font = game->menu.font_selected; - text = "Start game"; if (game->menu.options) text="Control settings"; - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.5, ALLEGRO_ALIGN_CENTRE, text); - font = game->menu.font; if (game->menu.selected==1) font = game->menu.font_selected; - text = "Options"; if (game->menu.options) text="Video settings"; - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.6, ALLEGRO_ALIGN_CENTRE, text); - font = game->menu.font; if (game->menu.selected==2) font = game->menu.font_selected; - text = "About"; if (game->menu.options) { - if ((game->music) && (game->fx)) - text="Sounds: all"; - else if (game->music) - text="Sounds: music only"; - else if (game->fx) - text="Sounds: fx only"; - else - text="Sounds: none"; - } - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.7, ALLEGRO_ALIGN_CENTRE, text); - font = game->menu.font; if (game->menu.selected==3) font = game->menu.font_selected; - text = "Exit"; if (game->menu.options) text="Back"; - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.8, ALLEGRO_ALIGN_CENTRE, text); + DrawMenuState(game); game->menu.cloud_position-=tps(game, 0.1*60); game->menu.cloud2_position-=tps(game, 0.025*60); @@ -203,7 +229,7 @@ void Menu_Load(struct Game *game) { game->menu.cloud_position = 100; game->menu.cloud2_position = 100; - game->menu.options = false; + game->menu.menustate = MENUSTATE_MAIN; game->menu.selected = 0; al_play_sample_instance(game->menu.music); @@ -232,25 +258,26 @@ int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { } else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) { game->menu.selected++; al_play_sample_instance(game->menu.click); - } else if ((!game->menu.options) && (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3)) || (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) { + } else if ((game->menu.menustate==MENUSTATE_MAIN) && (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3)) || (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) { al_play_sample_instance(game->menu.click); return 1; - } else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->menu.options) && (game->menu.selected==0)) { + } else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.menustate==MENUSTATE_MAIN) && (game->menu.selected==0)) { al_play_sample_instance(game->menu.click); UnloadGameState(game); game->gamestate = GAMESTATE_LOADING; game->loadstate = GAMESTATE_INTRO; - } else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->menu.options) && (game->menu.selected==2)) { + } else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.menustate==MENUSTATE_MAIN) && (game->menu.selected==2)) { al_play_sample_instance(game->menu.click); UnloadGameState(game); game->gamestate = GAMESTATE_LOADING; game->loadstate = GAMESTATE_ABOUT; - } else if (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->menu.options) && (game->menu.selected==1)) || (((game->menu.options) && ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) || (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3))))) { + } else if (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.menustate==MENUSTATE_MAIN) && (game->menu.selected==1)) || (((game->menu.menustate==MENUSTATE_OPTIONS) && ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) || (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3))))) { al_play_sample_instance(game->menu.click); - game->menu.options=!game->menu.options; + if (game->menu.menustate==MENUSTATE_MAIN) game->menu.menustate=MENUSTATE_OPTIONS; + else game->menu.menustate=MENUSTATE_MAIN; game->menu.selected=0; - PrintConsole(game, "options state changed %d", game->menu.options); - } else if ((game->menu.options) && (game->menu.selected==2)) { + PrintConsole(game, "menu state changed %d", game->menu.menustate); + } else if ((game->menu.menustate==MENUSTATE_OPTIONS) && (game->menu.selected==2)) { if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); al_detach_mixer(game->audio.music); } diff --git a/src/menu.h b/src/menu.h index c2a74d2..f8e1ebd 100644 --- a/src/menu.h +++ b/src/menu.h @@ -20,6 +20,7 @@ */ #include "main.h" +void DrawMenuState(struct Game *game); void Menu_Draw(struct Game *game); void Menu_Preload(struct Game *game); void Menu_Stop(struct Game *game); diff --git a/src/pause.c b/src/pause.c index ddade07..891ea20 100644 --- a/src/pause.c +++ b/src/pause.c @@ -24,34 +24,35 @@ #include "menu.h" int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { - if ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) || ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->pause.options==0) && (game->pause.selected==0))) { + if ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) || ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.menustate==MENUSTATE_PAUSE) && (game->menu.selected==0))) { al_play_sample_instance(game->menu.click); PrintConsole(game,"Game resumed."); al_destroy_bitmap(game->pause.bitmap); game->pause.bitmap = NULL; game->gamestate = game->loadstate; } - else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->pause.options==0) && (game->pause.selected==1)) { + else if ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.menustate==MENUSTATE_PAUSE) && (game->menu.selected==1)) { al_play_sample_instance(game->menu.click); game->gamestate=game->loadstate; UnloadGameState(game); game->gamestate = GAMESTATE_LOADING; game->loadstate = GAMESTATE_MAP; } else if (ev->keyboard.keycode==ALLEGRO_KEY_UP) { - game->pause.selected--; + game->menu.selected--; al_play_sample_instance(game->menu.click); } else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) { - game->pause.selected++; + game->menu.selected++; al_play_sample_instance(game->menu.click); - } else if ((!game->pause.options) && (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->pause.selected==3)) || (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) { + } else if ((game->menu.menustate==MENUSTATE_PAUSE) && (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3)) || (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) { al_play_sample_instance(game->menu.click); return 1; - } else if (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (!game->pause.options) && (game->pause.selected==2)) || (((game->pause.options) && ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) || (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->pause.selected==3))))) { + } else if (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.menustate==MENUSTATE_PAUSE) && (game->menu.selected==2)) || (((game->menu.menustate==MENUSTATE_OPTIONS) && ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE))) || (((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3))))) { al_play_sample_instance(game->menu.click); - game->pause.options=!game->pause.options; - game->pause.selected=0; - PrintConsole(game, "options state changed %d", game->pause.options); - } else if ((game->pause.options) && (game->pause.selected==2)) { + if (game->menu.menustate==MENUSTATE_PAUSE) game->menu.menustate=MENUSTATE_OPTIONS; + else game->menu.menustate=MENUSTATE_PAUSE; + game->menu.selected=0; + PrintConsole(game, "options state changed %d", game->menu.menustate); + } else if ((game->menu.menustate==MENUSTATE_OPTIONS) && (game->menu.selected==2)) { if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); al_detach_mixer(game->audio.music); } @@ -68,8 +69,8 @@ int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { } al_play_sample_instance(game->menu.click); } - if (game->pause.selected==-1) game->pause.selected=3; - if (game->pause.selected==4) game->pause.selected=0; + if (game->menu.selected==-1) game->menu.selected=3; + if (game->menu.selected==4) game->menu.selected=0; return 0; } @@ -98,8 +99,8 @@ void Pause_Load(struct Game* game) { al_draw_bitmap(game->pause.derpy, 0.47*al_get_display_width(game->display), al_get_display_height(game->display)*0.396, 0); al_set_target_bitmap(al_get_backbuffer(game->display)); al_destroy_bitmap(fade); - game->pause.selected=0; - game->pause.options=0; + game->menu.selected=0; + game->menu.menustate = MENUSTATE_PAUSE; PrintConsole(game,"Game paused."); al_play_sample_instance(game->menu.click); } @@ -109,29 +110,7 @@ void Pause_Draw(struct Game* game) { al_draw_text_with_shadow(game->menu.font_title, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.1, ALLEGRO_ALIGN_CENTRE, "Super Derpy"); al_draw_text_with_shadow(game->menu.font_subtitle, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.275, ALLEGRO_ALIGN_CENTRE, "Game paused."); - ALLEGRO_FONT *font; - char* text; - font = game->menu.font; if (game->pause.selected==0) font = game->menu.font_selected; - text = "Resume game"; if (game->pause.options) text="Control settings"; - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.5, ALLEGRO_ALIGN_CENTRE, text); - font = game->menu.font; if (game->pause.selected==1) font = game->menu.font_selected; - text = "Return to map"; if (game->pause.options) text="Video settings"; - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.6, ALLEGRO_ALIGN_CENTRE, text); - font = game->menu.font; if (game->pause.selected==2) font = game->menu.font_selected; - text = "Options"; if (game->pause.options) { - if ((game->music) && (game->fx)) - text="Sounds: all"; - else if (game->music) - text="Sounds: music only"; - else if (game->fx) - text="Sounds: fx only"; - else - text="Sounds: none"; - } - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.7, ALLEGRO_ALIGN_CENTRE, text); - font = game->menu.font; if (game->pause.selected==3) font = game->menu.font_selected; - text = "Exit"; if (game->pause.options) text="Back"; - al_draw_text_with_shadow(font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.8, ALLEGRO_ALIGN_CENTRE, text); + DrawMenuState(game); DrawConsole(game); }