-
Notifications
You must be signed in to change notification settings - Fork 1
/
BeatPath.cpp
376 lines (314 loc) · 12.9 KB
/
BeatPath.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
#include "BeatPath.h"
#include <SDL2_gfxPrimitives.h>
#include <algorithm>
#include <cmath>
BeatPath::BeatPath() {}
BeatPath::BeatPath(SDL_Renderer *r, SDL_Texture *note, float center, float width, RGB color, InitVariables var, StartEnd STARTEND, std::vector<PathMotion> PATHMOTION, std::vector<PathMotion> WIDTHMOTION,std::vector<ColorMotion> COLORMOTION, std::vector<BeatNote> beat_notes)
{
this->Renderer = r;
this->pathCenter = center;
this->pathColor = color;
this->currentPathColor = color;
this->SCREEN_WIDTH = var.screen_width;
this->pathWidth = width;
this->pathHighlightAlpha = var.path_highlight_alpha;
this->noteRadiusRatio = var.note_radius_ratio;
this->initVariables = var;
this->noteTexture = note;
beatNotes = beat_notes;
startEnd = STARTEND;
pathMotions = PATHMOTION;
pathWidthMotions = WIDTHMOTION;
colorMotions = COLORMOTION;
}
BeatPath::~BeatPath()
{
}
enums::noteHit BeatPath::computeVariables(double songPosition)
{
computeCenterOfPath(songPosition);
computePathWidth(songPosition);
computePathColor(songPosition);
isOn = pathIsOn(songPosition);
return computeBeatNotes(songPosition);
}
void BeatPath::renderPath(double songPosition, int timeBarY, double beatnote_buffer_time)
{
if (!isOn) { return; }
//Highlight path
drawPathHighlight(timeBarY);
//Draw Path Center
drawPathCenter(currentCenterOfPath, timeBarY);
//Draw borders
drawBorders(timeBarY);
//Draw beat notes
drawBeatNotes(songPosition, timeBarY, beatnote_buffer_time, currentCenterOfPath);
}
std::vector<int> BeatPath::getCurrentPathWidthCoordinates()
{
std::vector<int>start_end(2);
start_end[0] = currentCenterOfPath - currentPathWidth;
start_end[1] = currentCenterOfPath + currentPathWidth;
return start_end;
}
double BeatPath::getNextBeatTime()
{
if (!beatNotes.empty()) {
return beatNotes[0].start_position;
}
else { return -1.0; }
}
enums::noteHit BeatPath::registerKey(int key, double songPosition)
{
//Add key to registeredKeys
if (std::find(registeredKeys.begin(), registeredKeys.end(), key) == registeredKeys.end()) { registeredKeys.push_back(key); }
enums::noteHit hit = enums::NO_HIT;
//If there is already a hold, then theres nothing else to be calculated.
if (isHolding) { return hit; }
//Calculate hit accuracy.
//The fact that this function is called means there is a guaranteed hit
double beatNoteTimeDelta = std::abs(beatNotes[0].start_position - songPosition);
if (beatNoteTimeDelta <= initVariables.perfect_hit_buffer_time)
{
hit = enums::PERFECT;
}
else if (beatNoteTimeDelta <= initVariables.okay_hit_buffer_time)
{
hit = enums::OKAY;
}
if (beatNotes[0].note_type == enums::SINGLE_HIT){ beatNotes.erase(beatNotes.begin()); }
else if (beatNotes[0].note_type == enums::SINGLE_HOLD) { isHolding = true; }
return hit;
}
enums::noteHit BeatPath::deregisterKey(int key, double songPosition)
{
//Might need to check if this works 100%
if (std::find(registeredKeys.begin(), registeredKeys.end(), key) != registeredKeys.end())
{
registeredKeys.erase(std::remove(registeredKeys.begin(), registeredKeys.end(), key), registeredKeys.end());
}
enums::noteHit hit = enums::NO_HIT;
if ((!isHolding) || (!registeredKeys.empty())) { return hit; }
if (beatNotes[0].end_position - songPosition > initVariables.okay_hit_buffer_time)
{
hit = enums::BREAK;
}
else if (beatNotes[0].end_position - songPosition > initVariables.perfect_hit_buffer_time)
{
hit = enums::OKAY;
}
else { hit = enums::PERFECT; }
isHolding = false;
beatNotes.erase(beatNotes.begin());
return hit;
}
void BeatPath::drawBorders(int timeBarY)
{
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
SDL_Rect border = { 0,0,1,timeBarY };
border.x = currentCenterOfPath - currentPathWidth;
SDL_RenderFillRect(Renderer, &border);
border.x = currentCenterOfPath + currentPathWidth;
SDL_RenderFillRect(Renderer, &border);
}
void BeatPath::computePathColor(double currentPosition)
{
for (std::vector<ColorMotion>::iterator i = colorMotions.begin(); i != colorMotions.end(); ++i)
{
//Including '=' to account for 'instant color-changes'
if (i->end_position <= currentPosition)
{
currentPathColor = i->endColor;
}
else if (i->start_position < currentPosition && currentPosition < i->end_position)
{
currentPathColor = processColorMotion(*i, currentPosition);
return;
}
}
}
RGB BeatPath::processColorMotion(ColorMotion colorMotion, double currentPosition)
{
pathColor = colorMotion.startColor;
RGB color = { 255,255,255 };
if (colorMotion.motion == enums::LINEAR_SLIDE)
{
int start[3] = { colorMotion.startColor.r, colorMotion.startColor.g, colorMotion.startColor.b };
int end[3] = { colorMotion.endColor.r, colorMotion.endColor.g, colorMotion.endColor.b };
int *final[3] = { &color.r, &color.g, &color.b };
for (int i = 0; i < 3; ++i)
{
*final[i] = start[i] + ((int)(((currentPosition - colorMotion.start_position) / (colorMotion.end_position - colorMotion.start_position)) * ((double)(end[i] - start[i]))));
}
return color;
}
return color;
}
enums::noteHit BeatPath::computeBeatNotes(double songPosition)
{
enums::noteHit hit = enums::NO_HIT;
bool thereAreExpiredNotes = false;
if (!beatNotes.empty()) {
thereAreExpiredNotes = (beatNotes[0].note_type == enums::SINGLE_HIT && songPosition - beatNotes[0].start_position > initVariables.okay_hit_buffer_time) || (beatNotes[0].note_type == enums::SINGLE_HOLD && (songPosition > beatNotes[0].end_position || (songPosition - beatNotes[0].start_position > initVariables.okay_hit_buffer_time && !isHolding)));
}
//Delete expired notes and register a break
while (thereAreExpiredNotes)
{
if (beatNotes[0].note_type == enums::SINGLE_HIT || (beatNotes[0].note_type == enums::SINGLE_HOLD && !isHolding)) { hit = enums::BREAK; }
else if (beatNotes[0].note_type == enums::SINGLE_HOLD && songPosition > beatNotes[0].end_position && isHolding) { hit = enums::PERFECT; }
isHolding = false;
beatNotes.erase(beatNotes.begin());
if (!beatNotes.empty())
{
thereAreExpiredNotes = (beatNotes[0].note_type == enums::SINGLE_HIT && songPosition - beatNotes[0].start_position > initVariables.okay_hit_buffer_time) || (beatNotes[0].note_type == enums::SINGLE_HOLD && (songPosition > beatNotes[0].end_position || (songPosition - beatNotes[0].start_position > initVariables.okay_hit_buffer_time && !isHolding)));
}
else { thereAreExpiredNotes = false; }
}
return hit;
}
void BeatPath::drawBeatNotes(double songPosition, int timeBarY, double beatnote_buffer_time, int center_of_path)
{
for (std::vector<BeatNote>::iterator i = beatNotes.begin(); i != beatNotes.end(); ++i)
{
//Draw active notes
if (i->start_position - songPosition > beatnote_buffer_time) { break; }
renderBeatNotes(songPosition, timeBarY, beatnote_buffer_time, center_of_path, i);
}
}
void BeatPath::renderBeatNotes(double songPosition, int timeBarY, double beatnote_buffer_time, int center_of_path, std::vector<BeatNote>::iterator beat_note)
{
Sint16 x[4], y[4], noteCenterY;
//Compute noteCenterY
double noteToBufferRatio = (beat_note->start_position - songPosition) / (beatnote_buffer_time);
if (noteToBufferRatio < 0) {
noteToBufferRatio = 0;
}
noteCenterY = (Sint16) (((double)(timeBarY)) * (((double)1) - noteToBufferRatio));
//Experimental usage of rings as notes
int noteRadius = (int)(noteRadiusRatio * ((float)SCREEN_WIDTH));
if (beat_note->note_type == enums::SINGLE_HIT) {
SDL_Rect noteRect = { center_of_path - noteRadius,noteCenterY - noteRadius,noteRadius * 2,noteRadius * 2 };
SDL_RenderCopyEx(Renderer, noteTexture, NULL, ¬eRect, NULL, 0, SDL_FLIP_NONE);
}
else { SDL_Rect noteRect = {center_of_path-noteRadius,noteCenterY,noteRadius*2,noteRadius};
SDL_Rect partialRect = {0,50,100,50};
SDL_RenderCopyEx(Renderer, noteTexture, &partialRect, ¬eRect, NULL, 0, SDL_FLIP_NONE);
}
////Compute Vertex Array
//x[0] = (Sint16)center_of_path;
//y[0] = noteCenterY - ((Sint16)(noteRadiusRatio * ((float)SCREEN_WIDTH)));
//x[1] = (Sint16)center_of_path - ((Sint16)(noteRadiusRatio * ((float)SCREEN_WIDTH)));
//y[1] = noteCenterY;
//x[2] = (Sint16)center_of_path;
//y[2] = noteCenterY + ((Sint16)(noteRadiusRatio * ((float)SCREEN_WIDTH)));
//x[3] = (Sint16)center_of_path + ((Sint16)(noteRadiusRatio * ((float)SCREEN_WIDTH)));
//y[3] = noteCenterY;
////Render note
//filledPolygonRGBA(Renderer,x,y,4,0,0,0,255);
//That's all that's needed for SINGLE_HITs.
if (beat_note->note_type == enums::SINGLE_HIT) { return; }
//Blitting for SINGLE_HOLDs.
double holdNoteToBufferRatio = (beat_note->end_position - songPosition) / (beatnote_buffer_time);
if (holdNoteToBufferRatio > 1) { holdNoteToBufferRatio = 1; }
if (holdNoteToBufferRatio < 0) { holdNoteToBufferRatio = 0; }
int holdNoteCenterY = (int) (((double)(timeBarY)) * (((double)1) - holdNoteToBufferRatio));
/*SDL_Rect holdRect = { center_of_path - ((int)(noteRadiusRatio * ((float)SCREEN_WIDTH))), holdNoteCenterY, (int)(2.f * ((float)SCREEN_WIDTH) * noteRadiusRatio), ((int)noteCenterY) - holdNoteCenterY };
SDL_RenderFillRect(Renderer, &holdRect);*/
//Experimental usage of rings as notes
float ratio = 0.25f;
int thickness = (int)(ratio*((float)noteRadius));
SDL_Rect holdRect = { center_of_path - noteRadius,holdNoteCenterY,thickness,((int)noteCenterY) - holdNoteCenterY };
SDL_RenderFillRect(Renderer, &holdRect);
holdRect.x = center_of_path + noteRadius - thickness;
SDL_RenderFillRect(Renderer, &holdRect);
SDL_Rect partialRect = { 0,0,100,50 }, noteRect = {center_of_path-noteRadius,holdNoteCenterY-noteRadius,2*noteRadius,noteRadius};
SDL_RenderCopyEx(Renderer, noteTexture, &partialRect, ¬eRect, NULL, 0, SDL_FLIP_NONE);
}
void BeatPath::drawPathHighlight(int timeBarY)
{
SDL_SetRenderDrawBlendMode(Renderer, SDL_BLENDMODE_BLEND);
SDL_Rect highlight = { 0,0,0,timeBarY };
highlight.x = currentCenterOfPath - currentPathWidth;
highlight.w = 2 * currentPathWidth;
SDL_SetRenderDrawColor(Renderer, currentPathColor.r, currentPathColor.g, currentPathColor.b, 155);
SDL_RenderFillRect(Renderer, &highlight);
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
}
void BeatPath::computePathWidth(double currentPosition)
{
for (std::vector<PathMotion>::iterator i = pathWidthMotions.begin(); i != pathWidthMotions.end(); ++i)
{
if (i->end_position < currentPosition)
{
pathWidth = i->end_x;
}
else if (i->start_position < currentPosition && currentPosition < i->end_position)
{
currentPathWidth =(int)((((float)SCREEN_WIDTH) * processPathMotionX(*i, currentPosition, &pathWidth)));
return;
}
}
currentPathWidth = (int)(((float)SCREEN_WIDTH) * pathWidth);
}
void BeatPath::computeCenterOfPath(double currentPosition)
{
for (std::vector<PathMotion>::iterator i = pathMotions.begin(); i != pathMotions.end(); ++i)
{
if (i->end_position < currentPosition)
{
pathCenter = i->end_x;
}
else if (i->start_position < currentPosition && currentPosition < i->end_position)
{
currentCenterOfPath =(int)((((float)SCREEN_WIDTH) * processPathMotionX(*i, currentPosition, &pathCenter)));
return;
}
}
currentCenterOfPath =(int)( (((float)SCREEN_WIDTH)*(pathCenter)));
}
void BeatPath::drawPathCenter(int centerOfPath, int timeBarY)
{
SDL_Rect rect = {centerOfPath - ((int)(0.5f*((float)pathCenterThickness))),0,pathCenterThickness,timeBarY};
SDL_SetRenderDrawBlendMode(Renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
//SDL_RenderFillRect(Renderer, &rect);
}
float BeatPath::processPathMotionX(PathMotion pathMotion, double currentPosition, float *endpoint)
{
if (pathMotion.motion == enums::LINEAR_SLIDE)
{
*endpoint = pathMotion.end_x;
return (((float)pathMotion.start_x) + (((float)(pathMotion.end_x - pathMotion.start_x)) * (((float)(currentPosition - pathMotion.start_position))/((float)(pathMotion.end_position - pathMotion.start_position)))));
}
else if (pathMotion.motion == enums::HALF_SINE_SLIDE)
{
*endpoint = pathMotion.end_x;
return (float)(pathMotion.start_x + (std::sin(((currentPosition - pathMotion.start_position) / (pathMotion.end_position - pathMotion.start_position)) * 1.5708) * (pathMotion.amplitude - pathMotion.start_x)));
}
else if (pathMotion.motion == enums::FULL_SINE_SLIDE)
{
*endpoint = pathMotion.end_x;
return (float)(pathMotion.start_x + (std::sin(((currentPosition - pathMotion.start_position) / (pathMotion.end_position - pathMotion.start_position)) * 3.141) * (pathMotion.amplitude - pathMotion.start_x)));
}
else if (pathMotion.motion == enums::REVERSE_HALF_SINE_SLIDE)
{
*endpoint = pathMotion.end_x;
return (float)(pathMotion.start_x + ((std::sin(((currentPosition - pathMotion.start_position) / (pathMotion.end_position - pathMotion.start_position) * 1.5708) - 1.5708) + 1.0) * (pathMotion.end_x - pathMotion.start_x)));
}
return 0;
}
bool BeatPath::pathIsOn(double songPosition)
{
std::vector<double>::const_iterator a, b;
a = startEnd.start.begin();
b = startEnd.end.begin();
while (a != startEnd.start.end() && b != startEnd.end.end())
{
if (songPosition > *a && songPosition < *b)
{
return true;
}
++a; ++b;
}
return false;
}