states.c

Go to the documentation of this file.
00001 
00026 #include <assert.h>
00027 #include <stddef.h>
00028 #include "bflderrstate.h"
00029 #include "states.h"
00030 #include "splashstate.h"
00031 #include "gamestate.h"
00032 #include "menustate.h"
00033 #include "menu.h"
00034 
00040 static int currState = STATE_SPLASH;
00041 
00047 static Bool initalized = FALSE;
00048 
00056 static int Stub() {
00057     int state = currState + 1;
00058     if (state == STATE_MAX)
00059         return STATE_QUIT;
00060     return state;
00061 }
00062 
00068 static const struct State_t stateImpls[STATE_MAX] = {
00069     { SplashInit, SplashRun, SplashUninit },        // STATE_SPLASH
00070     { MenuRootInit, MenuRun, MenuRootUninit },      // STATE_MENUROOT
00071     { NULL, Stub, NULL },                           // STATE_MENUNAME
00072     { NULL, Stub, NULL },                           // STATE_MENUNET
00073     { NULL, Stub, NULL },                           // STATE_MENUCONN
00074     { NULL, Stub, NULL },                           // STATE_SHOWOPTS
00075     { GameInit, GameRun, GameUninit },              // STATE_PLAY
00076     { BfldErrInit, BfldErrRun, BfldErrUninit },     // STATE_BFLDERR
00077     { NULL, Stub, NULL }                            // STATE_DISCONN
00078 };
00079 
00080 int GetCurrentState() {
00081     return currState;
00082 }
00083 
00084 Bool RunState() {
00085     int result;
00086     // check for an uninitalized state
00087     if (!initalized) {
00088         // initialize the state
00089         if (stateImpls[currState].init) {   // only run non-NULL functions
00090             result = (*stateImpls[currState].init)();
00091             // check for error
00092             if (result <= STATE_QUIT) {
00093                 // non-recoverable error -- send back termination code
00094                 return FALSE;
00095             }
00096             // failed init; recoverable error
00097             if (result < STATE_MAX) {
00098                 // set the new state
00099                 currState = result;
00100                 // continue running states later
00101                 return TRUE;
00102             }
00103             // otherwise the init succeeded
00104             assert(result == STATE_OK);
00105         }
00106         // the state is initalized
00107         initalized = TRUE;
00108     }
00109     // run the state
00110     result = (*stateImpls[currState].run)();
00111     // check for state change
00112     if (result != currState) {
00113         assert(result < STATE_MAX);
00114         // call the uninitalize function and check for error
00115         if (stateImpls[currState].uninit &&   // only run non-NULL functions
00116         ((*stateImpls[currState].uninit)() != STATE_OK)) {
00117             // non-recoverable error -- send back termination code
00118             return FALSE;
00119         }
00120         // the state is uninitalized
00121         initalized = FALSE;
00122         // set the new state
00123         currState = result;
00124     }
00125     // check for error or quit
00126     if (result < 0) {
00127         // quit now
00128         return FALSE;
00129     }
00130     // no errors and no request to quit, so keep going
00131     return TRUE;
00132 }
00133 

Generated on Mon May 28 04:41:39 2007 for Retro Tank Super Attack by  doxygen 1.5.2