Skip to content

Commit b0e2e03

Browse files
committed
Moved internal functions into their own file.
Added missing test.
1 parent 71b1be3 commit b0e2e03

File tree

9 files changed

+352
-157
lines changed

9 files changed

+352
-157
lines changed

includes/RhythmGameUtilities/Parsers.hpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -91,53 +91,4 @@ std::vector<ChartSection> ParseSectionsFromChart(const char *contents)
9191
return sections;
9292
}
9393

94-
extern "C"
95-
{
96-
PACKAGE_API
97-
ChartSectionInternal *ParseSectionsFromChartInternal(const char *contents,
98-
int *outSize)
99-
{
100-
auto internalSections = ParseSectionsFromChart(contents);
101-
102-
*outSize = internalSections.size();
103-
104-
auto sections = (ChartSectionInternal *)malloc(
105-
internalSections.size() * sizeof(ChartSectionInternal));
106-
107-
for (auto i = 0; i < internalSections.size(); i += 1)
108-
{
109-
sections[i].name =
110-
(char *)malloc(strlen(internalSections[i].name.c_str()) + 1);
111-
strcpy(sections[i].name, internalSections[i].name.c_str());
112-
113-
sections[i].lines = (KeyValuePairInternal *)malloc(
114-
internalSections[i].lines.size() *
115-
sizeof(KeyValuePairInternal));
116-
117-
sections[i].lineCount = internalSections[i].lines.size();
118-
119-
for (auto j = 0; j < internalSections[i].lines.size(); j += 1)
120-
{
121-
sections[i].lines[j].key = (char *)malloc(
122-
strlen(internalSections[i].lines[j].first.c_str()) + 1);
123-
strcpy(sections[i].lines[j].key,
124-
internalSections[i].lines[j].first.c_str());
125-
126-
auto values = internalSections[i].lines[j].second;
127-
128-
for (auto k = 0; k < values.size(); k += 1)
129-
{
130-
sections[i].lines[j].values[k] =
131-
(char *)malloc(strlen(values[k].c_str()) + 1);
132-
strcpy(sections[i].lines[j].values[k], values[k].c_str());
133-
}
134-
135-
sections[i].lines[j].valueCount = values.size();
136-
}
137-
}
138-
139-
return sections;
140-
}
141-
}
142-
14394
} // namespace RhythmGameUtilities
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
3+
#include "Parsers.hpp"
4+
#include "Utilities.hpp"
5+
6+
#ifdef _WIN32
7+
#define PACKAGE_API __declspec(dllexport)
8+
#else
9+
#define PACKAGE_API
10+
#endif
11+
12+
namespace RhythmGameUtilities
13+
{
14+
15+
extern "C"
16+
{
17+
PACKAGE_API
18+
ChartSectionInternal *ParseSectionsFromChartInternal(const char *contents,
19+
int *outSize)
20+
{
21+
auto internalSections = ParseSectionsFromChart(contents);
22+
23+
*outSize = internalSections.size();
24+
25+
auto sections = (ChartSectionInternal *)malloc(
26+
internalSections.size() * sizeof(ChartSectionInternal));
27+
28+
for (auto i = 0; i < internalSections.size(); i += 1)
29+
{
30+
sections[i].name =
31+
(char *)malloc(strlen(internalSections[i].name.c_str()) + 1);
32+
strcpy(sections[i].name, internalSections[i].name.c_str());
33+
34+
sections[i].lines = (KeyValuePairInternal *)malloc(
35+
internalSections[i].lines.size() *
36+
sizeof(KeyValuePairInternal));
37+
38+
sections[i].lineCount = internalSections[i].lines.size();
39+
40+
for (auto j = 0; j < internalSections[i].lines.size(); j += 1)
41+
{
42+
sections[i].lines[j].key = (char *)malloc(
43+
strlen(internalSections[i].lines[j].first.c_str()) + 1);
44+
strcpy(sections[i].lines[j].key,
45+
internalSections[i].lines[j].first.c_str());
46+
47+
auto values = internalSections[i].lines[j].second;
48+
49+
for (auto k = 0; k < values.size(); k += 1)
50+
{
51+
sections[i].lines[j].values[k] =
52+
(char *)malloc(strlen(values[k].c_str()) + 1);
53+
strcpy(sections[i].lines[j].values[k], values[k].c_str());
54+
}
55+
56+
sections[i].lines[j].valueCount = values.size();
57+
}
58+
}
59+
60+
return sections;
61+
}
62+
}
63+
64+
} // namespace RhythmGameUtilities

includes/RhythmGameUtilities/RhythmGameUtilities.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
#include "Audio.hpp"
44
#include "Parsers.hpp"
5+
#include "ParsersInternal.hpp"
56
#include "Utilities.hpp"
7+
#include "UtilitiesInternal.hpp"

includes/RhythmGameUtilities/Utilities.hpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,6 @@ extern "C"
112112
return tick / resolution;
113113
}
114114

115-
PACKAGE_API int ConvertSecondsToTicksInternal(float seconds, int resolution,
116-
int *bpmChangesKeys,
117-
int *bpmChangesValues,
118-
int bpmChangesSize)
119-
{
120-
std::map<int, int> bpmChanges;
121-
122-
for (auto i = 0; i < bpmChangesSize; i += 1)
123-
{
124-
bpmChanges[bpmChangesKeys[i]] = bpmChangesValues[i];
125-
}
126-
127-
return ConvertSecondsToTicks(seconds, resolution, bpmChanges);
128-
}
129-
130115
PACKAGE_API bool IsOnTheBeat(float bpm, float currentTime)
131116
{
132117
auto beatInterval = SECONDS_PER_MINUTE / bpm;
@@ -154,34 +139,6 @@ extern "C"
154139
{
155140
return std::clamp(((v - a) / (b - a)), 0.0f, 1.0f);
156141
}
157-
158-
PACKAGE_API BeatBar *
159-
CalculateBeatBarsInternal(int *bpmChangesKeys, int *bpmChangesValues,
160-
int bpmChangesSize, int resolution, int ts,
161-
bool includeHalfNotes, int *outSize)
162-
{
163-
auto bpmChanges = std::map<int, int>();
164-
165-
for (auto i = 0; i < bpmChangesSize; i += 1)
166-
{
167-
bpmChanges[bpmChangesKeys[i]] = bpmChangesValues[i];
168-
}
169-
170-
auto internalBeatBars =
171-
CalculateBeatBars(bpmChanges, resolution, ts, includeHalfNotes);
172-
173-
*outSize = internalBeatBars.size();
174-
175-
auto beatBars =
176-
(BeatBar *)malloc(internalBeatBars.size() * sizeof(BeatBar));
177-
178-
for (auto i = 0; i < internalBeatBars.size(); i += 1)
179-
{
180-
beatBars[i] = internalBeatBars[i];
181-
}
182-
183-
return beatBars;
184-
}
185142
}
186143

187144
std::string Trim(const char *contents)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
3+
#include <map>
4+
5+
#include "Structs/BeatBar.h"
6+
7+
#include "Utilities.hpp"
8+
9+
#ifdef _WIN32
10+
#define PACKAGE_API __declspec(dllexport)
11+
#else
12+
#define PACKAGE_API
13+
#endif
14+
15+
namespace RhythmGameUtilities
16+
{
17+
18+
extern "C"
19+
{
20+
PACKAGE_API int ConvertSecondsToTicksInternal(float seconds, int resolution,
21+
int *bpmChangesKeys,
22+
int *bpmChangesValues,
23+
int bpmChangesSize)
24+
{
25+
std::map<int, int> bpmChanges;
26+
27+
for (auto i = 0; i < bpmChangesSize; i += 1)
28+
{
29+
bpmChanges[bpmChangesKeys[i]] = bpmChangesValues[i];
30+
}
31+
32+
return ConvertSecondsToTicks(seconds, resolution, bpmChanges);
33+
}
34+
35+
PACKAGE_API BeatBar *
36+
CalculateBeatBarsInternal(int *bpmChangesKeys, int *bpmChangesValues,
37+
int bpmChangesSize, int resolution, int ts,
38+
bool includeHalfNotes, int *outSize)
39+
{
40+
auto bpmChanges = std::map<int, int>();
41+
42+
for (auto i = 0; i < bpmChangesSize; i += 1)
43+
{
44+
bpmChanges[bpmChangesKeys[i]] = bpmChangesValues[i];
45+
}
46+
47+
auto internalBeatBars =
48+
CalculateBeatBars(bpmChanges, resolution, ts, includeHalfNotes);
49+
50+
*outSize = internalBeatBars.size();
51+
52+
auto beatBars =
53+
(BeatBar *)malloc(internalBeatBars.size() * sizeof(BeatBar));
54+
55+
for (auto i = 0; i < internalBeatBars.size(); i += 1)
56+
{
57+
beatBars[i] = internalBeatBars[i];
58+
}
59+
60+
return beatBars;
61+
}
62+
}
63+
64+
} // namespace RhythmGameUtilities

tests/RhythmGameUtilities/Parsers.cpp

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <cstring>
33
#include <iostream>
44

5-
#include "RhythmGameUtilities/Parsers.hpp"
5+
#include "RhythmGameUtilities/ParsersInternal.hpp"
66

77
using namespace RhythmGameUtilities;
88

@@ -69,41 +69,6 @@ auto contents = R"([Song]
6969
1248 = E soloend
7070
})";
7171

72-
void testParseSectionsFromChart()
73-
{
74-
auto sections = ParseSectionsFromChart(contents);
75-
76-
assert(sections.size() == 4);
77-
78-
assert(sections.at(0).name == "Song");
79-
assert(sections.at(1).name == "SyncTrack");
80-
assert(sections.at(2).name == "Events");
81-
assert(sections.at(3).name == "ExpertSingle");
82-
83-
std::cout << ".";
84-
}
85-
86-
void testParseValuesFromChartSections()
87-
{
88-
auto sections = ParseSectionsFromChart(contents);
89-
90-
assert(sections.size() == 4);
91-
92-
assert(sections.at(0).name == "Song");
93-
assert(sections.at(0).lines.size() == 12);
94-
95-
assert(sections.at(0).lines[0].first == "Name");
96-
assert(sections.at(0).lines[0].second[0] == "Example Song");
97-
98-
assert(sections.at(0).lines[6].first == "Resolution");
99-
assert(sections.at(0).lines[6].second[0] == "192");
100-
101-
assert(sections.at(0).lines[11].first == "MusicStream");
102-
assert(sections.at(0).lines[11].second[0] == "Example Song.ogg");
103-
104-
std::cout << ".";
105-
}
106-
10772
void testParseSectionsFromChartInternal()
10873
{
10974
int size = 0;
@@ -148,8 +113,6 @@ void testParseValuesFromChartSectionsInternal()
148113

149114
int main()
150115
{
151-
testParseSectionsFromChart();
152-
testParseValuesFromChartSections();
153116
testParseSectionsFromChartInternal();
154117
testParseValuesFromChartSectionsInternal();
155118

0 commit comments

Comments
 (0)