XFactorDev.net Article   XFactorDev.net Article

Prt-9- tick tick tick...Time is flying ... CTimer

by Ben_3D

 

We definetly need a way of keeping track of time....maybe we want a delay...wait for 5 seconds etc....how would we know 5 seconds has passed?  Well we use our friendly function "GetTickCount()" which returns to us how many milliseconds has passed since our xbox was switched on.  With a bit of maths we can use this to our advantage.

 

Let me show you the whole code....its not much..and I'll explain how it works in a sec:

 

#include <xtl.h>

 

class CTimer

{

public:

      // Constructor, set our member to 0.0f.

      CTimer() { m_ftime = 0.0f; m_fElapsedTime=0.0f; };

 

 

      void Reset() { m_fElapsedTime = m_ftime; }

 

      const float GetTime() { return m_ftime - m_fElapsedTime; m_fElapsedTime = m_ftime; };

     

 

      void UpdateAll()

      {

            m_ftime = (float)GetTickCount();

      }

 

      float m_fElapsedTime;

 

      static float m_ftime;

};

 

float CTimer::m_ftime = 0.0f;

 

 

Now first thing is first...its got a static m_ftime variable, which holds the current number of milliseconds since the machine was started...this gets updated by calling GetTickCount()...which can be called from any CTimer object...and will update all the objects...

 

I've done an example in the code which will display the number of seconds, milliseconds, and minutes since the xbe was started on the screen......

 

This class will get used all over the place, and is an extremely simple class. We need to keep track of time...we need to know has 3 seconds passed or 10 seconds etc.

This CTimer class accomplishes all this.

 

How it works:

-1- We create an instance of our class:

CTimer timer;

-2- Update the CTimer internal clock

timer.UpdateAll();

-3- Check if a delay time has passed.

if ( timer.GetTime() > 3000 ) // has 3 seconds passed?
{
// yes
.. do code

// Set the timer back to zero.
timer.Reset();
}



THATS IT!...can't be more simple :)

Also, you only need to call UpdateAll() to update the time in all your CTimer instances...as it uses a static variable to keep the time...saves you calling Update again and again for all your CTimer's...just call it once then check all your timers using GetTime().

 

 

So you could see it in action...I've put a few lines of code in the gameloop(..) function in game.cpp and added the CTimer to be members of the CGame class in game.h.... What it will do is show you the milliseconds, seconds and minutes since the xbe was started.

 

 

here is the code:

 

 

#include "code/graphics/graphics.h"

 

#include "code/text/text.h"

// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

#include "code/timer/timer.h"

 

// need this for swprintf function

#include <stdio.h>

 

 

class CGame

{

protected:

      // Lets create an instance of our CGraphics class!...m for member, and

      // I chose g for graphics.....not a very descriptive name but I like it.

      CGraphics m_g;

 

 

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      CTimer timerA;

      CTimer timerB;

      CTimer timerC;

 

      // not forgetting "D:\\media\\font\\Arial18Normal.bmf"

      CText textA;

      CText textB;

 

public:

      // Initilisation called only once in the constructor

      CGame();

 

      // GameLoop, gets called repeatedly while it returns 1;

      bool GameLoop();

 

      // Cleanup process, all done in our destructor

      ~CGame();

};

 

Here is game.cpp:

 

#include "game.h"

 

// Initilisation called only once in the constructor

CGame::CGame()

{

      m_g.Create();

 

      IDirect3DDevice8 *pD3DDevice = NULL;

      m_g.GetScreen( &pD3DDevice );

 

      textA.Create( &pD3DDevice );

      textB.Create( &pD3DDevice );

}

 

 

 

// GameLoop, gets called repeatedly while it returns 1;

bool CGame::GameLoop()

{

 

      IDirect3DDevice8 *pD3DDevice = NULL;

      m_g.GetScreen( &pD3DDevice );

 

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      static int millisec = 0;

      static int sec      = 0;

      static int min      = 0;

      static int hour     = 0;

 

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      // This will update all our timers, e.g. timerB..timerC etc

      timerA.UpdateAll();

 

      textB.TextOut(L"X-Factor Development - www.xfactordev.net", 50, 50, 0, 255, 255);

 

      wchar_t buffer[200];

      swprintf(buffer, L"Time Since XBOX Started:  Minutes: %d Sec: %d  mSec:%d", min, sec, millisec );

 

      textA.TextOut( buffer , 50, 100, 255, 255, 255);

 

 

 

      millisec = timerA.GetTime();

      if( millisec > 1000 )

            timerA.Reset();

 

 

      if( timerB.GetTime() > 1000 ) // 1000 is 1 second

      {

            sec ++;

            timerB.Reset();

 

            // only 60 seconds in a minute

            if( sec >= 60 )

                  sec = 0;

      }

 

      if( timerC.GetTime() > 60000 ) // 60000 is 60 seconds

      {

            min ++;

            timerC.Reset();

 

            // only 60 minuts in an hour

            if( min >= 60 )

                  min = 0;

      }

 

     

      pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

      pD3DDevice->Present(NULL, NULL, NULL, NULL);

     

 

      // Return 1 for the game to continue, 0 to terminate.

      return 1;

}

 

// Cleanup process, all done in our destructor

CGame::~CGame()

{

      m_g.Release();

 

 

      textA.Release();

      textB.Release();

}

 

 

 

 

 

 

 

 

 

 

  XFactorDev.net Article



      
webmaster@xfactordev.net