Skip to content

Commit

Permalink
Animation: fix and document the behavior of setGroup() with the frame…
Browse files Browse the repository at this point in the history
… index
  • Loading branch information
JonathSpirit committed Dec 11, 2024
1 parent d228580 commit e383d8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 6 additions & 0 deletions includes/FastEngine/accessor/C_animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,19 @@ class FGE_API Animation : public manager::BaseDataAccessor<
/**
* \brief Set the group of the animation by its name
*
* If the frame index overflow in the new group, it will be reset to 0.
* Else the frame index will be kept.
*
* \param group The name of the group
* \return \b true if the group was found, \b false otherwise
*/
bool setGroup(std::string_view group);
/**
* \brief Set the group of the animation by its index
*
* If the frame index overflow in the new group, it will be reset to 0.
* Else the frame index will be kept.
*
* \param groupIndex The index of the group
* \return \b true if the group was found, \b false otherwise
*/
Expand Down
15 changes: 10 additions & 5 deletions sources/accessor/C_animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ bool Animation::setGroup(std::string_view group)
{ //Same group
if (data->_groups[this->g_groupIndex]._groupName == group)
{
this->g_frameIndex = 0;
return true;
}
}
Expand All @@ -93,7 +92,10 @@ bool Animation::setGroup(std::string_view group)
if (data->_groups[i]._groupName == group)
{
this->g_groupIndex = i;
this->g_frameIndex = 0;
if (this->g_frameIndex >= data->_groups[i]._frames.size())
{
this->g_frameIndex = 0;
}
return true;
}
}
Expand All @@ -103,14 +105,17 @@ bool Animation::setGroup(Index groupIndex)
{
if (this->g_groupIndex == groupIndex)
{ //Same group
this->g_frameIndex = 0;
return true;
}

if (groupIndex < this->retrieve()->_groups.size())
auto data = this->retrieve();
if (groupIndex < data->_groups.size())
{
this->g_groupIndex = groupIndex;
this->g_frameIndex = 0;
if (this->g_frameIndex >= data->_groups[groupIndex]._frames.size())
{
this->g_frameIndex = 0;
}
return true;
}
return false;
Expand Down

0 comments on commit e383d8d

Please sign in to comment.