group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
InputRingBuffer.hpp
Go to the documentation of this file.
1
17
18#pragma once
19
21
22#include <array>
23#include <cstddef>
24#include <cstdint>
25
27{
28public:
32 static constexpr size_t k_capacity = 256;
33
34 struct Entry
35 {
36 uint32_t tick = 0;
38 bool valid = false;
39 };
40
42 void push(uint32_t tick, const InputSnapshot& input)
43 {
44 entries_[head_] = Entry{.tick = tick, .input = input, .valid = true};
45 head_ = (head_ + 1) % k_capacity;
46 if (count_ < k_capacity)
47 ++count_;
48 }
49
52 [[nodiscard]] const InputSnapshot* find(uint32_t tick) const
53 {
54 // Walk newest-to-oldest. Most replays start near the head so this
55 // is typically a 1-2 step search; worst case k_capacity steps.
56 for (size_t i = 0; i < count_; ++i) {
57 const size_t idx = (head_ + k_capacity - 1 - i) % k_capacity;
58 const Entry& e = entries_[idx];
59 if (!e.valid)
60 continue;
61 if (e.tick == tick)
62 return &e.input;
63 if (e.tick < tick)
64 break; // ring is monotonically increasing — no point searching further
65 }
66 return nullptr;
67 }
68
70 [[nodiscard]] size_t size() const noexcept { return count_; }
71
73 void clear() noexcept
74 {
75 head_ = 0;
76 count_ = 0;
77 for (auto& e : entries_)
78 e.valid = false;
79 }
80
81private:
82 std::array<Entry, k_capacity> entries_{};
83 size_t head_ = 0;
84 size_t count_ = 0;
85};
Per-tick player input snapshot for networking and prediction.
Definition InputRingBuffer.hpp:27
size_t head_
Next write index, wraps mod k_capacity.
Definition InputRingBuffer.hpp:83
std::array< Entry, k_capacity > entries_
Definition InputRingBuffer.hpp:82
static constexpr size_t k_capacity
Capacity in ticks.
Definition InputRingBuffer.hpp:32
size_t size() const noexcept
Number of valid entries currently buffered (≤ k_capacity).
Definition InputRingBuffer.hpp:70
size_t count_
Number of valid entries; saturates at k_capacity.
Definition InputRingBuffer.hpp:84
void push(uint32_t tick, const InputSnapshot &input)
Push the input that was just stamped + sent for tick.
Definition InputRingBuffer.hpp:42
void clear() noexcept
Drop everything (used on disconnect / reset).
Definition InputRingBuffer.hpp:73
const InputSnapshot * find(uint32_t tick) const
Find the input stamped with exactly tick.
Definition InputRingBuffer.hpp:52
Definition InputRingBuffer.hpp:35
bool valid
False for default-constructed slots that never got pushed.
Definition InputRingBuffer.hpp:38
InputSnapshot input
The input as sampled (and as sent to the server).
Definition InputRingBuffer.hpp:37
uint32_t tick
clientPredictTick when this input was stamped + sent.
Definition InputRingBuffer.hpp:36
One tick of player input, stamped with the tick it was sampled on.
Definition InputSnapshot.hpp:17