I was wondering if it was a good idea to have an animation handler then you pass an ID and it will set the frame loop based on the ID you passed for example
AnimationHandler::setAnimation(node, ID);
then in the function you look up the frame length based on the ID then play it.
I think this is a bad idea? Anyone know a better idea?
Thanks,
Dan
Animations using frames?
-
- Posts: 41
- Joined: Thu Feb 27, 2014 10:55 am
Re: Animations using frames?
why do you think it's a bad idea?
Re: Animations using frames?
I use a similer method
Load frame numbers into structure
Simple functions get the frame loop numbers
I use strings to set animations
Vector2d range = actiontable.getanimation("walk")
Load frame numbers into structure
Simple functions get the frame loop numbers
I use strings to set animations
Vector2d range = actiontable.getanimation("walk")
Re: Animations using frames?
Code: Select all
// include this file only once
#pragma once
// include the needed header files
#include "CSUtils.h"
// defines
#define MAX_ANIMATIONS 100
// use our namespace so we dont clutter the global namespace
namespace CS
{
// simple structure to hold the animation ranges for a model
struct CSActionTableEntry
{
stringc m_AnimationName;
int m_AnimationStart;
int m_AnimationEnd;
};
// wrapper class for managing animations
class CSActionTable
{
public:
CSActionTableEntry* m_Animations[MAX_ANIMATIONS];
CSActionTable::CSActionTable() { }
virtual CSActionTable::~CSActionTable() { }
// set all variables to a known value
virtual void initialize()
{
for (int x = 0; x < MAX_ANIMATIONS; x++) { CS_INIT(m_Animations[x]); }
}
// dual creation allows for better error handling
virtual bool create()
{
return true;
}
// cleanup whatever memory mess we made
virtual bool cleanup()
{
for (int x = 0; x < MAX_ANIMATIONS; x++) { CS_SAFE_DELETE(m_Animations[x]); }
return false;
}
virtual void updateEntry(stringc name, int start, int end)
{
for (int x = 0; x < MAX_ANIMATIONS; x++)
{
if ((m_Animations[x]) && (m_Animations[x]->m_AnimationName == name))
{
m_Animations[x]->m_AnimationStart = start;
m_Animations[x]->m_AnimationEnd = end;
}
}
}
virtual int getMaxIndex()
{
int value = 0;
for (int x = 0; x < MAX_ANIMATIONS; x++)
{
if (m_Animations[x])
{
if (value < m_Animations[x]->m_AnimationEnd)
value = m_Animations[x]->m_AnimationEnd;
}
}
return value;
}
virtual int addEntry(stringc name, int start, int end)
{
for (int x = 0; x < MAX_ANIMATIONS; x++)
{
if (!m_Animations[x])
{
m_Animations[x] = new CSActionTableEntry();
m_Animations[x]->m_AnimationName = name;
m_Animations[x]->m_AnimationStart = start;
m_Animations[x]->m_AnimationEnd = end;
return x;
}
}
return 0;
}
virtual void removeEntry(stringc name)
{
for (int x = 0; x < MAX_ANIMATIONS; x++)
{
if (!m_Animations[x])
{
if (m_Animations[x]->m_AnimationName == name)
{
delete(m_Animations[x]);
}
}
}
}
virtual CSActionTableEntry* getEntry(stringc name)
{
for (int x = 0; x < MAX_ANIMATIONS; x++)
{
if (m_Animations[x])
{
if (m_Animations[x]->m_AnimationName == name)
return m_Animations[x];
}
}
return NULL;
}
virtual CSActionTableEntry* getEntry(int index)
{
return m_Animations[index];
}
int getCount()
{
int count = 0;
for (int x = 0; x < MAX_ANIMATIONS; x++) if (m_Animations[x]) count++;
return count;
}
virtual void saveToDisk(stringc filename)
{
FILE* fp;
errno_t error = fopen_s(&fp, filename.c_str(), "w");
if (error == 0)
{
fprintf(fp, "ANIMATION_COUNT %d\n", getCount());
for (int x = 0; x < MAX_ANIMATIONS; x++)
{
if (m_Animations[x])
fprintf(fp, "%s %d %d\n", m_Animations[x]->m_AnimationName.c_str(), m_Animations[x]->m_AnimationStart, m_Animations[x]->m_AnimationEnd);
}
fclose(fp);
}
else CS_LOG("unable to save action table to disk........");
}
virtual void loadFromDisk(stringc filename)
{
CS_LOG2(" Loading Action Table", filename);
FILE* fp;
errno_t error = fopen_s(&fp, filename.c_str(), "r");
if (error == 0)
{
int count = 0;
char text[255];
int start, end;
fscanf_s(fp, "ANIMATION_COUNT %d\n", &count);
for (int x = 0; x < count; x++)
{
m_Animations[x] = new CSActionTableEntry();
fscanf_s(fp, "%s %d %d\n", &text, 255, &start, &end);
printf("name %s start %d end %d\n",text,start,end);
m_Animations[x]->m_AnimationName = stringc(text);
m_Animations[x]->m_AnimationStart = start;
m_Animations[x]->m_AnimationEnd = end;
}
fclose(fp);
}
else CS_LOG2("unable to load action table from disk........", filename);
}
};
} // end namespace
Code: Select all
virtual void createActionTable()
{
CS_LOG2("Loading Action Table", getDirectory("ActionTableDirectory") + getActionTableFileName());
CS_SAFE_CLEANUPANDDELETE(m_ActionTable);
m_ActionTable = new CSActionTable();
m_ActionTable->initialize();
m_ActionTable->create();
if (getActionTableFileName() != CS_DEFAULT)
m_ActionTable->loadFromDisk(getDirectory("ActionTableDirectory") + getActionTableFileName());
}
virtual void setAnimation(stringc name)
{
//CS_CHECK_RETURN((name == getAnimationName()), "CSActionTable::setAnimation() name is same");
if (name == getAnimationName()) return;
m_AnimationName = name;
CS_CHECK_RETURN(getActionTable(), "CSActionTable::setAniamtion() action table not valid");
CSActionTableEntry* e = getActionTable()->getEntry(name);
CS_CHECK_RETURN(e, "CSActionTable::setAnimation() cannot locate actiontable entry");
CS_CHECK_RETURN(getAnimatedMeshSceneNode(), "CSAtionTable::setAnimation() node is not valid")
getAnimatedMeshSceneNode()->setFrameLoop(e->m_AnimationStart, e->m_AnimationEnd);
}
Code: Select all
virtual void preFrame(const float &elapsedtime)
{
CSObject::preFrame(elapsedtime);
if (getBrain()) getBrain()->preFrame(elapsedtime);
if (getPhysXObject()) getPhysXObject()->preFrame(elapsedtime);
bool action = false;
if (getForward()) { action = true; if (getRun()) setAnimation("RUN"); else setAnimation("WALK"); if (getPhysXObject()) getPhysXObject()->addForce(getIn(), getMovingSpeed() * getRunSpeed()); }
if (getBackward()) { action = true; if (getRun()) setAnimation("RUN"); else setAnimation("WALK"); if (getPhysXObject()) getPhysXObject()->addForce(-getIn(), getMovingSpeed() * getRunSpeed()); }
if (getTurnLeft()) { action = true; if (getRun()) setAnimation("RUN"); else setAnimation("WALK"); rotate(false, elapsedtime); }
if (getTurnRight()) { action = true; if (getRun()) setAnimation("RUN"); else setAnimation("WALK"); rotate(true, elapsedtime); }
if (getStrafeLeft()) { action = true; if (getRun()) setAnimation("RUN"); else setAnimation("WALK"); if (getPhysXObject()) getPhysXObject()->addForce(-getLeft(), getMovingSpeed()); }
if (getStrafeRight()) { action = true; if (getRun()) setAnimation("RUN"); else setAnimation("WALK"); if (getPhysXObject()) getPhysXObject()->addForce(getLeft(), getMovingSpeed()); }
if (!action) stop();
}