From 11a1e4e6c416dc160d1864f12fdf104bdf799146 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 25 Oct 2024 19:14:53 +0200 Subject: fix silly bug --- CMakeLists.txt | 2 +- Museum.cpp | 36 ++++++++++++++++++++++-------------- Museum.h | 3 +++ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4173c7..630840f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.28) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) -set(CMAKE_BUILD_TYPE Debug) +# set(CMAKE_BUILD_TYPE Debug) find_package(SDL3 REQUIRED) find_package(cpr REQUIRED) diff --git a/Museum.cpp b/Museum.cpp index bf000e2..346acb7 100644 --- a/Museum.cpp +++ b/Museum.cpp @@ -29,30 +29,38 @@ void Museum::update() { } void Museum::skip_forward() { + lock_guard guard(this->jump_mutex); this->jump++; } void Museum::skip_backward() { + lock_guard guard(this->jump_mutex); this->paused = true; this->jump--; } +void Museum::process_jump() { + lock_guard guard(this->jump_mutex); + + if (this->jump > 0) { + // forward jump + for (; this->jump != 0; this->jump--) { + for (size_t i = 0; i < this->snapshot_ticks; i++) + this->update(); + } + } else if (this->jump < 0) { + // backward jump + for (; this->jump != 0; this->jump++) { + if (this->history.empty()) continue; + this->restore_snapshot(this->history.top()); + if (this->tick > 0) this->history.pop(); + } + } +} + void Museum::work() { while (this->working) { // immediately process jumps, even if paused - if (this->jump > 0) { - // forward jump - for (; this->jump != 0; this->jump--) { - for (size_t i = 0; i < this->snapshot_ticks; i++) - this->update(); - } - } else if (this->jump < 0) { - // backward jump - for (; this->jump != 0; this->jump++) { - if (this->history.empty()) continue; - this->restore_snapshot(this->history.top()); - if (this->tick > 0) this->history.pop(); - } - } + this->process_jump(); // wait with regular update if paused if (this->paused) continue; diff --git a/Museum.h b/Museum.h index bc5b590..445a8bf 100644 --- a/Museum.h +++ b/Museum.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "People.h" #include "Canvas.h" @@ -30,6 +31,8 @@ public: private: int jump = 0; + std::mutex jump_mutex; + void process_jump(); private: bool working = true; -- cgit v1.2.3