I'm working on overhauling some of the poorly thought out systems in my game. Right now I decided to work on the input system. What I thought I might do is have a buffer (an std::vector) full of abstract actions with some extra data and a timestamp. The player object will then process this buffer and execute the actions accordingly. The advantage of this system is twofold, firstly it abstracts the movement code from the input device (allowing multiple input devices) and second it makes it much easier to record or script inputs. So without further ado here is the interface I created:
#ifndef INPUT_H
#define INPUT_H
class input
{
public:
enum
{
DO_NOTHING = 0x00,
WALK_FORWARD,
WALK_BACKWARD,
WALK_RIGHT,
WALK_LEFT,
WALK_ANALOG,
SPRINT_ENABLE,
SPRINT_DISABLE,
JUMP,
SHOOT,
ZOOM_VIEW,
ROTATE_VIEW,
GRAB_OBJECT
};
input();
input(unsigned char i, vec d); //constructs a piece of input using an instruction and 3 floats of data, the timestamp is applied automatically
input(unsigned char i); //constructs a piece of input using an instruction, the timestamp is applied automatically
~input();
void fill(unsigned char i, vec d); //constructs a piece of input using an instruction and 3 floats of data, the timestamp is applied automatically
void fill(unsigned char i); //constructs a piece of input using an instruction, the timestamp is applied automatically
vec data;
unsigned char instruction;
unsigned long long timeStamp;
};
class inputBuffer
{
std::vector<input>* buffer;
public:
inputBuffer();
~inputBuffer();
void add(input a); //add some input to the bottom of the buffer
input readTop(); //read from the top of the buffer
input consumeTop(); //read from the top of the buffer and remove it from the buffer
void clear(); //clear the buffer
size_t size(); //get the number of actions in the buffer
};
#endif
Now most of the code is already implemented but I have a little trouble grasping two parts of it: the gathering of the data and what to do with the data at the end of each cycle. The problem with gathering the input is that currently I poll the input which is fine but it means the timestamp will only record when the input was polled rather than when the input event actually occurred. The second problem is that once the input gathering is done and it is time to execute the buffer do I execute the whole buffer or just part of it? Do I clear the buffer before gathering another cycle of input or do I just push back everything else in the buffer? Is anyone able offer advice to help me refine my design?