summaryrefslogblamecommitdiffstats
path: root/game/code/contexts/pausecontext.cpp
blob: b001c5f8fe4069a5d8af77328fd2dda980cd9a4c (plain) (tree)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575






























































































































































































































































































































































































































































































































































































                                                                                                      
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        
//
// Description: Implement PauseContext
//
// History:     21/05/2002 + Created -- NAME
//
//=============================================================================

//========================================
// System Includes
//========================================
#include <p3d/view.hpp>
#include <p3d/shadow.hpp>

// Foundation Tech
#include <raddebug.hpp>
#include <radtime.hpp>
#include <raddebugwatch.hpp>

//========================================
// Project Includes
//========================================
#include <ai/vehicle/vehicleairender.h>

#include <contexts/pausecontext.h>
#include <contexts/contextenum.h>
#include <memory/leakdetection.h>
#include <memory/srrmemory.h>
#include <worldsim/worldobject.h>

#include <camera/supercammanager.h>
#include <camera/animatedcam.h>
#include <interiors/interiormanager.h>
#include <meta/triggervolumetracker.h>
#include <mission/gameplaymanager.h>
#include <presentation/gui/guisystem.h>
#include <presentation/gui/ingame/guimanageringame.h>
#include <presentation/gui/ingame/guiscreenhud.h>
#include <presentation/gui/utility/hudmap.h>
#include <render/RenderManager/RenderManager.h>
#include <render/RenderManager/RenderLayer.h>
#include <render/DSG/StatePropDSG.h>
#include <sound/soundmanager.h>
#include <worldsim/worldphysicsmanager.h>
#include <worldsim/character/charactermanager.h>
#include <worldsim/character/footprint/footprintmanager.h>
#include <worldsim/avatarmanager.h>
#include <worldsim/traffic/trafficmanager.h>
#include <worldsim/ped/pedestrianmanager.h>
#include <worldsim/coins/coinmanager.h>
#include <worldsim/coins/sparkle.h>
#include <worldsim/hitnrunmanager.h>
#include <worldsim/parkedcars/parkedcarmanager.h>
#include <ai/actionbuttonmanager.h>
#include <render/breakables/breakablesmanager.h>
#include <render/particles/ParticleManager.h>
#include <worldsim/skidmarks/skidmarkgenerator.h>
#include <worldsim/skidmarks/skidmarkmanager.h>
#include <ai/actor/actormanager.h>
#include <gameflow/gameflow.h>
#include <roads/roadmanager.h>
#include <render/Loaders/AnimDynaPhysLoader.h>
#include <pedpaths/pathmanager.h>

#include <input/inputmanager.h>

#include <data/gamedatamanager.h>

#include <mission/animatedicon.h>

#include <render/animentitydsgmanager/animentitydsgmanager.h>
#include <presentation/presentation.h>
#include <presentation/tutorialmanager.h>

//******************************************************************************
//
// Global Data, Local Data, Local Classes
//
//******************************************************************************

// Static pointer to instance of singleton.
PauseContext* PauseContext::spInstance = NULL;

//******************************************************************************
//
// Public Member Functions
//
//******************************************************************************

//==============================================================================
// PauseContext::GetInstance
//==============================================================================
//
// Description: - Access point for the PauseContext singleton.  
//              - Creates the PauseContext if needed.
//
// Parameters:	None.
//
// Return:      Pointer to the PauseContext.
//
// Constraints: This is a singleton so only one instance is allowed.
//
//==============================================================================
PauseContext* PauseContext::GetInstance()
{
    if( spInstance == NULL )
    {
        spInstance = new(GMA_PERSISTENT) PauseContext;
        rAssert( spInstance );
    }
    
    return spInstance;
}

//==============================================================================
// PauseContext::PauseContext
//==============================================================================
// Description: Constructor.
//
// Parameters: None.
//
// Return:      N/A.
//
//==============================================================================
PauseContext::PauseContext()
:   mOldState( Input::ACTIVE_NONE ),
    m_quitGamePending( false ),
    m_waitingForContextSwitch( false )
{
}

//==============================================================================
// PauseContext::~PauseContext
//==============================================================================
// Description: Destructor.
//
// Parameters: None.
//
// Return:      N/A.
//
//==============================================================================
PauseContext::~PauseContext()
{
}

//******************************************************************************
//
// Private Member Functions
//
//******************************************************************************

//=============================================================================
// PauseContext::OnStart
//=============================================================================
// Description: Comment
//
// Parameters:  ( ContextEnum previousContext )
//
// Return:      void 
//
//=============================================================================
void PauseContext::OnStart( ContextEnum previousContext )
{
    SetMemoryIdentification( "PauseContext" );
    MEMTRACK_PUSH_FLAG( "Pause" );

    mOldState = GetInputManager()->GetGameState();
    if( mOldState == Input::ACTIVE_ANIM_CAM )
    {
        // deactivate anim cam state first, since the input manager
        // won't let us set the game state to anything else prior
        // to that
        //
        GetInputManager()->SetGameState( Input::DEACTIVE_ANIM_CAM );
        SuperCam* sc = GetSuperCamManager()->GetSCC( 0 )->GetActiveSuperCam();
        AnimatedCam* ac = dynamic_cast<AnimatedCam*>(sc);
        if(ac)
        {
            ac->Abort();
        }
    }
    GetInputManager()->SetGameState( Input::ACTIVE_FRONTEND );

	GetCoinManager()->ClearHUDCoins();

    m_quitGamePending = false;
}


extern void OutputHandler (const char * pString );


//=============================================================================
// PauseContext::OnStop
//=============================================================================
// Description: Comment
//
// Parameters:  ( ContextEnum nextContext )
//
// Return:      void 
//
//=============================================================================
void PauseContext::OnStop( ContextEnum nextContext )
{
    // do gameplay terminations only if next context is frontend context or exit.
    if( nextContext == CONTEXT_FRONTEND ||
        nextContext == CONTEXT_LOADING_GAMEPLAY ||
        nextContext == CONTEXT_EXIT )
    {
        rReleasePrintf("PauseContext::OnStop Begins\n");

        GetInputManager()->EnableReset( false );

        RenderLayer* l = GetRenderManager()->mpLayer( RenderEnums::GUI );
        rAssert( l );
        l->Thaw();
//        GetGameFlow()->SetQuickStartLoading( false );
        // STL reallocs, etc may result in actually allocating some memory during this process.
        // As such, reroute everything to temp.
        //
        HeapMgr()->PushHeap (GMA_TEMP);

        //
        // This is called to prevent DMA of destroyed textures,
        // because we don't swap buffers until the next frame.
        //
        p3d::pddi->DrawSync();

        //::radThreadSleep (500);

        //::rDebugSetOutputHandler (OutputHandler);

        //rReleaseString ("DrawSync\n");

        // Clear the shadow list
        AnimDynaPhysLoader::ClearShadowList();

        GetCoinManager()->Destroy();
        GetSparkleManager()->Destroy();
		GetHitnRunManager()->Destroy();

        StatePropDSG::RemoveAllSharedtPoses();


        GetInteriorManager()->OnGameplayEnd();

        //rReleaseString ("GetInteriorManager()->OnGameplayEnd()\n");

        const bool shutdown = true;
        GetSuperCamManager()->Init( shutdown );

        //rReleaseString ("GetSuperCamManager()->Init( shutdown )\n");

        TriggerVolumeTracker::GetInstance()->Cleanup();
        
        //rReleaseString ("TriggerVolumeTracker::GetInstance()->Cleanup()\n");

        // Clean up lights!
        //
        RenderLayer* rl = GetRenderManager()->mpLayer( RenderEnums::LevelSlot );
        rAssert( rl );
        for( unsigned int i = 0; i < rl->GetNumViews(); i++ )
        {
            rl->pView(i)->RemoveAllLights ();
        }  

        //rReleaseString ("Remove all lights from all views\n");

        //rReleaseString ("GetAvatarManager()->Destroy()\n");
        GetActorManager()->RemoveAllActors();
        GetActorManager()->RemoveAllSpawnPoints();

        GetPresentationManager()->Finalize();

        GetGameplayManager()->Finalize();
        //rReleaseString ("GetGameplayManager()->Finalize()\n");

        // needs to be finalized after gameplay manager (Mission shudown needs avatars)
        GetAvatarManager()->Destroy();

        ParkedCarManager::DestroyInstance();
        //rReleaseString ("ParkedCarManager::DestroyInstance()\n");

        TrafficManager::DestroyInstance();
        //rReleaseString ("TrafficManager::DestroyInstance()\n");

        PedestrianManager::DestroyInstance();
        //rReleaseString ("PedestrianManager::DestroyInstance()\n");

        GetCharacterManager()->Destroy();
        //rReleaseString ("GetCharacterManager()->Destroy()\n");

        GetVehicleCentral()->Unload();

        GetActionButtonManager( )->Destroy( );
        //rReleaseString ("GetActionButtonManager( )->Destroy( )\n");


		GetBreakablesManager()->FreeAllBreakables();
        //rReleaseString ("GetBreakablesManager()->FreeAllBreakables()\n");

		GetParticleManager()->ClearSystems();
        //rReleaseString ("GetParticleManager()->ClearSystems()\n");
        
        
        GetWorldPhysicsManager()->OnQuitLevel();    // just some cleanup checks...
        
        
        
        SkidMarkGenerator::ReleaseShaders();
        //rReleaseString ("SkidMarkGenerator::ReleaseShaders()\n");
        GetSkidmarkManager()->Destroy();
        GetFootprintManager()->FreeTextures();

        GetRenderManager()->ClearLevelLayerLights();
        rReleasePrintf("PauseContext::OnStop DumpAllLoadedData\n");
		GetRenderManager()->DumpAllLoadedData();
        //rReleaseString ("GetRenderManager()->DumpAllLoadedData()\n");




#ifdef DEBUGWATCH
        VehicleAIRender::Destroy();
        //rReleaseString ("VehicleAIRender::Destroy()\n");

#endif
        GetSoundManager()->OnGameplayEnd( nextContext == CONTEXT_FRONTEND );
        //rReleaseString ("GetSoundManager()->OnGameplayEnd ()\n");


        PathManager::GetInstance()->Destroy();
        //rReleaseString ("PathManager::GetInstance()->Destroy()\n");

        

        // TODO - Darryl?
        // 
        // all active vehicles should be destroyed
        // they are owned by the missions that created them.

        //This does cleanup.
        RoadManager::GetInstance()->Destroy();
        //rReleaseString ("RoadManager::GetInstance()->Destroy()\n");



        if( nextContext == CONTEXT_LOADING_GAMEPLAY )
        {
            // if reloading in-game, set new level and mission to load
            //
            int level = GetGuiSystem()->GetInGameManager()->GetNextLevelToLoad();
            GetGameplayManager()->SetLevelIndex( static_cast<RenderEnums::LevelEnum>( level ) );

            int mission = GetGuiSystem()->GetInGameManager()->GetNextMissionToLoad();
            GetGameplayManager()->SetMissionIndex( static_cast<RenderEnums::MissionEnum>( mission ) );
            //rReleaseString ("Set level and mission indices\n");

        }
        else
        {
            SetGameplayManager( NULL );
        }

        // Cleanup the Avatar Manager
        //
        GetAvatarManager()->ExitGame();
        //rReleaseString ("GetAvatarManager()->ExitGame()\n");


        // Flush out the special section used by physics to cache SkeletonInfos.
        //
        p3d::inventory->RemoveSectionElements (SKELCACHE);
        p3d::inventory->DeleteSection (SKELCACHE);
        rReleaseString ("Delete SKELCACHE inventory section\n");


        // release GUI in-game
        GetGuiSystem()->HandleMessage( GUI_MSG_RELEASE_INGAME );
        rReleaseString ("GetGuiSystem()->HandleMessage( GUI_MSG_RELEASE_INGAME )\n");

        CHudMap::ClearAllRegisteredIcons();

        // unregister GUI user input handler for active players
        //
        int activeControllerIDs = 0;
        for( int i = 0; i < MAX_PLAYERS; i++ )
        {
            int controllerID = GetInputManager()->GetControllerIDforPlayer( i );
            if( controllerID != -1 )
            {
                activeControllerIDs |= (1 << controllerID);
            }
        }
        rReleaseString ("Change active controllers\n");


        GetGuiSystem()->UnregisterUserInputHandlers( activeControllerIDs );
        rReleaseString ("Change active controllers\n");


        // enable screen clearing
        GetRenderManager()->mpLayer(RenderEnums::GUI)->pView( 0 )->SetClearMask( PDDI_BUFFER_ALL );
        rReleaseString ("Enable screen clearing\n");

#ifndef RAD_RELEASE
        // Dump out the contents of the inventory sections
        //
        p3d::inventory->Dump (true);
        rReleaseString ("Dump inventory\n");

#endif
        AnimatedIcon::ShutdownAnimatedIcons();
        
        GetAnimEntityDSGManager()->RemoveAll();

        GetTutorialManager()->EnableTutorialMode( false );
        
        HeapMgr()->PopHeap (GMA_TEMP);
    }

    if ( nextContext == CONTEXT_GAMEPLAY )
    {
        if( mOldState == Input::ACTIVE_ANIM_CAM )
        {
            GetInputManager()->SetGameState( Input::ACTIVE_GAMEPLAY );
        }
        else
        {
            GetInputManager()->SetGameState( mOldState );
        }

        int controllerID = GetInputManager()->GetControllerIDforPlayer( 0 );
        bool vibrationOn = GetInputManager()->IsRumbleEnabled();

        if ( GetGameplayManager()->GetGameType() == GameplayManager::GT_NORMAL &&
            GetAvatarManager()->GetAvatarForPlayer( 0 )->GetVehicle() )
        {
#ifdef RAD_PS2
            bool hasWheel = false;

            if ( controllerID != Input::USB0 &&
                 controllerID != Input::USB1 )
            {
                if ( GetInputManager()->GetController( Input::USB0 )->IsConnected() )
                {
                    GetInputManager()->SetRumbleForDevice( Input::USB0, vibrationOn );
                    hasWheel = true;
                }
                else if ( GetInputManager()->GetController( Input::USB1 )->IsConnected() )
                {
                    GetInputManager()->SetRumbleForDevice( Input::USB1, vibrationOn );
                    hasWheel = true;
                }
            }
            //I hate this.
            if ( !hasWheel )
#endif
            GetInputManager()->SetRumbleForDevice( controllerID, vibrationOn );
        }

    }
    else
    {
        GetInputManager()->SetGameState( Input::ACTIVE_ALL );
    }
    SetMemoryIdentification( "PauseContext Finished" );
    MEMTRACK_POP_FLAG( "" );
}

//=============================================================================
// PauseContext::OnUpdate
//=============================================================================
// Description: Comment
//
// Parameters:  ( unsigned int elapsedTime )
//
// Return:      void 
//
//=============================================================================
void PauseContext::OnUpdate( unsigned int elapsedTime )
{
    // update game data manager
    GetGameDataManager()->Update( elapsedTime );

    // update GUI system
    GetGuiSystem()->Update( elapsedTime );

    // update coin manager
    GetCoinManager()->Update( elapsedTime );
    GetSparkleManager()->Update( elapsedTime );

	//update hitnrun????
	//GetHitnRunManager()->Update(elapsedTime);

#ifdef RAD_DEMO
    GetGameplayManager()->UpdateIdleTime( elapsedTime );
#endif

    // Check to see if we're loading so we don't screw up when we quit the level.
    //
    if( GetGameplayManager()->GetLevelComplete() && !GetLoadingManager()->IsLoading() ) 
    {
        if( !m_quitGamePending )
        {
            m_quitGamePending = true;

#ifdef RAD_DEMO
            GetGuiSystem()->HandleMessage( GUI_MSG_QUIT_INGAME );
#else
            if( GetGameplayManager()->GetGameComplete() )
            {
                GetGuiSystem()->GotoScreen( CGuiWindow::GUI_SCREEN_ID_VIEW_CREDITS,
                                            0, 0, CLEAR_WINDOW_HISTORY );
            }
            else
            {
                GetGuiSystem()->GotoScreen( CGuiWindow::GUI_SCREEN_ID_LEVEL_END,
                                            0, 0, CLEAR_WINDOW_HISTORY );
            }
#endif // RAD_DEMO
        }
    }
    else
    {
        // update mission loading
        //
        GetGameplayManager()->PerformLoading();
    }
}

//=============================================================================
// PauseContext::OnSuspend
//=============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void 
//
//=============================================================================
void PauseContext::OnSuspend()
{
}

//=============================================================================
// PauseContext::OnResume
//=============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void 
//
//=============================================================================
void PauseContext::OnResume()
{
}

//=============================================================================
// PauseContext::OnHandleEvent
//=============================================================================
// Description: Comment
//
// Parameters:  ( EventEnum id, void* pEventData )
//
// Return:      void 
//
//=============================================================================
void PauseContext::OnHandleEvent( EventEnum id, void* pEventData )
{
}