aboutsummaryrefslogtreecommitdiff
path: root/Museum.cpp
blob: f6b8977bb6c34e48f95401ae2ba0e48d350dd60f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "Museum.h"

using namespace std;

Museum::Museum() : people(*this), canvas(*this) {
	this->worker = new std::thread(&Museum::work, this);
}

Museum::~Museum() {
	this->working = false;
	this->worker->join();
	if (this->worker != nullptr) {
		delete this->worker;
		this->worker = nullptr;
	}
}

void Museum::update() {
	this->people.update();
	this->canvas.update();
	this->tick++;
}

void Museum::skip_forward() {
	this->jump += this->snapshot_ticks;
}

void Museum::skip_backward() {

}

void Museum::work() {
	while (this->working) {
		// immediately process forward jumps, even if paused
		if (this->jump > 0) {
			while (--this->jump > 0)
				this->update();
		}

		// wait with regular update if paused
		if (this->paused) continue;

		// regular update
		auto next = chrono::steady_clock::now() + this->tick_interval;
		this->update();
		this_thread::sleep_until(next);
	}
}

void Museum::set_pause(bool paused) {
	this->paused = paused;
}