forked from createskyblue/OpenT12
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bezier.cpp
154 lines (116 loc) · 2.87 KB
/
Bezier.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
#include "OpenT12.h"
//https://github.com/ArduboyCollection/arduboy-moire
const int screenWidth = 128;
const int screenHeight = 64;
const int numberOfLines = 20;
const int maxSpeed = 6;
/*
* structs that describe the scene
*/
struct Point {
int x;
int y;
};
struct Velocity {
int dx;
int dy;
};
struct End {
Point p;
Velocity v;
};
struct Line {
End e0;
End e1;
};
struct Scene {
Line lines[numberOfLines];
int indexOfHeadLine = numberOfLines - 1;
};
/*
* scene initialization
*/
static Point randomPoint() {
return { random(screenWidth), random(screenHeight) };
}
static int randomSpeed() {
return random(1, maxSpeed);
}
static Velocity randomVelocity() {
return { randomSpeed(), randomSpeed() };
}
static End randomEnd() {
return { randomPoint(), randomVelocity() };
}
static Line randomLine() {
return { randomEnd(), randomEnd() };
}
static Point PointZero = { 0, 0 };
static Velocity VelocityZero = { 0, 0 };
static End EndZero = { PointZero, VelocityZero };
static Line LineZero = { EndZero, EndZero };
/*
* functions to advance basic value types
*/
static End endByAdvancingEnd(End en) {
int ndx = en.v.dx;
int ndy = en.v.dy;
int nx = en.p.x + ndx;
int ny = en.p.y + ndy;
if (nx < 0 || nx > screenWidth) {
ndx = -1 * sign(ndx) * randomSpeed();
nx = en.p.x + ndx;
}
if (ny < 0 || ny > screenHeight) {
ndy = -1 * sign(ndy) * randomSpeed();
ny = en.p.y + ndy;
}
return {
{ nx, ny },
{ ndx, ndy }
};
}
static Line lineByAdvancingLine(Line line) {
return {
endByAdvancingEnd(line.e0),
endByAdvancingEnd(line.e1)
};
}
/*
* functions to operate on pointer to scene
*/
static void sceneInit(Scene* scene) {
for (int i = 0; i < numberOfLines; i++) {
scene->lines[i] = LineZero;
}
scene->lines[scene->indexOfHeadLine] = randomLine();
}
static Line* sceneLine(Scene* scene, int index) {
int j = (scene->indexOfHeadLine + 1 + index) % numberOfLines;
return &(scene->lines[j]);
}
static void sceneAdvance(Scene* scene) {
Line head = scene->lines[scene->indexOfHeadLine];
Line nextHead = lineByAdvancingLine(head);
scene->indexOfHeadLine = (scene->indexOfHeadLine + 1) % numberOfLines;
scene->lines[scene->indexOfHeadLine] = nextHead;
}
static void drawLine(Line* line, uint16_t color) {
Disp.drawLine(line->e0.p.x, line->e0.p.y, line->e1.p.x, line->e1.p.y);
}
void RunSleepLoop(void) {
static Scene scene;
static bool SleepScreenInit = false;
if (!SleepScreenInit) {
sceneInit(&scene);
SleepScreenInit = true;
}
if (!SleepEvent) return;
Line* tail = sceneLine(&scene, 0);
drawLine(tail, 0);
sceneAdvance(&scene);
for (int i = 0; i < numberOfLines; i++) {
Line* line = sceneLine(&scene, i);
drawLine(line, 1);
}
}