EventManager.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. {
  11. /// @addtogroup scene
  12. /// @{
  13. /// This manager creates the events ands keeps track of them
  14. class EventManager
  15. {
  16. public:
  17. EventManager();
  18. ~EventManager();
  19. ANKI_USE_RESULT Error init(SceneGraph* scene);
  20. SceneGraph& getSceneGraph()
  21. {
  22. return *m_scene;
  23. }
  24. const SceneGraph& getSceneGraph() const
  25. {
  26. return *m_scene;
  27. }
  28. SceneAllocator<U8> getAllocator() const;
  29. SceneFrameAllocator<U8> getFrameAllocator() const;
  30. /// Create a new event
  31. /// @note It's thread-safe against itself.
  32. template<typename T, typename... Args>
  33. ANKI_USE_RESULT Error newEvent(T*& event, Args... args)
  34. {
  35. event = getAllocator().template newInstance<T>(this);
  36. Error err = event->init(std::forward<Args>(args)...);
  37. if(err)
  38. {
  39. getAllocator().deleteInstance(event);
  40. }
  41. else
  42. {
  43. LockGuard<Mutex> lock(m_mtx);
  44. m_events.pushBack(event);
  45. }
  46. return err;
  47. }
  48. /// Update
  49. ANKI_USE_RESULT Error updateAllEvents(Second prevUpdateTime, Second crntTime);
  50. /// Delete events that pending deletion
  51. void deleteEventsMarkedForDeletion(Bool fullCleanup);
  52. /// @note It's thread-safe against itself.
  53. void markEventForDeletion(Event* event);
  54. private:
  55. SceneGraph* m_scene = nullptr;
  56. IntrusiveList<Event> m_events;
  57. IntrusiveList<Event> m_eventsMarkedForDeletion;
  58. Mutex m_mtx;
  59. };
  60. /// @}
  61. } // end namespace anki