Nokia N97 SDK
Example Applications Guide

sprite.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : sprite.cpp
00004 *  Part of     : Graphics
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2004 - 2006 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include "Sprite.h"
00018 
00019 // ============================ MEMBER FUNCTIONS ===============================
00020 
00021 // -----------------------------------------------------------------------------
00022 // CSprite::CSprite()
00023 // C++ default constructor can NOT contain any code, that might leave.
00024 // -----------------------------------------------------------------------------
00025 //
00026 CSprite::CSprite( TInt aXVelocity,
00027                   TInt aYVelocity,
00028                   const TPoint& aInitialPosition,
00029                   CFbsBitmap* aImage,
00030                   CFbsBitmap* aMask )
00031 :   iPosition( aInitialPosition ),
00032     iXVelocity( aXVelocity ),
00033     iYVelocity( aYVelocity ),
00034     iImage(aImage),
00035     iMask(aMask)
00036     {
00037     // No implementation required
00038     }
00039 
00040 // -----------------------------------------------------------------------------
00041 // CSprite::NewL()
00042 // Two-phased constructor.
00043 // -----------------------------------------------------------------------------
00044 //
00045 CSprite* CSprite::NewL( TInt aXVelocity,
00046                         TInt aYVelocity,
00047                         const TPoint& aInitialPosition,
00048                         CFbsBitmap* aImage,
00049                         CFbsBitmap* aMask )
00050     {
00051     CSprite* self = NewLC( aXVelocity,aYVelocity,aInitialPosition,aImage,aMask );
00052     CleanupStack::Pop( self );
00053     return self;
00054     }
00055 
00056 // -----------------------------------------------------------------------------
00057 // CSprite::NewLC()
00058 // Two-phased constructor.
00059 // -----------------------------------------------------------------------------
00060 //
00061 CSprite* CSprite::NewLC( TInt aXVelocity,
00062                          TInt aYVelocity,
00063                          const TPoint& aInitialPosition,
00064                          CFbsBitmap* aImage,
00065                          CFbsBitmap* aMask )
00066     {
00067     CSprite* self = new ( ELeave ) CSprite( aXVelocity,
00068                                             aYVelocity,
00069                                             aInitialPosition,
00070                                             aImage,
00071                                             aMask );
00072     CleanupStack::PushL( self );
00073     self->ConstructL();
00074     return self;
00075     }
00076 
00077 // -----------------------------------------------------------------------------
00078 // CSprite::~CFocusEventDocument()
00079 // Destructor.
00080 // -----------------------------------------------------------------------------
00081 //
00082 CSprite::~CSprite()
00083     {
00084     // No implementation required
00085     }
00086 
00087 // -----------------------------------------------------------------------------
00088 // CSprite::ConstructL()
00089 // Symbian 2nd phase constructor can leave.
00090 // -----------------------------------------------------------------------------
00091 //
00092 void CSprite::ConstructL()
00093     {
00094     // No implementation required
00095     }
00096 
00097 // -----------------------------------------------------------------------------
00098 // CSprite::Move()
00099 // Move Sprite.
00100 // -----------------------------------------------------------------------------
00101 //
00102 void CSprite::Move( const TSize& aSize, const TRect& aBounds )
00103     {
00104     TBool okToMove = ETrue;
00105 
00106     // Calculate the new sprite position
00107     TPoint newPostion = iPosition + TPoint( iXVelocity,iYVelocity );
00108 
00109     // Check that the new sprite X position is not out of bounds
00110     TInt width = aSize.iWidth;
00111     TInt leftX = newPostion.iX;
00112     TInt rightX = newPostion.iX + width;
00113     if ( ( rightX > aBounds.iBr.iX ) || ( leftX < aBounds.iTl.iX ) )
00114         {
00115         // If it is, "bounce" the sprite off the edge of the boundary
00116         iXVelocity = -iXVelocity;
00117         // and don't let it move this tick
00118         okToMove = EFalse;
00119         }
00120 
00121     // Check that the new sprite Y position is not out of bounds
00122     TInt height = aSize.iHeight;
00123     TInt topY = newPostion.iY;
00124     TInt bottomY = newPostion.iY + height;
00125     if ( ( bottomY > aBounds.iBr.iY ) || ( topY < aBounds.iTl.iY ) )
00126         {
00127         // If it is, "bounce" the sprite off the edge of the boundary
00128         iYVelocity = -iYVelocity;
00129         // and don't let it move this tick
00130         okToMove = EFalse;
00131         }
00132 
00133     // If the sprite can move this tick
00134     if ( okToMove )
00135         {
00136         // Move it
00137         iPosition = newPostion;
00138         }
00139     }
00140 
00141 // -----------------------------------------------------------------------------
00142 // CSprite::Move()
00143 // Move Sprite.
00144 // -----------------------------------------------------------------------------
00145 //
00146 void CSprite::Adjust( const TSize& aSize, const TRect& aBounds )
00147     {
00148     // check if right and bottom extremities are within the rect
00149     TPoint pos = iPosition + TPoint( aSize.iWidth, aSize.iHeight );
00150     if (!aBounds.Contains(pos))
00151         {
00152         // we have to adjust the position
00153         if (pos.iX > aBounds.iBr.iX)
00154             iPosition.iX = aBounds.iBr.iX - aSize.iWidth;
00155 
00156         if (pos.iY > aBounds.iBr.iY)
00157             iPosition.iY = aBounds.iBr.iY - aSize.iHeight;
00158         }
00159     }
00160 
00161 // End of File

© Nokia 2009

Back to top