LoadSaveBinaryTest.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/LoadSaveBinaryTest.h>
  5. #include <Tests/General/LoadSaveSceneTest.h>
  6. #include <Jolt/Physics/PhysicsScene.h>
  7. #include <Utils/Log.h>
  8. #include <Jolt/Core/StreamWrapper.h>
  9. #include <Layers.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(LoadSaveBinaryTest)
  11. {
  12. JPH_ADD_BASE_CLASS(LoadSaveBinaryTest, Test)
  13. }
  14. void LoadSaveBinaryTest::Initialize()
  15. {
  16. // Create scene
  17. Ref<PhysicsScene> scene = LoadSaveSceneTest::sCreateScene();
  18. {
  19. // Create a new scene by instantiating the scene in a physics system and then converting it back to a scene
  20. PhysicsSystem system;
  21. BPLayerInterfaceImpl layer_interface;
  22. system.Init(mPhysicsSystem->GetMaxBodies(), 0, 1024, 1024, layer_interface, BroadPhaseCanCollide, ObjectCanCollide);
  23. scene->CreateBodies(&system);
  24. Ref<PhysicsScene> scene_copy = new PhysicsScene();
  25. scene_copy->FromPhysicsSystem(&system);
  26. // Replace the original scene
  27. scene = scene_copy;
  28. }
  29. stringstream data;
  30. // Write scene
  31. {
  32. StreamOutWrapper stream_out(data);
  33. scene->SaveBinaryState(stream_out, true, true);
  34. }
  35. // Clear scene
  36. scene = nullptr;
  37. // Read scene back in
  38. {
  39. StreamInWrapper stream_in(data);
  40. PhysicsScene::PhysicsSceneResult result = PhysicsScene::sRestoreFromBinaryState(stream_in);
  41. if (result.HasError())
  42. FatalError(result.GetError().c_str());
  43. scene = result.Get();
  44. }
  45. // Instantiate scene
  46. scene->CreateBodies(mPhysicsSystem);
  47. }