nsnake
Classic snake game for the terminal
StateManager.cpp
1 #include <Flow/StateManager.hpp>
2 #include <Flow/GameStateGame.hpp>
3 #include <Flow/GameStateMainMenu.hpp>
4 #include <Flow/InputManager.hpp>
5 #include <Misc/Utils.hpp>
6 #include <Config/Globals.hpp>
7 
9  currentState(NULL),
10  sharedInfo(0)
11 {
12  // The first state, Hardcoded
13  this->currentState = new GameStateMainMenu();
14  this->currentState->load();
15 }
16 StateManager::~StateManager()
17 {
18  if (this->currentState)
19  this->currentState->unload();
20 
21  SAFE_DELETE(this->currentState);
22 }
24 {
25  bool letsQuit = false;
26 
27  while (!letsQuit)
28  {
29  InputManager::update();
30 
31  // Updating the whole state.
32  // This value is returned from it tell us if
33  // we need to switch from the current state.
34  GameState::StateCode whatToDoNow;
35 
36  whatToDoNow = this->currentState->update();
37 
38  switch (whatToDoNow)
39  {
40  case GameState::CONTINUE:
41  // Just continue on the current state.
42  break;
43 
44  case GameState::QUIT:
45  this->currentState->unload();
46  delete this->currentState;
47  this->currentState = NULL;
48 
49  letsQuit = true;
50  break;
51 
52  case GameState::GAME_START:
53  {
54  this->currentState->unload();
55  delete this->currentState;
56 
57  this->currentState = new GameStateGame();
58  this->currentState->load();
59  break;
60  }
61 
62  case GameState::MAIN_MENU:
63  {
64  this->currentState->unload();
65  delete this->currentState;
66 
67  this->currentState = new GameStateMainMenu();
68  this->currentState->load();
69  break;
70  }
71 
72  default:
73  break;
74  }
75 
76  if (this->currentState)
77  this->currentState->draw();
78 
79  Utils::Time::delay_ms(100);
80  }
81 
82  // // Right before quitting, we must save current
83  // // user's settings
84  // Globals::saveSettings();
85 
86  // // And set the current profile as the default
87  // // to load next time.
88  // INI ini;
89  // if (! ini.load(Globals::Config::file))
90  // ini.create();
91 
92  // ini.set("profiles:default", Globals::Profiles::current->name);
93  // ini.save(Globals::Config::file);
94 }
95 
void run()
Main entry point and game loop.
virtual void draw()=0
Called every frame, where states draw stuff on screen.
This represents the actual game taking place.
virtual int unload()=0
Where every state destroys it&#39;s resources.
StateManager()
Initializes pretty much everything.
Definition: StateManager.cpp:8
virtual StateCode update()=0
Called every frame, where states calculate everything that can change.
virtual void load(int stack=0)=0
Where every state initializes it&#39;s resources.
StateCode
All possible transitions between states.
Definition: GameState.hpp:39