PhysicsMaterial.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/PhysicsMaterial.h>
  6. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  7. #include <Jolt/Core/StreamIn.h>
  8. #include <Jolt/Core/StreamOut.h>
  9. #include <Jolt/Core/Factory.h>
  10. JPH_NAMESPACE_BEGIN
  11. RefConst<PhysicsMaterial> PhysicsMaterial::sDefault;
  12. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(PhysicsMaterial)
  13. {
  14. JPH_ADD_BASE_CLASS(PhysicsMaterial, SerializableObject)
  15. }
  16. void PhysicsMaterial::SaveBinaryState(StreamOut &inStream) const
  17. {
  18. inStream.Write(GetRTTI()->GetHash());
  19. }
  20. void PhysicsMaterial::RestoreBinaryState(StreamIn &inStream)
  21. {
  22. // RTTI hash is read in sRestoreFromBinaryState
  23. }
  24. PhysicsMaterial::PhysicsMaterialResult PhysicsMaterial::sRestoreFromBinaryState(StreamIn &inStream)
  25. {
  26. PhysicsMaterialResult result;
  27. // Read the type of the material
  28. uint32 hash;
  29. inStream.Read(hash);
  30. if (inStream.IsEOF() || inStream.IsFailed())
  31. {
  32. result.SetError("Failed to read type hash");
  33. return result;
  34. }
  35. // Get the RTTI for the material
  36. const RTTI *rtti = Factory::sInstance->Find(hash);
  37. if (rtti == nullptr)
  38. {
  39. result.SetError("Failed to create instance of material");
  40. return result;
  41. }
  42. // Construct and read the data of the material
  43. Ref<PhysicsMaterial> material = reinterpret_cast<PhysicsMaterial *>(rtti->CreateObject());
  44. material->RestoreBinaryState(inStream);
  45. if (inStream.IsEOF() || inStream.IsFailed())
  46. {
  47. result.SetError("Failed to restore material");
  48. return result;
  49. }
  50. result.Set(material);
  51. return result;
  52. }
  53. JPH_NAMESPACE_END