-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphiccore.cpp
134 lines (116 loc) · 2.52 KB
/
graphiccore.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "graphiccore.h"
#include "core.h"
#ifdef ENABLE_CALC_TIME_MEASUREMENT
#include <QDebug>
#include <chrono>
#endif
GraphicConfiguration GraphicCore::config;
Running_Configuration GraphicCore::run_config;
StreetWidget* GraphicCore::street;
ChartWidget* GraphicCore::chart;
std::mutex GraphicCore::sync_mutex;
std::unique_ptr<std::thread> GraphicCore::run_thread;
std::atomic_bool GraphicCore::stop_thread;
bool GraphicCore::block_thread;
void GraphicCore::init()
{
street = nullptr;
chart = nullptr;
run_thread.reset();
}
void GraphicCore::init_gui(StreetWidget* street, ChartWidget* chart)
{
GraphicCore::street = street;
GraphicCore::chart = chart;
}
void GraphicCore::new_game()
{
stop();
Core::new_game();
street->update_data();
street->update();
add_chart_point();
}
void GraphicCore::reset()
{
stop();
Core::reset();
street->update();
}
void GraphicCore::start()
{
stop_thread = false;
if(!run_thread)
run_thread.reset(new std::thread([]()
{
while(!stop_thread)
{
{
std::lock_guard<std::mutex> lock(sync_mutex);
Core::step();
}
street->update_data();
emit street->start_update();
std::this_thread::sleep_for(std::chrono::milliseconds(config.get_delay()));
}
}));
}
void GraphicCore::stop()
{
stop_thread = true;
if(run_thread)
{
run_thread->join();
run_thread.reset();
}
}
void GraphicCore::step()
{
stop();
#ifdef ENABLE_CALC_TIME_MEASUREMENT
auto begin = std::chrono::high_resolution_clock::now();
#endif
sync_mutex.lock();
Core::step();
sync_mutex.unlock();
street->update_data();
street->update();
add_chart_point();
#ifdef ENABLE_CALC_TIME_MEASUREMENT
auto end = std::chrono::high_resolution_clock::now();
qInfo() << "calc: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
#endif
}
void GraphicCore::add_chart_point()
{
if(chart)
{
chart->set_slow_down_data(Core::get_slow_down_chance(), Core::get_avg_speed(), Core::get_time());
std::vector<std::size_t> speeds(Core::get_max_speed() + 1, 0);
for(std::size_t lane = 0; lane < Core::get_lanes(); ++lane)
{
for(std::size_t pos = 0; pos < Core::get_length(); ++pos)
{
if(!Core::is_car_at(pos, lane))
continue;
std::size_t speed = Core::get_speed(pos, lane);
if(speed > Core::get_max_speed())
continue;
++speeds[speed];
}
}
chart->set_car_speed_data(speeds);
}
}
bool GraphicCore::load(const std::string& file)
{
if(file == "")
;
return false;
}
bool GraphicCore::save(const std::string& file)
{
if(file == "")
;
return false;
}