implement pause

This commit is contained in:
Sebastian Krzyszkowiak 2012-03-02 16:52:13 +01:00
parent 4cba98f8bc
commit 78172b6c32
6 changed files with 94 additions and 20 deletions

View file

@ -6,7 +6,7 @@ SRCDIR=src
ODIR=obj ODIR=obj
LIBS=-lallegro-debug -lallegro_audio-debug -lallegro_acodec-debug -lallegro_image-debug -lallegro_font-debug -lallegro_ttf-debug -lm 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)) OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
OUTPUTDIR = bin OUTPUTDIR = bin

View file

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include "pause.h"
#include "level.h" #include "level.h"
void Level_Draw(struct Game *game) { void Level_Draw(struct Game *game) {
@ -53,6 +54,10 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
UnloadGameState(game); UnloadGameState(game);
game->loadstate = GAMESTATE_MENU; game->loadstate = GAMESTATE_MENU;
LoadGameState(game); LoadGameState(game);
} else if (ev->keyboard.keycode==ALLEGRO_KEY_P) {
game->gamestate = GAMESTATE_PAUSE;
game->loadstate = GAMESTATE_LEVEL;
Pause_Load(game);
} }
return 0; return 0;
} }

View file

@ -11,6 +11,7 @@
#include "intro.h" #include "intro.h"
#include "map.h" #include "map.h"
#include "level.h" #include "level.h"
#include "pause.h"
#include "config.h" #include "config.h"
/*! \brief Macro for preloading gamestate. /*! \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; } #define KEYDOWN_STATE(state, name) else if (game.gamestate==state) { if (name ## _Keydown(&game, &ev)) break; }
/*! \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;
void al_draw_text_with_shadow(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, int flags, char const *text) { 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); 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); PrintConsole(game, "Just stopping GAMESTATE_MENU..."); Menu_Stop(game);
} }
break; break;
UNLOAD_STATE(GAMESTATE_PAUSE, Pause)
UNLOAD_STATE(GAMESTATE_LOADING, Loading) UNLOAD_STATE(GAMESTATE_LOADING, Loading)
UNLOAD_STATE(GAMESTATE_ABOUT, About) UNLOAD_STATE(GAMESTATE_ABOUT, About)
UNLOAD_STATE(GAMESTATE_INTRO, Intro) UNLOAD_STATE(GAMESTATE_INTRO, Intro)
@ -122,6 +124,28 @@ void LoadGameState(struct Game *game) {
game->loadstate = -1; 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) { void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height, float val) {
if ((al_get_bitmap_width(source)==width) && (al_get_bitmap_height(source)==height)) { if ((al_get_bitmap_width(source)==width) && (al_get_bitmap_height(source)==height)) {
al_draw_bitmap(source, 0, 0, 0); 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)) { if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_TILDE)) {
game.showconsole = !game.showconsole; game.showconsole = !game.showconsole;
} }
KEYDOWN_STATE(GAMESTATE_PAUSE, Pause)
KEYDOWN_STATE(GAMESTATE_MENU, Menu) KEYDOWN_STATE(GAMESTATE_MENU, Menu)
KEYDOWN_STATE(GAMESTATE_LOADING, Loading) KEYDOWN_STATE(GAMESTATE_LOADING, Loading)
KEYDOWN_STATE(GAMESTATE_ABOUT, About) 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)) { if(redraw && al_is_event_queue_empty(game.event_queue)) {
redraw = false; redraw = false;
switch (game.gamestate) { DrawGameState(&game);
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;
}
DrawConsole(&game); DrawConsole(&game);
al_flip_display(); al_flip_display();
} }

View file

@ -16,6 +16,7 @@
/*! \brief Enum of all available gamestates. */ /*! \brief Enum of all available gamestates. */
enum gamestate_enum { enum gamestate_enum {
GAMESTATE_PAUSE,
GAMESTATE_LOADING, GAMESTATE_LOADING,
GAMESTATE_MENU, GAMESTATE_MENU,
GAMESTATE_ABOUT, GAMESTATE_ABOUT,
@ -77,6 +78,11 @@ struct Loading {
ALLEGRO_BITMAP *image; ALLEGRO_BITMAP *image;
}; };
/*! \brief Resources used by Pause state. */
struct Pause {
ALLEGRO_BITMAP *bitmap;
};
/*! \brief Resources used by About state. */ /*! \brief Resources used by About state. */
struct About { struct About {
ALLEGRO_BITMAP *fade_bitmap; ALLEGRO_BITMAP *fade_bitmap;
@ -139,6 +145,7 @@ struct Game {
struct About about; struct About about;
struct Map map; struct Map map;
struct Level level; struct Level level;
struct Pause pause;
}; };
/*! \brief Draws text with shadow. /*! \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); ALLEGRO_BITMAP* LoadFromCache(struct Game *game, char* filename, int width, int height);
float tps(struct Game *game, float t); float tps(struct Game *game, float t);
void DrawGameState(struct Game *game);
#endif #endif

41
src/pause.c Normal file
View file

@ -0,0 +1,41 @@
/*! \file pause.c
* \brief Pause state.
*/
#include <stdio.h>
#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);
}

11
src/pause.h Normal file
View file

@ -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);