Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
EventQueue.h
Go to the documentation of this file.
1#pragma once
2
3#include "message.h"
4#include <vector>
5#include <array>
6#include <mutex>
7
8// EventQueue: ECS-first double-buffered event queue with domain routing
9// Events are written to writeEvents during frame N and become readable in readEvents during frame N+1
10// after BeginFrame() swaps the buffers.
12{
13public:
15 {
16 // Initialize domain index vectors for read/write buffers
17 for (int i = 0; i < static_cast<int>(EventDomain::All); ++i)
18 {
19 m_readDomainIdx[i].reserve(64);
20 m_writeDomainIdx[i].reserve(64);
21 }
22 m_readEvents.reserve(256);
23 m_writeEvents.reserve(256);
24 }
25
26 ~EventQueue() = default;
27
28 // Singleton accessors
30 {
31 static EventQueue instance;
32 return instance;
33 }
34 static EventQueue& Get() { return GetInstance(); }
35
36 // Push an event to the write buffer (called during frame N, visible frame N+1)
37 void Push(const Message& msg)
38 {
39 std::lock_guard<std::mutex> lock(m_writeMutex);
40
41 size_t idx = m_writeEvents.size();
42 m_writeEvents.push_back(msg);
43
44 // Route to domain-specific index lists
45 int domainIdx = static_cast<int>(msg.domain);
46 if (domainIdx >= 0 && domainIdx < static_cast<int>(EventDomain::All))
47 {
48 m_writeDomainIdx[domainIdx].push_back(idx);
49 }
50
51 // Also route to "All" domain for systems that need all events
53 }
54
55 // Swap read/write buffers - called once per frame at the start
57 {
58 std::lock_guard<std::mutex> lock(m_writeMutex);
59
60 // Swap event vectors
62 m_writeEvents.clear();
63
64 // Swap domain index vectors
65 for (int i = 0; i <= static_cast<int>(EventDomain::All); ++i)
66 {
68 m_writeDomainIdx[i].clear();
69 }
70 }
71
72 // Get all events from the read buffer (frame N-1 events)
73 const std::vector<Message>& GetEvents() const
74 {
75 return m_readEvents;
76 }
77
78 // Get events for a specific domain from the read buffer
79 // Returns indices into the main event vector
80 const std::vector<size_t>& GetDomainIndices(EventDomain domain) const
81 {
82 int idx = static_cast<int>(domain);
83 if (idx >= 0 && idx <= static_cast<int>(EventDomain::All))
84 {
85 return m_readDomainIdx[idx];
86 }
87 static std::vector<size_t> empty;
88 return empty;
89 }
90
91 // Helper: iterate domain events with callback
92 template<typename Func>
94 {
95 const auto& indices = GetDomainIndices(domain);
96 for (size_t idx : indices)
97 {
98 if (idx < m_readEvents.size())
99 {
101 }
102 }
103 }
104
105private:
106 // Double buffer: read buffer contains events from previous frame (N-1)
107 // write buffer accumulates events for current frame (N)
108 std::vector<Message> m_readEvents;
109 std::vector<Message> m_writeEvents;
110
111 // Domain routing: indices into the event vectors by domain
112 // Index corresponds to EventDomain enum value
113 std::array<std::vector<size_t>, static_cast<int>(EventDomain::All) + 1> m_readDomainIdx;
114 std::array<std::vector<size_t>, static_cast<int>(EventDomain::All) + 1> m_writeDomainIdx;
115
116 std::mutex m_writeMutex; // Protect write operations
117};
toVisit push_back(childId)
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::array< std::vector< size_t >, static_cast< int >(EventDomain::All)+1 > m_writeDomainIdx
Definition EventQueue.h:114
void Push(const Message &msg)
Definition EventQueue.h:37
std::vector< Message > m_readEvents
Definition EventQueue.h:108
std::vector< Message > m_writeEvents
Definition EventQueue.h:109
const std::vector< size_t > & GetDomainIndices(EventDomain domain) const
Definition EventQueue.h:80
std::array< std::vector< size_t >, static_cast< int >(EventDomain::All)+1 > m_readDomainIdx
Definition EventQueue.h:113
void ForEachDomainEvent(EventDomain domain, Func &&callback) const
Definition EventQueue.h:93
~EventQueue()=default
const std::vector< Message > & GetEvents() const
Definition EventQueue.h:73
std::mutex m_writeMutex
Definition EventQueue.h:116
static EventQueue & GetInstance()
Definition EventQueue.h:29
static EventQueue & Get()
Definition EventQueue.h:34
void BeginFrame()
Definition EventQueue.h:56
EventDomain