-
Notifications
You must be signed in to change notification settings - Fork 1
/
BeatMap.cpp
386 lines (357 loc) · 11.8 KB
/
BeatMap.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
386
#include "BeatMap.h"
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <sstream>
#include <iostream>
BeatMap::BeatMap(SDL_Renderer *r, InitVariables var, Instruction nextInstruction, SDL_Texture *note)
{
this->Renderer = r;
this->initVariables = var;
this->instruction = nextInstruction;
this->SCREEN_HEIGHT = var.screen_height;
this->SCREEN_WIDTH = var.screen_width;
this->beatNoteBufferTime = var.note_buffer_time;
this->keySeparationThickness = var.keySeparation_thickness;
this->noteTexture = note;
this->numberOfKeys = nextInstruction.gameKeys;
generateKeyCoordinates();
generateKeyStatuses(var.keyBinds);
beatPath = getBeatPath();
}
BeatMap::BeatMap() {}
BeatMap::~BeatMap()
{
}
std::vector<BeatPath> BeatMap::getBeatPath()
{
boost::filesystem::path p(instruction.beatMapRootFolder + "\\" + std::to_string(instruction.gameKeys) + "key\\" + instruction.songDifficulty);
boost::filesystem::directory_iterator end_itr;
std::vector<std::string> pathDirectories;
for (boost::filesystem::directory_iterator itr(p); itr != end_itr; ++itr)
{
if (boost::filesystem::is_regular_file(itr->path()) && itr->path().extension().string() == ".txt")
{
pathDirectories.push_back(itr->path().string());
}
}
std::vector<BeatPath> beatPath;
for (std::vector<std::string>::iterator i = pathDirectories.begin(); i != pathDirectories.end(); ++i)
{
float startPosition, startWidth;
RGB startColor;
StartEnd startEnd;
std::vector<PathMotion> pathMotion,widthMotion;
std::vector<BeatNote> beatNotes;
std::vector<ColorMotion> colorMotion;
std::ifstream file(*i);
std::string line;
while (std::getline(file, line))
{
if (line == "#startposition")
{
std::string line2; std::getline(file, line2);
startPosition = parseStringToFraction(line2);
}
else if (line == "#startwidth")
{
std::string line2; std::getline(file, line2);
startWidth = parseStringToFraction(line2);
}
if (line == "#startcolor")
{
std::string line2; std::getline(file, line2);
boost::algorithm::trim_if(line2, boost::is_any_of("() "));
std::vector<std::string> lines;
boost::split(lines, line2, boost::is_any_of("|"));
startColor.r = std::stoi(lines[0]);
startColor.g = std::stoi(lines[1]);
startColor.b = std::stoi(lines[2]);
}
else if (line == "#startend")
{
std::string line2, line3; std::getline(file, line2); std::getline(file, line3);
startEnd.start = parseStringToVectorOfDoubles(line2);
startEnd.end = parseStringToVectorOfDoubles(line3);
}
else if (line == "#pathmotion")
{
std::string line2; std::getline(file, line2);
pathMotion = parseStringVectorToPathMotionVector(parseStringToVectorOfTrimmedStrings(line2));
}
else if (line == "#widthmotion")
{
std::string line2; std::getline(file, line2);
widthMotion = parseStringVectorToPathMotionVector(parseStringToVectorOfTrimmedStrings(line2));
}
else if (line == "#colormotion")
{
std::string line2; std::getline(file, line2);
colorMotion = parseStringVectorToColorMotionVector(parseStringToVectorOfTrimmedStrings(line2));
}
else if (line == "#beatnote")
{
std::string line2; std::getline(file, line2);
beatNotes = parseStringVectorToBeatNoteVector(parseStringToVectorOfTrimmedStrings(line2));
}
}
beatPath.push_back(BeatPath(Renderer, noteTexture, startPosition, startWidth, startColor, initVariables, startEnd, pathMotion, widthMotion, colorMotion, beatNotes));
}
return beatPath;
}
float BeatMap::parseStringToFraction(std::string line)
{
std::vector<std::string> lines;
std::vector<float> floats;
float number;
boost::replace_all(line, " ", "");
boost::split(lines, line, boost::is_any_of("/"));
for (std::vector<std::string>::iterator i = lines.begin(); i != lines.end(); ++i)
{
std::stringstream stream(*i);
stream >> number;
floats.push_back(number);
}
switch (floats.size())
{
case 2:
number = ((float)floats[0]) / ((float)floats[1]); break;
case 1:
number = floats[0]; break;
default:
number = -1.f;
}
return number;
}
std::vector<double> BeatMap::parseStringToVectorOfDoubles(std::string line)
{
std::vector<std::string> lines;
std::vector<double> doubles;
double number;
boost::split(lines, line, boost::is_any_of(","));
for (std::vector<std::string>::iterator i = lines.begin(); i != lines.end(); ++i)
{
boost::algorithm::trim(*i);
std::stringstream stream(*i);
stream >> number;
doubles.push_back(number);
}
return doubles;
}
std::vector<std::string> BeatMap::parseStringToVectorOfTrimmedStrings(std::string line)
{
std::vector<std::string> strings;
boost::split(strings, line, boost::is_any_of(","));
for (std::vector<std::string>::iterator i = strings.begin(); i != strings.end(); ++i)
{
boost::algorithm::trim_if(*i, boost::is_any_of("() "));
}
return strings;
}
std::vector<PathMotion> BeatMap::parseStringVectorToPathMotionVector(std::vector<std::string> strings)
{
std::vector<PathMotion> pathMotion;
std::vector<std::string> lines;
for (std::vector<std::string>::iterator i = strings.begin(); i != strings.end(); ++i)
{
boost::split(lines, *i, boost::is_any_of("|"));
PathMotion pM;
pM.motion = (enums::motions)std::stoi(lines[0]);
pM.start_position = std::stod(lines[1]);
pM.end_position = std::stod(lines[2]);
pM.start_x = parseStringToFraction(lines[3]);
pM.end_x = parseStringToFraction(lines[4]);
if (pM.motion == enums::HALF_SINE_SLIDE || pM.motion == enums::FULL_SINE_SLIDE) { pM.amplitude = parseStringToFraction(lines[5]); }
//Add more functions here for future motions that involve special parameters
pathMotion.push_back(pM);
}
return pathMotion;
}
std::vector<ColorMotion> BeatMap::parseStringVectorToColorMotionVector(std::vector<std::string> strings)
{
std::vector<ColorMotion> colorMotions;
std::vector<std::string> lines;
for (std::vector<std::string>::iterator i = strings.begin(); i != strings.end(); ++i)
{
boost::split(lines, *i, boost::is_any_of("|"));
ColorMotion cM;
RGB color;
cM.motion = (enums::motions)std::stoi(lines[0]);
cM.start_position = std::stod(lines[1]);
cM.end_position = std::stod(lines[2]);
color.r = std::stoi(lines[3]);
color.g = std::stoi(lines[4]);
color.b = std::stoi(lines[5]);
cM.startColor = color;
color.r = std::stoi(lines[6]);
color.g = std::stoi(lines[7]);
color.b = std::stoi(lines[8]);
cM.endColor = color;
colorMotions.push_back(cM);
}
return colorMotions;
}
std::vector<BeatNote> BeatMap::parseStringVectorToBeatNoteVector(std::vector<std::string> strings)
{
std::vector<BeatNote> beatNote;
std::vector<std::string> lines;
for (std::vector<std::string>::iterator i = strings.begin(); i != strings.end(); ++i)
{
boost::split(lines, *i, boost::is_any_of("|"));
BeatNote bN;
bN.note_type = (enums::noteType)std::stoi(lines[0]);
bN.start_position = std::stod(lines[1]);
//Safety precaution: auto make end_position = start_position if note type == SINGLE_HIT
if (bN.note_type == enums::SINGLE_HIT) { bN.end_position = std::stod(lines[1]); }
else { bN.end_position = std::stod(lines[2]); }
beatNote.push_back(bN);
}
return beatNote;
}
void BeatMap::generateKeyCoordinates()
{
std::vector<int> coordinates(numberOfKeys+1);
int interval = (int)(((float)SCREEN_WIDTH) / ((float)numberOfKeys));
coordinates[0] = 0;
for (int i = 0; i < numberOfKeys; i++) { coordinates[i+1] = (i+1) * interval; }
keyCoordinates = coordinates;
}
void BeatMap::generateKeyStatuses(std::vector<int> key_binds)
{
std::vector<KeyStatus> kS;
int start, end;
switch (numberOfKeys)
{
case 4:
start = 1; end = 5; break;
case 6:
start = 0; end = 6; break;
}
KeyStatus key = { 0,false,-1};
for (int i = start; i < end; i++)
{
key.key = key_binds[i]; kS.push_back(key);
}
keyStatuses = kS;
}
bool BeatMap::thereIsAnOverlap(int start, int end, std::vector<int> pathCoordinates)
{
return (start < pathCoordinates[1] && end > pathCoordinates[0]);
}
void BeatMap::render(Uint32 currentTick, double currentMusicPosition, int timeBarY)
{
//Render panel images for clicked keys
for (size_t i = 0, ilen = keyStatuses.size(); i < ilen; ++i)
{
if (keyStatuses[i].already_pressed)
{
int panelLeftX = keyCoordinates[i];
int panelWidth = keyCoordinates[i + 1] - panelLeftX;
SDL_Rect panelImage = { panelLeftX, timeBarY, panelWidth, SCREEN_HEIGHT -timeBarY };
RGB color;
if (keyStatuses[i].linked_path != -1)
{
color = beatPath[keyStatuses[i].linked_path].currentPathColor;
}
else
{
color.r = 0; color.g = 0; color.b = 0;
}
SDL_SetRenderDrawBlendMode(Renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(Renderer, color.r, color.g, color.b, 180);
SDL_RenderFillRect(Renderer, &panelImage);
}
}
//Render paths and beatnotes
for (std::vector<BeatPath>::iterator i = beatPath.begin(); i != beatPath.end(); ++i)
{
i->renderPath( currentMusicPosition, timeBarY, beatNoteBufferTime);
}
//Render separations
SDL_Rect a = { 0,timeBarY,keySeparationThickness,SCREEN_HEIGHT - timeBarY };
for (int i = 1; i < numberOfKeys; i++)
{
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
a.x = keyCoordinates[i] - ((int)(0.5f * ((float)keySeparationThickness)));
SDL_RenderFillRect(Renderer, &a);
}
}
void BeatMap::computeVariables(double songPosition, std::vector<enums::noteHit> *hits)
{
for (size_t i = 0, ilen = keyStatuses.size(); i < ilen; ++i)
{
if (keyStatuses[i].linked_path == -1) { continue; }
if (!thereIsAnOverlap(keyCoordinates[i], keyCoordinates[i + 1], beatPath[keyStatuses[i].linked_path].getCurrentPathWidthCoordinates()))
{
hits->push_back(beatPath[keyStatuses[i].linked_path].deregisterKey((int)i,songPosition));
keyStatuses[i].linked_path = -1;
}
}
for (std::vector<BeatPath>::iterator i = beatPath.begin(); i != beatPath.end(); ++i)
{
hits->push_back(i->computeVariables(songPosition));
}
}
enums::noteHit BeatMap::processInput(SDL_Event e, double songPosition)
{
//Determine which key is pressed
enums::noteHit hit = enums::NO_HIT;
int key = -1;
for (size_t i = 0, ilen = keyStatuses.size(); i < ilen; ++i)
{
if (e.key.keysym.sym == keyStatuses[i].key) { key = i; break; }
}
if (key == -1) { return hit; }
//Evaluate key-up/key-down scenarios
if (e.type == SDL_KEYUP && keyStatuses[key].already_pressed)
{
keyStatuses[key].already_pressed = false;
if (keyStatuses[key].linked_path != -1)
{
//Release linked path here
hit = beatPath[keyStatuses[key].linked_path].deregisterKey(key, songPosition);
keyStatuses[key].linked_path = -1;
}
}
else if (e.type == SDL_KEYDOWN && !keyStatuses[key].already_pressed)
{
keyStatuses[key].already_pressed = true;
keyStatuses[key].linked_path = getLinkedPath(keyCoordinates[key],keyCoordinates[key+1], songPosition);
if (keyStatuses[key].linked_path != -1) {
//Register key with linked_path here
hit = beatPath[keyStatuses[key].linked_path].registerKey(key, songPosition);
}
}
return hit;
}
int BeatMap::getLinkedPath(int start, int end, double songPosition)
{
std::vector<Path_Beat_Pair> pathBeatPairs;
Path_Beat_Pair pathBeatPair;
double nextBeatTime;
//Get all paths that have upcoming beats
for (size_t i = 0, ilen = beatPath.size(); i < ilen; i++)
{
if (beatPath[i].isOn && thereIsAnOverlap(start, end, beatPath[i].getCurrentPathWidthCoordinates()))
{
nextBeatTime = beatPath[i].getNextBeatTime();
if (nextBeatTime != -1 && (nextBeatTime - songPosition < initVariables.okay_hit_buffer_time || beatPath[i].isHolding))
{
pathBeatPair.first_beat = nextBeatTime;
pathBeatPair.path = i;
pathBeatPairs.push_back(pathBeatPair);
}
}
}
if (pathBeatPairs.empty()) { return -1; }
//Pick the path with the nearest beat
pathBeatPair = pathBeatPairs[0];
for (size_t i = 0, ilen = pathBeatPairs.size(); i < ilen; i++)
{
if (pathBeatPairs[i].first_beat < pathBeatPair.first_beat)
{
pathBeatPair = pathBeatPairs[i];
}
}
return pathBeatPair.path;
}