2011-12-12 04:42:39 +00:00
|
|
|
#include "animation.hpp"
|
|
|
|
|
2012-07-03 13:32:38 +00:00
|
|
|
#include <OgreHardwarePixelBuffer.h>
|
|
|
|
#include <OgreSkeletonInstance.h>
|
|
|
|
#include <OgreEntity.h>
|
|
|
|
#include <OgreBone.h>
|
|
|
|
#include <OgreSubMesh.h>
|
2012-07-18 07:17:39 +00:00
|
|
|
#include <OgreSceneManager.h>
|
2011-12-17 06:29:08 +00:00
|
|
|
|
2012-07-24 20:51:48 +00:00
|
|
|
|
2012-07-13 10:51:58 +00:00
|
|
|
namespace MWRender
|
|
|
|
{
|
2012-07-13 03:12:18 +00:00
|
|
|
|
|
|
|
Animation::Animation(OEngine::Render::OgreRenderer& _rend)
|
2012-07-17 17:57:15 +00:00
|
|
|
: mInsert(NULL)
|
2012-07-13 03:12:18 +00:00
|
|
|
, mRend(_rend)
|
2012-07-17 17:57:15 +00:00
|
|
|
, mTime(0.0f)
|
|
|
|
, mAnimate(0)
|
2012-07-22 00:39:57 +00:00
|
|
|
, mSkipFrame(false)
|
2012-07-13 03:12:18 +00:00
|
|
|
{
|
2011-12-17 06:29:08 +00:00
|
|
|
}
|
2012-02-20 13:02:24 +00:00
|
|
|
|
2012-07-13 03:12:18 +00:00
|
|
|
Animation::~Animation()
|
|
|
|
{
|
2012-07-18 07:17:39 +00:00
|
|
|
Ogre::SceneManager *sceneMgr = mInsert->getCreator();
|
|
|
|
for(size_t i = 0;i < mEntityList.mEntities.size();i++)
|
|
|
|
sceneMgr->destroyEntity(mEntityList.mEntities[i]);
|
|
|
|
mEntityList.mEntities.clear();
|
2012-07-13 03:12:18 +00:00
|
|
|
}
|
2012-01-07 03:52:15 +00:00
|
|
|
|
2012-07-24 20:51:48 +00:00
|
|
|
|
|
|
|
struct checklow {
|
|
|
|
bool operator()(const char &a, const char &b) const
|
|
|
|
{
|
|
|
|
return ::tolower(a) == ::tolower(b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool Animation::findGroupTimes(const std::string &groupname, float *starttime, float *stoptime, float *loopstarttime, float *loopstoptime)
|
|
|
|
{
|
|
|
|
const std::string &start = groupname+": start";
|
|
|
|
const std::string &startloop = groupname+": loop start";
|
|
|
|
const std::string &stop = groupname+": stop";
|
|
|
|
const std::string &stoploop = groupname+": loop stop";
|
|
|
|
|
|
|
|
*starttime = -1.0f;
|
|
|
|
*stoptime = -1.0f;
|
|
|
|
*loopstarttime = -1.0f;
|
|
|
|
*loopstoptime = -1.0f;
|
|
|
|
|
|
|
|
NifOgre::TextKeyMap::const_iterator iter;
|
|
|
|
for(iter = mTextKeys.begin();iter != mTextKeys.end();iter++)
|
|
|
|
{
|
|
|
|
if(*starttime >= 0.0f && *stoptime >= 0.0f && *loopstarttime >= 0.0f && *loopstoptime >= 0.0f)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::string::const_iterator strpos = iter->second.begin();
|
|
|
|
std::string::const_iterator strend = iter->second.end();
|
|
|
|
|
|
|
|
while(strpos != strend)
|
|
|
|
{
|
|
|
|
size_t strlen = strend-strpos;
|
|
|
|
std::string::const_iterator striter;
|
|
|
|
|
|
|
|
if(start.size() <= strlen &&
|
|
|
|
((striter=std::mismatch(strpos, strend, start.begin(), checklow()).first) == strend ||
|
|
|
|
*striter == '\r' || *striter == '\n'))
|
|
|
|
{
|
|
|
|
*starttime = iter->first;
|
|
|
|
*loopstarttime = iter->first;
|
|
|
|
}
|
|
|
|
else if(startloop.size() <= strlen &&
|
|
|
|
((striter=std::mismatch(strpos, strend, startloop.begin(), checklow()).first) == strend ||
|
|
|
|
*striter == '\r' || *striter == '\n'))
|
|
|
|
{
|
|
|
|
*loopstarttime = iter->first;
|
|
|
|
}
|
|
|
|
else if(stoploop.size() <= strlen &&
|
|
|
|
((striter=std::mismatch(strpos, strend, stoploop.begin(), checklow()).first) == strend ||
|
|
|
|
*striter == '\r' || *striter == '\n'))
|
|
|
|
{
|
|
|
|
*loopstoptime = iter->first;
|
|
|
|
}
|
|
|
|
else if(stop.size() <= strlen &&
|
|
|
|
((striter=std::mismatch(strpos, strend, stop.begin(), checklow()).first) == strend ||
|
|
|
|
*striter == '\r' || *striter == '\n'))
|
|
|
|
{
|
|
|
|
*stoptime = iter->first;
|
|
|
|
if(*loopstoptime < 0.0f)
|
|
|
|
*loopstoptime = iter->first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
strpos = std::find(strpos+1, strend, '\n');
|
|
|
|
while(strpos != strend && *strpos == '\n')
|
|
|
|
strpos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*starttime >= 0.0f && *stoptime >= 0.0f && *loopstarttime >= 0.0f && *loopstoptime >= 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-20 07:53:12 +00:00
|
|
|
void Animation::playGroup(std::string groupname, int mode, int loops)
|
2012-07-13 03:12:18 +00:00
|
|
|
{
|
2012-07-24 20:51:48 +00:00
|
|
|
float start, stop, loopstart, loopstop;
|
|
|
|
|
2012-07-13 03:12:18 +00:00
|
|
|
if(groupname == "all")
|
|
|
|
{
|
2012-07-24 21:14:32 +00:00
|
|
|
start = loopstart = 0.0f;
|
|
|
|
loopstop = stop = 0.0f;
|
2012-07-24 20:51:48 +00:00
|
|
|
|
|
|
|
if(mEntityList.mSkelBase)
|
|
|
|
{
|
|
|
|
Ogre::AnimationStateSet *aset = mEntityList.mSkelBase->getAllAnimationStates();
|
|
|
|
Ogre::AnimationStateIterator as = aset->getAnimationStateIterator();
|
|
|
|
while(as.hasMoreElements())
|
|
|
|
{
|
|
|
|
Ogre::AnimationState *state = as.getNext();
|
2012-07-24 21:14:32 +00:00
|
|
|
loopstop = stop = state->getLength();
|
2012-07-24 20:51:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 21:14:32 +00:00
|
|
|
else if(!findGroupTimes(groupname, &start, &stop, &loopstart, &loopstop))
|
2012-07-24 20:51:48 +00:00
|
|
|
throw std::runtime_error("Failed to find animation group "+groupname);
|
2012-07-24 21:14:32 +00:00
|
|
|
|
|
|
|
// FIXME: mode = 0 not yet supported
|
|
|
|
if(mode == 0)
|
|
|
|
mode = 1;
|
|
|
|
|
|
|
|
mStartTime = start;
|
|
|
|
mStopTime = stop;
|
|
|
|
mLoopStartTime = loopstart;
|
|
|
|
mLoopStopTime = loopstop;
|
|
|
|
|
|
|
|
mAnimate = loops;
|
|
|
|
mTime = ((mode==1) ? mStartTime : mLoopStartTime);
|
2012-07-24 20:51:48 +00:00
|
|
|
}
|
2011-12-15 05:33:10 +00:00
|
|
|
|
2012-07-20 07:53:12 +00:00
|
|
|
void Animation::skipAnim()
|
2012-07-13 03:12:18 +00:00
|
|
|
{
|
2012-07-22 00:39:57 +00:00
|
|
|
mSkipFrame = true;
|
2012-07-13 03:12:18 +00:00
|
|
|
}
|
2012-02-20 13:02:24 +00:00
|
|
|
|
2012-07-21 21:41:26 +00:00
|
|
|
void Animation::runAnimation(float timepassed)
|
|
|
|
{
|
2012-07-24 20:51:48 +00:00
|
|
|
if(mAnimate > 0 && !mSkipFrame)
|
2012-07-21 21:41:26 +00:00
|
|
|
{
|
|
|
|
mTime += timepassed;
|
2012-07-24 20:51:48 +00:00
|
|
|
if(mTime >= mLoopStopTime)
|
|
|
|
{
|
|
|
|
if(mAnimate > 1)
|
|
|
|
{
|
|
|
|
mAnimate--;
|
|
|
|
mTime = mTime - mLoopStopTime + mLoopStartTime;
|
|
|
|
}
|
|
|
|
else if(mTime >= mStopTime)
|
|
|
|
{
|
|
|
|
mAnimate--;
|
|
|
|
mTime = mStopTime;
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 21:41:26 +00:00
|
|
|
|
|
|
|
if(mEntityList.mSkelBase)
|
|
|
|
{
|
|
|
|
Ogre::AnimationStateSet *aset = mEntityList.mSkelBase->getAllAnimationStates();
|
|
|
|
Ogre::AnimationStateIterator as = aset->getAnimationStateIterator();
|
|
|
|
while(as.hasMoreElements())
|
|
|
|
{
|
|
|
|
Ogre::AnimationState *state = as.getNext();
|
|
|
|
state->setTimePosition(mTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-22 00:39:57 +00:00
|
|
|
mSkipFrame = false;
|
2012-07-21 21:41:26 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 13:02:24 +00:00
|
|
|
}
|