-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstars_effect.cpp
More file actions
50 lines (40 loc) · 1.31 KB
/
stars_effect.cpp
File metadata and controls
50 lines (40 loc) · 1.31 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
#include "stars_effect.hpp"
#include <algorithm>
#include <cmath>
#include "color_utils.hpp"
float show_time = 1.7f;
StarsEffect::StarsEffect(std::shared_ptr<ColorMaker> color_maker, const Rect& box, int num_stars)
: m_color_maker(color_maker), m_box(box)
{
for (int i=0; i<num_stars; i++)
m_stars.push_back(Star());
}
float fac = 0.5;
float t(float x)
{
if (x <= show_time / 2.0f)
return std::pow(x, fac) / std::pow(show_time / 2, fac);
if (x > show_time / 2.0f)
return std::pow(show_time / 2 - (x - show_time / 2), fac) / std::pow(show_time / 2, fac);
}
void StarsEffect::fill(EffectBuffer& buffer, const EffectState& state) {
int started = 0;
for (auto& star: m_stars) {
float elapsed = state.time - star.m_time;
if (elapsed < 0) {
continue;
}
if (elapsed > show_time) {
if (started > 2) {
continue;
}
star.m_time = state.time + (rand() % 800) / 100.0f;
star.m_x = (rand() % int(m_box.width())) + m_box.top_left.x;
star.m_y = (rand() % int(m_box.height())) + m_box.top_left.y;
star.m_color = m_color_maker->make(state);
started ++;
continue;
}
buffer.set(star.m_x, star.m_y, star.m_color * t(elapsed));
}
}