An example of an animation update function.
Code:
DWORD CAnimSeq::Update(float fDelta)
{
//Advance time by delta
m_fElapsed+=fDelta;
//Check whether or not to advance
if (m_fElapsed>=m_fMSPerFrame)
{
//Find out how many frames to advance
DWORD dwAdvanceBy=static_cast<DWORD>((m_fElapsed/m_fMSPerFrame));
//Decrement elapsed time to compensate
m_fElapsed-=(dwAdvanceBy*m_fMSPerFrame);
//Advance frame
m_dwCurFrame+=dwAdvanceBy;
//Check if we are at the end of the sequence or past it
if (m_dwCurFrame>=m_vFrames.size())
{
if (m_bLooped)
{
//Since we are looped, advance to 0, or past 0 depending on current frame
m_dwCurFrame-=static_cast<DWORD>(m_vFrames.size());
}
else
{
m_bActive=false; //else de-activate animation
m_dwCurFrame=static_cast<DWORD>(m_vFrames.size())-1;
}
}
}
//Return current frame to caller
//Useful inside of an animation loop so another call is not required to get
//current animation frame ID
return m_vFrames[m_dwCurFrame];
}
fMSPerFrame is calculated by:
Code:
void CAnimSeq::Create(float fFPS,bool bLooped)
{
m_fFPS=fFPS;
m_fElapsed=0.0f;
m_bLooped=bLooped;
m_fTotalSeconds=1000.0f/m_fFPS;
m_fMSPerFrame=0.0f;
}
DWORD CAnimSeq::AddFrame(DWORD dwFrameID)
{
m_vFrames.push_back(dwFrameID);
m_fMSPerFrame=(m_fTotalSeconds*1000)/m_vFrames.size();
return static_cast<DWORD>(m_vFrames.size()-1);
}
Each frame is simply a texture ID that resolves to a certain texture in the texture manager vector. All texture access is via the texture manager public functions. So my animation system is iterating through a vector of DWORD values over a specified amount of time.