Game Development Reference
In-Depth Information
Given that the sprite sheet is responsible for holding a sprite's data, we will need a way to
create animations.
animation& CreateAnimation(std::wstring name, size_t& animationID)
{
if ( GetAnimationID(name, animationID) )
{
return m_animations[animationID];
}
animation anim(name);
m_animations.push_back(anim);
animationID = m_animations.size()-1;
m_animationIDMap[name] = animationID;
return m_animations[animationID];
}
It is useful to give animations a name, as it makes them easier to refer to them, however,
workingwithdirectlywithstrings,especiallyinsituationsthatwoulddemandastringcom-
parison is not a good practice. So while we provide each animation with a name, this is
mostly for information and debugging purposes (such as displaying the name of the anim-
ation agiven sprite isplaying at runtime), rather once wecreate ananimation, wecreate an
identifier and we store the animation's name in a map.
bool GetAnimationID(const std::wstring& name, size_t& id)
{
id = SIZE_MAX;
if ( m_animationIDMap.find(name) == m_animationIDMap.end() )
{
// The animation does not exist in the map.
return false;
}
id = m_animationIDMap[name];
return true;
}
In retrieving the animation ID we will incur the cost of string comparisons, these will be
done during the animation construction which should happen during the loading stage of
the game. During runtime, when performance is critical, we will refer to the animation by
its non-string ID.
The following code provides an example on how sprite sheets are loaded and animations
canbecreated.Thisisacodedrivenexampleforsimplicity,ideallythissortofconstruction
could be done using data driven mechanisms, such as parsing an XML file.
Search WWH ::




Custom Search