-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenglwidget.cpp
510 lines (451 loc) · 16.5 KB
/
openglwidget.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// © Copyright (c) 2018 SqYtCO
#include "openglwidget.h"
#include "core.h"
#include "graphiccore.h"
#include <QKeyEvent>
#include <mutex>
#include <cmath>
#ifdef ENABLE_DRAW_TIME_MEASUREMENT
#include <QDebug>
#include <chrono>
#endif
OpenGLWidget::OpenGLWidget(QWidget* parent) : QOpenGLWidget(parent), cell_size(GraphicCore::get_config()->get_cell_size()), move_x(0), move_y(0)
{
// if update has to be done from other thread
QObject::connect(this, &OpenGLWidget::start_update, this, &OpenGLWidget::full_update);
}
OpenGLWidget::~OpenGLWidget()
{
GraphicCore::wait_for_calculation();
GraphicCore::stop_step();
GraphicCore::stop_generating();
}
bool OpenGLWidget::eventFilter(QObject*, QEvent* event)
{
// handle filtered events
switch(event->type())
{
case QEvent::KeyPress:
keyPressEvent(static_cast<QKeyEvent*>(event));
break;
case QEvent::MouseButtonPress:
mousePressEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::MouseButtonRelease:
mouseReleaseEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::MouseMove:
mouseMoveEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::Wheel:
wheelEvent(static_cast<QWheelEvent*>(event));
break;
default:
QOpenGLWidget::event(event);
}
// pass event to parent
return false;
}
void OpenGLWidget::wheelEvent(QWheelEvent* event)
{
// mousewheel with CTRL: zoom in/out
if(event->modifiers() == Qt::ControlModifier)
{
double factor = (1 + (event->angleDelta().y() / 360. * 0.3 /* = zoom-speed factor */));
factor = (factor <= 0) ? 0.1 : factor;
if(static_cast<std::size_t>(cell_size * factor) <= 1)
cell_size = 1;
else if(static_cast<std::size_t>(cell_size * factor) == cell_size)
{
if(factor > 0)
++cell_size;
else
--cell_size;
}
else if(static_cast<std::size_t>(cell_size * factor) > 8 * GraphicCore::get_config()->get_cell_size())
cell_size = 8 * GraphicCore::get_config()->get_cell_size();
else
cell_size *= factor;
}
// mousewheel with no modifier: move
else if(event->modifiers() == Qt::NoModifier)
{
move_x += event->angleDelta().x() / 360. * 30;
move_y += event->angleDelta().y() / 360. * 30;
}
update();
}
void OpenGLWidget::mousePressEvent(QMouseEvent* event)
{
mouse_pressed = true;
// calc pos of cell under mouse pointer
std::size_t x = static_cast<std::size_t>(event->pos().x() - null_pos_x) / cell_size;
std::size_t y = static_cast<std::size_t>(event->pos().y() - null_pos_y) / cell_size;
// check boundaries
if(x < Core::get_size_x() && y < Core::get_size_y()
// check (event->pos() - null_pos) >= 0 to avoid rounding errors (0 <-> -cell_size)
&& (event->pos().x() - null_pos_x) >= 0 && (event->pos().y() - null_pos_y) >= 0
// check if cells are locked
&& (!GraphicCore::get_config()->get_lock_after_first_generating() || Core::get_generation() == 0)
// check if cells are greater than 1px
&& cell_size > 1)
{
// halt background calculation
GraphicCore::stop_step();
std::lock_guard<decltype(GraphicCore::get_mutex())> system_lock(GraphicCore::get_mutex());
if(event->buttons() & Qt::LeftButton)
// revive on left click if it is set, otherwise kill
Core::set_cell_state(x, y,
(GraphicCore::get_config()->get_left_button_alive_right_dead()) ? Alive : Dead);
else if(event->buttons() & Qt::RightButton)
// kill on right click if it is set, otherwise revive
Core::set_cell_state(x, y,
(GraphicCore::get_config()->get_left_button_alive_right_dead()) ? Dead : Alive);
}
previous_pos = event->pos();
emit cell_changed();
update();
}
void OpenGLWidget::mouseReleaseEvent(QMouseEvent*)
{
mouse_pressed = false;
GraphicCore::calc_next_generation();
emit cell_changed();
}
void OpenGLWidget::mouseMoveEvent(QMouseEvent* event)
{
if(mouse_pressed)
{
int dif_x = previous_pos.x() - event->pos().x();
int dif_y = previous_pos.y() - event->pos().y();
bool dif_x_active = static_cast<std::size_t>(std::abs(dif_x)) >= cell_size;
bool dif_y_active = static_cast<std::size_t>(std::abs(dif_y)) >= cell_size;
while(dif_x_active || dif_y_active)
{
dif_x_active = static_cast<std::size_t>(std::abs(dif_x)) >= cell_size;
dif_y_active = static_cast<std::size_t>(std::abs(dif_y)) >= cell_size;
if(dif_x_active)
{
if(dif_x > 0)
dif_x -= cell_size;
else
dif_x += cell_size;
}
if(dif_y_active)
{
if(dif_y > 0)
dif_y -= cell_size;
else
dif_y += cell_size;
}
QPoint temp(event->pos().x() + dif_x, event->pos().y() + dif_y);
// calc pos of cell under mouse pointer
std::size_t x = static_cast<std::size_t>(temp.x() - null_pos_x) / cell_size;
std::size_t y = static_cast<std::size_t>(temp.y() - null_pos_y) / cell_size;
if(x < Core::get_size_x() && y < Core::get_size_y()
&& (temp.x() - null_pos_x) >= 0 && (temp.y() - null_pos_y) >= 0
&& (!GraphicCore::get_config()->get_lock_after_first_generating() || Core::get_generation() == 0))
{
GraphicCore::stop_step();
std::lock_guard<decltype(GraphicCore::get_mutex())> system_lock(GraphicCore::get_mutex());
if(event->buttons() & Qt::LeftButton)
// revive on left click if it is set, otherwise kill
Core::set_cell_state(x, y, (GraphicCore::get_config()->get_left_button_alive_right_dead()) ? Alive : Dead);
else if(event->buttons() & Qt::RightButton)
// kill on right click if it is set, otherwise revive
Core::set_cell_state(x, y, (GraphicCore::get_config()->get_left_button_alive_right_dead()) ? Dead : Alive);
}
}
// calc pos of cell under mouse pointer
std::size_t x = static_cast<std::size_t>(event->pos().x() - null_pos_x) / cell_size;
std::size_t y = static_cast<std::size_t>(event->pos().y() - null_pos_y) / cell_size;
// check boundaries
if(x < Core::get_size_x() && y < Core::get_size_y()
// check (event->pos() - null_pos) >= 0 to avoid rounding errors (0 <-> -cell_size)
&& (event->pos().x() - null_pos_x) >= 0 && (event->pos().y() - null_pos_y) >= 0
// check if cells are locked
&& (!GraphicCore::get_config()->get_lock_after_first_generating() || Core::get_generation() == 0))
{
GraphicCore::stop_step();
std::lock_guard<decltype(GraphicCore::get_mutex())> system_lock(GraphicCore::get_mutex());
if(event->buttons() & Qt::LeftButton)
// revive on left click if it is set, otherwise kill
Core::set_cell_state(x, y, (GraphicCore::get_config()->get_left_button_alive_right_dead()) ? Alive : Dead);
else if(event->buttons() & Qt::RightButton)
// kill on right click if it is set, otherwise revive
Core::set_cell_state(x, y, (GraphicCore::get_config()->get_left_button_alive_right_dead()) ? Dead : Alive);
previous_pos = event->pos();
emit cell_changed();
update();
}
}
}
void OpenGLWidget::keyPressEvent(QKeyEvent* event)
{
// move: [up/down/left/right] arrow
// fast move: CTRL + [up/down/left/right] arrow
if(event->key() == Qt::Key_Up)
{
move_y += GraphicCore::get_config()->get_cell_size();
if(event->modifiers() == Qt::ControlModifier)
move_y += GraphicCore::get_config()->get_cell_size() * 10;
}
else if(event->key() == Qt::Key_Down)
{
move_y -= GraphicCore::get_config()->get_cell_size();
if(event->modifiers() == Qt::ControlModifier)
move_y -= GraphicCore::get_config()->get_cell_size() * 10;
}
else if(event->key() == Qt::Key_Left)
{
move_x += GraphicCore::get_config()->get_cell_size();
if(event->modifiers() == Qt::ControlModifier)
move_x += GraphicCore::get_config()->get_cell_size() * 10;
}
else if(event->key() == Qt::Key_Right)
{
move_x -= GraphicCore::get_config()->get_cell_size();
if(event->modifiers() == Qt::ControlModifier)
move_x -= GraphicCore::get_config()->get_cell_size() * 10;
}
// zoom: [+/-]
else if(event->key() == Qt::Key_Plus)
{
if(cell_size <= 10)
++cell_size;
else if(cell_size < 8 * GraphicCore::get_config()->get_cell_size())
cell_size *= 1.25;
}
else if(event->key() == Qt::Key_Minus)
{
if(cell_size <= 10 && cell_size > 1)
--cell_size;
else if(cell_size > 1)
cell_size *= 0.8;
}
// next generation: space
else if(event->key() == Qt::Key_Space)
{
if(GraphicCore::step_running())
GraphicCore::stop_step();
else
GraphicCore::step();
// avoid double repainting
return;
}
// start/stop autogenerating: r
else if(event->key() == Qt::Key_R)
{
// if autogenerating is running: stop
if(GraphicCore::generating_running())
GraphicCore::stop_generating();
// if autogenerating is stopped: start
else
{
GraphicCore::stop_step();
GraphicCore::start_generating();
// avoid double repainting
return;
}
}
// set all cells dead: a
// set all cells alive: CTRL + a
else if(event->key() == Qt::Key_A)
{
GraphicCore::stop_generating();
if(event->modifiers() == Qt::NoModifier)
GraphicCore::reset_cells(Dead);
else if(event->modifiers() == Qt::ControlModifier)
GraphicCore::reset_cells(Alive);
}
// new random cells: n
else if(event->key() == Qt::Key_N)
{
GraphicCore::new_system();
emit new_system_created();
}
// reset position: m
else if(event->key() == Qt::Key_M)
{
reset_movement();
}
else
return;
update();
}
void OpenGLWidget::initializeGL()
{
// setup OpenGL viewport and mode
glViewport(0, 0, width(), height());
glMatrixMode(GL_PROJECTION);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
void OpenGLWidget::paintGL()
{
std::lock_guard<decltype(GraphicCore::get_mutex())> system_lock(GraphicCore::get_mutex());
#ifdef ENABLE_DRAW_TIME_MEASUREMENT
// begin of time measuring
auto begin = std::chrono::high_resolution_clock::now();
#endif
// load matrix
glLoadIdentity();
// set 0/0 to top-left-corner
glOrtho(0, width(), height(), 0, -1, 1);
// set null_pos central (moved by move_x/move_y and scaled by scale-factor)
null_pos_x = move_x + (width() / 2) - static_cast<big_signed>((Core::get_size_x() / 2) * cell_size);
null_pos_y = move_y + (height() / 2) - static_cast<big_signed>((Core::get_size_y() / 2) * cell_size);
// expand if borders are visible
while(Core::get_config()->get_border_behavior() == Border_Behavior::Borderless &&
// check right border
((null_pos_x + static_cast<big_signed>(Core::get_size_x() * cell_size) - width()) <= 0 ||
// check lower border
(null_pos_y + static_cast<big_signed>(Core::get_size_y() * cell_size) - height()) <= 0 ||
// check left and upper borders
(null_pos_x) > 0 || (null_pos_y) > 0))
{
// if used algorithm does not support expanding, break to avoid infinite loop
if(!Core::expand())
break;
// update current null_pos
null_pos_x = move_x + (width() / 2) - static_cast<big_signed>((Core::get_size_x() / 2) * cell_size);
null_pos_y = move_y + (height() / 2) - static_cast<big_signed>((Core::get_size_y() / 2) * cell_size);
emit new_system_created();
}
// set background (+ dead cell) color
glClearColor(GraphicCore::get_config()->get_background_color().red() / 255.f,
GraphicCore::get_config()->get_background_color().green() / 255.f,
GraphicCore::get_config()->get_background_color().blue() / 255.f, 1);
draw_cells();
draw_grid();
#ifdef ENABLE_DRAW_TIME_MEASUREMENT
// give out measured time
auto end = std::chrono::high_resolution_clock::now();
qDebug() << "drawing: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "µs";
#endif
}
void OpenGLWidget::draw_cells()
{
// calc visible cells
std::size_t x_begin = static_cast<std::size_t>(-1 * null_pos_x) / cell_size;
std::size_t y_begin = static_cast<std::size_t>(-1 * null_pos_y) / cell_size;
std::size_t x_end = x_begin + static_cast<std::size_t>(width()) / cell_size + 3;
std::size_t y_end = y_begin + static_cast<std::size_t>(height()) / cell_size + 3;
// check boundaries
if(x_begin > Core::get_size_x())
x_begin = 0;
if(y_begin > Core::get_size_y())
y_begin = 0;
if(x_end <= x_begin || x_end > Core::get_size_x())
x_end = Core::get_size_x();
if(y_end <= y_begin || y_end > Core::get_size_y())
y_end = Core::get_size_y();
// draw dead cells as background (performance improvement)
glColor3ub(static_cast<GLubyte>(GraphicCore::get_config()->get_dead_color().red()),
static_cast<GLubyte>(GraphicCore::get_config()->get_dead_color().green()),
static_cast<GLubyte>(GraphicCore::get_config()->get_dead_color().blue()));
big_signed graphic_size_x = static_cast<big_signed>(Core::get_size_x() * cell_size);
big_signed graphic_size_y = static_cast<big_signed>(Core::get_size_y() * cell_size);
// if field is out of sight, don't draw any cell
if(null_pos_x > (width() + graphic_size_x) || null_pos_x < (-1 * graphic_size_x) ||
null_pos_y > (height() + graphic_size_y) || null_pos_y < (-1 * graphic_size_y))
return;
GLint real_null_pos_x = (null_pos_x < 0) ? (null_pos_x % static_cast<GLint>(cell_size)) : (null_pos_x > width()) ? width() : static_cast<GLint>(null_pos_x);
GLint real_null_pos_y = (null_pos_y < 0) ? (null_pos_y % static_cast<GLint>(cell_size)) : (null_pos_y > height()) ? height() : static_cast<GLint>(null_pos_y);
if(graphic_size_x + null_pos_x < width())
{
if(graphic_size_y + null_pos_y < height())
glRecti(real_null_pos_x, static_cast<GLint>(real_null_pos_y), static_cast<GLint>(null_pos_x + graphic_size_x), static_cast<GLint>(null_pos_y + graphic_size_y));
else
glRecti(real_null_pos_x, static_cast<GLint>(real_null_pos_y), static_cast<GLint>(null_pos_x + graphic_size_x), height());
}
else
{
if(graphic_size_y + null_pos_y < height())
glRecti(real_null_pos_x, real_null_pos_y, width(), static_cast<GLint>(null_pos_y + graphic_size_y));
else
glRecti(real_null_pos_x, real_null_pos_y, width(), height());
}
GLint x1 = real_null_pos_x, y1 = real_null_pos_y, x2, y2;
// draw line by line visible cells
for(std::size_t a = y_begin; a < y_end; ++a)
{
// y-coord of right lower corner
y2 = static_cast<GLint>((a - y_begin + 1) * cell_size) + real_null_pos_y;
for(std::size_t b = x_begin; b < x_end; ++b)
{
x2 = static_cast<GLint>((b - x_begin + 1) * cell_size) + real_null_pos_x;
// alive cells
if(Core::get_cell_state(b, a))
{
// if autogenerating is running, use only alive color
if(GraphicCore::generating_running() || Core::get_next_cell_state(b, a))
// alive cell color
glColor3ub(static_cast<GLubyte>(GraphicCore::get_config()->get_alive_color().red()),
static_cast<GLubyte>(GraphicCore::get_config()->get_alive_color().green()),
static_cast<GLubyte>(GraphicCore::get_config()->get_alive_color().blue()));
else
// dying cell color
glColor3ub(static_cast<GLubyte>(GraphicCore::get_config()->get_dying_color().red()),
static_cast<GLubyte>(GraphicCore::get_config()->get_dying_color().green()),
static_cast<GLubyte>(GraphicCore::get_config()->get_dying_color().blue()));
glRecti(x1, y1, x2, y2);
}
// dead but in next generation reviving cells; use only if autogenerating is not running
else if(!GraphicCore::generating_running() && Core::get_next_cell_state(b, a))
{
// reviving cell color
glColor3ub(static_cast<GLubyte>(GraphicCore::get_config()->get_reviving_color().red()),
static_cast<GLubyte>(GraphicCore::get_config()->get_reviving_color().green()),
static_cast<GLubyte>(GraphicCore::get_config()->get_reviving_color().blue()));
glRecti(x1, y1, x2, y2);
}
x1 = x2;
}
y1 = y2;
// start each row
x1 = real_null_pos_x;
}
}
void OpenGLWidget::draw_grid()
{
if(GraphicCore::get_config()->get_grid_active())
{
// actual null position
GLint real_null_pos_x = (null_pos_x < 0) ? (null_pos_x % static_cast<GLint>(cell_size)) : (null_pos_x > width()) ? width() : static_cast<GLint>(null_pos_x);
GLint real_null_pos_y = (null_pos_y < 0) ? (null_pos_y % static_cast<GLint>(cell_size)) : (null_pos_y > height()) ? height() : static_cast<GLint>(null_pos_y);
GLint x_end = width() + 3 * static_cast<GLint>(cell_size) + real_null_pos_x;
GLint y_end = height() + 3 * static_cast<GLint>(cell_size) + real_null_pos_y;
glColor3f(0.5, 0.5, 0.5); // fixed line color
glLineWidth(1.0); // fixed line width
for(GLint line_coord_x = real_null_pos_x, line_coord_y = real_null_pos_y; line_coord_x <= x_end || line_coord_y <= y_end; line_coord_x += cell_size, line_coord_y += cell_size)
{
if(line_coord_x <= x_end)
{
glBegin(GL_LINES);
glVertex2i(line_coord_x, real_null_pos_y);
glVertex2i(line_coord_x, y_end);
glEnd();
}
if(line_coord_y <= y_end)
{
glBegin(GL_LINES);
glVertex2i(real_null_pos_x, line_coord_y);
glVertex2i(x_end, line_coord_y);
glEnd();
}
}
}
}
void OpenGLWidget::full_update()
{
GraphicCore::update_generation_counter();
update();
}
void OpenGLWidget::reset_movement()
{
move_x = 0;
move_y = 0;
cell_size = GraphicCore::get_config()->get_cell_size();
update();
}