Skip to content

Commit

Permalink
Big refactor and add custom mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kuroni committed Jul 18, 2020
1 parent 08946fb commit 10c74dd
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 32 deletions.
7 changes: 7 additions & 0 deletions include/header.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#define BONGO_KEYPRESS_THRESHOLD 0.031

#include <iostream>
#include <fstream>
Expand Down Expand Up @@ -50,3 +51,9 @@ bool init();

void draw();
}; // namespace mania

namespace custom {
bool init();

void draw();
}; // namespace custom
12 changes: 7 additions & 5 deletions src/ctb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ double timer_right_key = -1;

bool init() {
// getting configs
Json::Value ctb = data::cfg["catch"];

bool chk[256];
std::fill(chk, chk + 256, false);
left_key_value = data::cfg["catch"]["left"];
left_key_value = ctb["left"];
for (Json::Value &v : left_key_value) {
chk[v.asInt()] = true;
}
right_key_value = data::cfg["catch"]["right"];
right_key_value = ctb["right"];
for (Json::Value &v : right_key_value) {
if (chk[v.asInt()]) {
data::error_msg("Overlapping osu!catch keybinds", "Error reading configs");
return false;
}
}
dash_key_value = data::cfg["catch"]["dash"];
dash_key_value = ctb["dash"];

// importing sprites
bg.setTexture(data::load_texture("img/catch/bg.png"));
Expand Down Expand Up @@ -79,14 +81,14 @@ void draw() {
window.draw(mid);
}
if (key_state == 1) {
if ((clock() - timer_right_key) / CLOCKS_PER_SEC > 0.031) {
if ((clock() - timer_right_key) / CLOCKS_PER_SEC > BONGO_KEYPRESS_THRESHOLD) {
window.draw(left);
timer_left_key = clock();
} else {
window.draw(mid);
}
} else if (key_state == 2) {
if ((clock() - timer_left_key) / CLOCKS_PER_SEC > 0.031) {
if ((clock() - timer_left_key) / CLOCKS_PER_SEC > BONGO_KEYPRESS_THRESHOLD) {
window.draw(right);
timer_right_key = clock();
} else {
Expand Down
135 changes: 135 additions & 0 deletions src/custom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "header.hpp"

namespace custom {
struct key {
Json::Value key_value;
sf::Sprite sprite;
bool status;
double timer;

key(Json::Value _key_value) {
sprite = sf::Sprite();
if (_key_value.isMember("keyCodes") && _key_value["keyCodes"].isArray()) {
key_value = _key_value["keyCodes"];
} else {
data::error_msg("Custom keyCodes values is not set correctly", "Error reading configs");
throw;
}
if (_key_value.isMember("image") && _key_value["image"].isString()) {
sprite = sf::Sprite();
sprite.setTexture(data::load_texture(_key_value["image"].asString()));
} else {
data::error_msg("Custom image path is not set correctly", "Error reading configs");
throw;
}
status = false;
timer = -1;
}

bool is_pressed() {
for (Json::Value &v : key_value) {
if (GetKeyState(v.asInt()) & WM_KEYDOWN) {
return true;
}
}
return false;
}

void draw() {
window.draw(sprite);
timer = clock();
}
};

struct key_container {
std::vector<key> keys;
sf::Sprite default_sprite;
int key_state;

key_container(Json::Value key_container_value) {
key_state = -1;
if (key_container_value.isObject()) {
if (!key_container_value.isMember("defaultImage")
|| !key_container_value["defaultImage"].isString()
|| !key_container_value.isMember("keys")
|| !key_container_value["keys"].isArray()) {
data::error_msg("Key container's object error", "Error reading configs");
throw;
} else {
default_sprite = sf::Sprite();
default_sprite.setTexture(data::load_texture(key_container_value["defaultImage"].asString()));
for (Json::Value &child_key : key_container_value["keys"]) {
keys.push_back(key(child_key));
}
}
} else {
data::error_msg("Key container must be an object", "Error reading configs");
throw;
}
}

void draw() {
bool is_any_key_pressed = false;
for (int i = 0; i < keys.size(); i++) {
key& current_key = keys[i];
if (current_key.is_pressed()) {
is_any_key_pressed = true;
if (!current_key.status) {
key_state = i;
current_key.status = true;
}
} else {
current_key.status = false;
}
}
if (!is_any_key_pressed) {
key_state = -1;
window.draw(default_sprite);
}
if (key_state > -1) {
key& on_key = keys[key_state];
double last_press = -1;
for (int i = 0; i < keys.size(); i++) {
if (i != key_state) {
last_press = std::max(last_press, keys[i].timer);
}
}
if ((clock() - last_press) / CLOCKS_PER_SEC > BONGO_KEYPRESS_THRESHOLD) {
on_key.draw();
} else {
window.draw(default_sprite);
}
}
}
};

std::vector<key_container> key_containers;
sf::Sprite bg;

bool init() {
// getting configs
try {
Json::Value custom = data::cfg["custom"];
key_containers.clear();
for (Json::Value& current_key_container : custom["keyContainers"]) {
key_containers.push_back(key_container(current_key_container));
}
if (!custom.isMember("background") || !custom["background"].isString()) {
data::error_msg("Custom background not found", "Error reading config");
return false;
}
bg.setTexture(data::load_texture(custom["background"].asString()));
} catch (...) {
return false;
}
return true;
}

void draw() {
window.draw(bg);

for (key_container& current : key_containers) {
current.draw();
}
}
}; // namespace taiko
13 changes: 10 additions & 3 deletions src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void create_config() {
"key1": [90],
"key2": [88],
"smoke": [67],
"wave": [86]
"wave": []
},
"taiko": {
"leftCentre": [88],
Expand All @@ -47,6 +47,9 @@ void create_config() {
"4K": true,
"key4K": [68, 70, 74, 75],
"key7K": [83, 68, 70, 32, 74, 75, 76]
},
"custom": {
"keyContainers": []
}
})V0G0N";
std::string error;
Expand All @@ -70,10 +73,10 @@ bool update(Json::Value &cfg_default, Json::Value &cfg) {
error_msg("Value type error in config.json", "Error reading configs");
return false;
}
if (cfg_default[key].isArray()) {
if (cfg_default[key].isArray() && !cfg_default[key].empty()) {
for (Json::Value &v : cfg[key]) {
if (v.type() != cfg_default[key][0].type()) {
error_msg("Value type error in config.json", "Error reading configs");
error_msg("Value type in array error in config.json", "Error reading configs");
return false;
}
}
Expand All @@ -83,6 +86,8 @@ bool update(Json::Value &cfg_default, Json::Value &cfg) {
} else {
cfg_default[key] = cfg[key];
}
} else {
cfg_default[key] = cfg[key];
}
}
return is_update;
Expand Down Expand Up @@ -121,6 +126,8 @@ bool init() {
return ctb::init();
case 4:
return mania::init();
case 5:
return custom::init();
default:
error_msg("Mode value is not correct", "Error reading configs");
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
case 4:
mania::draw();
break;
case 5:
custom::draw();
}

window.display();
Expand Down
12 changes: 7 additions & 5 deletions src/mania.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ bool is_4K;

bool init() {
// getting configs
is_4K = data::cfg["mania"]["4K"].asBool();
Json::Value mania = data::cfg["mania"];

is_4K = mania["4K"].asBool();

for (int i = 0; i < 2; i++) {
left_key_value_4K[i] = data::cfg["mania"]["key4K"][i].asInt();
left_key_value_4K[i] = mania["key4K"][i].asInt();
}
for (int i = 0; i < 2; i++) {
right_key_value_4K[i] = data::cfg["mania"]["key4K"][i + 2].asInt();
right_key_value_4K[i] = mania["key4K"][i + 2].asInt();
}

for (int i = 0; i < 4; i++) {
left_key_value_7K[i] = data::cfg["mania"]["key7K"][i].asInt();
left_key_value_7K[i] = mania["key7K"][i].asInt();
}
for (int i = 0; i < 4; i++) {
right_key_value_7K[i] = data::cfg["mania"]["key7K"][i + 3].asInt();
right_key_value_7K[i] = mania["key7K"][i + 3].asInt();
}

// importing sprites
Expand Down
26 changes: 14 additions & 12 deletions src/osu.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "header.hpp"
#define BONGO_KEYPRESS_THRESHOLD 0.031

namespace osu {
Json::Value left_key_value, right_key_value, smoke_key_value, wave_key_value;
Expand All @@ -20,6 +19,7 @@ double timer_left_key = -1;
double timer_right_key = -1;
double timer_wave_key = -1;

// 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;
Expand All @@ -37,37 +37,39 @@ std::tuple<double, double> bezier(double ratio, std::vector<double> &points, int

bool init() {
// getting configs
is_mouse = data::cfg["osu"]["mouse"].asBool();
Json::Value osu = data::cfg["osu"];

paw_r = data::cfg["osu"]["paw"][0].asInt();
paw_g = data::cfg["osu"]["paw"][1].asInt();
paw_b = data::cfg["osu"]["paw"][2].asInt();
is_mouse = osu["mouse"].asBool();

paw_edge_r = data::cfg["osu"]["pawEdge"][0].asInt();
paw_edge_g = data::cfg["osu"]["pawEdge"][1].asInt();
paw_edge_b = data::cfg["osu"]["pawEdge"][2].asInt();
paw_r = osu["paw"][0].asInt();
paw_g = osu["paw"][1].asInt();
paw_b = osu["paw"][2].asInt();

paw_edge_r = osu["pawEdge"][0].asInt();
paw_edge_g = osu["pawEdge"][1].asInt();
paw_edge_b = osu["pawEdge"][2].asInt();

bool chk[256];
std::fill(chk, chk + 256, false);
left_key_value = data::cfg["osu"]["key1"];
left_key_value = osu["key1"];
for (Json::Value &v : left_key_value) {
chk[v.asInt()] = true;
}
right_key_value = data::cfg["osu"]["key2"];
right_key_value = osu["key2"];
for (Json::Value &v : right_key_value) {
if (chk[v.asInt()]) {
data::error_msg("Overlapping osu! keybinds", "Error reading configs");
return false;
}
}
wave_key_value = data::cfg["osu"]["wave"];
wave_key_value = osu["wave"];
for (Json::Value &v : wave_key_value) {
if (chk[v.asInt()]) {
data::error_msg("Overlapping osu! keybinds", "Error reading configs");
return false;
}
}
smoke_key_value = data::cfg["osu"]["smoke"];
smoke_key_value = osu["smoke"];

is_letterbox = data::cfg["resolution"]["letterboxing"].asBool();
osu_x = data::cfg["resolution"]["width"].asInt();
Expand Down
15 changes: 8 additions & 7 deletions src/taiko.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ double timer_centre_key[2] = {-1, -1};
bool init() {
// getting configs
bool chk[256];

std::fill(chk, chk + 256, false);
rim_key_value[0] = data::cfg["taiko"]["leftRim"];
Json::Value taiko = data::cfg["taiko"];

rim_key_value[0] = taiko["leftRim"];
for (Json::Value &v : rim_key_value[0]) {
chk[v.asInt()] = true;
}
centre_key_value[0] = data::cfg["taiko"]["leftCentre"];
centre_key_value[0] = taiko["leftCentre"];
for (Json::Value &v : centre_key_value[0]) {
if (chk[v.asInt()]) {
data::error_msg("Overlapping osu!taiko keybinds", "Error reading configs");
Expand All @@ -28,11 +29,11 @@ bool init() {
}

std::fill(chk, chk + 256, false);
rim_key_value[1] = data::cfg["taiko"]["rightRim"];
rim_key_value[1] = taiko["rightRim"];
for (Json::Value &v : rim_key_value[1]) {
chk[v.asInt()] = true;
}
centre_key_value[1] = data::cfg["taiko"]["rightCentre"];
centre_key_value[1] = taiko["rightCentre"];
for (Json::Value &v : centre_key_value[1]) {
if (chk[v.asInt()]) {
data::error_msg("Overlapping osu!taiko keybinds", "Error reading configs");
Expand Down Expand Up @@ -94,14 +95,14 @@ void draw() {
window.draw(up[i]);
}
if (key_state[i] == 1) {
if ((clock() - timer_centre_key[i]) / CLOCKS_PER_SEC > 0.031) {
if ((clock() - timer_centre_key[i]) / CLOCKS_PER_SEC > BONGO_KEYPRESS_THRESHOLD) {
window.draw(rim[i]);
timer_rim_key[i] = clock();
} else {
window.draw(up[i]);
}
} else if (key_state[i] == 2) {
if ((clock() - timer_rim_key[i]) / CLOCKS_PER_SEC > 0.031) {
if ((clock() - timer_rim_key[i]) / CLOCKS_PER_SEC > BONGO_KEYPRESS_THRESHOLD) {
window.draw(centre[i]);
timer_centre_key[i] = clock();
} else {
Expand Down

0 comments on commit 10c74dd

Please sign in to comment.