LoggingBodyActivationListener.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Body/BodyActivationListener.h>
  6. /// Activation listener that just logs the activations/deactivations
  7. class LoggingBodyActivationListener : public BodyActivationListener
  8. {
  9. public:
  10. // Activation callback type
  11. enum class EType
  12. {
  13. Activated,
  14. Deactivated,
  15. };
  16. // Entry written when an activation callback happens
  17. struct LogEntry
  18. {
  19. EType mType;
  20. BodyID mBodyID;
  21. };
  22. virtual void OnBodyActivated(const BodyID &inBodyID, uint64 inBodyUserData) override
  23. {
  24. lock_guard lock(mLogMutex);
  25. mLog.push_back({ EType::Activated, inBodyID });
  26. }
  27. virtual void OnBodyDeactivated(const BodyID &inBodyID, uint64 inBodyUserData) override
  28. {
  29. lock_guard lock(mLogMutex);
  30. mLog.push_back({ EType::Deactivated, inBodyID });
  31. }
  32. void Clear()
  33. {
  34. mLog.clear();
  35. }
  36. size_t GetEntryCount() const
  37. {
  38. return mLog.size();
  39. }
  40. // Check if we have logged an event with a particular type and involving a particular body
  41. bool Contains(EType inType, const BodyID &inBodyID) const
  42. {
  43. for (const LogEntry &e : mLog)
  44. if (e.mType == inType && e.mBodyID == inBodyID)
  45. return true;
  46. return false;
  47. }
  48. private:
  49. Mutex mLogMutex; // Callbacks are made from a thread, make sure we don't corrupt the log
  50. Array<LogEntry> mLog;
  51. };