locked
Question about DirectXTK Simple Sample (Windows 8.1)

    Question

  • The DirectXTK simple sample(windows 8.1), only works fine in landscape view, it does not consider the scenario in which the device orientation may change. My app also uses SpriteBatch to draw some texture. It works fine in landscape view, but when i rotate the device orientation, it does not work well. 

    I know the SpriteBatch.Begin has an override function accepts an transformMatrix,

    So i made some changes to the DirectX simple sample to support all 4 orientations.

    I just noted that when the device orientation changes, the DeviceResources class will generate two transfom matrixes; one for 2D, the other for 3D transform;

    	case DXGI_MODE_ROTATION_ROTATE270:
    		m_orientationTransform2D = 
    			Matrix3x2F::Rotation(270.0f) *
    			Matrix3x2F::Translation(0.0f, m_logicalSize.Width);
    		m_orientationTransform3D = ScreenRotation::Rotation90;
    		break;

    In DirectXTK3DSceneRenderer.cpp, in the Render() function, I tried to pass the 3D transform to the Begin function(see the following code snippet), but this does not work! I also tried to pass the 2D transform. But the 2D transform is of D2D1::Matrix3x2F, while the Begin function accepts an XMMatrix. i don't know how to correctly convert the D2D1::Matrix3x2F to XMMatrix.

    So my question is: to support different orientations, what transform matrix should i pass to SpriteBatch.Begin() function??

    Any feedbacks would be greatly appreciated! Thanks in advance!

        // Draw sprite
        //m_sprites->Begin();
    
    	XMMATRIX _3DTransform = XMLoadFloat4x4(&(m_deviceResources->GetOrientationTransform3D()));
    
    	m_sprites->Begin(DirectX::SpriteSortMode_Deferred, nullptr, nullptr, nullptr, nullptr, nullptr, _3DTransform);
    
        m_sprites->Draw(m_texture2.Get(), XMFLOAT2(10, 75), nullptr, Colors::White);
    
        m_font->DrawString(m_sprites.get(), L"DirectXTK Simple Sample", XMFLOAT2(100, 10), Colors::Yellow);
        m_sprites->End();

    Thursday, January 16, 2014 11:02 AM

Answers

  • This is addressed in the context of Windows phone here.

    http://www.catalinzima.com/2012/12/handling-orientation-in-a-windows-phone-8-game/

    Seems like a common enough scenario that SpriteBatch should have some extra support for this rather than always having to use a custom transform...

    Tuesday, January 21, 2014 9:15 PM
  • Thanks Chuck!!

    I solved this issue by converting the D2D1::Matrix3x2F to XMMatrix manually. It may not be an elegant way, but it works well.

    		// Get the 2D orientation transform.
    		D2D1::Matrix3x2F _2DTransform = m_deviceResources->GetOrientationTransform2D();
    
    		// Convert the Matrix3X2F tranform to XMMATRIX.
    		DirectX::XMMATRIX transformMaxtrix =
    		{
    			_2DTransform._11, _2DTransform._12, 0, 0,
    			_2DTransform._21, _2DTransform._22, 0, 0,
    						   0,			     0,	1, 0,
    			_2DTransform._31, _2DTransform._32, 0, 1
    		};
    
    		// To make sure the sprite is drawn in the correct position when the device orientation changes, 
    		// we pass the 2D orientation transform to the Begin function.
    		m_spriteBatch->Begin(DirectX::SpriteSortMode_Deferred, nullptr, nullptr, nullptr, nullptr, nullptr, 
    		  	transformMaxtrix);

    I appreciate your help, anyway.

    • Marked as answer by dxdx Wednesday, January 22, 2014 7:31 AM
    Wednesday, January 22, 2014 7:29 AM

All replies

  • Could anybody help me on this issue? I was stuck by it for days..
    Friday, January 17, 2014 1:39 AM
  • This is addressed in the context of Windows phone here.

    http://www.catalinzima.com/2012/12/handling-orientation-in-a-windows-phone-8-game/

    Seems like a common enough scenario that SpriteBatch should have some extra support for this rather than always having to use a custom transform...

    Tuesday, January 21, 2014 9:15 PM
  • Thanks Chuck!!

    I solved this issue by converting the D2D1::Matrix3x2F to XMMatrix manually. It may not be an elegant way, but it works well.

    		// Get the 2D orientation transform.
    		D2D1::Matrix3x2F _2DTransform = m_deviceResources->GetOrientationTransform2D();
    
    		// Convert the Matrix3X2F tranform to XMMATRIX.
    		DirectX::XMMATRIX transformMaxtrix =
    		{
    			_2DTransform._11, _2DTransform._12, 0, 0,
    			_2DTransform._21, _2DTransform._22, 0, 0,
    						   0,			     0,	1, 0,
    			_2DTransform._31, _2DTransform._32, 0, 1
    		};
    
    		// To make sure the sprite is drawn in the correct position when the device orientation changes, 
    		// we pass the 2D orientation transform to the Begin function.
    		m_spriteBatch->Begin(DirectX::SpriteSortMode_Deferred, nullptr, nullptr, nullptr, nullptr, nullptr, 
    		  	transformMaxtrix);

    I appreciate your help, anyway.

    • Marked as answer by dxdx Wednesday, January 22, 2014 7:31 AM
    Wednesday, January 22, 2014 7:29 AM
  • I've submitted a change to DirectXTK on the CodePlex to include a "SetRotation" function for SpriteBatch to simplify this in the January 2014 release. When I next refresh those samples on MSDN Code Gallery, it will include the rotation support.
    Wednesday, January 22, 2014 8:26 PM
  • Good News!Thanks Chuck!!
    Sunday, January 26, 2014 3:13 AM