sipexgameengine.cpp

来自「an example for sip for symbian」· C++ 代码 · 共 703 行 · 第 1/2 页

CPP
703
字号
/*
* ==============================================================================
*  Name        : SIPExGameEngine.cpp
*  Part of     : SIPExEngine
*  Interface   : 
*  Description : 
*  Version     : 
*
*  Copyright (c) 2004-2006 Nokia Corporation.
*  This material, including documentation and any related 
*  computer programs, is protected by copyright controlled by 
*  Nokia Corporation.
* ==============================================================================
*/

// INCLUDES
#include    "SIPExGameEngine.h"
#include    "SIPExStateViewNotifier.h"
#include    "SIPExGameObserver.h"
#include    "SIPExStateAcceptingSIP.h"
#include    "SIPExStateConnecting.h"
#include    "SIPExStateIdle.h"
#include    "SIPExStateInviting.h"
#include    "SIPExStateLocal.h"
#include    "SIPExStateRemote.h"
#include    "SIPExStateRegistering.h"
#include    "SIPExStateRegistered.h"
#include    "SIPExSocketEngine.h"
#include    "SIPExSIPEngine.h"
#include    "SIPExEngine.pan"
#include    <in_sock.h> // TInetAddr

// Remove exports in unit test build
#ifdef CPPUNIT_TEST
#undef EXPORT_C
#define EXPORT_C
#endif

// CONSTANTS
const TUid KUidSIPExApp = { 0xA00001EB };

// -----------------------------------------------------------------------------
// CSIPExEngine::NewL
// Static constructor.
// -----------------------------------------------------------------------------
//
EXPORT_C CSIPExEngine* CSIPExEngine::NewL( MSIPExGameObserver& aGameObserver )
    {
    CSIPExEngine* self = NewLC( aGameObserver );
    CleanupStack::Pop(self);
    return self;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::NewLC
// Static constructor. The instance is left to the CleanupStack.
// -----------------------------------------------------------------------------
//    
EXPORT_C CSIPExEngine* CSIPExEngine::NewLC( MSIPExGameObserver& aGameObserver )
    {
    CSIPExEngine* self = new (ELeave) CSIPExEngine( aGameObserver );
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::CSIPExEngine
// C++ default constructor. Initializes the observer member variable.
// -----------------------------------------------------------------------------
//
CSIPExEngine::CSIPExEngine( MSIPExGameObserver& aGameObserver )
: iGameObserver( aGameObserver )
    {
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::~CSIPExEngine
// Destructor. Destroy all members.
// -----------------------------------------------------------------------------
//
EXPORT_C CSIPExEngine::~CSIPExEngine()
    {
    DestroySocketEngine();
    delete iSIPEngine;

    // Delete all state objects
    delete iStateIdle;
    delete iStateRegistering;
    delete iStateRegistered;
    delete iStateInviting;
    delete iStateConnecting;
    delete iStateLocal;
    delete iStateRemote;
    delete iStateAcceptingSIP;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::ConstructL
// Symbian 2nd phase constructor can leave.
// Initializes the game data, socket and sip engines and all state objects.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::ConstructL()
    {
    // Init all state objects
    iStateIdle          = new (ELeave) TSIPExStateIdle;
    iStateRegistering   = new (ELeave) TSIPExStateRegistering;
    iStateRegistered    = new (ELeave) TSIPExStateRegistered;
    iStateInviting      = new (ELeave) TSIPExStateInviting;
    iStateConnecting    = new (ELeave) TSIPExStateConnecting;
    iStateLocal         = new (ELeave) TSIPExStateLocal;
    iStateRemote        = new (ELeave) TSIPExStateRemote;
    iStateAcceptingSIP  = new (ELeave) TSIPExStateAcceptingSIP;

    iSocketEngine = CSIPExSocketEngine::NewL( *this );
    iSIPEngine = CSIPExSIPEngine::NewL( KUidSIPExApp, this );

    // Set the first state as active state
    iActiveState = iStateIdle;

    // Reset game data
    ResetGame();
    }


// ----------------------------------------------------------------------------
// New functions.
// ----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// CSIPExEngine::SetViewNotifier
// Sets a new reference to the iNotifier.
// -----------------------------------------------------------------------------
//
EXPORT_C void CSIPExEngine::SetViewNotifier( MSIPExStateViewNotifier& aNotifier )
    {
    iNotifier = &aNotifier;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::ResetGame
// Resets the game area, moves and cursor position.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::ResetGame()
    {
    for ( TInt i=0 ; i < KBoxCountX; i++ ) 
        {
		for ( TInt j=0 ; j < KBoxCountY; j++ ) 
            {
			iBoard[i][j] = -1;
            }
        }

	iCursor = 0;
    iMoves = 0;
    iPeer = EUnknown;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::SetBoard
// Sets the value (aValue) to the board to the place specified in aX and aY.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::SetBoard( const TInt aX, const TInt aY, const TInt aValue )
    {
    __ASSERT_DEBUG( aX < KBoxCountX && aY < KBoxCountY, 
                    Panic( EBoardValueOOR ) );
    iBoard[ aX ][ aY ] = aValue;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::IncreaseMovesBy
// Increases the iMoves by aAmount.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::IncreaseMovesBy( const TInt aAmount )
    {
    iMoves += aAmount;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::BoardValue
// Returns the board value from the specified place.
// -----------------------------------------------------------------------------
//
EXPORT_C TInt CSIPExEngine::BoardValue( TInt aX, TInt aY )
    {
    __ASSERT_DEBUG( aX < KBoxCountX && aY < KBoxCountY, 
                    Panic( EBoardValueOOR ) );
    return iBoard[ aX ][ aY ];
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::Cursor
// Returns the iCursor value.
// -----------------------------------------------------------------------------
//
EXPORT_C TInt CSIPExEngine::Cursor()
    {
    return iCursor;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::SetCursor
// Sets a new value to the iCursor.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::SetCursor( const TInt aNewValue )
    {
    iCursor = aNewValue;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::ChangeState
// Changes a new value to the iActiveState and notifies the game observer.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::ChangeState( TSIPExState& aNewState )
    {
    iActiveState = &aNewState;

    TEngineState state( EIdle );
    if( iActiveState == iStateIdle || iActiveState == iStateRegistering )
        {
        state = EIdle;
        }
    else if( iActiveState == iStateRegistered )
        {
        state = EEnabled;
        }
    else if ( iActiveState == iStateInviting ||
              iActiveState == iStateAcceptingSIP ||
              iActiveState == iStateConnecting )
        {
        state = EActivating;
        }
    else if( iActiveState == iStateLocal || iActiveState == iStateRemote )      
        {
        state = EActive;
        }
    else { /*Nothing to do*/ }

    iGameObserver.GameStateChanged( state );
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::SendMessage
// Formats and sends the message to the socket.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::SendMessage( const TInt aX, const TInt aY )
    {
    __ASSERT_DEBUG( aX < KBoxCountX && aY < KBoxCountY, 
                    Panic( EBoardValueOOR ) );

    TBuf8<8> msg;
    msg.Format( KMoveFormatStr(), aX, aY );

    iSocketEngine->Write( msg );
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::Info
// Calls the view notifier.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::InfoL( const TDesC& aInfoTxt )
    {
    __ASSERT_DEBUG( iNotifier, Panic( ENoGameViewNotifier ) );

    iNotifier->ShowInfoL( aInfoTxt );
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::StatusInfo
// Calls the view notifier.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::StatusInfoL( const TDesC& aTxt )
    {
    __ASSERT_DEBUG( iNotifier, Panic( ENoGameViewNotifier ) );

    iNotifier->ShowStatusInfoL( aTxt );
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::SetRemote
// Sets the remote peer move to the board into the specified place.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::SetRemote( const TInt aX, const TInt aY )
    {
    SetBoard( aX, aY, 2 );
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::DestroySocketEngine
// Deletes the socket engine.
// -----------------------------------------------------------------------------
//
void CSIPExEngine::DestroySocketEngine()
    {
    delete iSocketEngine;
    iSocketEngine = NULL;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::SIPEngine
// Returns the pointer to the SIP engine. The ownership is not transfered.
// -----------------------------------------------------------------------------
//
CSIPExSIPEngine* CSIPExEngine::SIPEngine() 
    { 
    return iSIPEngine; 
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::SocketEngine
// Returns the pointer to the socket engine. The ownership is not transfered.
// The engine will be created if not already done.
// -----------------------------------------------------------------------------
//
CSIPExSocketEngine* CSIPExEngine::SocketEngineL() 
    { 
    if( !iSocketEngine )
        {
        iSocketEngine = CSIPExSocketEngine::NewL( *this );
        }
    return iSocketEngine;
    }

// -----------------------------------------------------------------------------
// CSIPExEngine::GameObserver
// Returns the reference to the game observer.
// -----------------------------------------------------------------------------
//
MSIPExGameObserver& CSIPExEngine::GameObserver()
    { 
    return iGameObserver; 
    }

// ----------------------------------------------------------------------------
// From Game engine
// ----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// CSIPExEngine::InviteL
// From MSIPExGameEngine. Redirects the call to the active state object.
// -----------------------------------------------------------------------------
//

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?