-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircle_effect.cpp
175 lines (137 loc) · 5.57 KB
/
circle_effect.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
#include "circle_effect.hpp"
#include "color_utils.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <functional>
using DrawPixel = std::function<void(int x, int y, const Color3& color)>;
static inline void draw_circle(const DrawPixel& draw, const Color3& color, const Point& center, const float& radius, const bool fill)
{
const int x_min = std::floor(center.x - radius) - 1;
const int x_max = std::ceil(center.x + radius) + 1;
const int y_min = std::max(0, int(std::floor(center.y - radius) - 1));
const int y_max = std::min(EffectBuffer::height(), int(std::ceil(center.y + radius) + 1));
for (int y = y_min; y < y_max; ++y) {
float b = float(y) - center.y;
for (int x = x_min; x < x_max; ++x) {
float a = float(x) - center.x;
float d = std::sqrt(a*a + b*b) - radius;
if (fill) {
if (d < 1.0) {
float coeff = float(std::min(1.0f, 1.0f - d));
draw(fmod2(x, EffectBuffer::Width), y, color * coeff);
}
} else {
if (d < 1.0 && d > -1.5) {
float coeff = (d > 0 ? 1.0f - d : 1.0f + d);
draw(fmod2(x, EffectBuffer::Width), y, color * float(std::min(1.0f, coeff)));
}
}
}
}
}
CircleEffect::CircleEffect(const Point& center, const float radius, const Color3& color, const bool fill)
: m_center(center), m_radius(radius), m_color(color), m_fill(fill)
{}
void CircleEffect::fill(EffectBuffer& buffer, const EffectState& state)
{
(void) state;
using namespace std::placeholders;
draw_circle(std::bind(&EffectBuffer::set, &buffer, _1, _2, _3), m_color, m_center, m_radius, m_fill);
}
ExtendingCircleEffect::ExtendingCircleEffect(float radius, const Color3& color, float duration, double time)
: m_radius(radius), m_duration(duration), m_start(time-duration), m_color(color), m_circle(Point(0, 0), 0, color, false)
{}
void ExtendingCircleEffect::fill(EffectBuffer& buffer, const EffectState& state)
{
double elapsed = state.time - m_start;
if (elapsed < 0) {
return;
}
if (elapsed > m_duration) {
m_start = state.time + ((rand() % 500) / 100.0f);
float x = rand() % buffer.width();
float y = rand() % buffer.height();
m_circle.set_center(Point(x, y));
m_color = HSVtoRGB(Color3(rand() / (float)RAND_MAX, 1.0f, 1.0f));
return;
}
float half_duration = m_duration / 2;
float progress = (half_duration - std::abs(elapsed - half_duration)) / half_duration;
float radius = pow(progress, 0.25f) * m_radius;
float sqrt_progress = std::pow(progress, 0.3);
if (radius < 1.1f) {
return;
}
m_circle.set_color(m_color * progress);
m_circle.set_radius(radius * sqrt_progress);
m_circle.fill(buffer, state);
}
ExplodingCircleEffect::ExplodingCircleEffect(float radius, float duration, double time)
: m_radius(radius), m_duration(duration), m_start(time - duration), m_circle(Point(0, 0), 0, Color3(1, 1, 1), true)
{}
void ExplodingCircleEffect::fill(EffectBuffer& buffer, const EffectState& state)
{
double elapsed = state.time - m_start;
if (elapsed <= 0.0f) {
return;
}
if (elapsed > m_duration) {
m_start = state.time + ((rand() % 400) / 100.0f);
float x = rand() % buffer.width();
float y = rand() % buffer.height();
m_circle.set_center(Point(x, y));
return;
}
float progress = elapsed / m_duration;
float progress_squared = progress*progress;
float inverse_progress = 1.0f - progress;
float inverse_squared_progress = 1.0f - progress_squared;
m_circle.set_radius(std::pow(progress, 0.3) * m_radius);
m_circle.set_color(Color3(inverse_squared_progress,
inverse_progress * inverse_squared_progress * .5,
std::max(0.0f, 1.0f - (progress * 5.0f)) * inverse_squared_progress * .3));
m_circle.fill(buffer, state);
}
DisolvingCircleFieldEffect::DisolvingCircleFieldEffect(unsigned int count)
: m_count(count)
{
for (unsigned i = 0; i < count; ++i) {
circles.emplace_back();
}
}
void DisolvingCircleFieldEffect::fill(EffectBuffer& buffer, const EffectState& state)
{
for (auto& cd : circles) {
double elapsed = state.time - cd.m_start;
if (elapsed < 0.0) {
continue;
}
if (elapsed > cd.m_lenghth) {
if (0.0 != cd.m_start) {
occupied.set( cd.m_center.y * buffer.width() + cd.m_center.x , false);
}
cd.m_start = state.time + 1.0 + 3.0 * double(rand()) / double(RAND_MAX) * 0.3;
cd.m_lenghth = (rand() % 3) + 2;
cd.m_radius = (rand() % 4) + 2;
cd.m_color = HSVtoRGB(Color3(rand() / (float)RAND_MAX, 1.0f, 1.0f));
unsigned int pos;
do {
pos = rand() % (EffectBuffer::Height * EffectBuffer::Width);
} while (occupied[pos]);
occupied.set(pos);
unsigned int x = pos % EffectBuffer::Width;
unsigned int y = (pos - x) / EffectBuffer::Width;
cd.m_center = Point(x, y);
continue;
}
float progress(elapsed / cd.m_lenghth);
float radius = -1.0 * std::pow(progress - 1.0f, 2) + 1.0f;
draw_circle(
[&](int x, int y, const Color3& color) {
int pos = y * EffectBuffer::Width + x;
buffer[pos] += color * float(1.0f - std::pow(progress, 2));
},
cd.m_color, cd.m_center, cd.m_radius * radius, true);
}
}