-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
121 lines (98 loc) · 2.9 KB
/
widget.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
#include "test.h"
#include "widget.h"
#include "./ui_widget.h"
#include <cuda_runtime.h>
#include <QPainter>
#include <cstring>
#include <chrono>
#include <iostream>
int Widget::toIndex(int row, int col) {
if (row < 0) {
row += kHeight;
} else if (row >= kHeight) {
row -= kHeight;
}
if (col < 0) {
col += kWidth;
} else if (col >= kWidth) {
col -= kWidth;
}
return row*kWidth + col;
}
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
// Window properties
setMinimumSize(kWidth,kHeight);
// GPU grid memory
cudaMalloc(&dArray1_, kWidth*kHeight*sizeof(bool));
cudaMalloc(&dArray2_, kWidth*kHeight*sizeof(bool));
// CPU grid memory
array1_ = new bool[kWidth*kHeight];
// Create a QImage to be used with the QPainter for quick drawing
img_ = new QImage(kWidth, kHeight, QImage::Format_ARGB32);
initializeGame();
}
void Widget::initializeGame() {
// Fill array1_ with the initial conditions
// Set all to false
std::memset(array1_, 0, kWidth*kHeight);
// Set some to true to start something interesting
for (auto col : { kWidth/4, kWidth/2, 3*kWidth/4 }) {
for (int row=0; row<kHeight; ++row) {
array1_[toIndex(row,col)] = true;
}
}
for (auto row : { kHeight/4, kHeight/2, 3*kHeight/4 }) {
for (int col=0; col<kWidth; ++col) {
array1_[toIndex(row,col)] = true;
}
}
// Copy grid to GPU
cudaMemcpy(dArray1_, array1_, kWidth*kHeight*sizeof(bool), cudaMemcpyHostToDevice);
fillImage();
update();
}
void Widget::nextIteration() {
// Copy grid from GPU
cudaMemcpy(array1_, dArray1_, kWidth*kHeight*sizeof(bool), cudaMemcpyDeviceToHost);
// Update image data based on array of bools
fillImage();
// Update grid
cudaNextStep(dArray1_, dArray2_, kWidth, kHeight);
// Roll buffers
std::swap(dArray1_, dArray2_);
update();
}
void Widget::fillImage() {
// Always set image based on array1_
QRgb *data = reinterpret_cast<QRgb*>(img_->bits());
for (int row=0; row<kHeight; ++row) {
// Create a simple gradient from top to bottom
const int blueValue = 255 * static_cast<double>(row)/(kHeight+1);
for (int col=0; col<kWidth; ++col) {
const auto index = toIndex(row,col);
if (array1_[index]) {
// Create a simple gradient from left to right
const int redValue = 255 * static_cast<double>(col)/(kWidth+1);
// Cell is alove, color
data[index] = qRgb(redValue,255,255-blueValue);
} else {
// Cell is dead, set color as black
data[index] = qRgb(0,0,0);
}
}
}
}
void Widget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
QRectF source(0,0,img_->width(), img_->height());
QRectF target(0,0,img_->width(), img_->height());
painter.drawImage(target, *img_, source);
nextIteration();
}
Widget::~Widget() {
delete ui;
delete[] array1_;
cudaFree(dArray1_);
cudaFree(dArray2_);
}