SDEngine
Game Engine
Loading...
Searching...
No Matches
EventManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <variant>
5
6#include "EventVariant.hpp"
8#include "SD/export.hpp"
9
10namespace sd {
11
14 m_arena = arena_alloc(
15 ArenaParams{.reserve_size = mb(1uz), .commit_size = kb(4uz), .name = "EventManagerArena"});
16 }
18 if (m_arena)
19 arena_release(m_arena);
20 }
21
22 EventManager(const EventManager&) = delete;
26
27 [[nodiscard]] bool has_resize_event() const {
28 return std::ranges::any_of(m_events, [](const auto& e) {
29 return std::holds_alternative<WindowResizeEvent>(e.event) ||
30 std::holds_alternative<SwapchainOutOfDateEvent>(e.event);
31 });
32 }
33
34 template<typename T>
35 [[nodiscard]] bool has_event() const {
36 return std::ranges::any_of(m_events,
37 [](const auto& e) { return std::holds_alternative<T>(e.event); });
38 }
39
40 template<typename T>
41 void clear_type() {
42 U64 write = 0;
43 for (U64 i = 0; i < m_events.count; ++i) {
44 if (!std::holds_alternative<T>(m_events.data[i].event)) {
45 if (write != i)
46 m_events.data[write] = m_events.data[i];
47 ++write;
48 }
49 }
50 m_events.count = write;
51 }
52
53 void clear() {
54 m_events.clear();
55 m_arena->clear();
56 }
57
58 template<typename T, typename... Args>
59 void push_event(Args&&... args) {
60 m_events.push(m_arena, EventVariant{.event = T(std::forward<Args>(args)...), .handled = false});
61 }
62
63 auto begin() { return m_events.begin(); }
64 auto end() { return m_events.end(); }
65
66
68 Arena* m_arena = nullptr;
69};
70
71} // namespace sd
SD_EXPORT Arena * arena_alloc(ArenaParams params={})
Definition arena.cpp:91
consteval U64 mb(U64 n)
Definition arena.hpp:19
consteval U64 kb(U64 n)
Definition arena.hpp:16
SD_EXPORT void arena_release(Arena *arena)
Definition arena.cpp:142
Definition Application.hpp:22
Definition arena.hpp:67
U64 reserve_size
Definition arena.hpp:69
Definition arena_vec.hpp:6
Definition arena.hpp:85
Definition EventManager.hpp:12
EventManager & operator=(const EventManager &)=delete
auto end()
Definition EventManager.hpp:64
void clear_type()
Definition EventManager.hpp:41
void push_event(Args &&... args)
Definition EventManager.hpp:59
bool has_event() const
Definition EventManager.hpp:35
void clear()
Definition EventManager.hpp:53
EventManager(EventManager &&)=default
EventManager()
Definition EventManager.hpp:13
EventManager(const EventManager &)=delete
~EventManager()
Definition EventManager.hpp:17
EventManager & operator=(EventManager &&)=default
ArenaVec< EventVariant > m_events
Definition EventManager.hpp:67
bool has_resize_event() const
Definition EventManager.hpp:27
auto begin()
Definition EventManager.hpp:63
Definition EventVariant.hpp:19
std::variant< KeyPressedEvent, KeyReleasedEvent, KeyTypedEvent, MousePressedEvent, MouseReleasedEvent, MouseMovedEvent, MouseScrolledEvent, WindowResizeEvent, WindowCloseEvent, SwapchainOutOfDateEvent, AppTerminateEvent > event
Definition EventVariant.hpp:36
consteval U64 type_id_of()
Definition type_id.hpp:6
std::uint64_t U64
Definition types.hpp:16