Using this class:
class myAnimObject
{
public:
myAnimObject();
~myAnimObject() {};
void setID(int ID) {m_ID = ID;}
void initAnim(int startFrame, int endFrame, float framesPerSec);
void animateObject(int curTime);
private:
int m_ID;
int m_startFrame, m_endFrame, m_numFrames;
float m_curFrame, m_fPerSec;
int m_prevTime;
};
//
myAnimObject::myAnimObject()
:m_startFrame(0), m_endFrame(0), m_numFrames(0),
m_curFrame(0), m_fPerSec(0),
m_prevTime(0)
{
}
//
void myAnimObject::initAnim(int startFrame, int endFrame, float framesPerSec)
{
m_startFrame = startFrame;
m_curFrame = startFrame;
m_endFrame = endFrame;
m_numFrames = m_endFrame - m_startFrame + 1;
m_fPerSec = framesPerSec;
m_prevTime = dbTimer();
dbSetObjectFrame(m_ID, startFrame);
}
//
void myAnimObject::animateObject(int curTime)
{
// curTime - prevTime is in milliseconds
float frameProgress = float(curTime - m_prevTime) * m_fPerSec / 1000.0;
m_curFrame += frameProgress;
while (m_curFrame > m_endFrame) m_curFrame -= m_numFrames; // wrap around
m_prevTime = curTime;
dbSetObjectFrame(m_ID, int(m_curFrame + 0.5));
}
you specify the start frame, end frame, and fps. Here is an example:
myAnimObject Gun;
Gun.setID(1);
Gun.initAnim(1, 50, 60)
and to play the animation you put this in your loop:
int curTime = dbTimer();
Gun.animateObject(curTime);