This repository was archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (70 loc) · 2.06 KB
/
main.cpp
File metadata and controls
88 lines (70 loc) · 2.06 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
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
// Override base class with your custom functionality
class Starfield : public olc::PixelGameEngine
{
public:
Starfield()
{
sAppName = "Starfield";
}
const int nStars = 1000;
struct sStar
{
float fAngle = 0.0f;
float fDistance = 0.0f;
float fSpeed = 0.0f;
olc::Pixel col = olc::WHITE;
};
std::vector<sStar> vStars;
olc::vf2d vOrigin;
olc::Renderable rApple;
public:
float Random(float a, float b)
{
return (b - a) * (float(rand()) / float(RAND_MAX)) + a;
}
bool OnUserCreate() override
{
vStars.resize(nStars);
// Initial stars
for (auto& star : vStars)
{
star.fAngle = Random(0.0f, 2.0f * 3.14159f);
star.fSpeed = Random(10.0f, 100.0f);
star.fDistance = Random(20.0f, 200.0f);
float lum = Random (0.3f, 1.0f);
star.col = olc::PixelF(lum, lum, lum, 1.0f);
}
vOrigin = { float(ScreenWidth() / 2), float(ScreenHeight() / 2) };
rApple.Load("assets/apple.png");
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
Clear(olc::BLACK);
// Update Stars & Draw
for (auto& star : vStars)
{
star.fDistance += star.fSpeed * fElapsedTime * (star.fDistance / 100.0f);
if (star.fDistance > 200.0f)
{
star.fAngle = Random(0.0f, 2.0f * 3.14159f);
star.fSpeed = Random(10.0f, 100.0f);
star.fDistance = Random(20.0f, 200.0f);
float lum = Random (0.3f, 1.0f);
star.col = olc::PixelF(lum, lum, lum, 1.0f);
}
Draw(olc::vf2d(cos(star.fAngle), sin(star.fAngle)) * star.fDistance + vOrigin, star.col * (star.fDistance / 100.0f));
}
DrawDecal(GetMousePos(), rApple.Decal());
return true;
}
};
int main()
{
Starfield demo;
if (demo.Construct(256, 240, 4, 4))
demo.Start();
return 0;
}