1
// SuperTuxKart - a fun racing game with go-kart
2
// Copyright (C) 2009 Marianne Gagnon
4
// This program is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU General Public License
6
// as published by the Free Software Foundation; either version 3
7
// of the License, or (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
#include "states_screens/state_manager.hpp"
21
#include "audio/sfx_manager.hpp"
22
#include "audio/music_manager.hpp"
23
#include "config/stk_config.hpp"
24
#include "guiengine/engine.hpp"
25
#include "guiengine/modaldialog.hpp"
26
#include "guiengine/screen.hpp"
27
#include "input/input_device.hpp"
28
#include "input/input_manager.hpp"
29
#include "main_loop.hpp"
30
#include "modes/world.hpp"
31
#include "states_screens/dialogs/race_paused_dialog.hpp"
32
#include "utils/translation.hpp"
34
using namespace GUIEngine;
36
StateManager* state_manager_singleton = NULL;
38
StateManager* StateManager::get()
40
if (state_manager_singleton == NULL)
41
state_manager_singleton = new StateManager();
42
return state_manager_singleton;
45
// ============================================================================
49
#pragma mark Player Management
52
// ----------------------------------------------------------------------------
54
StateManager::ActivePlayer* StateManager::getActivePlayer(const int id)
56
ActivePlayer *returnPlayer = NULL;
57
if (id < m_active_players.size() && id >= 0)
59
returnPlayer = m_active_players.get(id);
63
fprintf(stderr, "getActivePlayer(): id out of bounds\n");
68
assert( returnPlayer->m_id == id );
73
// ----------------------------------------------------------------------------
75
const PlayerProfile* StateManager::getActivePlayerProfile(const int id)
77
ActivePlayer* a = getActivePlayer(id);
78
if (a == NULL) return NULL;
79
return a->getProfile();
80
} // getActivePlayerProfile
82
// ----------------------------------------------------------------------------
84
void StateManager::updateActivePlayerIDs()
86
const int amount = m_active_players.size();
87
for (int n=0; n<amount; n++)
89
m_active_players[n].m_id = n;
91
} // updateActivePlayerIDs
93
// ----------------------------------------------------------------------------
95
int StateManager::createActivePlayer(PlayerProfile *profile, InputDevice *device)
99
p = new ActivePlayer(profile, device);
100
i = m_active_players.size();
101
m_active_players.push_back(p);
103
updateActivePlayerIDs();
106
} // createActivePlayer
108
// ----------------------------------------------------------------------------
110
void StateManager::removeActivePlayer(int id)
112
m_active_players.erase(id);
113
updateActivePlayerIDs();
114
} // removeActivePlayer
116
// ----------------------------------------------------------------------------
118
int StateManager::activePlayerCount()
120
return m_active_players.size();
121
} // activePlayerCount
123
// ----------------------------------------------------------------------------
125
void StateManager::resetActivePlayers()
127
const int amount = m_active_players.size();
128
for(int i=0; i<amount; i++)
130
m_active_players[i].setDevice(NULL);
132
m_active_players.clearAndDeleteAll();
133
} // resetActivePlayers
135
// ----------------------------------------------------------------------------
139
#pragma mark misc stuff
142
bool StateManager::throttleFPS()
144
return m_game_mode != GUIEngine::GAME &&
145
GUIEngine::getCurrentScreen()->throttleFPS();
148
// ----------------------------------------------------------------------------
150
void StateManager::escapePressed()
152
// in input sensing mode
153
if(input_manager->isInMode(InputManager::INPUT_SENSE_KEYBOARD) ||
154
input_manager->isInMode(InputManager::INPUT_SENSE_GAMEPAD) )
156
ModalDialog::dismiss();
157
input_manager->setMode(InputManager::MENU);
159
// when another modal dialog is visible
160
else if(ModalDialog::isADialogActive())
162
ModalDialog::getCurrent()->escapePressed();
165
else if(m_game_mode == GAME)
167
if(World::getWorld()->getPhase()!=WorldStatus::RESULT_DISPLAY_PHASE)
168
new RacePausedDialog(0.8f, 0.6f);
173
if (getCurrentScreen()->onEscapePressed()) popMenu();
177
// ----------------------------------------------------------------------------
179
void StateManager::onGameStateChange(GameState new_state)
181
if (new_state == GAME)
183
irr_driver->hidePointer();
184
input_manager->setMode(InputManager::INGAME);
186
else // menu (including in-game menu)
188
irr_driver->showPointer();
189
input_manager->setMode(InputManager::MENU);
190
sfx_manager->positionListener( Vec3(0,0,0), Vec3(0,1,0) );
192
if (new_state == MENU)
194
Screen* screen = GUIEngine::getCurrentScreen();
197
music_manager->startMusic(
198
GUIEngine::getCurrentScreen()->getMusic());
202
} // onGameStateChange
204
// ----------------------------------------------------------------------------
206
void StateManager::onTopMostScreenChanged()
208
if (m_game_mode == MENU && GUIEngine::getCurrentScreen() != NULL)
210
music_manager->startMusic(GUIEngine::getCurrentScreen()->getMusic());
212
} // onTopMostScreenChanged
214
// ----------------------------------------------------------------------------
216
void StateManager::onStackEmptied()
218
GUIEngine::cleanUp();
222
// ============================================================================
226
#pragma mark ActivePlayer
229
StateManager::ActivePlayer::ActivePlayer(PlayerProfile* player,
233
m_magic_number = 0xAC1EF1AE;
242
// ----------------------------------------------------------------------------
243
StateManager::ActivePlayer::~ActivePlayer()
248
m_magic_number = 0xDEADBEEF;
252
// ----------------------------------------------------------------------------
254
void StateManager::ActivePlayer::setPlayerProfile(PlayerProfile* player)
257
assert(m_magic_number == 0xAC1EF1AE);
260
} // setPlayerProfile
262
// ----------------------------------------------------------------------------
264
void StateManager::ActivePlayer::setDevice(InputDevice* device)
267
assert(m_magic_number == 0xAC1EF1AE);
270
// unset player from previous device he was assigned to, if any
271
if (m_device != NULL) m_device->setPlayer(NULL);
275
// inform the devce of its new owner
276
if (device != NULL) device->setPlayer(this);