X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fgame%2Fcomponent.h;h=6406cb5743e3f417f0cb7f27a08c92e439aecef8;hb=c71b8c2151e097f435c8f76ffa123cc71f9d12ec;hp=6b49dd19e8c6e3f64da05ceb550616d1e2c31624;hpb=86057ab12bea9aaf40be2f1f2a0a3e64e94f0313;p=libs%2Fgame.git diff --git a/source/game/component.h b/source/game/component.h index 6b49dd1..6406cb5 100644 --- a/source/game/component.h +++ b/source/game/component.h @@ -2,6 +2,7 @@ #define MSP_GAME_COMPONENT_H_ #include +#include "accessguard.h" #include "handle.h" namespace Msp::Game { @@ -20,6 +21,51 @@ public: Handle get_entity() const { return entity; } }; +template +class BufferedComponent: public Component +{ +public: + using Data = T; + +protected: + T data[2]; + uint8_t read_index = 0; + uint8_t write_index = 0; + bool written = false; + + BufferedComponent(Handle e): Component(e) { } + + const T &read() const; + T &write(); + +public: + virtual void prepare_tick() { write_index = 1-read_index; written = false; } + virtual void commit_tick() { if(written) read_index = write_index; } +}; + +template +const T &BufferedComponent::read() const +{ +#ifdef DEBUG + AccessGuard::get_instance().check>(); +#endif + return data[read_index]; +} + +template +T &BufferedComponent::write() +{ +#ifdef DEBUG + AccessGuard::get_instance().check>(); +#endif + if(!written && write_index!=read_index) + { + data[write_index] = data[read_index]; + written = true; + } + return data[write_index]; +} + } // namespace Msp::Game #endif