Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 00:37, 14 November 2007.

Simple timer for games

From Forum Nokia Wiki

This article presents a simple timer class suitable for games. This class uses CIdle to generate the timing events.

The CIdle class is an active object that runs whenever there are no other active objects with higher priority ready to run. Different from other approaches found in the Symbian OS API, CIdle does not use a predefined time interval to generate events.

Defining the class

Here is the class declaration:

class CCustomTimer : public CBase
{
  public:
 
    // Creates an instance of this class. MTimerListener
    // is an object interested in handling the timer event.
    static CCustomTimer* NewL (MTimerListener & aListener);
		
    ~CCustomTimer ();
		
    // Starts the timer.
    void Start  ();
 
    // Cancels the timer.
    void Cancel ();
			
 
  private:
 
    // Callback used internally to notify the listener.
    // Required by CIdle.
    static TInt IdleCallBack (TAny* aPtr);
 
  private:
 
    // Constructor, stores the reference to the listener
    // object.
    CCustomTimer (MTimerListener & aListener)
    : iListener (& aListener)		   
    {}	
 
    // Second part of the two-phase constructor.
    void ConstructL ();
		
  private:		
		
    
    MTimerListener * iListener;  // The object that handles the timer event.
    CIdle*  iIdle;		 
};

The MTimerListener interface is defined as follows:

class MTimerListener
{
  public:
    	
    virtual void OnTimer () = 0;		
};

The CCustomTimer class implementation is defined this way:

void CCustomTimer::ConstructL ()
{
  iIdle = CIdle::NewL (CIdle::EPriorityIdle);	
}
 
CCustomTimer::~CCustomTimer ()
{
  if (iIdle)
  {	
     iIdle->Cancel ();
     delete iIdle;
  }
}
 
void CCustomTimer::Start ()
{
  if (!iIdle->IsActive () )
   iIdle->Start (TCallBack (IdleCallBack, this) );
}
 
void CCustomTimer::Cancel ()
{
  iIdle->Cancel();	
}
 
 
TInt CCustomTimer::IdleCallBack (TAny* aPtr)
{
  CCustomTimer* me = ((CCustomTimer*)aPtr);
 
  me->iListener->OnTimer ();
  return ETrue;
}

Usage

// Suppose we plan the game to run at 30 frames per second. Then, we
// need to calculate the required time step to meet this requisite.
// (time in microseconds)
const TInt KTargetTimeStep = 1000000 / 30;
 
void MyGame::OnTimer ()
{
   // Handle input.
   ProcessInput ();
 
   // Now we are going to calculate how many steps the game should process,
   // so it is not late.
 
   // Keep current time.
   TTime time;
   time.HomeTime();
 
   // Calculate elapsed time since the last update.
   // The iStartTime variable is a member of the MyGame class.
   TInt elapsed = I64LOW (time.MicroSecondsFrom (iStartTime).Int64());
 
   // Update total game time. The iTotalTime variable
   // is also a member of the MyGame class.
   iTotalTime += elapsed;
 
   // Calculate how many updates should be processed. Notice that the
   // division is an integer division (it means 5 / 2 = 2).
   elapsed = iTotalTime / KTargetTimeStep;
 
   for (TInt i = 0; i < elapsed; ++i)
    Update ();
 
   // Subtract the time steps processed.
   iTotalTime -= elapsed * KTargetTimeStep;
   iStartTime = time.Int64();
 
   // Draw.
   Render ();
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
Retrieve SMS Inbox ! Problems with SMShandler. gregb34 Symbian Networking & Messaging 75 2008-08-06 20:01
How to use keyboard? gacon Symbian User Interface 3 2008-07-25 15:40
Using Ao_timer to repeat every interval korakotc Python 0 2006-02-09 07:14
SNAP Mobile sample games abhisheik SNAP Mobile 2 2006-07-11 21:04
Strange timer behaviour Digish General Symbian C++ 14 2008-01-05 06:09
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZgamesQ
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX