LoadSaveBinaryTest.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. ObjectVsBroadPhaseLayerFilterImpl object_vs_broadphase_layer_filter;
  23. ObjectLayerPairFilterImpl object_vs_object_layer_filter;
  24. system.Init(mPhysicsSystem->GetMaxBodies(), 0, 1024, 1024, layer_interface, object_vs_broadphase_layer_filter, object_vs_object_layer_filter);
  25. scene->CreateBodies(&system);
  26. Ref<PhysicsScene> scene_copy = new PhysicsScene();
  27. scene_copy->FromPhysicsSystem(&system);
  28. // Replace the original scene
  29. scene = scene_copy;
  30. }
  31. stringstream data;
  32. // Write scene
  33. {
  34. StreamOutWrapper stream_out(data);
  35. scene->SaveBinaryState(stream_out, true, true);
  36. }
  37. // Clear scene
  38. scene = nullptr;
  39. // Read scene back in
  40. {
  41. StreamInWrapper stream_in(data);
  42. PhysicsScene::PhysicsSceneResult result = PhysicsScene::sRestoreFromBinaryState(stream_in);
  43. if (result.HasError())
  44. FatalError(result.GetError().c_str());
  45. scene = result.Get();
  46. }
  47. // Instantiate scene
  48. scene->CreateBodies(mPhysicsSystem);
  49. }