R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
InputBuffer Class Reference

Buffer for storing player input history. More...

#include <InputBuffer.hpp>

Collaboration diagram for InputBuffer:
Collaboration graph

Classes

struct  StoredInput
 Structure representing a stored input with metadata. More...
 

Public Member Functions

 InputBuffer ()=default
 Default constructor.
 
 ~InputBuffer ()=default
 Default destructor.
 
void addInput (uint32_t frameNumber, InputAction action, InputState state)
 Add an input to the buffer.
 
std::vector< StoredInputgetInputsSince (uint32_t startFrame) const
 Get all inputs since a given frame.
 
void clearUntil (uint32_t frameNumber)
 Clear inputs up to a given frame.
 
std::optional< StoredInputgetLastInput () const
 Get the last stored input.
 
void clear ()
 Clear the buffer completely.
 
size_t size () const
 Get the number of stored inputs.
 

Private Attributes

std::deque< StoredInput_inputs
 
uint32_t _oldestFrame = 0
 

Detailed Description

Buffer for storing player input history.

InputBuffer maintains a history of player inputs with their frame numbers. This history is essential for:

  • Client-side prediction
  • Server reconciliation
  • Input replay after server correction

Client prediction architecture:

  1. Player presses a key (frame N)
  2. Input stored in buffer
  3. Prediction applied immediately (responsive)
  4. Input sent to server
  5. Server responds with authoritative state (frame N-latency)
  6. Client compares and corrects if necessary
  7. Replay stored inputs from frame N

Definition at line 36 of file InputBuffer.hpp.

Constructor & Destructor Documentation

◆ InputBuffer()

InputBuffer::InputBuffer ( )
default

Default constructor.

◆ ~InputBuffer()

InputBuffer::~InputBuffer ( )
default

Default destructor.

Member Function Documentation

◆ addInput()

void InputBuffer::addInput ( uint32_t  frameNumber,
InputAction  action,
InputState  state 
)

Add an input to the buffer.

Stores a new input with its frame number for later replay. Inputs are maintained in chronological order.

Parameters
frameNumberFrame number where input was captured
actionPlayer action (MOVE_UP, SHOOT, etc.)
stateInput state (PRESSED, RELEASED, HELD)
Note
Inputs are automatically sorted by frameNumber

Definition at line 10 of file InputBuffer.cpp.

References _inputs, and _oldestFrame.

◆ clear()

void InputBuffer::clear ( )

Clear the buffer completely.

Removes all stored inputs. Used during disconnection or game reset.

Definition at line 59 of file InputBuffer.cpp.

References _inputs.

◆ clearUntil()

void InputBuffer::clearUntil ( uint32_t  frameNumber)

Clear inputs up to a given frame.

Removes inputs before a certain frame to save memory. Usually called after receiving a server ACK.

Parameters
frameNumberFrame up to which to clear (exclusive)
Note
Inputs from frameNumber onwards are kept
Allows limiting buffer size in production

Definition at line 40 of file InputBuffer.cpp.

References _inputs, and _oldestFrame.

◆ getInputsSince()

std::vector< InputBuffer::StoredInput > InputBuffer::getInputsSince ( uint32_t  startFrame) const

Get all inputs since a given frame.

Returns all stored inputs from a specific frame onwards. Used to replay inputs after a server correction.

Parameters
startFrameStarting frame number (inclusive)
Returns
Vector of inputs from startFrame to current frame
Note
Returned inputs are in chronological order
If startFrame is older than oldest input, returns from beginning

Definition at line 28 of file InputBuffer.cpp.

References _inputs.

◆ getLastInput()

std::optional< InputBuffer::StoredInput > InputBuffer::getLastInput ( ) const

Get the last stored input.

Returns
Optional containing the last input, or empty if buffer is empty
Note
Useful to know the last player state

Definition at line 52 of file InputBuffer.cpp.

References _inputs.

◆ size()

size_t InputBuffer::size ( ) const

Get the number of stored inputs.

Returns
Number of inputs in the buffer
Note
Useful for debugging and memory monitoring

Definition at line 63 of file InputBuffer.cpp.

References _inputs.

Member Data Documentation

◆ _inputs

std::deque<StoredInput> InputBuffer::_inputs
private

Definition at line 126 of file InputBuffer.hpp.

Referenced by addInput(), clear(), clearUntil(), getInputsSince(), getLastInput(), and size().

◆ _oldestFrame

uint32_t InputBuffer::_oldestFrame = 0
private

Definition at line 127 of file InputBuffer.hpp.

Referenced by addInput(), and clearUntil().


The documentation for this class was generated from the following files: