blob: deb4bb1cf8982a90eaf879e9a0bb0c0ccb43c084 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd. All rights reserved.
//
// File: gameflow.cpp
//
// Description: Implementation for GameFlow class.
//
// History: + Stolen and cleaned up from Penthouse -- Darwin Chau
//
//=============================================================================
#define DONTCHECKVECTORRESIZING
//========================================
// System Includes
//========================================
#include <raddebug.hpp>
#include <radtime.hpp>
//========================================
// Project Includes
//========================================
#include <gameflow/gameflow.h>
#include <contexts/entrycontext.h>
#include <contexts/bootupcontext.h>
#include <contexts/frontendcontext.h>
#include <contexts/pausecontext.h>
#include <contexts/exitcontext.h>
#include <contexts/gameplay/loadinggameplaycontext.h>
#include <contexts/gameplay/gameplaycontext.h>
#include <contexts/demo/loadingdemocontext.h>
#include <contexts/demo/democontext.h>
#include <contexts/supersprint/loadingsupersprintcontext.h>
#include <contexts/supersprint/supersprintcontext.h>
#include <contexts/supersprint/supersprintfecontext.h>
#include <sound/soundmanager.h>
#include <main/commandlineoptions.h>
#include <main/game.h>
#include <memory/memoryutilities.h>
#include <memory/srrmemory.h>
//******************************************************************************
//
// Global Data, Local Data, Local Classes
//
//******************************************************************************
//
// Static pointer to instance of this singleton.
//
GameFlow* GameFlow::spInstance = NULL;
//******************************************************************************
//
// Public Member Functions
//
//******************************************************************************
//==============================================================================
// GameFlow::CreateInstance
//==============================================================================
//
// Description: Create the gameflow controller.
//
// Parameters: None.
//
// Return: Pointer to the created gameflow controller.
//
// Constraints: This is a singleton so only one instance is allowed.
//
//==============================================================================
GameFlow* GameFlow::CreateInstance()
{
MEMTRACK_PUSH_GROUP( "GameFlow" );
rAssert( spInstance == NULL );
spInstance = new(GMA_PERSISTENT) GameFlow;
rAssert( spInstance );
MEMTRACK_POP_GROUP( "GameFlow" );
return spInstance;
}
//==============================================================================
// GameFlow::GetInstance
//==============================================================================
//
// Description: Return the gameflow controller.
//
// Parameters: None.
//
// Return: Pointer to the created gameflow controller.
//
//==============================================================================
GameFlow* GameFlow::GetInstance()
{
//rAssert( spInstance != NULL );
return spInstance;
}
//==============================================================================
// GameFlow::DestroyInstance
//==============================================================================
//
// Description: Destroy the gameflow controller.
//
// Parameters: None.
//
// Return: None.
//
//==============================================================================
void GameFlow::DestroyInstance()
{
rAssert( spInstance != NULL );
delete( GMA_PERSISTENT, spInstance );
spInstance = NULL;
}
//==============================================================================
// GameFlow::PushContext
//==============================================================================
//
// Description: Change the context. Pushes the new context onto the stack.
// The previous context(s) are preserved on the stack.
// Note: the new context will not take effect until the next
// timer tick (game loop update).
//
// Parameters: context - the new context
//
// Return: None.
//
//==============================================================================
void GameFlow::PushContext( ContextEnum context )
{
//
// Really bad news if this assert triggers. There's already a context
// change in the queue when this one was requested.
//
// This assert cannot be ignored, you must fix the problem.
//
rAssert( mCurrentContext == mNextContext );
MEMTRACK_PUSH_GROUP( "GameFlow" );
mNextContext = context;
mContextStack.push( context );
MEMTRACK_POP_GROUP( "GameFlow" );
}
//==============================================================================
// GameFlow::PopContext
//==============================================================================
//
// Description: Restore the previous context.
//
// Note: the new context will not take effect until the next
// timer tick (game loop update).
//
// Constraints: Obviously we have a problem if there's nothing on the stack.
//
// Parameters: None.
//
// Return: None.
//
//==============================================================================
void GameFlow::PopContext()
{
rAssert( mContextStack.size() > 1 );
//
// Really bad news if this assert triggers. There's already a context
// change in the queue when this one was requested.
//
// This assert cannot be ignored, you must fix the problem.
//
rAssert( mCurrentContext == mNextContext );
mContextStack.pop();
mNextContext = mContextStack.top();
}
//==============================================================================
// GameFlow::SetContext
//==============================================================================
//
// Description: Change the context and clear out the stack.
//
// Note: the new context will not take effect until the next
// timer tick (game loop update).
//
// Parameters: context - the new context
//
// Return: None.
//
//==============================================================================
void GameFlow::SetContext( ContextEnum context )
{
while( !mContextStack.empty() )
{
mContextStack.pop();
}
this->PushContext( context );
}
//==============================================================================
// GameFlow::OnTimerDone
//==============================================================================
//
// Description: This routine is invoked to run the game. It gets called by the
// dispatcher once per frame.
//
// Parameters: elapsedtime - time it actually took for timer to expire
// pUserData - custom user data
//
// Return: None.
//
//==============================================================================
void GameFlow::OnTimerDone( unsigned int elapsedtime, void* pUserData )
{
//////////////////////////////////////////////////
// Debugging stuff.
//////////////////////////////////////////////////
bool printMemory = CommandLineOptions::Get( CLO_PRINT_MEMORY );
if( printMemory )
{
static int accumulatedTime = 0;
const int printTimeIntervalMs = 3000;
accumulatedTime += elapsedtime;
if( accumulatedTime > printTimeIntervalMs )
{
accumulatedTime %= printTimeIntervalMs;
Memory::PrintMemoryStatsToTty();
}
}
#ifndef RAD_RELEASE
// HACK to prevent elapsedtime from being ridiculously huge.
// This is so that when we set breakpoints we don't have really huge
// elapsedtime values screwing us up.
if( elapsedtime > 1000 )
{
elapsedtime = 20;
}
#endif
//////////////////////////////////////////////////
// Switch contexts if appropriate.
//////////////////////////////////////////////////
// If current and next contexts are different...
if( mCurrentContext != mNextContext )
{
mpContexts[mCurrentContext]->Stop( mNextContext );
mpContexts[mNextContext]->Start( mCurrentContext );
mCurrentContext = mNextContext;
}
//////////////////////////////////////////////////
// See if have anything to update.
//////////////////////////////////////////////////
// If current context is exit, then stop the game.
if( mCurrentContext == CONTEXT_EXIT )
{
GetGame()->Stop();
return;
}
//////////////////////////////////////////////////
// Update managers and controllers.
//////////////////////////////////////////////////
//
// Run the once-per-frame sound update
//
SoundManager::GetInstance()->UpdateOncePerFrame( elapsedtime, mCurrentContext );
// Update the current context.
mpContexts[mCurrentContext]->Update( elapsedtime );
}
//******************************************************************************
//
// Private Member Functions
//
//******************************************************************************
//==============================================================================
// GameFlow::GameFlow
//==============================================================================
//
// Description: Constructor.
//
// Parameters: None.
//
// Return: N/A.
//
//==============================================================================//
GameFlow::GameFlow() :
mpITimer( NULL ),
mCurrentContext( CONTEXT_ENTRY ),
mNextContext( CONTEXT_ENTRY )
{
//
// Initialize members.
//
int i = CONTEXT_ENTRY;
for( ; i < NUM_CONTEXTS; ++i )
{
mpContexts[i] = NULL;
}
//
// Create the context controllers.
//
mpContexts[CONTEXT_ENTRY] = GetEntryContext();
mpContexts[CONTEXT_BOOTUP] = GetBootupContext();
mpContexts[CONTEXT_FRONTEND] = GetFrontEndContext();
mpContexts[CONTEXT_LOADING_DEMO] = GetLoadingDemoContext();
mpContexts[CONTEXT_DEMO] = GetDemoContext();
mpContexts[CONTEXT_LOADING_SUPERSPRINT] = GetLoadingSuperSprintContext();
mpContexts[CONTEXT_SUPERSPRINT] = GetSPCTX();
mpContexts[CONTEXT_SUPERSPRINT_FE] = GetSuperSprintFEContext();
mpContexts[CONTEXT_LOADING_GAMEPLAY] = GetLoadingGameplayContext();
mpContexts[CONTEXT_GAMEPLAY] = GetGameplayContext();
mpContexts[CONTEXT_PAUSE] = GetPauseContext();
mpContexts[CONTEXT_EXIT] = GetExitContext();
//
// Since we're starting with the entry context, call its Start function
// for the sake of symmetry and to allow memory tagging to work properly
//
mpContexts[mCurrentContext]->Start( mCurrentContext );
}
//==============================================================================
// GameFlow::~GameFlow
//==============================================================================
//
// Description: Destructor.
//
// Parameters: None.
//
// Return: N/A.
//
//==============================================================================//
GameFlow::~GameFlow()
{
//
// Release the context controllers.
//
int i = CONTEXT_ENTRY;
for( ; i < NUM_CONTEXTS; ++i )
{
rAssert( mpContexts[i] != NULL );
mpContexts[i]->DestroyInstance();
}
}
#undef DONTCHECKVECTORRESIZING
|