X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgame%2Fcomponent.h;h=e97b511a6ed658edbc9c221b2ca14a7ffa2927f7;hb=12c863fc1bc5456a4b3aceacc88904d76bd1d8bb;hp=6914c15282383a004b09117875e191dd56f6a6cd;hpb=248d62f7240d342982ade65a510be912b867fe49;p=libs%2Fgame.git diff --git a/source/game/component.h b/source/game/component.h index 6914c15..e97b511 100644 --- a/source/game/component.h +++ b/source/game/component.h @@ -2,13 +2,15 @@ #define MSP_GAME_COMPONENT_H_ #include +#include "accessguard.h" #include "handle.h" +#include "mspgame_api.h" namespace Msp::Game { class Entity; -class Component +class MSPGAME_API Component { protected: Handle entity; @@ -18,12 +20,53 @@ public: virtual ~Component() = default; 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(); - virtual void pre_tick() { } - virtual void tick(Time::TimeDelta) { } - virtual void post_tick() { } +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