From e2479261c56c8d1a64b51ad4f084ad3c18505359 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 27 Jan 2019 10:10:33 +0500 Subject: GameState double-buffering --- src/GlobalState.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/GlobalState.cpp') diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp index 9675d4b..afa1a56 100644 --- a/src/GlobalState.cpp +++ b/src/GlobalState.cpp @@ -9,6 +9,7 @@ //Global game variables std::unique_ptr nc; std::unique_ptr gs; +std::shared_ptr gsReadOnly; std::unique_ptr render; bool isRunning; bool isPhysRunning; @@ -16,6 +17,7 @@ EventListener listener; bool isMoving[5] = { 0,0,0,0,0 }; std::thread threadPhys; State state; +std::mutex gsCopyMutex; void PhysExec(); @@ -194,6 +196,10 @@ void PhysExec() { listener.HandleAllEvents(); + gsCopyMutex.lock(); + gsReadOnly = std::make_shared(*gs.get()); + gsCopyMutex.unlock(); + timer.Update(); } } @@ -212,8 +218,9 @@ void GlobalState::Exec() { render.reset(); } -GameState *GlobalState::GetGameState() { - return gs.get(); +std::shared_ptr GlobalState::GetGameState() { + std::lock_guard guard(gsCopyMutex); + return gsReadOnly; } Render *GlobalState::GetRender() { -- cgit v1.2.3