PhysicsMaterial.cpp 1.6 KB

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