-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnow.cpp
More file actions
92 lines (69 loc) · 1.59 KB
/
Snow.cpp
File metadata and controls
92 lines (69 loc) · 1.59 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
#include "../Include/defGameEngine.hpp"
class Snow : public def::GameEngine
{
public:
Snow()
{
GetWindow()->SetTitle("Snow");
}
private:
struct Flake
{
def::Vector2f pos;
float time = 0.0f;
float speed = 1.0f;
};
std::list<Flake> flakes;
int flakeRadius = 4;
float speedDiff = 0.0f;
private:
float rnd(float min, float max)
{
return (float)rand() / (float)RAND_MAX * (max - min) + min;
}
protected:
bool OnUserCreate() override
{
srand(time(nullptr));
flakes.resize(1024);
for (auto& f : flakes)
{
f.pos = { rnd(0.0f, GetWindow()->GetScreenWidth()), float(-flakeRadius) };
f.speed = rnd(100.0f, 200.0f);
}
return true;
}
bool OnUserUpdate(float deltaTime) override
{
if (GetInput()->GetKeyState(def::Key::UP).held)
speedDiff += deltaTime * 100.0f;
if (GetInput()->GetKeyState(def::Key::DOWN).held)
speedDiff -= deltaTime * 100.0f;
speedDiff = std::max(0.0f, speedDiff);
for (auto& f : flakes)
{
f.time += deltaTime;
f.pos.y = f.time * (f.speed + speedDiff);
if (f.pos.y >= GetWindow()->GetScreenHeight() + flakeRadius)
{
f.pos = { rnd(-speedDiff / 10.0f, GetWindow()->GetScreenWidth()), float(-flakeRadius) };
f.time = 0.0f;
}
}
Clear(def::BLACK);
for (auto& f : flakes)
{
f.pos.x += deltaTime * speedDiff / 10.0f;
float c = abs(1.0f - 1.0f / f.speed * 200.0f);
FillCircle(f.pos + def::Vector2f(f.pos.x + cos(f.time * f.speed * 0.1f), 0.0f), (float)flakeRadius * c, def::Pixel::Float(c, c, c));
}
return true;
}
};
int main()
{
Snow app;
app.Construct(1280, 720, 1, 1);
app.Run();
return 0;
}