Double Buffering for Game objects, what's a nice clean generic C++ way?
- by Gary
This is in C++.
So, I'm starting from scratch writing a game engine for fun and learning from the ground up. One of the ideas I want to implement is to have game object state (a struct) be double-buffered. For instance, I can have subsystems updating the new game object data while a render thread is rendering from the old data by guaranteeing there is a consistent state stored within the game object (the data from last time). After rendering of old and updating of new is finished, I can swap buffers and do it again.
Question is, what's a good forward-looking and generic OOP way to expose this to my classes while trying to hide implementation details as much as possible? Would like to know your thoughts and considerations.
I was thinking operator overloading could be used, but how do I overload assign for a templated class's member within my buffer class?
for instance, I think this is an example of what I want:
doublebuffer<Vector3> data;
data.x=5; //would write to the member x within the new buffer
int a=data.x; //would read from the old buffer's x member
data.x+=1; //I guess this shouldn't be allowed
If this is possible, I could choose to enable or disable double-buffering structs without changing much code.
This is what I was considering:
template <class T>
class doublebuffer{
T T1;
T T2;
T * current=T1;
T * old=T2;
public:
doublebuffer();
~doublebuffer();
void swap();
operator=()?...
};
and a game object would be like this:
struct MyObjectData{
int x;
float afloat;
}
class MyObject: public Node {
doublebuffer<MyObjectData> data;
functions...
}
What I have right now is functions that return pointers to the old and new buffer, and I guess any classes that use them have to be aware of this. Is there a better way?