diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-25 17:26:20 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-25 17:26:20 +0200 |
commit | b31d7a677481b4a09168c43d203bfd6d7badf577 (patch) | |
tree | 13550fa7f33d21aea8a7b03981707db2c08fc9a2 /Museum.cpp | |
parent | c9f5ac8722190efeb58fda1eec9e6160d5204127 (diff) |
implement mementos snapshots
Diffstat (limited to 'Museum.cpp')
-rw-r--r-- | Museum.cpp | 49 |
1 files changed, 43 insertions, 6 deletions
@@ -20,22 +20,39 @@ void Museum::update() { this->canvas.update(); this->collision.update(); this->tick++; + + unsigned long long next_snapshot = this->snapshot_ticks; + if (!this->history.empty()) next_snapshot += this->history.top().tick; + if (next_snapshot > this->tick) return; + this->history.push(this->save_snapshot()); + printf("saved snapshot at tick %llu\n", this->tick); } void Museum::skip_forward() { - this->jump += this->snapshot_ticks; + this->jump++; } - void Museum::skip_backward() { - + this->jump--; } void Museum::work() { while (this->working) { - // immediately process forward jumps, even if paused + // immediately process jumps, even if paused if (this->jump > 0) { - for (; this->jump != 0; this->jump--) - this->update(); + // forward jump + for (; this->jump != 0; this->jump--) { + printf("jumping forward %u ticks\n", this->snapshot_ticks); + 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; + printf("restoring snapshot from tick %llu\n", this->history.top().tick); + this->restore_snapshot(this->history.top()); + this->history.pop(); + } } // wait with regular update if paused @@ -48,3 +65,23 @@ void Museum::work() { } } +static void memories_concat(Memories & a, Memories b) { + a.insert(a.end(), make_move_iterator(b.begin()), make_move_iterator(b.end())); +} + +Museum::Snapshot Museum::save_snapshot() { + Snapshot snapshot = { + .tick = this->tick, + .memories = {}, + }; + memories_concat(snapshot.memories, this->canvas.save()); + memories_concat(snapshot.memories, this->people.save()); + return snapshot; +} + +void Museum::restore_snapshot(const Snapshot & snapshot) { + this->tick = snapshot.tick; + this->canvas.restore(snapshot.memories); + this->people.restore(snapshot.memories); +} + |