locked
How to animate a sprite using a sprite sheet with DirectXTK?

    Question

  • Hi,

    I am trying to learn how to do in VS 2013 using

    Visual C++ -->Windows Store --> DirectX game(XAML)

    and DirectXTK

    an animation of a sprite from a sprite sheet with 2 frames

    and to move it around.

     I want when sprite rich the limits of screen to change the direction.

    Any idea how to do it?

    Thanks


    ADRIAN DIBU

    Tuesday, March 25, 2014 12:24 AM

Answers

  • AnimatedTexture is a helper you can just drop into your project rather than modify the DirectXTK library. You could even just copy & paste the code into whatever header defines the object that you use it in.

    I would suggest spending some time reading a reference on C++ programming which covers concepts around header files, compilation modules, libraries, etc. that are not specific to Windows.

    DirectXTK is a C++ static library without making use of WinRT in order to keep it simple and standard, as well as enable it to support the breadth of platforms it does (Win32 desktop, Windows Store, Windows phone, Xbox One).

    You may also want to take a look at the DirectXTK CodePlex discussion threads and the various platform samples.

    • Marked as answer by adibu Wednesday, March 26, 2014 11:49 PM
    Wednesday, March 26, 2014 6:02 PM

All replies

  • You can model the solution on the XNA Game Studio tutorials...

    http://msdn.microsoft.com/en-us/library/bb203866.aspx

    Tuesday, March 25, 2014 12:30 AM
  • Hi,

    Thanks for the prompt answer.

    VS 2013 hasn't XNA and I want to use DirectX 2D is better for Store and WinRT device.

    Best regards


    ADRIAN DIBU

    Tuesday, March 25, 2014 1:20 AM
  • DirectXTK is a C++ library designed to be very similar to XNA Game Studio at least in terms of the commonly useful helper classes. Specifically the SpriteBatch class is a C++ implementation of the C# SpriteBatch class, so XNA Game Studio tutorials teach you techniques that can be used in C++ with SpriteBatch.

    To be more clear, I've updated the DirectXTK documentation to include the example C++ code to do the animated sprite: https://directxtk.codeplex.com/wikipage?title=AnimatedTexture

    For sprite-based 2D rendering, Direct2D is not necessary "better" for Windows Store, WinRT (i.e. Windows Runtime) platforms, or Windows RT (aka the Windows on ARM platform). Direct2D is a good choice for vector-based rendering using arcs, wide lines, circles, etc. although it is not supported on all target platforms that are supported by Direct3D 11. Direct3D is a great choice for 2D sprite-based games as it allows for sophisticated shader effects and high-performance textured shape rendering.



    Tuesday, March 25, 2014 4:23 AM
  • Hi,

    Thanks for the answer.

    I am a newbie so do me a favor and tell me how

    can I test your code after I start with:

    VS 2013 Visual c++ --> Windows Store --> DirectX game(XAML)

    Is it possible to used with VS 2013 after I add to VC++ Include and Lib of

    DirectXTK?

    Best regards


    ADRIAN DIBU

    Tuesday, March 25, 2014 1:16 PM
  • Hi,

    I am trying to add AnimatedTexture Class

    I selected Add Class --> Visual C++ -- >

    I put the name and I selected Windows Store

    I got this errors:

    error C2027: use of undefined type 'AnimatedTexture.h'

    error C2011: 'AnimatedTexture' : 'class' type redefinition

    error C2065: '__abi_reference_count' : undeclared identifier

    error C2228: left of '.Decrement' must have class/struct/union

    I don't know how to add your class in my project.

    Please help me.

    Thanks


    ADRIAN DIBU

    Tuesday, March 25, 2014 2:20 PM
  • It's a standard C++ header like the rest of DirectXTK. It's not a WinRT component. Just #include the file in your code and add it as a file to your project.

    See the instructions on adding DirectXTK to a Windows Store app as a starting point...

    Tuesday, March 25, 2014 5:15 PM
  • Hi,

    Before adding AnimatedTexture I did what you said

    and when I built it I have this errors

    error C1192: #using failed on 'C:\Users\ADRIAN\Documents\Visual Studio 2013\Projects\DirectXGameXMALTest\DirectXGameXMALTest\DirectXTK\Bin\Windows81\Win32\Debug\DirectXTK.winmd'

    IntelliSense: command-line error: cannot open metadata file "C:\Users\ADRIAN\Documents\Visual Studio 2013\Projects\DirectXGameXMALTest\DirectXGameXMALTest\DirectXTK\Bin\Windows81\Win32\Debug\DirectXTK.winmd" c:\Users\ADRIAN\Documents\Visual Studio 2013\Projects\DirectXGameXMALTest\DirectXGameXMALTest\App.xaml.cpp

    Thanks


    ADRIAN DIBU

    Tuesday, March 25, 2014 9:26 PM
  • The instructions on CodePlex include an important step that you may have missed...

    In your application's project, right-click on the Project and use "References...", then "Add New Reference...", and then check the DirectXTK project name and click OK. For a Windows Store app, you need to set Reference Assembly Outputto falsesince DirectXTK is a static C++ library and not a WinRT component

    You use #include for DirectXTK which is C++ code, not #using which is for WinRT components.


    Wednesday, March 26, 2014 6:39 AM

  • Thanks for answer it worked.

    Now I am confused:

    Where I suppose to add the AnimatedTexture in DirectXTK or in main project?

    If in DirectXTK I put the AnimatedTexture.h in DirectXTK \ Inc and AnimatedTexture.cpp
    in DirectXTK \ Src and I build the project.

    If I create the class in main project
    Add Class --> Visual C++ -- >
    I put the name and I selected Windows Store

    I will copy the class in .cpp file and I suppose to do a .h header file.
    When I created a Test1 C++ I got:
    for .h
    #pragma once
    ref class Test1 sealed
    {
    public:
     Test1();
    };

    and for .cpp
    #include "pch.h"
    #include "Test1.h"

    Test1::Test1()
    {
    }


    I want to add the code for AnimatedTexture and I don't know how
    to make the header please show me how to do it.


    #include "pch.h"
    #include "AnimatedTexture.h"

    //Test1::Test1()
    //{
    //}

    class AnimatedTexture
    {
    public:
        AnimatedTexture( const DirectX::XMFLOAT2& origin,
                         float rotation,
                         float scale,
                         float depth ) :
            mPaused(false),
            mFrameCount(0),
            mTextureWidth(0),
            mTextureHeight(0),
            mTimePerFrame(0.f),
            mTotalElapsed(0.f),
            mRotation( rotation ),
            mScale( scale, scale ),
            mDepth( depth ),
            mOrigin( origin )
        {
        }

        void Load( ID3D11ShaderResourceView* texture, int frameCount, int framesPerSecond )
        {
            if ( frameCount < 0 || framesPerSecond <= 0 )
                throw std::invalid_argument( "AnimatedTexture" );

            mPaused = false;
            mFrameCount = frameCount;
            mTimePerFrame = 1.f / float(framesPerSecond);
            mTotalElapsed = 0.f;
            mTexture = texture;

            if ( texture )
            {
                Microsoft::WRL::ComPtr<ID3D11Resource> resource;
                texture->GetResource( resource.GetAddressOf() );

                D3D11_RESOURCE_DIMENSION dim;
                resource->GetType( &dim );

                if ( dim != D3D11_RESOURCE_DIMENSION_TEXTURE2D )
                    throw std::exception( "AnimatedTexture expects a Texture2D" );

                Microsoft::WRL::ComPtr<ID3D11Texture2D> tex2D;
                resource.As( &tex2D );

                D3D11_TEXTURE2D_DESC desc;
                tex2D->GetDesc( &desc );

                mTextureWidth = int( desc.Width );
                mTextureHeight = int( desc.Height );
            }
        }

        void Update( float elapsed )
        {
            if ( mPaused )
                return;

            mTotalElapsed += elapsed;

            if ( mTotalElapsed > mTimePerFrame )
            {
                ++mFrame;
                mFrame = mFrame % mFrameCount;
                mTotalElapsed -= mTimePerFrame;
            }
        }

        void Draw( DirectX::SpriteBatch* batch, const DirectX::XMFLOAT2& screenPos ) const
        {
            Draw( batch, mFrame, screenPos );
        }

        void Draw( DirectX::SpriteBatch* batch, int frame, const DirectX::XMFLOAT2& screenPos ) const
        {
            int frameWidth = mTextureWidth / mFrameCount;

            RECT sourceRect;
            sourceRect.left = frameWidth * frame;
            sourceRect.top = 0;
            sourceRect.right = sourceRect.left + frameWidth;
            sourceRect.bottom = mTextureHeight;

            batch->Draw( mTexture.Get(), screenPos, &sourceRect, DirectX::Colors::White,
                         mRotation, mOrigin, mScale, DirectX::SpriteEffects_None, mDepth );
        }

        void Reset()
        {
            mFrame = 0;
            mTotalElapsed = 0.f;
        }

        void Stop()
        {
            mPaused = true;
            mFrame = 0;
            mTotalElapsed = 0.f;
        }

        void Play() { mPaused = false; }
        void Paused() { mPaused = true; }

        bool IsPaused() const { return mPaused; }

    private:
        bool                                                mPaused;
        int                                                 mFrame;
        int                                                 mFrameCount;
        int                                                 mTextureWidth;
        int                                                 mTextureHeight;
        float                                               mTimePerFrame;
        float                                               mTotalElapsed;
        float                                               mDepth;
        float                                               mRotation;
        DirectX::XMFLOAT2                                   mOrigin;
        DirectX::XMFLOAT2                                   mScale;
        Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>    mTexture;
    };

    Thanks


    ADRIAN DIBU

    Wednesday, March 26, 2014 1:14 PM
  • AnimatedTexture is a helper you can just drop into your project rather than modify the DirectXTK library. You could even just copy & paste the code into whatever header defines the object that you use it in.

    I would suggest spending some time reading a reference on C++ programming which covers concepts around header files, compilation modules, libraries, etc. that are not specific to Windows.

    DirectXTK is a C++ static library without making use of WinRT in order to keep it simple and standard, as well as enable it to support the breadth of platforms it does (Win32 desktop, Windows Store, Windows phone, Xbox One).

    You may also want to take a look at the DirectXTK CodePlex discussion threads and the various platform samples.

    • Marked as answer by adibu Wednesday, March 26, 2014 11:49 PM
    Wednesday, March 26, 2014 6:02 PM