use preprocessor macros in gamestate management to get rid of lots of duplicated code

This commit is contained in:
Sebastian Krzyszkowiak 2012-02-25 21:53:33 +01:00
parent 7b30cd7b45
commit 3d50b5d3a4

View file

@ -8,6 +8,13 @@
#include "level.h" #include "level.h"
#include "config.h" #include "config.h"
#define PRELOAD_STATE(state, name) case state:\
PrintConsole(game, "Preload %s...", #state); DrawConsole(game); al_flip_display(); name ## _Preload(game); break;
#define UNLOAD_STATE(state, name) case state:\
PrintConsole(game, "Unload %s...", #state); name ## _Unload(game); break;
#define LOAD_STATE(state, name) case state:\
PrintConsole(game, "Load %s...", #state); name ## _Load(game); break;
float FPS = 60; float FPS = 60;
int DISPLAY_WIDTH = 800; int DISPLAY_WIDTH = 800;
int DISPLAY_HEIGHT = 500; int DISPLAY_HEIGHT = 500;
@ -44,103 +51,45 @@ void DrawConsole(struct Game *game) {
} }
void PreloadGameState(struct Game *game) { void PreloadGameState(struct Game *game) {
if (game->loadstate==GAMESTATE_MENU) { switch (game->loadstate) {
PrintConsole(game, "Preload GAMESTATE_MENU..."); PRELOAD_STATE(GAMESTATE_MENU, Menu)
DrawConsole(game); PRELOAD_STATE(GAMESTATE_LOADING, Loading)
al_flip_display(); PRELOAD_STATE(GAMESTATE_ABOUT, About)
Menu_Preload(game); PRELOAD_STATE(GAMESTATE_INTRO, Intro)
} PRELOAD_STATE(GAMESTATE_MAP, Map)
else if (game->loadstate==GAMESTATE_LOADING) { PRELOAD_STATE(GAMESTATE_LEVEL, Level)
PrintConsole(game, "Preload GAMESTATE_LOADING..."); default:
DrawConsole(game); PrintConsole(game, "ERROR: Attempted to preload unknown gamestate %d!", game->loadstate);
al_flip_display(); break;
Loading_Preload(game);
}
else if (game->loadstate==GAMESTATE_ABOUT) {
PrintConsole(game, "Preload GAMESTATE_ABOUT...");
DrawConsole(game);
al_flip_display();
About_Preload(game);
}
else if (game->loadstate==GAMESTATE_INTRO) {
PrintConsole(game, "Preload GAMESTATE_INTRO...");
DrawConsole(game);
al_flip_display();
Intro_Preload(game);
}
else if (game->loadstate==GAMESTATE_MAP) {
PrintConsole(game, "Preload GAMESTATE_MAP...");
DrawConsole(game);
al_flip_display();
Map_Preload(game);
}
else if (game->loadstate==GAMESTATE_LEVEL) {
PrintConsole(game, "Preload GAMESTATE_LEVEL...");
DrawConsole(game);
al_flip_display();
Level_Preload(game);
} else {
PrintConsole(game, "ERROR: Attempted to preload unknown gamestate %d!", game->loadstate);
} }
PrintConsole(game, "finished"); PrintConsole(game, "finished");
} }
void UnloadGameState(struct Game *game) { void UnloadGameState(struct Game *game) {
if (game->gamestate==GAMESTATE_MENU) { switch (game->gamestate) {
PrintConsole(game, "Unload GAMESTATE_MENU..."); UNLOAD_STATE(GAMESTATE_MENU, Menu)
Menu_Unload(game); UNLOAD_STATE(GAMESTATE_LOADING, Loading)
} UNLOAD_STATE(GAMESTATE_ABOUT, About)
else if (game->gamestate==GAMESTATE_LOADING) { UNLOAD_STATE(GAMESTATE_INTRO, Intro)
PrintConsole(game, "Unload GAMESTATE_LOADING..."); UNLOAD_STATE(GAMESTATE_MAP, Map)
Loading_Unload(game); UNLOAD_STATE(GAMESTATE_LEVEL, Level)
} default:
else if (game->gamestate==GAMESTATE_ABOUT) { PrintConsole(game, "ERROR: Attempted to unload unknown gamestate %d!", game->gamestate);
PrintConsole(game, "Unload GAMESTATE_ABOUT..."); break;
About_Unload(game);
}
else if (game->gamestate==GAMESTATE_INTRO) {
PrintConsole(game, "Unload GAMESTATE_INTRO...");
Intro_Unload(game);
}
else if (game->gamestate==GAMESTATE_MAP) {
PrintConsole(game, "Unload GAMESTATE_MAP...");
Map_Unload(game);
}
else if (game->gamestate==GAMESTATE_LEVEL) {
PrintConsole(game, "Unload GAMESTATE_LEVEL...");
Level_Unload(game);
} else {
PrintConsole(game, "ERROR: Attempted to unload unknown gamestate %d!", game->gamestate);
} }
PrintConsole(game, "finished"); PrintConsole(game, "finished");
} }
void LoadGameState(struct Game *game) { void LoadGameState(struct Game *game) {
if (game->loadstate==GAMESTATE_MENU) { switch (game->loadstate) {
PrintConsole(game, "Load GAMESTATE_MENU..."); LOAD_STATE(GAMESTATE_MENU, Menu)
Menu_Load(game); LOAD_STATE(GAMESTATE_LOADING, Loading)
} LOAD_STATE(GAMESTATE_ABOUT, About)
else if (game->loadstate==GAMESTATE_LOADING) { LOAD_STATE(GAMESTATE_INTRO, Intro)
PrintConsole(game, "Load GAMESTATE_LOADING..."); LOAD_STATE(GAMESTATE_MAP, Map)
Loading_Load(game); LOAD_STATE(GAMESTATE_LEVEL, Level)
} default:
else if (game->loadstate==GAMESTATE_ABOUT) { PrintConsole(game, "ERROR: Attempted to load unknown gamestate %d!", game->loadstate);
PrintConsole(game, "Load GAMESTATE_ABOUT...");
About_Load(game);
}
else if (game->loadstate==GAMESTATE_INTRO) {
PrintConsole(game, "Load GAMESTATE_INTRO...");
Intro_Load(game);
}
else if (game->loadstate==GAMESTATE_MAP) {
PrintConsole(game, "Load GAMESTATE_MAP...");
Map_Load(game);
}
else if (game->loadstate==GAMESTATE_LEVEL) {
PrintConsole(game, "Load GAMESTATE_LEVEL...");
Level_Load(game);
} else {
PrintConsole(game, "ERROR: Attempted to load unknown gamestate %d!", game->loadstate);
} }
PrintConsole(game, "finished"); PrintConsole(game, "finished");
game->gamestate = game->loadstate; game->gamestate = game->loadstate;