diff --git a/include/MeshUnmanaged.hpp b/include/MeshUnmanaged.hpp index 47ee2824..cbcfa7aa 100644 --- a/include/MeshUnmanaged.hpp +++ b/include/MeshUnmanaged.hpp @@ -156,7 +156,7 @@ class MeshUnmanaged : public ::Mesh { void Unload() { if (vboId != nullptr) { ::UnloadMesh(*this); - vboId = nullptr; + set(::Mesh{}); } } diff --git a/include/ModelUnmanaged.hpp b/include/ModelUnmanaged.hpp index 3f1c43be..1f28418f 100644 --- a/include/ModelUnmanaged.hpp +++ b/include/ModelUnmanaged.hpp @@ -95,8 +95,7 @@ class ModelUnmanaged : public ::Model { void Unload() { if (meshes != nullptr || materials != nullptr) { ::UnloadModel(*this); - meshes = nullptr; - materials = nullptr; + set(::Model{}); } } diff --git a/tests/raylib_cpp_test.cpp b/tests/raylib_cpp_test.cpp index d2539e85..48348e09 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[]) { @@ -207,6 +208,47 @@ int main(int argc, char* argv[]) { AssertEqual(boundingBox.GetMin(), (raylib::Vector3{9, 8, 7})); } + // Model Unload resets all fields (CPU-only data, no meshes, so no GPU calls) + { + raylib::ModelUnmanaged model; + model.meshes = static_cast<::Mesh*>(MemAlloc(sizeof(::Mesh))); + model.meshMaterial = static_cast(MemAlloc(sizeof(int))); + model.skeleton.boneCount = 1; + model.skeleton.bones = static_cast<::BoneInfo*>(MemAlloc(sizeof(::BoneInfo))); + model.skeleton.bindPose = static_cast<::Transform*>(MemAlloc(sizeof(::Transform))); + model.currentPose = static_cast<::Transform*>(MemAlloc(sizeof(::Transform))); + model.boneMatrices = static_cast<::Matrix*>(MemAlloc(sizeof(::Matrix))); + + model.Unload(); + Assert(model.meshes == nullptr, "Expected meshes to be null after Unload"); + Assert(model.materials == nullptr, "Expected materials to be null after Unload"); + Assert(model.meshMaterial == nullptr, "Expected meshMaterial to be null after Unload"); + Assert(model.skeleton.bones == nullptr, "Expected bones to be null after Unload"); + Assert(model.skeleton.bindPose == nullptr, "Expected bindPose to be null after Unload"); + Assert(model.currentPose == nullptr, "Expected currentPose to be null after Unload"); + Assert(model.boneMatrices == nullptr, "Expected boneMatrices to be null after Unload"); + AssertEqual(model.meshCount, 0); + AssertEqual(model.materialCount, 0); + AssertEqual(model.skeleton.boneCount, 0u); + + // Unloading twice is a no-op. + model.Unload(); + } + + // Model move leaves the moved-from Model empty + { + raylib::Model source(::Model{}); + source.meshes = static_cast<::Mesh*>(MemAlloc(sizeof(::Mesh))); + source.meshMaterial = static_cast(MemAlloc(sizeof(int))); + source.boneMatrices = static_cast<::Matrix*>(MemAlloc(sizeof(::Matrix))); + + raylib::Model target(std::move(source)); + Assert(source.meshes == nullptr, "Expected moved-from meshes to be null"); + Assert(source.meshMaterial == nullptr, "Expected moved-from meshMaterial to be null"); + Assert(source.boneMatrices == nullptr, "Expected moved-from boneMatrices to be null"); + Assert(target.meshes != nullptr, "Expected moved-to meshes to be set"); + } + TraceLog(LOG_INFO, "TEST: raylib-cpp test"); TraceLog(LOG_INFO, "---------------------"); return 0;