introduce menustates

This commit is contained in:
Sebastian Krzyszkowiak 2012-03-07 22:06:19 +01:00
parent f6e4b0625c
commit b0a4369678
5 changed files with 86 additions and 72 deletions

1
TODO
View file

@ -1,6 +1,7 @@
- moar documentation - moar documentation
- control settings - control settings
- video settings - video settings
- command line options
- playable levels :D - playable levels :D

View file

@ -56,6 +56,15 @@ struct Level {
double derpy_pos; /*!< Position of Derpy on screen. */ 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. */ /*! \brief Resources used by Menu state. */
struct Menu { struct Menu {
ALLEGRO_BITMAP *menu_fade_bitmap; ALLEGRO_BITMAP *menu_fade_bitmap;
@ -83,8 +92,7 @@ struct Menu {
ALLEGRO_FONT *font; ALLEGRO_FONT *font;
ALLEGRO_FONT *font_selected; ALLEGRO_FONT *font_selected;
int selected; int selected;
bool options; enum menustate_enum menustate;
//bool draw_while_fading;
bool loaded; bool loaded;
}; };
@ -98,8 +106,6 @@ struct Loading {
struct Pause { struct Pause {
ALLEGRO_BITMAP *bitmap; ALLEGRO_BITMAP *bitmap;
ALLEGRO_BITMAP *derpy; ALLEGRO_BITMAP *derpy;
int selected;
bool options;
}; };
/*! \brief Resources used by About state. */ /*! \brief Resources used by About state. */

View file

@ -23,6 +23,54 @@
#include "config.h" #include "config.h"
#include "menu.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) { void Menu_Draw(struct Game *game) {
if (!game->menu.loaded) { if (!game->menu.loaded) {
game->gamestate=GAMESTATE_LOADING; 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_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"); 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; DrawMenuState(game);
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);
game->menu.cloud_position-=tps(game, 0.1*60); game->menu.cloud_position-=tps(game, 0.1*60);
game->menu.cloud2_position-=tps(game, 0.025*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.cloud_position = 100;
game->menu.cloud2_position = 100; game->menu.cloud2_position = 100;
game->menu.options = false; game->menu.menustate = MENUSTATE_MAIN;
game->menu.selected = 0; game->menu.selected = 0;
al_play_sample_instance(game->menu.music); 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) { } else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) {
game->menu.selected++; game->menu.selected++;
al_play_sample_instance(game->menu.click); 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); al_play_sample_instance(game->menu.click);
return 1; 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); al_play_sample_instance(game->menu.click);
UnloadGameState(game); UnloadGameState(game);
game->gamestate = GAMESTATE_LOADING; game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_INTRO; 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); al_play_sample_instance(game->menu.click);
UnloadGameState(game); UnloadGameState(game);
game->gamestate = GAMESTATE_LOADING; game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_ABOUT; 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); 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; game->menu.selected=0;
PrintConsole(game, "options state changed %d", game->menu.options); PrintConsole(game, "menu state changed %d", game->menu.menustate);
} else if ((game->menu.options) && (game->menu.selected==2)) { } else if ((game->menu.menustate==MENUSTATE_OPTIONS) && (game->menu.selected==2)) {
if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0"); if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0");
al_detach_mixer(game->audio.music); al_detach_mixer(game->audio.music);
} }

View file

@ -20,6 +20,7 @@
*/ */
#include "main.h" #include "main.h"
void DrawMenuState(struct Game *game);
void Menu_Draw(struct Game *game); void Menu_Draw(struct Game *game);
void Menu_Preload(struct Game *game); void Menu_Preload(struct Game *game);
void Menu_Stop(struct Game *game); void Menu_Stop(struct Game *game);

View file

@ -24,34 +24,35 @@
#include "menu.h" #include "menu.h"
int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { 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); al_play_sample_instance(game->menu.click);
PrintConsole(game,"Game resumed."); PrintConsole(game,"Game resumed.");
al_destroy_bitmap(game->pause.bitmap); al_destroy_bitmap(game->pause.bitmap);
game->pause.bitmap = NULL; game->pause.bitmap = NULL;
game->gamestate = game->loadstate; 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); al_play_sample_instance(game->menu.click);
game->gamestate=game->loadstate; game->gamestate=game->loadstate;
UnloadGameState(game); UnloadGameState(game);
game->gamestate = GAMESTATE_LOADING; game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_MAP; game->loadstate = GAMESTATE_MAP;
} else if (ev->keyboard.keycode==ALLEGRO_KEY_UP) { } else if (ev->keyboard.keycode==ALLEGRO_KEY_UP) {
game->pause.selected--; game->menu.selected--;
al_play_sample_instance(game->menu.click); al_play_sample_instance(game->menu.click);
} else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) { } else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) {
game->pause.selected++; game->menu.selected++;
al_play_sample_instance(game->menu.click); 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); al_play_sample_instance(game->menu.click);
return 1; 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); al_play_sample_instance(game->menu.click);
game->pause.options=!game->pause.options; if (game->menu.menustate==MENUSTATE_PAUSE) game->menu.menustate=MENUSTATE_OPTIONS;
game->pause.selected=0; else game->menu.menustate=MENUSTATE_PAUSE;
PrintConsole(game, "options state changed %d", game->pause.options); game->menu.selected=0;
} else if ((game->pause.options) && (game->pause.selected==2)) { 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"); if ((game->music) && (game->fx)) { game->music=0; SetConfigOption("SuperDerpy", "music", "0");
al_detach_mixer(game->audio.music); 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); al_play_sample_instance(game->menu.click);
} }
if (game->pause.selected==-1) game->pause.selected=3; if (game->menu.selected==-1) game->menu.selected=3;
if (game->pause.selected==4) game->pause.selected=0; if (game->menu.selected==4) game->menu.selected=0;
return 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_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_set_target_bitmap(al_get_backbuffer(game->display));
al_destroy_bitmap(fade); al_destroy_bitmap(fade);
game->pause.selected=0; game->menu.selected=0;
game->pause.options=0; game->menu.menustate = MENUSTATE_PAUSE;
PrintConsole(game,"Game paused."); PrintConsole(game,"Game paused.");
al_play_sample_instance(game->menu.click); 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_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."); 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; DrawMenuState(game);
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);
DrawConsole(game); DrawConsole(game);
} }