-
Notifications
You must be signed in to change notification settings - Fork 0
/
startrek_warp.cpp
249 lines (210 loc) · 6.18 KB
/
startrek_warp.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include <string>
class WarpEffect : public olc::PixelGameEngine
{
public:
WarpEffect()
{
sAppName = "Star Trek Next Generation Warp Effect";
}
const int nStars = 250 / 2;
//const int nStars = 1000;
struct sStar
{
float fAngle = 0.0f;
float fDistance = 0.0f; // distance away from the center of the screen
float fSpeed = 0.0f;
olc::Pixel col = olc::WHITE;
};
std::vector<sStar> vStars;
olc::vf2d vOrigin;
//int lineLength = 0;
//std::string algorithm = "";
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.1459f);
star.fSpeed = Random(50.0f, 180.0f);
star.fDistance = Random(10.0f, ScreenWidth() + 20.0f);
float lum = Random(0.3f, 1.0f);
star.col = olc::PixelF(lum, lum, lum, 1.0f);
}
vOrigin = { float(ScreenWidth() / 2), float(ScreenHeight() / 2) };
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); // * (star.fDistance / 100.0f) == slower for increased distance
if (star.fDistance > (ScreenWidth() + 20.0f)) // randomly generate a new position for the star (when crossing a trashold)
{
star.fAngle = Random(0.0f, 2.0f * 3.1459f);
star.fSpeed = Random(50.0f, 180.0f);
star.fDistance = Random(10.0f, ScreenWidth() + 20.0f);
float lum = Random(0.3f, 1.0f);
star.col = olc::PixelF(lum, lum, lum, 1.0f);
}
// " * (star.fDistance / 100.0f)" == non linearity (the star becomes darker the further away) [in principle]
//Draw(olc::vf2d(cos(star.fAngle), sin(star.fAngle)) * star.fDistance + vOrigin, star.col * (star.fDistance / 100.0f));
Draw(olc::vf2d(cos(star.fAngle) + 0.2f, sin(star.fAngle) + 0.2f) * star.fDistance + vOrigin, star.col * (star.fDistance / 100.0f));
DrawLineGradient(
olc::vf2d(cos(star.fAngle), sin(star.fAngle)) * star.fDistance + vOrigin,
olc::vf2d(cos(star.fAngle), sin(star.fAngle)) * (star.fDistance + (5.0f * 2.0f)) + vOrigin,
olc::BLUE * (star.fDistance / 50.0f),
olc::GREEN * (star.fDistance / 50.0f)
);
DrawLineGradient(
olc::vf2d(cos(star.fAngle), sin(star.fAngle)) * (star.fDistance + (5.0f * 2.0f)) + vOrigin,
olc::vf2d(cos(star.fAngle), sin(star.fAngle)) * (star.fDistance + (10.0f * 2.0f)) + vOrigin,
olc::YELLOW * (star.fDistance / 50.0f),
olc::RED * (star.fDistance / 50.0f)
);
}
DrawString(10, 10, "To boldly go...\nWhere no man has gone before.", olc::Pixel(34, 112, 192));
// FPS
//DrawString(10, 10, "FPS: " + std::to_string(GetFPS()));
/*
// TEST LINE DRAWING ALGORITHM
olc::vi2d mousePos = GetMousePos();
olc::vi2d middleScreen = { ScreenWidth() / 2, ScreenHeight() / 2 };
lineLength = (middleScreen - mousePos).mag();
DrawLineGradient(
middleScreen,
{ mousePos.x, mousePos.y },
olc::RED,
olc::WHITE
);
DrawString(middleScreen, middleScreen.str());
DrawString(mousePos, mousePos.str());
DrawString(10, 20, "Length: " + std::to_string(lineLength));
if (algorithm != "") DrawString(10, 30, "Algorithm: " + algorithm);
*/
return true;
}
void DrawLineGradient(const olc::vi2d& pos1, const olc::vi2d& pos2, olc::Pixel p1 = olc::WHITE, olc::Pixel p2 = olc::BLACK, uint32_t pattern = 0xFFFFFFFF)
{
DrawLineGradient(pos1.x, pos1.y, pos2.x, pos2.y, p1, p2, pattern);
}
void DrawLineGradient(int32_t x1, int32_t y1, int32_t x2, int32_t y2, olc::Pixel p1 = olc::WHITE, olc::Pixel p2 = olc::BLACK, uint32_t pattern = 0xFFFFFFFF)
{
int x, y, dx, dy, dx1, dy1, px, py, xe, ye, i;
dx = x2 - x1; dy = y2 - y1;
bool inverted = false; // changes from which color we are going to start interpolating
//algorithm = "";
auto rol = [&](void) { pattern = (pattern << 1) | (pattern >> 31); return pattern & 1; };
// straight lines idea by gurkanctn
if (dx == 0) // Line is vertical
{
if (y2 < y1) { std::swap(y1, y2); inverted = true; }
for (y = y1; y <= y2; y++)
if (rol())
{
float t = (float)(y - y1) / (float)(y2 - y1);
if (!inverted)
Draw(x1, y, olc::PixelLerp(p1, p2, t));
else
Draw(x1, y, olc::PixelLerp(p2, p1, t));
}
return;
}
if (dy == 0) // Line is horizontal
{
if (x2 < x1) { std::swap(x1, x2); inverted = true; }
for (x = x1; x <= x2; x++)
if (rol())
{
float t = (float)(x - x1) / (float)(x2 - x1);
if (!inverted)
Draw(x, y1, olc::PixelLerp(p1, p2, t));
else
Draw(x, y1, olc::PixelLerp(p2, p1, t));
}
return;
}
// Line is Funk-aye
dx1 = abs(dx); dy1 = abs(dy);
px = 2 * dy1 - dx1; py = 2 * dx1 - dy1;
if (dy1 <= dx1)
{
if (dx >= 0)
{
x = x1; y = y1; xe = x2;
}
else
{
x = x2; y = y2; xe = x1;
}
if (rol()) Draw(x, y, dx >= 0 ? p1 : p2);
for (i = 0; x < xe; i++)
{
x = x + 1;
if (px < 0)
px = px + 2 * dy1;
else
{
if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) y = y + 1; else y = y - 1;
px = px + 2 * (dy1 - dx1);
}
if (rol())
{
olc::vi2d src = { x1, y1 }, dst = { x2, y2 };
int d = (src - dst).mag();
float t = (float)abs(x - x1) / (float)d;
Draw(x, y, olc::PixelLerp(p1, p2, t));
//algorithm = "Funk-eye 2 (> dx hor)";
}
}
}
else
{
if (dy >= 0)
{
x = x1; y = y1; ye = y2;
}
else
{
x = x2; y = y2; ye = y1;
}
if (rol()) Draw(x, y, dy >= 0 ? p1 : p2);
for (i = 0; y < ye; i++)
{
y = y + 1;
if (py <= 0)
py = py + 2 * dx1;
else
{
if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0)) x = x + 1; else x = x - 1;
py = py + 2 * (dx1 - dy1);
}
if (rol())
{
olc::vi2d src = { x1, y1 }, dst = { x2, y2 };
int d = (src - dst).mag();
float t = (float)abs(y - y1) / (float)d;
Draw(x, y, olc::PixelLerp(p1, p2, t));
//algorithm = "Funk-eye 4 (> dy hor)";
}
}
}
}
};
int main()
{
WarpEffect demo;
if (demo.Construct(256, 240, 4, 4))
//if (demo.Construct(1920, 1080, 1, 1))
demo.Start();
return 0;
}