Skip to content

Commit

Permalink
Refactor (move bezier to input)
Browse files Browse the repository at this point in the history
TODO: Maybe put get_xy to input as well
  • Loading branch information
kuroni committed Aug 13, 2020
1 parent 5aaddf9 commit 6c4ca9b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
9 changes: 3 additions & 6 deletions include/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ sf::Texture &load_texture(std::string path);
}; // namespace data

namespace input {
void init();
void init();

sf::Keyboard::Key ascii_to_key(int ascii_key);
bool is_pressed(int key_code);

bool is_pressed(int ascii_key);

bool is_pressed(sf::Keyboard::Key key);
std::tuple<double, double> bezier(double ratio, std::vector<double> &points, int length);
}; // namespace input

namespace osu {
Expand All @@ -44,7 +42,6 @@ bool init();
void draw();

void cleanup();

}; // namespace osu

namespace taiko {
Expand Down
2 changes: 1 addition & 1 deletion src/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct key_container {
if (key_state > -1) {
key& on_key = keys[key_state];
double last_press = -1;
for (int i = 0; i < keys.size(); i++) {
for (int i = 0; i < (int)keys.size(); i++) {
if (i != key_state) {
last_press = std::max(last_press, keys[i].timer);
}
Expand Down
15 changes: 15 additions & 0 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,20 @@ bool is_pressed(int key_code) {
return sf::Keyboard::isKeyPressed(ascii_to_key(key_code));
}

// bezier curve for osu and custom
std::tuple<double, double> bezier(double ratio, std::vector<double> &points, int length) {
double fact[22] = {0.001, 0.001, 0.002, 0.006, 0.024, 0.12, 0.72, 5.04, 40.32, 362.88, 3628.8, 39916.8, 479001.6, 6227020.8, 87178291.2, 1307674368.0, 20922789888.0, 355687428096.0, 6402373705728.0, 121645100408832.0, 2432902008176640.0, 51090942171709440.0};
int nn = (length / 2) - 1;
double xx = 0;
double yy = 0;

for (int point = 0; point <= nn; point++) {
double tmp = fact[nn] / (fact[point] * fact[nn - point]) * pow(ratio, point) * pow(1 - ratio, nn - point);
xx += points[2 * point] * tmp;
yy += points[2 * point + 1] * tmp;
}

return std::make_tuple(xx / 1000, yy / 1000);
}
};

1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
is_reload = true;
break;
}

default:
is_reload = false;
}
Expand Down
24 changes: 4 additions & 20 deletions src/osu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,6 @@ static int _XlibErrorHandler(Display *display, XErrorEvent *event) {
}
#endif

// bezier curve for osu and custom
std::tuple<double, double> bezier(double ratio, std::vector<double> &points, int length) {
double fact[22] = {0.001, 0.001, 0.002, 0.006, 0.024, 0.12, 0.72, 5.04, 40.32, 362.88, 3628.8, 39916.8, 479001.6, 6227020.8, 87178291.2, 1307674368.0, 20922789888.0, 355687428096.0, 6402373705728.0, 121645100408832.0, 2432902008176640.0, 51090942171709440.0};
int nn = (length / 2) - 1;
double xx = 0;
double yy = 0;

for (int point = 0; point <= nn; point++) {
double tmp = fact[nn] / (fact[point] * fact[nn - point]) * pow(ratio, point) * pow(1 - ratio, nn - point);
xx += points[2 * point] * tmp;
yy += points[2 * point + 1] * tmp;
}

return std::make_tuple(xx / 1000, yy / 1000);
}

bool init() {
// getting configs
Json::Value osu = data::cfg["osu"];
Expand Down Expand Up @@ -355,7 +339,7 @@ void draw() {
double centreleft1 = 159 + 0.69 * dist / 2;
for (int i = 1; i < oof; i++) {
std::vector<double> bez = {211, 159, centreleft0, centreleft1, x, y};
auto [p0, p1] = bezier(1.0 * i / oof, bez, 6);
auto [p0, p1] = input::bezier(1.0 * i / oof, bez, 6);
pss.push_back(p0);
pss.push_back(p1);
}
Expand Down Expand Up @@ -384,15 +368,15 @@ void draw() {
t2 *= push / le;
for (int i = 1; i < oof; i++) {
std::vector<double> bez = {x, y, x + s, y + t, a + s2, b + t2, a, b};
auto [p0, p1] = bezier(1.0 * i / oof, bez, 8);
auto [p0, p1] = input::bezier(1.0 * i / oof, bez, 8);
pss.push_back(p0);
pss.push_back(p1);
}
pss.push_back(a);
pss.push_back(b);
for (int i = oof - 1; i > 0; i--) {
std::vector<double> bez = {1.0 * a1, 1.0 * a2, centreright0, centreright1, a, b};
auto [p0, p1] = bezier(1.0 * i / oof, bez, 6);
auto [p0, p1] = input::bezier(1.0 * i / oof, bez, 6);
pss.push_back(p0);
pss.push_back(p1);
}
Expand All @@ -407,7 +391,7 @@ void draw() {

std::vector<double> pss2 = {pss[0] + dx, pss[1] + dy};
for (int i = 1; i < iter; i++) {
auto [p0, p1] = bezier(1.0 * i / iter, pss, 38);
auto [p0, p1] = input::bezier(1.0 * i / iter, pss, 38);
pss2.push_back(p0 + dx);
pss2.push_back(p1 + dy);
}
Expand Down

0 comments on commit 6c4ca9b

Please sign in to comment.