# Animation with D3DX

## Recommended Posts

Hey everyone I was wondering if you could point me to a good example of a basic animation example in d3dx. I was thinking about animation and obviously the first thing that comes to mind would be one of these:
if(KEYSTATE(VK_RIGHT))
{
doWalkRightAni();
}


or
if(KEYSTATE(VK_RIGHT))
{
player[1].frameCount++;
drawPlayerFrame(player[1].frameCount);
}


Basically what I'm saying is that... I update each frame (everytime the character moves) every cpu cycle or do I use a function that does an animated walk (using timers for inbetween frames)? Which is more efficient (or is there an even better way)?

##### Share on other sites
The way I do it (for 2D anyway) is I have a timer running that is called every 250ms or so. At that point I update the frame.

If you update the frame everytime the key is down, it will be way too fast.

Heres an example that I'm going to use (it allows for 3 types of animations)
enum LOOPSTYLE{    REPEAT = 0,		// 123 123 123		REVERSE,	// 12321232123		NONE,		// 123, deactivates and resets to 1};class Animation{public:	UINT m_nFrameCount;	UINT m_nCurFrame;	// m_nStaticFrame is the frame that its on when the anim is active,	// but not updating.	UINT m_nStaticFrame;	UINT m_nLoopStyle;	bool m_bActive;			// Will draw	bool m_bUpdate;			// Will update frame		void Update()	{		if(!m_bUpdate)		{			m_nCurFrame = m_nStaticFrame;			return;		}		static UINT nRevMod = 1;				switch(m_nLoopStyle)		{					case REPEAT:			{				++m_nCurFrame;				if(m_nCurFrame == m_nFrameCount)					m_nCurFrame = 0;				break;			}					case REVERSE:			{				if(m_nCurFrame == 0)					nRevMod = 1;								if(m_nCurFrame == m_nFrameCount)					nRevMod = -1;				m_nCurFrame += nRevMod;				break;			}					case NONE:			{				++m_nCurFrame;				if(m_nCurFrame == m_nFrameCount)				{					m_nCurFrame = m_nStaticFrame;					m_bActive = true;					m_bUpdate = false;				}				break;			}		}	}};Animation Anim = { 3, 0, 1, REVERSE, true, false};StartTimer(ANIM_TIMER, 250);if(IsTime(ANIM_TIMER))Anim.Update();

Okay, the reverse code might not work correctly, but that's the basic idea (since I'm using unsigned integers, I tend to use code that makes it negative (which makes it go to the max value for an UINT), but I believe that this works.)

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627639
• Total Posts
2978344

• 10
• 12
• 22
• 13
• 33