Every state should be written in its own file. This could be the intro state:
bool InitIntro() {return true;}
void ShutIntro() {}
5bool UpdateIntro()
{
if(StateActive.time()>3 || Kb.bp(KB_ESC)) {
StateMenu.set(1.0);
}
10 return true;
}
void DrawIntro()
{
15 D.clear(BLACK);
D.text (0, 0, ”Intro”);
}
State StateIntro(UpdateIntro, DrawIntro, InitIntro, ShutIntro);
20
This looks a lot like the default state we used so far. The word ‘Intro’ is added before all functions to keep it comprehensable. The actual stated is constructed in the last line:
State StateIntro(UpdateIntro, DrawIntro, InitIntro, ShutIntro);
The state is given a name, and all functions are added to the state. The only thing which is not present is an InitPre() function. That one is not part of a state. It is the first function the application will call when started.
Take a look at the constructor of the State class:
State(Bool (*update)(), void (*draw)(), Bool (*init)()=NULL, void (*shut)()=NULL);
Does the asterisk ring a bell? Indeed, it’s a pointer. A function pointer to be more precise. The constructor expects us to provide it with the functions that should be ran in this state. Also, the init and shut functions are optional. (They have default value which is a null pointer.) If you don’t need to initialize or cleanup, you may leave this empty.