Constraint.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Constraints/Constraint.h>
  5. #include <Physics/StateRecorder.h>
  6. #include <ObjectStream/TypeDeclarations.h>
  7. #include <Core/StreamIn.h>
  8. #include <Core/StreamOut.h>
  9. #include <Core/Factory.h>
  10. namespace JPH {
  11. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(ConstraintSettings)
  12. {
  13. JPH_ADD_BASE_CLASS(ConstraintSettings, SerializableObject)
  14. JPH_ADD_ATTRIBUTE(ConstraintSettings, mDrawConstraintSize)
  15. }
  16. void ConstraintSettings::SaveBinaryState(StreamOut &inStream) const
  17. {
  18. inStream.Write(GetRTTI()->GetHash());
  19. inStream.Write(mDrawConstraintSize);
  20. }
  21. void ConstraintSettings::RestoreBinaryState(StreamIn &inStream)
  22. {
  23. // Type hash read by sRestoreFromBinaryState
  24. inStream.Read(mDrawConstraintSize);
  25. }
  26. ConstraintSettings::ConstraintResult ConstraintSettings::sRestoreFromBinaryState(StreamIn &inStream)
  27. {
  28. ConstraintResult result;
  29. // Read the type of the constraint
  30. uint32 hash;
  31. inStream.Read(hash);
  32. if (inStream.IsEOF() || inStream.IsFailed())
  33. {
  34. result.SetError("Failed to read type id");
  35. return result;
  36. }
  37. // Get the RTTI for the shape
  38. const RTTI *rtti = Factory::sInstance.Find(hash);
  39. if (rtti == nullptr)
  40. {
  41. result.SetError("Failed to resolve type. Type not registered in factory?");
  42. return result;
  43. }
  44. // Construct and read the data of the shape
  45. Ref<ConstraintSettings> constraint = reinterpret_cast<ConstraintSettings *>(rtti->CreateObject());
  46. constraint->RestoreBinaryState(inStream);
  47. if (inStream.IsEOF() || inStream.IsFailed())
  48. {
  49. result.SetError("Failed to restore constraint");
  50. return result;
  51. }
  52. result.Set(constraint);
  53. return result;
  54. }
  55. void Constraint::SaveState(StateRecorder &inStream) const
  56. {
  57. inStream.Write(mEnabled);
  58. }
  59. void Constraint::RestoreState(StateRecorder &inStream)
  60. {
  61. inStream.Read(mEnabled);
  62. }
  63. } // JPH