diff --git a/include/ModelAnimation.hpp b/include/ModelAnimation.hpp index a6597181..dcb28195 100644 --- a/include/ModelAnimation.hpp +++ b/include/ModelAnimation.hpp @@ -58,6 +58,7 @@ class ModelAnimation : public ::ModelAnimation { } ModelAnimation result(modelAnimations[index]); modelAnimations[index].keyframePoses = nullptr; + modelAnimations[index].keyframeCount = 0; ::UnloadModelAnimations(modelAnimations, count); return result; } @@ -103,9 +104,14 @@ class ModelAnimation : public ::ModelAnimation { */ void Unload() { if (keyframePoses != nullptr) { - ::UnloadModelAnimations(this, 1); + // Free the poses directly, as ::UnloadModelAnimations() would also free this object. + for (int i = 0; i < keyframeCount; i++) { + RL_FREE(keyframePoses[i]); + } + RL_FREE(keyframePoses); keyframePoses = nullptr; } + keyframeCount = 0; } static void Unload(ModelAnimation* modelAnimation, int count) { ::UnloadModelAnimations(modelAnimation, count); } diff --git a/tests/raylib_cpp_test.cpp b/tests/raylib_cpp_test.cpp index d2539e85..79217312 100644 --- a/tests/raylib_cpp_test.cpp +++ b/tests/raylib_cpp_test.cpp @@ -3,6 +3,7 @@ #include "raylib-assert.h" // clang-format on #include +#include #include int main(int argc, char* argv[]) { @@ -187,6 +188,36 @@ int main(int argc, char* argv[]) { AssertEqual(text.ToString().substr(0, 5), "Lorem"); } + // ModelAnimation + { + std::string animFile = path + "/resources/guyanim.iqm"; + int count = raylib::ModelAnimation::LoadCount(animFile); + AssertGreater(count, 0); + + { + // The vector going out of scope must unload each animation without freeing the vector's storage. + std::vector anims = raylib::ModelAnimation::Load(animFile); + AssertEqual(static_cast(anims.size()), count); + AssertGreater(anims[0].GetKeyframeCount(), 0); + AssertNotNull(anims[0].GetKeyframePoses()); + + anims[0].Unload(); + AssertNull(anims[0].GetKeyframePoses()); + AssertEqual(anims[0].GetKeyframeCount(), 0); + } + + { + raylib::ModelAnimation anim = raylib::ModelAnimation::Load(animFile, 0); + AssertGreater(anim.GetKeyframeCount(), 0); + AssertNotNull(anim.GetKeyframePoses()); + + raylib::ModelAnimation moved(std::move(anim)); + AssertNull(anim.GetKeyframePoses()); + AssertEqual(anim.GetKeyframeCount(), 0); + AssertNotNull(moved.GetKeyframePoses()); + } + } + // BoundingBox { ::Vector3 min = {1, 2, 3}; diff --git a/tests/resources/guyanim.iqm b/tests/resources/guyanim.iqm new file mode 100644 index 00000000..45f1c18a Binary files /dev/null and b/tests/resources/guyanim.iqm differ