-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfire.cpp
More file actions
116 lines (96 loc) · 2.26 KB
/
fire.cpp
File metadata and controls
116 lines (96 loc) · 2.26 KB
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
#include "fire.h"
#include <QColor>
#define NCOLORS 256
typedef enum {
K_TYPE1 = 0,
K_TYPE2 = 1
} FireKind;
class Fire::Priv
{
public:
Priv () : kind(K_TYPE1) {
for (int i=0; i<NCOLORS; i++) {
QColor color;
color.setHsl(i/3, 255, qMin(255, i*2));
palette.push_back(color.rgb());
}
}
QVector<QRgb> palette;
FireKind kind;
};
Fire::Fire(int w, int h) : Effect (w, h)
{
d = new Priv();
reset();
}
Fire::~Fire()
{
delete d;
}
void Fire::create()
{
for (int i=0; i<w; i++)
setValue(i, h-1, static_cast<uchar>(abs(32768 + rand()) % 256));
}
void Fire::destroy()
{
for (int i=0; i<w; i++)
setValue(i, h-1, 0);
}
void Fire::update()
{
uchar v1, v2, v3, v4;
int new_value;
create();
for (int j=0; j<(h-1); j++) {
for (int i=0; i<w; i++) {
switch (d->kind) {
case K_TYPE1:
v2 = getValue(i, j+1);
v1 = i>0 ? getValue(i-1, j+1) : v2;
v3 = (i+1)<w ? getValue(i+1, j+1) : v2;
v4 = (j+2)<h ? getValue(i, j+2) : v2;
new_value = (v1+v2+v3+v4)*32/129;
break;
case K_TYPE2:
v1 = i>0 ? getValue(i-1, j+1) : getValue(i, j+1);
v2 = (j+2)<h ? getValue(i, j+2) : getValue(i, j+1);
v3 = (i+1)<w ? getValue(i+1, j+1) : getValue(i, j+1);
if ((j+3) < h)
v4 = getValue(i, j+3);
else if ((j+2) < h)
v4 = getValue(i, j+2);
else
v4 = getValue(i, j+1);
new_value = (v1+v2+v3+v4)*64/257;
break;
}
setValue(i, j, static_cast<uchar>(new_value));
}
}
}
int Fire::defaultRefreshRate()
{
return 30;
}
const QVector<QRgb>& Fire::palette() const
{
return d->palette;
}
QPair<int, QVector<QString>> Fire::fxKindList() const
{
QVector<QString> v;
v.append("Type 1");
v.append("Type 2");
return QPair<int, QVector<QString>>(d->kind, v);
}
void Fire::setFxKind(int kind)
{
switch (kind) {
case K_TYPE1:
case K_TYPE2:
reset();
d->kind = FireKind(kind);
break;
}
}