X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=examples%2Ffpp-game%2Fgame-camera.cpp;h=ce050832f95e3c66278c58bd5b35c47446104bfa;hb=1b19fd140ff139b5854a1a62447faf31b175d8f6;hp=0b7255bf4fcb944a07ff221034888beccb07f847;hpb=b26d446b0cb6a316abc3a79d4fc70d0ae1b7994c;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/fpp-game/game-camera.cpp b/examples/fpp-game/game-camera.cpp index 0b7255b..ce05083 100644 --- a/examples/fpp-game/game-camera.cpp +++ b/examples/fpp-game/game-camera.cpp @@ -17,9 +17,9 @@ #include "game-camera.h" +#include #include #include -#include using namespace Dali; @@ -27,55 +27,55 @@ namespace { // Input sensitivity, the larger value, the more sensitive input // Default value has been chosen empirically -const float CAMERA_SENSITIVITY ( 90.0f ); +const float CAMERA_SENSITIVITY(90.0f); // Vertical angle limit of the camera -const float CAMERA_VERTICAL_LIMIT ( 80.0f ); +const float CAMERA_VERTICAL_LIMIT(80.0f); // Position where camera is instantiated by default -const Vector3 CAMERA_DEFAULT_POSITION ( 1.0f, -1.5f, -3.0f ); +const Vector3 CAMERA_DEFAULT_POSITION(1.0f, -1.5f, -3.0f); // Field-of-View in degrees -const float CAMERA_DEFAULT_FOV ( 60.0f ); +const float CAMERA_DEFAULT_FOV(60.0f); // Near plane -const float CAMERA_DEFAULT_NEAR ( 0.1f ); +const float CAMERA_DEFAULT_NEAR(0.1f); // Far plane -const float CAMERA_DEFAULT_FAR ( 100.0f ); +const float CAMERA_DEFAULT_FAR(100.0f); // Default forward vector -const Vector3 CAMERA_FORWARD ( 0.0f, 0.0f, 1.0f ); +const Vector3 CAMERA_FORWARD(0.0f, 0.0f, 1.0f); // Default up vector -const Vector3 CAMERA_UP ( Vector3::YAXIS ); -} +const Vector3 CAMERA_UP(Vector3::YAXIS); +} // namespace GameCamera::GameCamera() -: mFovY( CAMERA_DEFAULT_FOV ), - mNear( CAMERA_DEFAULT_NEAR ), - mFar( CAMERA_DEFAULT_FAR ), - mWalkingTouchId( -1 ), - mLookingTouchId( -1 ), - mPortraitMode( false ) +: mFovY(CAMERA_DEFAULT_FOV), + mNear(CAMERA_DEFAULT_NEAR), + mFar(CAMERA_DEFAULT_FAR), + mWalkingTouchId(-1), + mLookingTouchId(-1), + mPortraitMode(false) { } GameCamera::~GameCamera() { mTimer.Stop(); - mCameraActor.Remove( mInterceptorActor ); + mCameraActor.Remove(mInterceptorActor); } -void GameCamera::Initialise( CameraActor defaultCamera, float fovY, float near, float far, const Vector2& sceneSize ) +void GameCamera::Initialise(CameraActor defaultCamera, float fovY, float near, float far, const Vector2& sceneSize) { mCameraActor = defaultCamera; mFovY = fovY; mNear = near; - mFar = far; + mFar = far; - mSceneSize = sceneSize; + mSceneSize = sceneSize; mPortraitMode = mSceneSize.x < mSceneSize.y ? true : false; // Initialise default camera @@ -85,8 +85,8 @@ void GameCamera::Initialise( CameraActor defaultCamera, float fovY, float near, CreateInterceptorActor(); // Start timer - mTimer = Timer::New( 16 ); - mTimer.TickSignal().Connect( this, &GameCamera::OnTick ); + mTimer = Timer::New(16); + mTimer.TickSignal().Connect(this, &GameCamera::OnTick); mTimer.Start(); } @@ -94,52 +94,52 @@ bool GameCamera::OnTick() { // --------------------------------------------------------------------- // update rotation - Vector2 tmp( mScreenLookDelta ); + Vector2 tmp(mScreenLookDelta); mScreenLookDelta = Vector2::ZERO; - if( mPortraitMode ) + if(mPortraitMode) { - float yaw = ( (tmp.y / mSceneSize.y ) * CAMERA_SENSITIVITY ); - float pitch = ( (tmp.x / mSceneSize.x ) * CAMERA_SENSITIVITY ); + float yaw = ((tmp.y / mSceneSize.y) * CAMERA_SENSITIVITY); + float pitch = ((tmp.x / mSceneSize.x) * CAMERA_SENSITIVITY); mCameraYawPitch.y -= yaw; mCameraYawPitch.x -= pitch; - if( abs( mCameraYawPitch.y ) > CAMERA_VERTICAL_LIMIT ) + if(abs(mCameraYawPitch.y) > CAMERA_VERTICAL_LIMIT) { - mCameraYawPitch.y = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.y < 0) ? -1.0f : 1.0f ); + mCameraYawPitch.y = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.y < 0) ? -1.0f : 1.0f); } } else { - float yaw = ( (tmp.y / mSceneSize.x ) * CAMERA_SENSITIVITY ); - float pitch = ( (tmp.x / mSceneSize.y ) * CAMERA_SENSITIVITY ); + float yaw = ((tmp.y / mSceneSize.x) * CAMERA_SENSITIVITY); + float pitch = ((tmp.x / mSceneSize.y) * CAMERA_SENSITIVITY); mCameraYawPitch.x -= yaw; mCameraYawPitch.y -= pitch; - if( abs( mCameraYawPitch.x ) > CAMERA_VERTICAL_LIMIT ) + if(abs(mCameraYawPitch.x) > CAMERA_VERTICAL_LIMIT) { - mCameraYawPitch.x = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.x < 0) ? -1.0f : 1.0f ); + mCameraYawPitch.x = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.x < 0) ? -1.0f : 1.0f); } } Quaternion rotation; - Quaternion rotX( Degree( mCameraYawPitch.x), Vector3( 1.0f, 0.0f, 0.0f ) ); - Quaternion rotY( Degree( mCameraYawPitch.y), Vector3( 0.0f, 1.0f, 0.0f ) ); - if (mPortraitMode ) + Quaternion rotX(Degree(mCameraYawPitch.x), Vector3(1.0f, 0.0f, 0.0f)); + Quaternion rotY(Degree(mCameraYawPitch.y), Vector3(0.0f, 1.0f, 0.0f)); + if(mPortraitMode) { - Quaternion rotZ( Degree( mPortraitMode ? 90.0 : 0.0f), Vector3( 0.0f, 0.0f, 1.0f ) ); - rotation = ( rotZ * rotX * rotY ); + Quaternion rotZ(Degree(mPortraitMode ? 90.0 : 0.0f), Vector3(0.0f, 0.0f, 1.0f)); + rotation = (rotZ * rotX * rotY); } else { - rotation = ( rotY * rotX ); + rotation = (rotY * rotX); } - mCameraActor.SetProperty( Actor::Property::ORIENTATION, rotation ); + mCameraActor.SetProperty(Actor::Property::ORIENTATION, rotation); // --------------------------------------------------------------------- // update position - Vector3 position( mCameraPosition ); + Vector3 position(mCameraPosition); // Rotate CAMERA_FORWARD vector - Vector3 forwardVector = rotation.Rotate( CAMERA_FORWARD ); + Vector3 forwardVector = rotation.Rotate(CAMERA_FORWARD); // Cancel vertical movement forwardVector.y = 0.0f; @@ -148,15 +148,15 @@ bool GameCamera::OnTick() forwardVector.Normalize(); // compute sideways vector - Vector3 sidewaysVector = forwardVector.Cross( CAMERA_UP ); + Vector3 sidewaysVector = forwardVector.Cross(CAMERA_UP); sidewaysVector.Normalize(); - const float forwardSpeed( mScreenWalkDelta.y / mSceneSize.y ); - const float sidewaysSpeed( mScreenWalkDelta.x / mSceneSize.x ); + const float forwardSpeed(mScreenWalkDelta.y / mSceneSize.y); + const float sidewaysSpeed(mScreenWalkDelta.x / mSceneSize.x); // Adjust walking speed - if ( mPortraitMode ) + if(mPortraitMode) { position += forwardVector * (forwardSpeed * 0.5f); } @@ -167,7 +167,7 @@ bool GameCamera::OnTick() position += sidewaysVector * (sidewaysSpeed * 0.5f); - mCameraActor.SetProperty( Actor::Property::POSITION, position ); + mCameraActor.SetProperty(Actor::Property::POSITION, position); mCameraPosition = position; @@ -176,15 +176,15 @@ bool GameCamera::OnTick() void GameCamera::InitialiseDefaultCamera() { - mCameraActor.SetProperty( Dali::Actor::Property::NAME, "GameCamera" ); - mCameraActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER ); - mCameraActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); - mCameraActor.SetFieldOfView( Radian( Degree( mFovY ) ) ); + mCameraActor.SetProperty(Dali::Actor::Property::NAME, "GameCamera"); + mCameraActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + mCameraActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + mCameraActor.SetFieldOfView(Radian(Degree(mFovY))); // should be read from file - mCameraActor.SetNearClippingPlane( mNear ); - mCameraActor.SetFarClippingPlane( mFar ); - mCameraActor.SetProperty( Actor::Property::POSITION, CAMERA_DEFAULT_POSITION ); + mCameraActor.SetNearClippingPlane(mNear); + mCameraActor.SetFarClippingPlane(mFar); + mCameraActor.SetProperty(Actor::Property::POSITION, CAMERA_DEFAULT_POSITION); // Camera position is shadowed in order to avoid using.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ) mCameraPosition = CAMERA_DEFAULT_POSITION; @@ -193,89 +193,88 @@ void GameCamera::InitialiseDefaultCamera() void GameCamera::CreateInterceptorActor() { mInterceptorActor = Actor::New(); - mInterceptorActor.SetProperty( Dali::Actor::Property::NAME, "GameInputInterceptor" ); - mInterceptorActor.SetProperty( Actor::Property::SIZE, Vector3( mSceneSize.x, mSceneSize.y, 1 ) ); - mInterceptorActor.SetProperty( Actor::Property::POSITION, Vector3( 0.0, 0.0, 1.0 ) ); - mInterceptorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER ); - mInterceptorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); - mCameraActor.Add( mInterceptorActor ); + mInterceptorActor.SetProperty(Dali::Actor::Property::NAME, "GameInputInterceptor"); + mInterceptorActor.SetProperty(Actor::Property::SIZE, Vector3(mSceneSize.x, mSceneSize.y, 1)); + mInterceptorActor.SetProperty(Actor::Property::POSITION, Vector3(0.0, 0.0, 1.0)); + mInterceptorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + mInterceptorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + mCameraActor.Add(mInterceptorActor); // Connect TouchedSignal to interceptor actor - mInterceptorActor.TouchedSignal().Connect( this, &GameCamera::OnTouch ); + mInterceptorActor.TouchedSignal().Connect(this, &GameCamera::OnTouch); } -bool GameCamera::OnTouch( Actor actor, const TouchEvent& touch ) +bool GameCamera::OnTouch(Actor actor, const TouchEvent& touch) { - for( int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i ) + for(int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i) { - int id = touch.GetDeviceId( i ); - Vector2 tmp( touch.GetScreenPosition( i ) ); + int id = touch.GetDeviceId(i); + Vector2 tmp(touch.GetScreenPosition(i)); Vector2 position; - float halfWindowSize; - if( mPortraitMode ) + float halfWindowSize; + if(mPortraitMode) { - position.x = tmp.y; - position.y = tmp.x; + position.x = tmp.y; + position.y = tmp.x; halfWindowSize = mSceneSize.y / 2; } else { - position.x = tmp.x; - position.y = tmp.y; + position.x = tmp.x; + position.y = tmp.y; halfWindowSize = mSceneSize.x / 2; } // touch started - if( touch.GetState( i ) == PointState::STARTED ) + if(touch.GetState(i) == PointState::STARTED) { // start looking - if( position.x > halfWindowSize && mLookingTouchId < 0 ) + if(position.x > halfWindowSize && mLookingTouchId < 0) { - mLookingTouchId = id; + mLookingTouchId = id; mOldTouchLookPosition = position; } // start walking - else if( position.x < halfWindowSize && mWalkingTouchId < 0 ) + else if(position.x < halfWindowSize && mWalkingTouchId < 0) { - mWalkingTouchId = id; + mWalkingTouchId = id; mOldTouchWalkPosition = position; - mScreenWalkDelta = Vector2::ZERO; + mScreenWalkDelta = Vector2::ZERO; } } - else if( touch.GetState( i ) == PointState::FINISHED || - touch.GetState( i ) == PointState::LEAVE || - touch.GetState( i ) == PointState::INTERRUPTED - ) + else if(touch.GetState(i) == PointState::FINISHED || + touch.GetState(i) == PointState::LEAVE || + touch.GetState(i) == PointState::INTERRUPTED) { // terminate look - if( mLookingTouchId == id ) + if(mLookingTouchId == id) { - mScreenLookDelta = Vector2::ZERO; + mScreenLookDelta = Vector2::ZERO; mOldTouchLookPosition = Vector2::ZERO; - mLookingTouchId = -1; + mLookingTouchId = -1; } // terminate walking - else if( mWalkingTouchId == id ) + else if(mWalkingTouchId == id) { - mScreenWalkDelta = Vector2::ZERO; + mScreenWalkDelta = Vector2::ZERO; mOldTouchWalkPosition = Vector2::ZERO; - mWalkingTouchId = -1; + mWalkingTouchId = -1; } } else // on motion { // update looking - if( mLookingTouchId == id ) + if(mLookingTouchId == id) { - mScreenLookDelta.x += ( position.x - mOldTouchLookPosition.x ); - mScreenLookDelta.y += ( position.y - mOldTouchLookPosition.y ); + mScreenLookDelta.x += (position.x - mOldTouchLookPosition.x); + mScreenLookDelta.y += (position.y - mOldTouchLookPosition.y); mOldTouchLookPosition = position; } // update walking - else if ( mWalkingTouchId == id ) + else if(mWalkingTouchId == id) { - mScreenWalkDelta.x += ( position.x - mOldTouchWalkPosition.x ); - mScreenWalkDelta.y += ( position.y - mOldTouchWalkPosition.y ); + mScreenWalkDelta.x += (position.x - mOldTouchWalkPosition.x); + mScreenWalkDelta.y += (position.y - mOldTouchWalkPosition.y); mOldTouchWalkPosition = position; } }