-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGroThread.cpp
262 lines (188 loc) · 5.35 KB
/
GroThread.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/////////////////////////////////////////////////////////////////////////////////////////
//
// gro is protected by the UW OPEN SOURCE LICENSE, which is summarized here.
// Please see the file LICENSE.txt for the complete license.
//
// THE SOFTWARE (AS DEFINED BELOW) AND HARDWARE DESIGNS (AS DEFINED BELOW) IS PROVIDED
// UNDER THE TERMS OF THIS OPEN SOURCE LICENSE (“LICENSE”). THE SOFTWARE IS PROTECTED
// BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THIS SOFTWARE OTHER THAN AS
// AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
//
// BY EXERCISING ANY RIGHTS TO THE SOFTWARE AND/OR HARDWARE PROVIDED HERE, YOU ACCEPT AND
// AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE
// CONSIDERED A CONTRACT, THE UNIVERSITY OF WASHINGTON (“UW”) GRANTS YOU THE RIGHTS
// CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
//
// TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
//
//
#include <QtGui>
#include "GroThread.h"
#define RESIZE \
middle = QSize(size.width()/2,size.height()/2); \
delete image; \
image = new QImage(size, QImage::Format_RGB32);
#define RENDER if ( world ) { \
GroPainter painter(size,image); \
world->render ( &painter ); \
emit renderedImage(*image); }
#define CHANGE_STATE(__state__) state = __state__; emit stateChange();
GroThread::GroThread(QObject *parent)
: QThread(parent),
state(NO_PROGRAM),
size(800,800),
zoom(1.0),
middle(400,400),
resized(false),
world(NULL)
{
abort = false;
image = new QImage(size, QImage::Format_RGB32);
}
GroThread::~GroThread()
{
mutex.lock();
abort = true;
condition.wakeOne();
mutex.unlock();
wait();
delete image;
//delete world;
}
void GroThread::start_interpreter(void)
{
QMutexLocker locker(&mutex);
if ( world != NULL && state == READY ) {
CHANGE_STATE(RUNNING);
if ( !isRunning() ) {
//start ( TimeCriticalPriority ); // this worked for mac, but made things slow on win
start();
abort = false;
} else {
condition.wakeOne();
}
}
}
void GroThread::stop_interpreter(void)
{
QMutexLocker locker(&mutex);
abort = true;
}
void GroThread::run()
{
timer.start();
forever {
if ( abort || world->get_stop_flag() ) {
mutex.lock();
CHANGE_STATE(READY);
mutex.unlock();
RENDER;
return;
}
try {
world->update();
}
catch ( std::string err ) {
mutex.lock();
error_string = err;
CHANGE_STATE(DEAD);
mutex.unlock();
return;
}
if (resized) {
mutex.lock();
resized = false;
RESIZE;
mutex.unlock();
}
if ( timer.elapsed() > 33 ) {
RENDER;
timer.start();
}
fflush(stdout);
}
}
bool GroThread::parse ( const char * path ) {
//if ( world )
// delete world;
world = new World(this);
register_gro_functions();
current_program = new gro_Program ( path, 0, NULL ); // creates a program object
world->set_program ( current_program ); // sets the world's program pointer
try {
world->init(); // sets up chipmunk and attempts to parse the program
}
catch(std::string err) { // checks for parse errors
error_string = err;
delete world;
world = NULL;
CHANGE_STATE(NO_PROGRAM);
return false;
}
CHANGE_STATE(READY); // parsing successful!
RESIZE;
RENDER;
return true;
}
void GroThread::resize ( QSize s ) {
QMutexLocker locker(&mutex);
resized = true;
size = s;
if ( world && !isRunning() ) {
RESIZE;
RENDER;
}
}
void GroThread::step ( void ) {
if ( world != NULL && state == READY ) {
try {
world->update();
}
catch ( std::string err ) {
mutex.lock();
error_string = err;
CHANGE_STATE(DEAD);
mutex.unlock();
return;
}
RESIZE;
RENDER;
}
}
bool GroThread::snapshot ( QString pathname ) {
return image->save(pathname);
}
void GroThread::moreCells ( void ) {
if ( has_valid_world() ) {
world->set_param ( "population_max", world->get_param ( "population_max" ) + 100 );
}
}
void GroThread::select ( QPoint a, QPoint b ) {
if ( has_valid_world() ) {
world->deselect_all_cells();
world->select_cells(a.x()-middle.width(),a.y()-middle.height(),b.x()-middle.width(),b.y()-middle.height());
if ( !isRunning() ) {
RENDER;
}
}
}
void GroThread::set_zoom ( float z ) {
QMutexLocker locker(&mutex);
resized = true;
zoom = z;
if ( world ) world->set_zoom ( z );
if ( world && !isRunning() ) {
RESIZE;
RENDER;
}
}
void GroThread::dump ( QString filename ) {
QMutexLocker locker(&mutex);
if ( has_valid_world() ) {
FILE * fp = fopen ( filename.toLatin1(), "w" );
if ( fp ) {
world->dump(fp);
}
fclose ( fp );
}
}