-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
330 lines (288 loc) · 9.95 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
File: main.cpp
Purpose: Blicket detector for Sifteo Cubes
@author Jay B. Martin
@version 0.1 16 Mar 2013
*/
#include <sifteo.h>
#include "assets.gen.h"
using namespace Sifteo;
// Number of cubes to deploy (N blickets + 1 blicket detector)
static const unsigned gNumCubes = 5;
Random gRandom;
// METADATA
static Metadata M = Metadata()
.title("Blicket Tutorial")
.package("blickets", "0.1")
.icon(Icon)
.cubeRange(gNumCubes);
AssetSlot gMainSlot = AssetSlot::allocate()
.bootstrap(BootstrapAssets);
// GLOBALS
static VideoBuffer vbuf[CUBE_ALLOCATION]; // one video-buffer per cube
static CubeSet newCubes; // new cubes as a result of paint()
static CubeSet lostCubes; // lost cubes as a result of paint()
static CubeSet reconnectedCubes; // reconnected (lost->new) cubes as a result of paint()
static CubeSet dirtyCubes; // dirty cubes as a result of paint()
static CubeSet activeCubes; // cubes showing the active scene
static AssetLoader loader; // global asset loader (each cube will have symmetric assets)
static AssetConfiguration<1> config; // global asset configuration (will just hold the bootstrap group)
// Experiment Globals
static CubeSet blicketCubes;
static CubeSet nonBlicketCubes;
static CubeID blicketDetector = 0;
static CubeID blicket1 = 1;
static CubeID blicket2 = 3;
static CubeID nonBlicket1 = 2;
static CubeID nonBlicket2 = 4;
// Set of 7 experimental conditions (see Gopnik & Sobel (2000))
AssetImage conditions[] = {Set1IdenticalObjects, Set2SameColorDifferentShape,
Set3SameShapeDifferentColors, Set4DifferentColorsAndShapes,
Set5SameShapeConflictsOnColor, Set6SameColorConflictsOnShape,
Set7ConflictsOnShapeAndColor};
class Conditions {
int condition;
public:
void set_condition(int);
AssetImage get_condition();
int get_condition_number();
void next_condition();
Conditions();
};
void Conditions::set_condition(int condition_) { condition = condition_; };
AssetImage Conditions::get_condition() { return conditions[condition]; };
int Conditions::get_condition_number() { return condition; };
// TODO(Jay): Make condtions after 0 randomly selected.
void Conditions::next_condition() { condition = (condition + 1) % 7; };
Conditions::Conditions(){ condition = 0; }
// Initialize conditions
Conditions cond;
// FUNCTIONS
static void playSfx(const AssetAudio& sfx) {
static int i=0;
AudioChannel(i).play(sfx);
i = 1 - i;
}
static void stopSfx() {
for (int i=0; i < 8; i++) {
AudioChannel(i).stop();
}
}
static Int2 getRestPosition(Side s) {
// Look up the top-left pixel of the bar for the given side.
// We use a switch so that the compiler can optimize this
// however if feels is best.
switch(s) {
case TOP: return vec(64 - Bars[0].pixelWidth()/2, 0);
case LEFT: return vec(0, 64 - Bars[1].pixelHeight()/2);
case BOTTOM: return vec(64 - Bars[2].pixelWidth()/2, 128 - Bars[2].pixelHeight());
case RIGHT: return vec(128 - Bars[3].pixelWidth(), 64 - Bars[3].pixelHeight()/2);
default: return vec(0,0);
}
}
static int barSpriteCount(CubeID cid) {
// How many bars are showing on this cube?
ASSERT(activeCubes.test(cid));
int result = 0;
for(int i=0; i < 4; ++i) {
if (!vbuf[cid].sprites[i].isHidden()) {
result++;
}
}
return result;
}
static bool showSideBar(CubeID cid, Side s) {
// If cid is not showing a bar on side s, show it and check if the
// blicket detector shold go off.
ASSERT(activeCubes.test(cid));
if (vbuf[cid].sprites[s].isHidden()) {
vbuf[cid].sprites[s].setImage(Bars[s]);
vbuf[cid].sprites[s].move(getRestPosition(s));
if (barSpriteCount(cid) == 1 && cid == 0) {
vbuf[cid].bg0.image(vec(0,0), Backgrounds, 1);
}
else {
vbuf[cid].bg0.image(vec(0,0), cond.get_condition(), cid - 1);
}
return true;
} else {
return false;
}
}
static bool hideSideBar(CubeID cid, Side s) {
// If cid is showing a bar on side s, hide it and check if the
// blicket detector should turn off.
ASSERT(activeCubes.test(cid));
if (!vbuf[cid].sprites[s].isHidden()) {
vbuf[cid].sprites[s].hide();
if (barSpriteCount(cid) == 0 && cid == 0) {
vbuf[cid].bg0.image(vec(0,0), Backgrounds, 0);
}
else {
vbuf[cid].bg0.image(vec(0,0), cond.get_condition(), cid - 1);
}
return true;
} else {
return false;
}
}
static void activateCube(CubeID cid) {
// Mark cube as active and render its canvas
//
activeCubes.mark(cid);
vbuf[cid].initMode(BG0_SPR_BG1);
if (cid == 0) {
vbuf[cid].bg0.image(vec(0,0), Backgrounds, 0);
} else {
vbuf[cid].bg0.image(vec(0,0), cond.get_condition(), cid - 1);
}
auto neighbors = vbuf[cid].physicalNeighbors();
for(int side=0; side<4; ++side) {
if (neighbors.hasNeighborAt(Side(side))) {
showSideBar(cid, Side(side));
} else {
hideSideBar(cid, Side(side));
}
}
}
static void paintWrapper() {
// Clear the palette
newCubes.clear();
lostCubes.clear();
reconnectedCubes.clear();
dirtyCubes.clear();
// Fire events
System::paint();
// Dynamically load assets just-in-time
if (!(newCubes | reconnectedCubes).empty()) {
AudioTracker::pause();
playSfx(SfxSong);
loader.start(config);
while(!loader.isComplete()) {
for(CubeID cid : (newCubes | reconnectedCubes)) {
vbuf[cid].bg0rom.hBargraph(
vec(0, 4), loader.cubeProgress(cid, 128), BG0ROMDrawable::ORANGE, 8
);
}
// Fire events while we wait
System::paint();
}
loader.finish();
AudioTracker::resume();
}
// Repaint cubes
for(CubeID cid : dirtyCubes) {
activateCube(cid);
}
// Also, handle lost cubes, if you so desire :)
}
static void onCubeConnect(void* ctxt, unsigned cid) {
// This cube is either new or reconnected
if (lostCubes.test(cid)) {
// This is a reconnected cube since it was already lost this paint()
lostCubes.clear(cid);
reconnectedCubes.mark(cid);
} else {
// This is a brand-spanking new cube
newCubes.mark(cid);
}
// Begin showing some loading art (have to use BG0ROM since we don't have assets)
dirtyCubes.mark(cid);
auto& g = vbuf[cid];
g.attach(cid);
g.initMode(BG0_ROM);
g.bg0rom.fill(vec(0, 0), vec(16, 16), BG0ROMDrawable::SOLID_BG);
g.bg0rom.text(vec(1, 1), "Hold on!", BG0ROMDrawable::BLUE);
g.bg0rom.text(vec(1, 14), "Adding Cube...", BG0ROMDrawable::BLUE);
}
static void onCubeDisconnect(void* ctxt, unsigned cid) {
// Mark as lost and clear from other cube sets
lostCubes.mark(cid);
newCubes.clear(cid);
reconnectedCubes.clear(cid);
dirtyCubes.clear(cid);
activeCubes.clear(cid);
}
static void onCubeRefresh(void* ctxt, unsigned cid) {
// Mark this cube for a future repaint
dirtyCubes.mark(cid);
}
static bool isActive(NeighborID nid) {
// Does this nid indicate an active cube?
return nid.isCube() && activeCubes.test(nid);
}
static void onNeighborAdd(void* ctxt, unsigned cube0, unsigned side0, unsigned cube1, unsigned side1) {
// Update art on active cubes (not loading cubes or base)
bool sfx = false;
if (isActive(cube0) && cube0 == blicketDetector && blicketCubes.test(cube1)) {
sfx |= showSideBar(cube1, Side(side1));
sfx |= showSideBar(cube0, Side(side0));
}
if (isActive(cube1) && cube1 == blicketDetector && blicketCubes.test(cube0)) {
sfx |= showSideBar(cube0, Side(side0));
sfx |= showSideBar(cube1, Side(side1));
}
if (sfx) { playSfx(SfxSong); }
}
static void onNeighborRemove(void* ctxt, unsigned cube0, unsigned side0, unsigned cube1, unsigned side1) {
// Update art on active cubes (not loading cubes or base)
bool sfx = false;
if (isActive(cube0) && cube0 == blicketDetector && blicketCubes.test(cube1)) {
sfx |= hideSideBar(cube1, Side(side1));
sfx |= hideSideBar(cube0, Side(side0));
}
if (isActive(cube1) && cube1 == blicketDetector && blicketCubes.test(cube0)) {
sfx |= hideSideBar(cube1, Side(side1));
sfx |= hideSideBar(cube0, Side(side0));
}
if (sfx) { playSfx(SfxDetach); }
if (sfx) { stopSfx(); }
}
// Candidate function for interventions
static void onCubeTouch(void* ctxt, unsigned cid) {
cond.next_condition();
LOG("Condition %i\n", cond.get_condition_number());
for(CubeID cid : CubeSet::connected()) {
vbuf[cid].attach(cid);
activateCube(cid);
}
}
// When pressed, move to the next condition
void onTouch(void* ctxt, unsigned id) {
CubeID cube(id);
// On touchout (similar to mouseout)
if (cube.isTouching() == 0) {
cond.next_condition();
LOG("Condition %i\n", cond.get_condition_number());
for(CubeID cid : CubeSet::connected()) {
vbuf[cid].attach(cid);
activateCube(cid);
}
}
}
void main() {
blicketCubes.mark(blicket1);
blicketCubes.mark(blicket2);
nonBlicketCubes.mark(nonBlicket1);
nonBlicketCubes.mark(nonBlicket2);
// Initialize asset configuration and loader
config.append(gMainSlot, BootstrapAssets);
loader.init();
// Subscribe to events (See pubsub design pattern)
Events::cubeConnect.set(onCubeConnect);
Events::cubeDisconnect.set(onCubeDisconnect);
Events::cubeRefresh.set(onCubeRefresh);
Events::neighborAdd.set(onNeighborAdd);
Events::neighborRemove.set(onNeighborRemove);
// Events::cubeTouch.set(onCubeTouch);
Events::cubeTouch.set(onTouch);
// Initialize cubes
for(CubeID cid : CubeSet::connected()) {
vbuf[cid].attach(cid);
activateCube(cid);
}
// Run loop
for(;;) {
paintWrapper();
}
}