revamp pause functionality

This commit is contained in:
Sebastian Krzyszkowiak 2012-09-26 20:00:11 +02:00
parent e99f1e5936
commit 1ba89400d6
8 changed files with 65 additions and 25 deletions

View file

@ -227,11 +227,17 @@ void Level_Logic(struct Game *game) {
} }
} }
void Level_Draw(struct Game *game) { void Level_Resume(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);
al_set_sample_instance_position(game->level.music, game->level.music_pos); al_set_sample_instance_position(game->level.music, game->level.music_pos);
al_set_sample_instance_playing(game->level.music, true);
} }
void Level_Pause(struct Game *game) {
game->level.music_pos = al_get_sample_instance_position(game->level.music);
al_set_sample_instance_playing(game->level.music, false);
}
void Level_Draw(struct Game *game) {
if (game->level.current_level!=1) Moonwalk_Draw(game); if (game->level.current_level!=1) Moonwalk_Draw(game);
else { else {
@ -396,10 +402,7 @@ void Level_Load(struct Game *game) {
} }
int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (ev->keyboard.keycode==ALLEGRO_KEY_ESCAPE) { if ((game->debug) && (ev->keyboard.keycode==ALLEGRO_KEY_F2)) {
game->level.music_pos = al_get_sample_instance_position(game->level.music);
al_set_sample_instance_playing(game->level.music, false);
} else if ((game->debug) && (ev->keyboard.keycode==ALLEGRO_KEY_F2)) {
game->level.hp -= 0.1; game->level.hp -= 0.1;
if (game->level.hp <= 0) game->level.hp=0.001; if (game->level.hp <= 0) game->level.hp=0.001;
} else if ((game->debug) && (ev->keyboard.keycode==ALLEGRO_KEY_F3)) { } else if ((game->debug) && (ev->keyboard.keycode==ALLEGRO_KEY_F3)) {
@ -449,6 +452,7 @@ void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
game->level.current_level = game->level.input.current_level; game->level.current_level = game->level.input.current_level;
game->level.derpy_sheets = NULL; game->level.derpy_sheets = NULL;
game->level.derpy = NULL; game->level.derpy = NULL;
game->level.unloading = false;
Pause_Preload(game); Pause_Preload(game);
RegisterDerpySpritesheet(game, "walk"); RegisterDerpySpritesheet(game, "walk");
RegisterDerpySpritesheet(game, "fly"); RegisterDerpySpritesheet(game, "fly");
@ -470,7 +474,10 @@ void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
} }
void Level_Unload(struct Game *game) { void Level_Unload(struct Game *game) {
if (game->level.unloading) return;
game->level.unloading = true;
Pause_Unload_Real(game); Pause_Unload_Real(game);
FadeGameState(game, false);
if (game->level.current_level!=1) Moonwalk_Unload(game); if (game->level.current_level!=1) Moonwalk_Unload(game);
else { else {
TM_Destroy(); TM_Destroy();

View file

@ -22,6 +22,8 @@
void SelectDerpySpritesheet(struct Game *game, char* name); void SelectDerpySpritesheet(struct Game *game, char* name);
void Level_Passed(struct Game *game); void Level_Passed(struct Game *game);
void Level_Pause(struct Game *game);
void Level_Resume(struct Game *game);
void Level_Draw(struct Game *game); void Level_Draw(struct Game *game);
void Level_Logic(struct Game *game); void Level_Logic(struct Game *game);
void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)); void Level_Preload(struct Game *game, void (*progress)(struct Game*, float));

View file

@ -347,7 +347,10 @@ bool Welcome(struct Game *game, struct TM_Action *action, enum TM_ActionState st
bool PassLevel(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { bool PassLevel(struct Game *game, struct TM_Action *action, enum TM_ActionState state) {
if (state == TM_ACTIONSTATE_DESTROY) { if (state == TM_ACTIONSTATE_DESTROY) {
Level_Passed(game); Level_Passed(game);
TM_AddBackgroundAction(&FadeOut, NULL, 0, "fadeout"); Level_Unload(game);
game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_MAP;
//TM_AddBackgroundAction(&FadeOut, NULL, 0, "fadeout");
} }
return true; return true;
} }

View file

@ -59,9 +59,15 @@
/*! \brief Macro for drawing active gamestate. */ /*! \brief Macro for drawing active gamestate. */
#define DRAW_STATE(state, name) case state:\ #define DRAW_STATE(state, name) case state:\
name ## _Draw(game); break; name ## _Draw(game); break;
/*! \brief Macro for invocing logic function of active gamestate. */ /*! \brief Macro for invoking logic function of active gamestate. */
#define LOGIC_STATE(state, name) case state:\ #define LOGIC_STATE(state, name) case state:\
name ## _Logic(game); break; name ## _Logic(game); break;
/*! \brief Macro for invoking pause function of active gamestate. */
#define PAUSE_STATE(state, name) case state:\
PrintConsole(game, "Pause %s...", #state); name ## _Pause(game); break;
/*! \brief Macro for invoking resume function of active gamestate. */
#define RESUME_STATE(state, name) case state:\
PrintConsole(game, "Resume %s...", #state); name ## _Resume(game); break;
double old_time = 0, fps; double old_time = 0, fps;
int frames_done = 0; int frames_done = 0;
@ -242,6 +248,24 @@ void LogicGameState(struct Game *game) {
} }
} }
void PauseGameState(struct Game *game) {
switch (game->loadstate) {
PAUSE_STATE(GAMESTATE_LEVEL, Level)
default:
// not every gamestate needs to have pause function
break;
}
}
void ResumeGameState(struct Game *game) {
switch (game->loadstate) {
RESUME_STATE(GAMESTATE_LEVEL, Level)
default:
// not every gamestate needs to have resume function
break;
}
}
void FadeGameState(struct Game *game, bool in) { void FadeGameState(struct Game *game, bool in) {
ALLEGRO_BITMAP* bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight); ALLEGRO_BITMAP* bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(bitmap); al_set_target_bitmap(bitmap);

View file

@ -119,6 +119,7 @@ struct Level {
float hp; /*!< Player health points (0-1). */ float hp; /*!< Player health points (0-1). */
bool handle_input; /*!< When false, player looses control over Derpy. */ bool handle_input; /*!< When false, player looses control over Derpy. */
bool failed; /*!< Indicates if player failed level. */ bool failed; /*!< Indicates if player failed level. */
bool unloading; /*!< Indicated if level is already being unloaded. */
float meter_alpha; /*!< Alpha level of HP meter. */ float meter_alpha; /*!< Alpha level of HP meter. */
int sheet_rows; /*!< Number of rows in current spritesheet. */ int sheet_rows; /*!< Number of rows in current spritesheet. */
int sheet_cols; /*!< Number of cols in current spritesheet. */ int sheet_cols; /*!< Number of cols in current spritesheet. */
@ -315,6 +316,12 @@ void UnloadGameState(struct Game *game);
/*! \brief Loads gamestate set in game->loadstate. */ /*! \brief Loads gamestate set in game->loadstate. */
void LoadGameState(struct Game *game); void LoadGameState(struct Game *game);
/*! \brief Pauses gamestate set in game->loadstate. */
void PauseGameState(struct Game *game);
/*! \brief Resumes gamestate set in game->loadstate. */
void ResumeGameState(struct Game *game);
/*! \brief Finds path for data file. */ /*! \brief Finds path for data file. */
char* GetDataFilePath(char* filename); char* GetDataFilePath(char* filename);

View file

@ -429,6 +429,7 @@ int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
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;
ResumeGameState(game);
game->gamestate = game->loadstate; game->gamestate = game->loadstate;
break; break;
case 1: case 1:
@ -499,6 +500,7 @@ int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
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;
ResumeGameState(game);
game->gamestate = game->loadstate; game->gamestate = game->loadstate;
break; break;
default: default:

View file

@ -99,5 +99,5 @@ void Moonwalk_UnloadBitmaps(struct Game *game) {
} }
void Moonwalk_Unload(struct Game *game) { void Moonwalk_Unload(struct Game *game) {
FadeGameState(game, false); //FadeGameState(game, false);
} }

View file

@ -71,32 +71,27 @@ void Pause_Preload(struct Game* game) {
} }
void Pause_Load(struct Game* game) { void Pause_Load(struct Game* game) {
game->gamestate=game->loadstate; PauseGameState(game);
game->loadstate=GAMESTATE_PAUSE;
DrawGameState(game);
game->loadstate=game->gamestate;
game->gamestate=GAMESTATE_PAUSE;
ALLEGRO_BITMAP *fade = al_create_bitmap(game->viewportWidth, game->viewportHeight); ALLEGRO_BITMAP *fade = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(fade); al_set_target_bitmap(fade);
al_clear_to_color(al_map_rgb(0,0,0)); al_clear_to_color(al_map_rgb(0,0,0));
al_set_target_bitmap(al_get_backbuffer(game->display)); al_set_target_bitmap(al_get_backbuffer(game->display));
al_draw_tinted_bitmap(fade,al_map_rgba_f(1,1,1,0.75),0,0,0); game->pause.bitmap = fade;
game->pause.bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(game->pause.bitmap);
al_draw_bitmap(al_get_backbuffer(game->display), -1*(al_get_display_width(game->display)-game->viewportWidth)/2, -1*(al_get_display_height(game->display)-game->viewportHeight)/2, 0);
al_draw_bitmap(game->pause.derpy, 0.47*game->viewportWidth, game->viewportHeight*0.396, 0);
al_set_target_bitmap(al_get_backbuffer(game->display));
al_destroy_bitmap(fade);
ChangeMenuState(game,MENUSTATE_PAUSE); ChangeMenuState(game,MENUSTATE_PAUSE);
PrintConsole(game,"Game paused."); PrintConsole(game,"Game paused.");
al_play_sample_instance(game->menu.click); al_play_sample_instance(game->menu.click);
} }
void Pause_Draw(struct Game* game) { void Pause_Draw(struct Game* game) {
al_draw_bitmap(game->pause.bitmap, 0, 0, 0); game->gamestate=game->loadstate;
game->loadstate=GAMESTATE_PAUSE;
DrawGameState(game);
game->loadstate=game->gamestate;
game->gamestate=GAMESTATE_PAUSE;
al_draw_tinted_bitmap(game->pause.bitmap,al_map_rgba_f(1,1,1,0.75),0,0,0);
al_draw_bitmap(game->pause.derpy, 0.47*game->viewportWidth, game->viewportHeight*0.396, 0);
al_draw_text_with_shadow(game->menu.font_title, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.1, ALLEGRO_ALIGN_CENTRE, "Super Derpy"); al_draw_text_with_shadow(game->menu.font_title, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.1, ALLEGRO_ALIGN_CENTRE, "Super Derpy");
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, "Game paused."); 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, "Game paused.");
DrawMenuState(game); DrawMenuState(game);
} }