~ubuntu-branches/ubuntu/precise/supertuxkart/precise

« back to all changes in this revision

Viewing changes to src/states_screens/state_manager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-24 22:36:25 UTC
  • mfrom: (1.1.9 upstream) (6.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110224223625-ygrjfpg92obovuch
Tags: 0.7+dfsg1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  SuperTuxKart - a fun racing game with go-kart
 
2
//  Copyright (C) 2009 Marianne Gagnon
 
3
//
 
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.
 
8
//
 
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.
 
13
//
 
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.
 
17
 
 
18
 
 
19
#include "states_screens/state_manager.hpp"
 
20
 
 
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"
 
33
 
 
34
using namespace GUIEngine;
 
35
 
 
36
StateManager* state_manager_singleton = NULL;
 
37
 
 
38
StateManager* StateManager::get()
 
39
{
 
40
    if (state_manager_singleton == NULL) 
 
41
        state_manager_singleton = new StateManager();
 
42
    return state_manager_singleton;
 
43
}   // get
 
44
 
 
45
// ============================================================================
 
46
 
 
47
#if 0
 
48
#pragma mark -
 
49
#pragma mark Player Management
 
50
#endif
 
51
 
 
52
// ----------------------------------------------------------------------------
 
53
 
 
54
StateManager::ActivePlayer* StateManager::getActivePlayer(const int id)
 
55
{
 
56
    ActivePlayer *returnPlayer = NULL;
 
57
    if (id < m_active_players.size() && id >= 0)
 
58
    {
 
59
        returnPlayer = m_active_players.get(id);
 
60
    }
 
61
    else
 
62
    {
 
63
        fprintf(stderr, "getActivePlayer(): id out of bounds\n");
 
64
        assert(false);
 
65
        return NULL;
 
66
    }
 
67
    
 
68
    assert( returnPlayer->m_id == id );
 
69
    
 
70
    return returnPlayer;
 
71
}   // getActivePlayer
 
72
 
 
73
// ----------------------------------------------------------------------------
 
74
 
 
75
const PlayerProfile* StateManager::getActivePlayerProfile(const int id)
 
76
{
 
77
    ActivePlayer* a = getActivePlayer(id);
 
78
    if (a == NULL) return NULL;
 
79
    return a->getProfile();
 
80
}   // getActivePlayerProfile
 
81
 
 
82
// ----------------------------------------------------------------------------
 
83
 
 
84
void StateManager::updateActivePlayerIDs()
 
85
{
 
86
    const int amount = m_active_players.size();
 
87
    for (int n=0; n<amount; n++)
 
88
    {
 
89
        m_active_players[n].m_id = n;
 
90
    }
 
91
}   // updateActivePlayerIDs
 
92
 
 
93
// ----------------------------------------------------------------------------
 
94
 
 
95
int StateManager::createActivePlayer(PlayerProfile *profile, InputDevice *device)
 
96
{
 
97
    ActivePlayer *p;
 
98
    int i;
 
99
    p = new ActivePlayer(profile, device);
 
100
    i = m_active_players.size();
 
101
    m_active_players.push_back(p);
 
102
    
 
103
    updateActivePlayerIDs();
 
104
    
 
105
    return i;
 
106
}   // createActivePlayer
 
107
 
 
108
// ----------------------------------------------------------------------------
 
109
 
 
110
void StateManager::removeActivePlayer(int id)
 
111
{
 
112
    m_active_players.erase(id);
 
113
    updateActivePlayerIDs();
 
114
}   // removeActivePlayer
 
115
 
 
116
// ----------------------------------------------------------------------------
 
117
 
 
118
int StateManager::activePlayerCount()
 
119
{
 
120
    return m_active_players.size();
 
121
}   // activePlayerCount
 
122
 
 
123
// ----------------------------------------------------------------------------
 
124
 
 
125
void StateManager::resetActivePlayers()
 
126
{
 
127
    const int amount = m_active_players.size();
 
128
    for(int i=0; i<amount; i++)
 
129
    {
 
130
        m_active_players[i].setDevice(NULL);
 
131
    }
 
132
    m_active_players.clearAndDeleteAll();
 
133
}   // resetActivePlayers
 
134
 
 
135
// ----------------------------------------------------------------------------
 
136
 
 
137
#if 0
 
138
#pragma mark -
 
139
#pragma mark misc stuff
 
140
#endif
 
141
 
 
142
bool StateManager::throttleFPS()
 
143
{
 
144
    return m_game_mode != GUIEngine::GAME  &&
 
145
           GUIEngine::getCurrentScreen()->throttleFPS();
 
146
}   // throttleFPS
 
147
 
 
148
// ----------------------------------------------------------------------------
 
149
 
 
150
void StateManager::escapePressed()
 
151
{
 
152
    // in input sensing mode
 
153
    if(input_manager->isInMode(InputManager::INPUT_SENSE_KEYBOARD) ||
 
154
       input_manager->isInMode(InputManager::INPUT_SENSE_GAMEPAD) )
 
155
    {
 
156
        ModalDialog::dismiss();
 
157
        input_manager->setMode(InputManager::MENU);
 
158
    }
 
159
    // when another modal dialog is visible
 
160
    else if(ModalDialog::isADialogActive())
 
161
    {
 
162
        ModalDialog::getCurrent()->escapePressed();
 
163
    }
 
164
    // In-game
 
165
    else if(m_game_mode == GAME)
 
166
    {
 
167
        if(World::getWorld()->getPhase()!=WorldStatus::RESULT_DISPLAY_PHASE)
 
168
            new RacePausedDialog(0.8f, 0.6f);
 
169
    }
 
170
    // In menus
 
171
    else
 
172
    {
 
173
        if (getCurrentScreen()->onEscapePressed()) popMenu();
 
174
    }
 
175
}   // escapePressed
 
176
 
 
177
// ----------------------------------------------------------------------------
 
178
 
 
179
void StateManager::onGameStateChange(GameState new_state)
 
180
{
 
181
    if (new_state == GAME)
 
182
    {
 
183
        irr_driver->hidePointer();
 
184
        input_manager->setMode(InputManager::INGAME);
 
185
    }
 
186
    else  // menu (including in-game menu)
 
187
    {
 
188
        irr_driver->showPointer();
 
189
        input_manager->setMode(InputManager::MENU);
 
190
        sfx_manager->positionListener( Vec3(0,0,0), Vec3(0,1,0) );
 
191
        
 
192
        if (new_state == MENU)
 
193
        {
 
194
            Screen* screen = GUIEngine::getCurrentScreen();
 
195
            if (screen != NULL)
 
196
            {
 
197
                music_manager->startMusic(
 
198
                    GUIEngine::getCurrentScreen()->getMusic());
 
199
            }
 
200
        }
 
201
    }    
 
202
}   // onGameStateChange
 
203
 
 
204
// ----------------------------------------------------------------------------
 
205
 
 
206
void StateManager::onTopMostScreenChanged()
 
207
{
 
208
    if (m_game_mode == MENU && GUIEngine::getCurrentScreen() != NULL)
 
209
    {
 
210
        music_manager->startMusic(GUIEngine::getCurrentScreen()->getMusic());
 
211
    }
 
212
}   // onTopMostScreenChanged
 
213
 
 
214
// ----------------------------------------------------------------------------
 
215
 
 
216
void StateManager::onStackEmptied()
 
217
{
 
218
    GUIEngine::cleanUp();
 
219
    main_loop->abort();
 
220
}   // onStackEmptied
 
221
 
 
222
// ============================================================================
 
223
 
 
224
#if 0
 
225
#pragma mark -
 
226
#pragma mark ActivePlayer
 
227
#endif
 
228
 
 
229
StateManager::ActivePlayer::ActivePlayer(PlayerProfile* player, 
 
230
                                         InputDevice *device)
 
231
{
 
232
#ifdef DEBUG
 
233
    m_magic_number = 0xAC1EF1AE;
 
234
#endif
 
235
    
 
236
    m_player = player;
 
237
    m_device = NULL;
 
238
    m_kart = NULL;
 
239
    setDevice(device);
 
240
}  // ActivePlayer
 
241
 
 
242
// ----------------------------------------------------------------------------
 
243
StateManager::ActivePlayer::~ActivePlayer()
 
244
{
 
245
    setDevice(NULL);
 
246
    
 
247
#ifdef DEBUG
 
248
    m_magic_number = 0xDEADBEEF;
 
249
#endif
 
250
}   // ~ActivePlayer
 
251
 
 
252
// ----------------------------------------------------------------------------
 
253
 
 
254
void StateManager::ActivePlayer::setPlayerProfile(PlayerProfile* player)
 
255
{
 
256
#ifdef DEBUG
 
257
    assert(m_magic_number == 0xAC1EF1AE);
 
258
#endif
 
259
    m_player = player;
 
260
}   // setPlayerProfile
 
261
 
 
262
// ----------------------------------------------------------------------------
 
263
 
 
264
void StateManager::ActivePlayer::setDevice(InputDevice* device)
 
265
{
 
266
#ifdef DEBUG
 
267
    assert(m_magic_number == 0xAC1EF1AE);
 
268
#endif
 
269
    
 
270
    // unset player from previous device he was assigned to, if any
 
271
    if (m_device != NULL) m_device->setPlayer(NULL);
 
272
    
 
273
    m_device = device;
 
274
    
 
275
    // inform the devce of its new owner
 
276
    if (device != NULL) device->setPlayer(this);
 
277
}   // setDevice