2021-12-31 00:57:06 +01:00
|
|
|
#include "marker.hpp"
|
2017-08-17 19:10:53 +02:00
|
|
|
|
2022-01-04 01:31:17 +01:00
|
|
|
Marker first_available_marker_from_folder(std::filesystem::path assets_folder) {
|
|
|
|
for (auto& folder : std::filesystem::directory_iterator(assets_folder / "textures" / "markers")) {
|
|
|
|
try {
|
|
|
|
return Marker{folder};
|
|
|
|
} catch (const std::runtime_error&) {}
|
2019-01-18 01:25:29 +01:00
|
|
|
}
|
|
|
|
throw std::runtime_error("No valid marker found");
|
|
|
|
}
|
2017-08-17 19:10:53 +02:00
|
|
|
|
2019-01-18 01:25:29 +01:00
|
|
|
Marker::Marker(std::filesystem::path folder) {
|
2022-01-04 01:31:17 +01:00
|
|
|
if (not validMarkerFolder(folder)) {
|
|
|
|
std::stringstream err;
|
|
|
|
err << "Invalid marker folder : " << folder.string();
|
|
|
|
throw std::runtime_error(err.str());
|
2021-12-31 14:59:39 +01:00
|
|
|
}
|
2022-01-04 01:31:17 +01:00
|
|
|
|
|
|
|
initFromFolder(folder);
|
|
|
|
return;
|
2017-08-17 19:10:53 +02:00
|
|
|
}
|
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
std::optional<std::reference_wrapper<sf::Texture>>
|
|
|
|
Marker::getSprite(MarkerEndingState state, float seconds) {
|
|
|
|
std::ostringstream frameName;
|
|
|
|
int frame = static_cast<int>((seconds * 30.f + 16.f));
|
|
|
|
if (frame >= 0 and frame <= 15) {
|
|
|
|
frameName << "ma" << std::setfill('0') << std::setw(2) << frame;
|
|
|
|
std::string frameStr = frameName.str();
|
|
|
|
return textures[frameName.str()];
|
|
|
|
} else {
|
|
|
|
if (state == MarkerEndingState_MISS) {
|
|
|
|
if (frame >= 16 and frame <= 23) {
|
|
|
|
frameName << "ma" << std::setfill('0') << std::setw(2) << frame;
|
|
|
|
return textures[frameName.str()];
|
|
|
|
}
|
|
|
|
} else if (frame >= 16 and frame <= 31) {
|
|
|
|
switch (state) {
|
|
|
|
case MarkerEndingState_EARLY:
|
|
|
|
frameName << "h1";
|
|
|
|
break;
|
|
|
|
case MarkerEndingState_GOOD:
|
|
|
|
frameName << "h2";
|
|
|
|
break;
|
|
|
|
case MarkerEndingState_GREAT:
|
|
|
|
frameName << "h3";
|
|
|
|
break;
|
|
|
|
case MarkerEndingState_PERFECT:
|
|
|
|
frameName << "h4";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
frameName << std::setfill('0') << std::setw(2) << frame - 16;
|
|
|
|
return textures[frameName.str()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
2019-01-14 21:43:56 +01:00
|
|
|
}
|
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
const std::map<std::string, sf::Texture>& Marker::getTextures() const {
|
|
|
|
return textures;
|
2019-01-17 15:37:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 01:25:29 +01:00
|
|
|
bool Marker::validMarkerFolder(std::filesystem::path folder) {
|
|
|
|
std::stringstream filename;
|
|
|
|
// ma00 ~ ma23
|
|
|
|
for (int i = 0; i < 24; ++i) {
|
2021-12-31 14:59:39 +01:00
|
|
|
filename.str("");
|
2019-01-18 01:25:29 +01:00
|
|
|
filename << "ma" << std::setw(2) << std::setfill('0') << i << ".png";
|
|
|
|
std::string s_filename = filename.str();
|
2021-12-31 14:59:39 +01:00
|
|
|
if (not std::filesystem::exists(folder / filename.str())) {
|
|
|
|
return false;
|
2019-01-18 01:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-31 14:59:39 +01:00
|
|
|
// h100 ~ h115 + h200 ~ h215 + h300 ~ h315 + h400 ~ h415
|
|
|
|
for (int j = 1; j <= 4; ++j) {
|
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
filename.str("");
|
|
|
|
filename << "h" << 100 * j + i << ".png";
|
|
|
|
std::string s_filename = filename.str();
|
|
|
|
if (not std::filesystem::exists(folder / filename.str())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2019-01-18 01:25:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Marker::initFromFolder(std::filesystem::path folder) {
|
2021-12-31 14:59:39 +01:00
|
|
|
textures.clear();
|
|
|
|
path = folder;
|
|
|
|
|
|
|
|
// Chargement des h100~115 / h200~215 / h300~h315 / h400~415
|
|
|
|
for (int sup = 1; sup <= 4; sup++) {
|
|
|
|
for (int i = 0; i <= 15; i++) {
|
|
|
|
sf::Texture tex;
|
|
|
|
if (!tex.loadFromFile(path.string() + "/h" + std::to_string(i + 100 * sup) + ".png")) {
|
|
|
|
std::stringstream err;
|
|
|
|
err << "Unable to load marker " << folder << "\nfailed on image"
|
|
|
|
<< (path.string() + "/h" + std::to_string(i + 100 * sup)
|
|
|
|
+ ".png");
|
|
|
|
throw std::runtime_error(err.str());
|
|
|
|
}
|
|
|
|
tex.setSmooth(true);
|
|
|
|
textures.insert({"h" + std::to_string(i + 100 * sup), tex});
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 01:25:29 +01:00
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
// Chargement de ma00~23
|
|
|
|
for (int i = 0; i <= 23; i++) {
|
|
|
|
sf::Texture tex;
|
|
|
|
std::string fichier;
|
|
|
|
if (i < 10) {
|
|
|
|
fichier = "ma0" + std::to_string(i);
|
|
|
|
} else {
|
|
|
|
fichier = "ma" + std::to_string(i);
|
|
|
|
}
|
2019-01-18 01:25:29 +01:00
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
if (!tex.loadFromFile(path.string() + "/" + fichier + ".png")) {
|
|
|
|
std::stringstream err;
|
|
|
|
err << "Unable to load marker " << folder << "\nfailed on image"
|
|
|
|
<< (path.string() + "/" + fichier + ".png");
|
|
|
|
throw std::runtime_error(err.str());
|
|
|
|
}
|
|
|
|
tex.setSmooth(true);
|
|
|
|
textures.insert({fichier, tex});
|
|
|
|
}
|
2019-01-18 01:25:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:43:56 +01:00
|
|
|
/*
|
|
|
|
sf::Texture Marker::getSprite(MarkerEndingState state, int frame) {
|
2017-08-17 19:10:53 +02:00
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
int lower;
|
|
|
|
int upper;
|
|
|
|
switch(state) {
|
|
|
|
case MarkerEndingState_MISS:
|
|
|
|
lower = 16;
|
|
|
|
upper = 32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lower = 0;
|
|
|
|
upper = 15;
|
|
|
|
}
|
2017-08-17 19:10:53 +02:00
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
if (!(frame >= lower and frame <= upper)) {
|
|
|
|
std::cerr << "Requested access to a non-existent marker frame :
|
|
|
|
" << frame; throw std::runtime_error("Requested access to a non-existent marker
|
|
|
|
frame : " +std::to_string(frame));
|
|
|
|
}
|
2017-08-17 19:10:53 +02:00
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
std::string tex_key;
|
|
|
|
switch (state) {
|
|
|
|
case MarkerEndingState_MISS:
|
|
|
|
tex_key += "ma";
|
|
|
|
break;
|
|
|
|
case MarkerEndingState_EARLY:
|
|
|
|
tex_key += "h1";
|
|
|
|
break;
|
|
|
|
case MarkerEndingState_GOOD:
|
|
|
|
tex_key += "h2";
|
|
|
|
break;
|
|
|
|
case MarkerEndingState_GREAT:
|
|
|
|
tex_key += "h3";
|
|
|
|
break;
|
|
|
|
case MarkerEndingState_PERFECT:
|
|
|
|
tex_key += "h4";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (frame < 10) {
|
|
|
|
tex_key += "0";
|
|
|
|
}
|
2017-08-17 19:10:53 +02:00
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
return textures[tex_key+std::to_string(frame)];
|
2017-08-17 19:10:53 +02:00
|
|
|
|
|
|
|
}
|
2019-01-14 21:43:56 +01:00
|
|
|
*/
|