-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral.cpp
385 lines (340 loc) · 10.6 KB
/
spiral.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "spiral.h"
bool spiralInit;
bool firstPass;
extern bool waitingForInput;
extern int testChoice, screen;
extern void CALLBACK MidiInProc(HMIDIIN hMidiIn, UINT wMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);
extern DWORD note, velocity, duration, noteOnOrOff;
extern std::queue<int> inputQueue;
void Spiral::run(RenderWindow &window, int &width, int &height) {
srand((unsigned)time(NULL));
firstPass = false;
spiralInit = false;
currColor = Color::Black;
totalMarkerTicks = 0;
totalIterations = 0;
shapesVectorStartingIndex = 0;
quadrant = TR;
leftAsymptote = (float)(VideoMode::getDesktopMode().width / 2.f);
rightAsymptote = leftAsymptote + ADJUSTMENT_WIDTH;
topAsymptote = (float)(VideoMode::getDesktopMode().height / 2.f);
bottomAsymptote = topAsymptote + ADJUSTMENT_WIDTH;
ticksInCurrentQuadrant = (int)TICKS_IN_FIRST_QUADRANT;
markerX = leftAsymptote;
markerY = 0.f;
radius = topAsymptote - markerY;
HMIDIIN hMidiDevice = NULL;
DWORD nMidiPort = 0;
UINT nMidiDeviceNum;
MMRESULT rv;
nMidiDeviceNum = midiInGetNumDevs();
if (nMidiDeviceNum == 0) {
fprintf(stderr, "midiInGetNumDevs() return 0...");
while (1) {}
exit(1);
}
rv = midiInOpen(&hMidiDevice, nMidiPort, (DWORD)(void*)MidiInProc, 0, CALLBACK_FUNCTION);
if (rv != MMSYSERR_NOERROR) {
fprintf(stderr, "midiInOpen() failed...rv=%d", rv);
while (1) {}
exit(1);
}
midiInStart(hMidiDevice);
arrowTex.loadFromFile("Images/Arrow.png");
arrow.setTexture(arrowTex);
arrow.setOrigin(16, 8);
burstTex.loadFromFile("Images/bigLeaf.png");
for (int i = 0; i < 32; i++) {
burst[i].setTexture(burstTex);
burst[i].setOrigin(151, 296);
}
drawingActive = true;
drawingClock.restart();
//main loop
while (drawingActive) {
pollInput();
if (update(window)) {
render(window);
}
totalIterations++;
}
Texture saveTex;
if (!saveTex.create((unsigned int)width, (unsigned int)height)) {
std::cout << "Image problem\n";
while (1) {}
exit(1);
}
//update texture from screen and save it to file
Image saveImage;
saveImage.create((unsigned int)width, (unsigned int)height, Color::Black);
window.display();
saveTex.update(window);
saveImage = saveTex.copyToImage();
if (!saveImage.saveToFile("Images/savedSpiral.png")) {
std::cout << "Image problem\n";
while (1) {}
exit(1);
}
midiInStop(hMidiDevice);
midiInClose(hMidiDevice);
hMidiDevice = NULL;
}
void Spiral::refreshStartingSprite(RenderWindow &window) {
startingTexture.update(window);
startingSprite.setTexture(startingTexture);
}
void Spiral::awaitFirstMIDIBytes() {
while (inputQueue.empty()) {
pollInput();
}
}
//check for MIDI bytes
void Spiral::pollInput() {
if (_kbhit()) {
int c = _getch();
if (c == VK_ESCAPE) {
for (int temp = 0; temp < 100; temp++)
std::cout << "PROBLEM!!!";
}
if (c == 'q') {
for (int temp = 0; temp < 100; temp++)
std::cout << "PROBLEM!!!";
}
}
}
bool Spiral::update(RenderWindow &window) {
bool needToRender = false;
if (timeForMarkerTick()) { //sets next (x,y) point on the spiral
MarkerTick(window);
}
if (!inputQueue.empty()) {
grabAndPopNextInput();
addCorrespondingShapes(); //this sets correct position, rotation, color, scale etc. of shape(s)
needToRender = true;
}
if (timeForNextArrow()) { //every TICKS_PER_ARROW ticks, draw another arrow at correct rotation
addNextArrow();
needToRender = true;
}
if (timeToSwitchQuadrants()) {
switchQuadrants();
needToRender = true;
}
return needToRender;
}
bool Spiral::grabAndPopNextInput() {
durationIn = inputQueue.front();
inputQueue.pop();
noteIn = inputQueue.front();
inputQueue.pop();
velocityIn = inputQueue.front();
inputQueue.pop();
return true;
}
void Spiral::addCorrespondingShapes() {
//update drawing variables based on input
int layers = 4 - (noteIn / 30); //note values 21-108 become layers 5-1, based on 20-note chunks
int satelliteSpacing = (noteIn + 4) % 12;
if (satelliteSpacing == 0)
satelliteSpacing = 12;
float scalingFactor = (float)(velocityIn) / 100.f;
float rotation = 90.f - thetaDegrees;
int burstIndex = 1; //0
int currLayer = 1;
burstX[burstIndex] = markerX;
burstY[burstIndex] = markerY;
burst[burstIndex].setPosition(burstX[burstIndex], burstY[burstIndex]);
burst[burstIndex].setColor(currColor);
burst[burstIndex].setRotation(rotation);
burst[burstIndex].setScale(scalingFactor, scalingFactor);
if (layers > 1) {
float baseXSpacing = (satelliteSpacing * 20) * cos(thetaRadians);
float baseYSpacing = (satelliteSpacing * 20) * sin(thetaRadians);
for (int currLayer = 1; currLayer < layers; currLayer++) {
//layer indices: | 1 | 2-3 | 4-7 | 8-15 | 16-31 |
// who spawns who: | 1 -> 2,3 | 2 -> 4,5 | 3 -> 6,7 | 4 -> 8,9 | ... | 15 -> 30-31 | n -> 2n,2n+1
//for each index in previous layer, get satellite x,y points for two satellites
for (int index = int(pow(2, currLayer - 1)); index < int(pow (2, currLayer)); index++) {
//next point 1
burstIndex++;
burstX[burstIndex] = burstX[index] + baseXSpacing * pow(0.7f, float(currLayer));//(distance equation); //alt: burstX[2*index]
burstY[burstIndex] = burstY[index] - baseYSpacing * pow(0.7f, float(currLayer));//(distance equation); //alt: burstY[2*index]
//create & initialize satellite bursts
burst[burstIndex].setColor(currColor);
burst[burstIndex].setRotation(rotation);
burst[burstIndex].setPosition(burstX[burstIndex], burstY[burstIndex]);
burst[burstIndex].setScale(scalingFactor * pow(0.5f, float(currLayer)), scalingFactor * pow(0.5f, float(currLayer)));
burstIndex++;
//next point 2
burstX[burstIndex] = burstX[index] - baseXSpacing * pow(0.7f, float(currLayer));// (distance equation); //alt: burstX[(2*index) + 1]
burstY[burstIndex] = burstY[index] + baseYSpacing * pow(0.7f, float(currLayer));// (distance equation); //alt: burstY[(2*index) + 1]
//create & initialize satellite bursts
burst[burstIndex].setColor(currColor);
burst[burstIndex].setRotation(rotation);
burst[burstIndex].setPosition(burstX[burstIndex], burstY[burstIndex]);
burst[burstIndex].setScale(scalingFactor * pow(0.5f, float(currLayer)), scalingFactor * pow(0.5f, float(currLayer)));
}
}
}
for (int i = 1; i <= burstIndex; i++) {
shapesVector.push_back(burst[i]);
}
}
bool Spiral::timeForMarkerTick() {
if (drawingClock.getElapsedTime().asSeconds() * MARKER_TICKS_PER_SECOND > totalMarkerTicks)
return true;
else
return false;
}
void Spiral::MarkerTick(RenderWindow &window) {
float MARKER_TICKS = 2334.f;
float colorsPerChunk = MARKER_TICKS / 3.5f;
float colorShiftPerTick = 255.f / colorsPerChunk;
static int ticksInChunk;
static int transitionBlock;
static float r, g, b;
if (!firstPass) {
ticksInChunk = totalMarkerTicks;
transitionBlock = 1;
r = 0; g = 0; b = 0;
firstPass = true;
}
if (ticksInChunk > colorsPerChunk) {
ticksInChunk = 0;
transitionBlock++;
}
switch (transitionBlock) {
case 1:
r += colorShiftPerTick;
currColor.r = Uint8(r);
std::cout << float(currColor.r) << " ";
std::cout << r << "\n";
break;
case 2:
g += colorShiftPerTick;
currColor.g = Uint8(g);
break;
case 3:
r -= colorShiftPerTick;
currColor.r = Uint8(r);
break;
case 4:
b += colorShiftPerTick;
currColor.b = Uint8(b);
break;
case 5:
g -= colorShiftPerTick;
currColor.g = Uint8(g);
break;
case 6:
r += colorShiftPerTick;
currColor.r = Uint8(r);
break;
case 7:
g += colorShiftPerTick;
currColor.g = Uint8(g);
break;
default:
break;
}
ticksInChunk++;
static float tickAngle, verticalAsymptote, horizontalAsymptote;
const int TR = 1, BR = 2, BL = 3, TL = 4;
switch (quadrant) {
case TR:
if (currNoOfQuadrantTicks == 0) {
radius = topAsymptote - markerY;
verticalAsymptote = leftAsymptote;
horizontalAsymptote = topAsymptote;
ticksInCurrentQuadrant = (int)(TICKS_IN_FIRST_QUADRANT * (radius / originalRadius));
tickAngle = 90.f / ticksInCurrentQuadrant;
}
thetaDegrees = 90.f - (tickAngle * currNoOfQuadrantTicks);
break;
case BR:
if (currNoOfQuadrantTicks == 0) {
radius = markerX - rightAsymptote;
verticalAsymptote = rightAsymptote;
horizontalAsymptote = topAsymptote;
ticksInCurrentQuadrant = (int)(TICKS_IN_FIRST_QUADRANT * (radius / originalRadius));
tickAngle = 90.f / ticksInCurrentQuadrant;
}
thetaDegrees = 360.f - (tickAngle * currNoOfQuadrantTicks);
break;
case BL:
if (currNoOfQuadrantTicks == 0) {
radius = markerY - bottomAsymptote;
verticalAsymptote = rightAsymptote;
horizontalAsymptote = bottomAsymptote;
ticksInCurrentQuadrant = (int)(TICKS_IN_FIRST_QUADRANT * (radius / originalRadius));
tickAngle = 90.f / ticksInCurrentQuadrant;
}
thetaDegrees = 270.f - (tickAngle * currNoOfQuadrantTicks);
break;
case TL:
if (currNoOfQuadrantTicks == 0) {
radius = leftAsymptote - markerX;
verticalAsymptote = leftAsymptote;
horizontalAsymptote = bottomAsymptote;
ticksInCurrentQuadrant = (int)(TICKS_IN_FIRST_QUADRANT * (radius / originalRadius));
tickAngle = 90.f / ticksInCurrentQuadrant;
}
thetaDegrees = 180.f - (tickAngle * currNoOfQuadrantTicks);
break;
default:
//exit(1);
break;
}
thetaRadians = thetaDegrees * (PI / 180);
markerX = verticalAsymptote + (radius * cos(thetaRadians));
markerY = horizontalAsymptote - (radius * sin(thetaRadians));
currNoOfQuadrantTicks++;
totalMarkerTicks++;
tempShape.setPosition(markerX, markerY);
std::cout << "radius: " << radius << "\n";
if (radius < 10) drawingActive = false;
}
bool Spiral::timeForNextArrow() {
if (totalMarkerTicks % TICKS_PER_ARROW == 0)
return true;
else
return false;
}
void Spiral::addNextArrow() {
arrow.setPosition(markerX, markerY);
//seven color chunks spaced evenly along full spiral (Black->Red | Red->Yellow | ... ... ... | Violet->White)
arrow.setColor(currColor); //current color will be updated IN MarkerTick() function
float rotation = 90.f - thetaDegrees;
arrow.setRotation(rotation);
shapesVector.push_back(arrow);
}
bool Spiral::timeToSwitchQuadrants() {
//reset currNoOfQuadrantTicks each time a quadrant is completed
if (currNoOfQuadrantTicks >= ticksInCurrentQuadrant)
return true;
else
return false;
}
void Spiral::switchQuadrants() {
quadrant++;
//order of quadrants (spiral) = TR, BR, BL, TL, TR, BR, BL, TL, etc.
if (quadrant > TL)
quadrant = TR;
currNoOfQuadrantTicks = 0;
}
void Spiral::render(RenderWindow &window) {
window.clear();
if (!spiralInit) {
shapesVector.clear();
spiralInit = true;
}
if (!shapesVector.empty()) {
for (unsigned int i = shapesVectorStartingIndex; i < shapesVector.size() - 1; i++) {
window.draw(shapesVector[i]);
}
}
window.display();
}
void Spiral::saveToImage() {
//implementation in Test class
}