-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
210 lines (163 loc) · 5.21 KB
/
main.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
/*
* Wormy game prototype
*/
#include <sifteo.h>
#include "assets.gen.h"
using namespace Sifteo;
static const unsigned gNumCubes = 1;
Random gRandom;
static AssetSlot MainSlot = AssetSlot::allocate()
.bootstrap(GameAssets);
static Metadata M = Metadata()
.title("Wormy")
.package("sucotronic.wormy", "0.5")
.icon(Icon)
.cubeRange(gNumCubes);
class StarDemo {
public:
static const unsigned numStars = 8;
static const float textSpeed = 0.2f;
static const float bgScrollSpeed = 10.0f;
static const float bgTiltSpeed = 1.0f;
static const float starEmitSpeed = 60.0f;
static const float starTiltSpeed = 3.0f;
void init(CubeID cube)
{
frame = 0;
bg.x = 0;
bg.y = 0;
vid.initMode(BG0_SPR_BG1);
vid.attach(cube);
for (unsigned i = 0; i < numStars; i++)
initStar(i);
// Our background is 18x18 to match BG0, and it seamlessly tiles
vid.bg0.image(vec(0,0), Background);
// Allocate 16x2 tiles on BG1 for text at the bottom of the screen
vid.bg1.setMask(BG1Mask::filled(vec(0,14), vec(16,2)));
}
void update(TimeDelta timeStep)
{
/*
* Action triggers
*/
switch (frame) {
/* case 0:
text.set(0, -20);
textTarget = text;
break;
case 100:
writeText(" Hello traveler ");
textTarget.y = 8;
break;
case 200:
textTarget.y = -20;
break;
case 250:
writeText(" Tilt me to fly ");
textTarget.y = 8;
break;
case 350:
textTarget.y = -20;
break;
case 400:
writeText(" Enjoy subspace ");
textTarget.y = 64-8;
break;
case 500:
textTarget.x = 128;
break;
case 550:
text.x = 128;
textTarget.x = 0;
writeText(" and be careful ");
break;
case 650:
textTarget.y = -20;
break;*/
case 800: {
text.set(-4, 128);
textTarget.set(-4, 128-16);
/*
* This is the *average* FPS so far, as measured by the game.
* If the cubes aren't all running at the same frame rate, this
* will be roughly the rate of the slowest cube, due to how the
* flow control in System::paint() works.
*/
String<17> buf;
float fps = frame / fpsTimespan;
buf << (int)fps << "." << Fixed((int)(fps * 100) % 100, 2, true)
<< " FPS avg ";
writeText(buf);
break;
}
/* case 900:
textTarget.y = 128;
break;
*/
case 2048:
frame = 0;
fpsTimespan = 0;
break;
}
/*
* We respond to the accelerometer
*/
Int2 accel = vid.physicalAccel().xy();
Float2 tilt = accel * starTiltSpeed;
/*
* Update starfield animation
*/
for (unsigned i = 0; i < numStars; i++) {
const Float2 center = { 64 - 30.5f, 64 - 30.5f };
vid.sprites[i].setImage(Star, frame % Star.numFrames());
vid.sprites[i].move(stars[i].pos + center);
stars[i].pos += float(timeStep) * (stars[i].velocity + tilt);
if (stars[i].pos.x > 80 || stars[i].pos.x < -80 ||
stars[i].pos.y > 80 || stars[i].pos.y < -80)
initStar(i);
}
/*
* Update global animation
*/
frame++;
fpsTimespan += timeStep;
Float2 bgVelocity = accel * bgTiltSpeed + vec(0.0f, -1.0f) * bgScrollSpeed;
bg += float(timeStep) * bgVelocity;
vid.bg0.setPanning(bg.round());
text += (textTarget - text) * textSpeed;
vid.bg1.setPanning(text.round());
}
private:
struct {
Float2 pos, velocity;
} stars[numStars];
VideoBuffer vid;
unsigned frame;
Float2 bg, text, textTarget;
float fpsTimespan;
void writeText(const char *str)
{
// Text on BG1, in the 16x2 area we allocated
vid.bg1.text(vec(0,14), Font, str);
}
void initStar(int id)
{
float angle = gRandom.uniform(0, 2 * M_PI);
float speed = gRandom.uniform(starEmitSpeed * 0.5f, starEmitSpeed);
stars[id].pos.set(0, 0);
stars[id].velocity.setPolar(angle, speed);
}
};
void main()
{
static StarDemo instances[gNumCubes];
for (unsigned i = 0; i < arraysize(instances); i++)
instances[i].init(i);
TimeStep ts;
while (1) {
for (unsigned i = 0; i < arraysize(instances); i++)
instances[i].update(ts.delta());
System::paint();
ts.next();
}
}