EventManager.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Common.h>
  7. #include <AnKi/Util/List.h>
  8. #include <AnKi/Math.h>
  9. namespace anki {
  10. /// @addtogroup scene
  11. /// @{
  12. /// This manager creates the events ands keeps track of them
  13. class EventManager
  14. {
  15. public:
  16. EventManager();
  17. ~EventManager();
  18. ANKI_USE_RESULT Error init(SceneGraph* scene);
  19. SceneGraph& getSceneGraph()
  20. {
  21. return *m_scene;
  22. }
  23. const SceneGraph& getSceneGraph() const
  24. {
  25. return *m_scene;
  26. }
  27. SceneAllocator<U8> getAllocator() const;
  28. SceneFrameAllocator<U8> getFrameAllocator() const;
  29. /// Create a new event
  30. /// @note It's thread-safe against itself.
  31. template<typename T, typename... Args>
  32. ANKI_USE_RESULT Error newEvent(T*& event, Args... args)
  33. {
  34. event = getAllocator().template newInstance<T>(this);
  35. Error err = event->init(std::forward<Args>(args)...);
  36. if(err)
  37. {
  38. getAllocator().deleteInstance(event);
  39. }
  40. else
  41. {
  42. LockGuard<Mutex> lock(m_mtx);
  43. m_events.pushBack(event);
  44. }
  45. return err;
  46. }
  47. /// Update
  48. ANKI_USE_RESULT Error updateAllEvents(Second prevUpdateTime, Second crntTime);
  49. /// Delete events that pending deletion
  50. void deleteEventsMarkedForDeletion(Bool fullCleanup);
  51. /// @note It's thread-safe against itself.
  52. void markEventForDeletion(Event* event);
  53. private:
  54. SceneGraph* m_scene = nullptr;
  55. IntrusiveList<Event> m_events;
  56. IntrusiveList<Event> m_eventsMarkedForDeletion;
  57. Mutex m_mtx;
  58. };
  59. /// @}
  60. } // end namespace anki