-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarField.cpp
More file actions
82 lines (62 loc) · 1.47 KB
/
StarField.cpp
File metadata and controls
82 lines (62 loc) · 1.47 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
#include "../Include/defGameEngine.hpp"
constexpr size_t STARS_COUNT = 200;
class Sample : public def::GameEngine
{
public:
Sample()
{
GetWindow()->SetTitle("Space");
}
struct Star
{
float angle;
float distance;
float speed;
def::Pixel col;
};
std::vector<Star> stars;
def::Vector2f origin;
float bound;
protected:
float RandFloat(float min, float max)
{
return min + (max - min) * ((float)rand() / (float)RAND_MAX);
}
void AddStar(size_t i)
{
stars[i].angle = RandFloat(0.0f, 2.0f * 3.14159f);
stars[i].speed = RandFloat(10.0f, 100.0f);
stars[i].distance = RandFloat(bound / 10.0f * 0.5f, bound * 0.5f);
float lum = RandFloat(0.5f, 1.0f);
stars[i].col = def::Pixel::Float(lum, lum, lum);
}
bool OnUserCreate() override
{
origin = GetWindow()->GetScreenSize() / 2;
bound = float(std::max(GetWindow()->GetScreenWidth(), GetWindow()->GetScreenHeight())) * 0.8f;
stars.resize(STARS_COUNT);
for (size_t i = 0; i < STARS_COUNT; i++)
AddStar(i);
return true;
}
bool OnUserUpdate(float deltaTime) override
{
Clear(def::BLACK);
for (size_t i = 0; i < STARS_COUNT; i++)
{
auto& s = stars[i];
s.distance += s.speed * deltaTime * (s.distance / (bound * 0.5f));
if (s.distance > bound)
AddStar(i);
Draw(origin + def::Vector2f(cos(s.angle), sin(s.angle)) * s.distance, s.col * (s.distance / bound * 0.5f));
}
return true;
}
};
int main()
{
Sample demo;
demo.Construct(256, 240, 4, 4);
demo.Run();
return 0;
}