PhysicsMaterial.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Collision/PhysicsMaterial.h>
  5. #include <Physics/Collision/PhysicsMaterialSimple.h>
  6. #include <Core/StreamIn.h>
  7. #include <Core/StreamOut.h>
  8. #include <Core/Factory.h>
  9. namespace JPH {
  10. RefConst<PhysicsMaterial> PhysicsMaterial::sDefault = new PhysicsMaterialSimple("Default", Color::sGrey);
  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. }
  22. PhysicsMaterial::PhysicsMaterialResult PhysicsMaterial::sRestoreFromBinaryState(StreamIn &inStream)
  23. {
  24. PhysicsMaterialResult result;
  25. // Read the type of the material
  26. uint32 hash;
  27. inStream.Read(hash);
  28. if (inStream.IsEOF() || inStream.IsFailed())
  29. {
  30. result.SetError("Failed to read type hash");
  31. return result;
  32. }
  33. // Get the RTTI for the material
  34. const RTTI *rtti = Factory::sInstance.Find(hash);
  35. if (rtti == nullptr)
  36. {
  37. result.SetError("Failed to create instance of material");
  38. return result;
  39. }
  40. // Construct and read the data of the material
  41. Ref<PhysicsMaterial> material = reinterpret_cast<PhysicsMaterial *>(rtti->CreateObject());
  42. material->RestoreBinaryState(inStream);
  43. if (inStream.IsEOF() || inStream.IsFailed())
  44. {
  45. result.SetError("Failed to restore material");
  46. return result;
  47. }
  48. result.Set(material);
  49. return result;
  50. }
  51. } // JPH