Game Development Community

Pausing TGB Simulation on Window Backgrounded

by Justin DuJardin · 07/03/2006 (6:26 pm) · 2 comments

Download Code File

Firstly -You may Skip Part 1 if you download the zip file and extract it into your "installationPath\engine\source" directory, rebuild your project. -- then Skip To Part 2.

There are tutorials on TGB TDN outlining how to compile your binary with free Visual Studio 2005 Express edition.
the one by Jason Cahill is a wonderful example - Find it Here


Part 1 - Merging Source Changes

Open up t2dSceneGraph.h and make the following modifications beginning at line (393)
Additions in Bold
///-----------------------------------------------------------------------------
/// Scene Ticker
///
/// The scene ticker basically updates time for all scenegraph's in a given T2D
/// instance.  It inherits from ITickable which is a helper class that allows
/// any class that inherits from it to receive tick updates.
///-----------------------------------------------------------------------------
class t2dSceneTicker : public SimObject, public virtual ITickable
{
private:
    typedef SimObject Parent;

public:
    t2dSceneTicker();
    ~t2dSceneTicker() {};

    /// Register/Unregister SceneGraph.
    void registerUpdate( t2dSceneGraph* pT2DSceneGraph );
    void unregisterUpdate( t2dSceneGraph* pT2DSceneGraph );
    /// Get Register List Count.
    S32 getRegisteredCount( void ) { return mSceneGraphSet.size(); };

    /// Stock Client Per-Frame Update.
    virtual void advanceTime( F32 elapsedTime );
    virtual void interpolateTick( F32 delta ) {};
    virtual void processTick(){};

[b]    /// Set simluation timescale
    void setTimeScale( F32 timeScale ) { mTimeScale = timeScale; };
[/b]
private:
    [b]F32         mTimeScale;[/b]
    SimSet      mSceneGraphSet;
};

next : open up t2dSceneGraph.cc and around line (66) you'll find the scene ticker declared. Add this console method below it.
Additions in Bold
//-----------------------------------------------------------------------------
// Global Scene Ticker.
//-----------------------------------------------------------------------------
t2dSceneTicker  gSceneTicker;

[b]//-----------------------------------------------------------------------------
// Set Simulation TimeScale
//-----------------------------------------------------------------------------
ConsoleFunction( setSimTimeScale, void, 2, 2, "setSimTimeScale( timeScale ) - multiplier for time (default 1.0)" )
{
   gSceneTicker.setTimeScale( dAtof(argv[1] ) );
}
[/b]

then again around line 195 change it to look like the below
//-----------------------------------------------------------------------------
// SceneGraph Updater.
//-----------------------------------------------------------------------------
t2dSceneTicker::t2dSceneTicker()
{
   // Make sure we receive ticks.
   setProcessTicks();

[b]   // Default to realtime.
   mTimeScale = 1.0f;[/b]
}

then around line 225 change it to look like the following
//-----------------------------------------------------------------------------
// Advance Time.
//-----------------------------------------------------------------------------
void t2dSceneTicker::advanceTime( F32 elapsedTime )
{
    // Update All Registered SceneGraphs.
    for ( U32 n = 0; n < mSceneGraphSet.size(); n++ )
	   static_cast<t2dSceneGraph*>( mSceneGraphSet[n] )->updateScene( elapsedTime[b] * mTimeScale[/b] );
}
now, open up winWindow.cc and around line (674) add the following code
additions in bold
...
     case WM_ACTIVATE:
        windowActive = LOWORD(wParam) != WA_INACTIVE;
        [b]if( Game->isRunning() )
           Con::executef( 2, "windowFocusChanged", windowActive ? "1"
: "0" );[/b]
        if ( windowActive )
...
finally, open macCarbEvents.cc and around line 164, at the end of
_OnActivate(), add the following code
additions in bold
platState.sleepTicks = Platform::getBackgroundSleepTime();
     platState.backgrounded = true;
  }
  [b]if( Game->isRunning() )
     Con::executef( 2, "windowFocusChanged", platState.backgrounded ?
"1" : "0" );[/b]
}

after that you're set for engine changes. rebuild your binary and let's go.

Part 2 - Adding your script resource.

The above accomplished two things.
1 - We can set a timescale to modulate how fast time is simulated (setting to 0.0 means time is stopped and 1.0 being realtime. 1.5 being sped up, etc.)
2 - We get script notification of when the game window becomes focused or loses focus to know when to pause the simulation.


Script Resource
Download Here

Now we need to add script to pause the game when the window loses focus. Simply extract the zip file below into your "instalationlPath\games\resources\" path and you should be good to go.

Load up TGB and when the level builder loads hit (Ctrl-R) or navigate the menu to Project->Resources.

Select the 'pauseWindowInactive' resource and click 'Add to Project'. Then hit save and you should be ready to go--provided of course you built your new executable!

-------

Testing : open the fish demo, level 9, play it then click on your windows taskbar and watch the fish freeze. click back on TGB and there they go!

Hope this helps some of you out!

#1
10/21/2006 (9:59 pm)
what to do when the user drag the window of the game but we want the game to keep on running
#2
06/09/2008 (6:07 pm)
Justin,
The script resource is not available anymore, could you add it back?
Thanks,
Eyal.