R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
InputBuffer.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by samuelBleau on 26/11/2025.
4** File description:
5** InputBuffer.cpp
6*/
7
8#include "InputBuffer.hpp"
9
10void InputBuffer::addInput(uint32_t frameNumber, InputAction action, InputState state) {
11 if (_inputs.empty() || frameNumber >= _oldestFrame) {
12 _inputs.push_back({frameNumber, action, state});
13 if (_inputs.size() == 1) {
14 _oldestFrame = frameNumber;
15 }
16 } else {
17 auto it = _inputs.begin();
18 while (it != _inputs.end() && it->frameNumber < frameNumber) {
19 ++it;
20 }
21 _inputs.insert(it, {frameNumber, action, state});
22 if (frameNumber < _oldestFrame) {
23 _oldestFrame = frameNumber;
24 }
25 }
26}
27
28std::vector<InputBuffer::StoredInput> InputBuffer::getInputsSince(uint32_t startFrame) const {
29 std::vector<StoredInput> result;
30
31 for (const auto &input : _inputs) {
32 if (input.frameNumber >= startFrame) {
33 result.push_back(input);
34 }
35 }
36
37 return result;
38}
39
40void InputBuffer::clearUntil(uint32_t frameNumber) {
41 while (!_inputs.empty() && _inputs.front().frameNumber < frameNumber) {
42 _inputs.pop_front();
43 }
44
45 if (!_inputs.empty()) {
46 _oldestFrame = _inputs.front().frameNumber;
47 } else {
48 _oldestFrame = frameNumber;
49 }
50}
51
52std::optional<InputBuffer::StoredInput> InputBuffer::getLastInput() const {
53 if (_inputs.empty()) {
54 return std::nullopt;
55 }
56 return _inputs.back();
57}
58
60 _inputs.clear();
61}
62
63size_t InputBuffer::size() const {
64 return _inputs.size();
65}
InputAction
Available player input actions.
InputState
State of an input action.
void clear()
Clear the buffer completely.
uint32_t _oldestFrame
std::deque< StoredInput > _inputs
void addInput(uint32_t frameNumber, InputAction action, InputState state)
Add an input to the buffer.
void clearUntil(uint32_t frameNumber)
Clear inputs up to a given frame.
std::optional< StoredInput > getLastInput() const
Get the last stored input.
std::vector< StoredInput > getInputsSince(uint32_t startFrame) const
Get all inputs since a given frame.
size_t size() const
Get the number of stored inputs.