diff --git a/Makefile b/Makefile index 02e2bcc..16f56c6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ SRCDIR=src ODIR=obj LIBS=-lallegro-debug -lallegro_audio-debug -lallegro_acodec-debug -lallegro_image-debug -lallegro_font-debug -lallegro_ttf-debug -lm -_OBJ = config.o main.o about.o intro.o loading.o map.o menu.o level.o +_OBJ = config.o main.o about.o intro.o loading.o map.o menu.o level.o pause.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) OUTPUTDIR = bin diff --git a/src/level.c b/src/level.c index c4f14b5..c3d8d93 100644 --- a/src/level.c +++ b/src/level.c @@ -4,6 +4,7 @@ #include #include +#include "pause.h" #include "level.h" void Level_Draw(struct Game *game) { @@ -53,6 +54,10 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { UnloadGameState(game); game->loadstate = GAMESTATE_MENU; LoadGameState(game); + } else if (ev->keyboard.keycode==ALLEGRO_KEY_P) { + game->gamestate = GAMESTATE_PAUSE; + game->loadstate = GAMESTATE_LEVEL; + Pause_Load(game); } return 0; } diff --git a/src/main.c b/src/main.c index 10d112d..22de56f 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,7 @@ #include "intro.h" #include "map.h" #include "level.h" +#include "pause.h" #include "config.h" /*! \brief Macro for preloading gamestate. @@ -35,7 +36,7 @@ #define KEYDOWN_STATE(state, name) else if (game.gamestate==state) { if (name ## _Keydown(&game, &ev)) break; } /*! \brief Macro for drawing active gamestate. */ #define DRAW_STATE(state, name) case state:\ - name ## _Draw(&game); break; + name ## _Draw(game); break; void al_draw_text_with_shadow(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, int flags, char const *text) { al_draw_text(font, al_map_rgba(0,0,0,128), x+1, y+1, flags, text); @@ -94,6 +95,7 @@ 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) @@ -122,6 +124,28 @@ void LoadGameState(struct Game *game) { 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) + default: + game->showconsole = true; + 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 ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height, float val) { if ((al_get_bitmap_width(source)==width) && (al_get_bitmap_height(source)==height)) { al_draw_bitmap(source, 0, 0, 0); @@ -310,6 +334,7 @@ int main(int argc, char **argv){ if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_TILDE)) { game.showconsole = !game.showconsole; } + KEYDOWN_STATE(GAMESTATE_PAUSE, Pause) KEYDOWN_STATE(GAMESTATE_MENU, Menu) KEYDOWN_STATE(GAMESTATE_LOADING, Loading) KEYDOWN_STATE(GAMESTATE_ABOUT, About) @@ -330,24 +355,7 @@ int main(int argc, char **argv){ if(redraw && al_is_event_queue_empty(game.event_queue)) { redraw = false; - switch (game.gamestate) { - DRAW_STATE(GAMESTATE_MENU, Menu) - 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) - default: - game.showconsole = true; - 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; - } + DrawGameState(&game); DrawConsole(&game); al_flip_display(); } diff --git a/src/main.h b/src/main.h index 5055717..a966e05 100644 --- a/src/main.h +++ b/src/main.h @@ -16,6 +16,7 @@ /*! \brief Enum of all available gamestates. */ enum gamestate_enum { + GAMESTATE_PAUSE, GAMESTATE_LOADING, GAMESTATE_MENU, GAMESTATE_ABOUT, @@ -77,6 +78,11 @@ struct Loading { ALLEGRO_BITMAP *image; }; +/*! \brief Resources used by Pause state. */ +struct Pause { + ALLEGRO_BITMAP *bitmap; +}; + /*! \brief Resources used by About state. */ struct About { ALLEGRO_BITMAP *fade_bitmap; @@ -139,6 +145,7 @@ struct Game { struct About about; struct Map map; struct Level level; + struct Pause pause; }; /*! \brief Draws text with shadow. @@ -172,4 +179,6 @@ void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height, float val); ALLEGRO_BITMAP* LoadFromCache(struct Game *game, char* filename, int width, int height); float tps(struct Game *game, float t); +void DrawGameState(struct Game *game); + #endif diff --git a/src/pause.c b/src/pause.c new file mode 100644 index 0000000..c80d2a0 --- /dev/null +++ b/src/pause.c @@ -0,0 +1,41 @@ +/*! \file pause.c + * \brief Pause state. + */ + +#include +#include "pause.h" + +int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { + if ((ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) || (ev->keyboard.keycode==ALLEGRO_KEY_P)) { + al_destroy_bitmap(game->pause.bitmap); + game->gamestate = game->loadstate; + } + return 0; +} + +void Pause_Load(struct Game* game) { + game->gamestate=game->loadstate; + DrawGameState(game); + game->gamestate=GAMESTATE_PAUSE; + ALLEGRO_BITMAP *fade = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display)); + al_set_target_bitmap(fade); + al_clear_to_color(al_map_rgb(0,0,0)); + 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); + al_draw_text_with_shadow(game->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,"Game paused!"); + game->pause.bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display)); + al_set_target_bitmap(game->pause.bitmap); + al_draw_bitmap(al_get_backbuffer(game->display), 0, 0, 0); + al_set_target_bitmap(al_get_backbuffer(game->display)); +} + +void Pause_Draw(struct Game* game) { + al_draw_bitmap(game->pause.bitmap, 0, 0, 0); + DrawConsole(game); +} + +void Pause_Unload(struct Game* game) { + al_destroy_bitmap(game->pause.bitmap); + game->gamestate=game->loadstate; + UnloadGameState(game); +} \ No newline at end of file diff --git a/src/pause.h b/src/pause.h new file mode 100644 index 0000000..99b8c23 --- /dev/null +++ b/src/pause.h @@ -0,0 +1,11 @@ +/*! \file pause.h + * \brief Pause state headers. + */ + +#include "main.h" + +void Pause_Draw(struct Game *game); +void Pause_Preload(struct Game *game); +void Pause_Unload(struct Game *game); +void Pause_Load(struct Game *game); +int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev);