00001 00002 // Copyright (C) 2004-2007 by The Allacrost Project 00003 // All Rights Reserved 00004 // 00005 // This code is licensed under the GNU GPL version 2. It is free software 00006 // and you may modify it and/or redistribute it under the terms of this license. 00007 // See http://www.gnu.org/copyleft/gpl.html for details. 00009 00016 #include "mode_manager.h" 00017 #include "system.h" 00018 #include "boot.h" 00019 00020 using namespace std; 00021 00022 using namespace hoa_utils; 00023 using namespace hoa_system; 00024 using namespace hoa_boot; 00025 00026 00027 template<> hoa_mode_manager::GameModeManager* Singleton<hoa_mode_manager::GameModeManager>::_singleton_reference = NULL; 00028 00029 namespace hoa_mode_manager { 00030 00031 GameModeManager* ModeManager = NULL; 00032 bool MODE_MANAGER_DEBUG = false; 00033 00034 // **************************************************************************** 00035 // ***** GameMode class 00036 // **************************************************************************** 00037 00038 GameMode::GameMode() { 00039 if (MODE_MANAGER_DEBUG) cout << "MODE MANAGER: GameMode constructor invoked" << endl; 00040 00041 // The value of this member should later be replaced by the child class 00042 mode_type = MODE_MANAGER_DUMMY_MODE; 00043 } 00044 00045 GameMode::GameMode(uint8 mt) { 00046 if (MODE_MANAGER_DEBUG) cout << "MODE MANAGER: GameMode constructor invoked" << endl; 00047 mode_type = mt; 00048 } 00049 00050 GameMode::~GameMode() { 00051 if (MODE_MANAGER_DEBUG) cout << "MODE MANAGER: GameMode destructor invoked" << endl; 00052 } 00053 00054 // **************************************************************************** 00055 // ***** GameModeManager class 00056 // **************************************************************************** 00057 00058 // This constructor must be defined for the singleton macro 00059 GameModeManager::GameModeManager() { 00060 if (MODE_MANAGER_DEBUG) cout << "MODE MANAGER: GameModeManager constructor invoked" << endl; 00061 _pop_count = 0; 00062 _state_change = false; 00063 } 00064 00065 00066 // The destructor frees all the modes still on the stack 00067 GameModeManager::~GameModeManager() { 00068 if (MODE_MANAGER_DEBUG) cout << "MODE MANAGER: GameModeManager destructor invoked" << endl; 00069 // Delete any game modes on the stack 00070 while (_game_stack.size() != 0) { 00071 delete _game_stack.back(); 00072 _game_stack.pop_back(); 00073 } 00074 00075 // Delete any game modes on the push stack 00076 while (_push_stack.size() != 0) { 00077 delete _push_stack.back(); 00078 _push_stack.pop_back(); 00079 } 00080 } 00081 00082 00083 // Empties out the game stack and then initializes it by placing BootMode on the stack top. 00084 bool GameModeManager::SingletonInitialize() { 00085 // Delete any game modes on the stack 00086 while (_game_stack.size() != 0) { 00087 delete _game_stack.back(); 00088 _game_stack.pop_back(); 00089 } 00090 00091 // Delete any game modes on the push stack 00092 while (_push_stack.size() != 0) { 00093 delete _push_stack.back(); 00094 _push_stack.pop_back(); 00095 } 00096 00097 // Reset the pop counter 00098 _pop_count = 0; 00099 00100 // Create a new BootMode and push it onto the true game stack immediately 00101 BootMode* BM = new BootMode(); 00102 _game_stack.push_back(BM); 00103 _state_change = true; 00104 00105 return true; 00106 } 00107 00108 00109 00110 // Free the top mode on the stack and pop it off 00111 void GameModeManager::Pop() { 00112 _pop_count++; 00113 _state_change = true; 00114 } 00115 00116 00117 00118 // Pop off all game modes 00119 void GameModeManager::PopAll() { 00120 _pop_count = static_cast<uint32>(_game_stack.size()); 00121 } 00122 00123 00124 // Push a new game mode onto the stack 00125 void GameModeManager::Push(GameMode* gm) { 00126 _push_stack.push_back(gm); 00127 _state_change = true; 00128 } 00129 00130 00131 // Returns the mode type of the game mode on the top of the stack 00132 uint8 GameModeManager::GetGameType() { 00133 if (_game_stack.empty()) 00134 return MODE_MANAGER_DUMMY_MODE; 00135 else 00136 return _game_stack.back()->mode_type; 00137 } 00138 00139 00140 // Returns a pointer to the game mode that's currently on the top of the stack 00141 GameMode* GameModeManager::GetTop() { 00142 if (_game_stack.empty()) 00143 return NULL; 00144 else 00145 return _game_stack.back(); 00146 } 00147 00148 00149 // Checks if any game modes need to be pushed or popped off the stack, then updates the top stack mode. 00150 void GameModeManager::Update() { 00151 // If a Push() or Pop() function was called, we need to adjust the state of the game stack. 00152 if (_state_change == true) { 00153 // Pop however many game modes we need to from the top of the stack 00154 while (_pop_count != 0) { 00155 if (_game_stack.empty()) { 00156 if (MODE_MANAGER_DEBUG) { 00157 cerr << "MODE MANAGER WARNING: Tried to pop off more game modes than were on the stack!" << endl; 00158 } 00159 break; // Exit the loop 00160 } 00161 delete _game_stack.back(); 00162 _game_stack.pop_back(); 00163 _pop_count--; 00164 } 00165 00166 // Push any new game modes onto the true game stack. 00167 while (_push_stack.size() != 0) { 00168 _game_stack.push_back(_push_stack.back()); 00169 _push_stack.pop_back(); 00170 } 00171 00172 // Make sure there is a game mode on the stack, otherwise we'll get a segementation fault. 00173 if (_game_stack.empty()) { 00174 cerr << "MODE MANAGER ERROR: Game stack is empty! Now re-initializing boot mode." << endl; 00175 SingletonInitialize(); 00176 } 00177 00178 // Call the newly active game mode's Reset() function to re-initialize the game mode 00179 _game_stack.back()->Reset(); 00180 00181 // Reset the state change variable 00182 _state_change = false; 00183 00184 // Call the system manager and tell it that the active game mode changed so it can update timers accordingly 00185 SystemManager->ExamineSystemTimers(); 00186 } // if (_state_change == true) 00187 00188 // Call the Update function on the top stack mode (the active game mode) 00189 _game_stack.back()->Update(); 00190 } 00191 00192 00193 // Checks if any game modes need to be pushed or popped off the stack, then updates the top stack mode. 00194 void GameModeManager::Draw() { 00195 if (_game_stack.size() == 0) { 00196 return; 00197 } 00198 00199 _game_stack.back()->Draw(); 00200 } 00201 00202 00203 // Used for debugging purposes ONLY. Prints the contents of the game mode stack. 00204 void GameModeManager::DEBUG_PrintStack() { 00205 cout << "MODE MANAGER DEBUG: Printing Game Stack" << endl; 00206 if (_game_stack.size() == 0) { 00207 cout << "***Game stack is empty!" << endl; 00208 return; 00209 } 00210 00211 cout << "***top of stack***" << endl; 00212 for (int32 i = static_cast<int32>(_game_stack.size()) - 1; i >= 0; i--) 00213 cout << " index: " << i << " type: " << _game_stack[i]->mode_type << endl; 00214 cout << "***bottom of stack***" << endl; 00215 } 00216 00217 } // namespace hoa_mode_manager
1.5.1