mirror of
https://github.com/haya3218/SDfmL.git
synced 2024-08-14 23:57:09 +00:00
Refactor code
This commit is contained in:
parent
9a37d1140d
commit
d20212fe33
2 changed files with 33 additions and 15 deletions
|
@ -207,22 +207,13 @@ bool Render::Init(string window_name) {
|
|||
logFile.open("log.txt", std::ofstream::out | std::ofstream::trunc);
|
||||
logFile.close();
|
||||
|
||||
// "touch" the file first
|
||||
std::ofstream config;
|
||||
config.open("conf.toml", std::ios::app);
|
||||
config.close();
|
||||
fstream oFile("conf.toml");
|
||||
oFile.seekg(0,std::ios::end);
|
||||
unsigned int size = oFile.tellg();
|
||||
if(!size) {
|
||||
// if file doesnt actually have anything (which it should when the file doesnt exist... yet)
|
||||
std::ofstream config;
|
||||
config.open("conf.toml", std::ios::app);
|
||||
config << "[config]" << endl
|
||||
<< "soundfont = \"data/gm.sf2\"" << endl;
|
||||
config.close();
|
||||
if (!touchFile("conf.toml"))
|
||||
{
|
||||
tomlExport("conf.toml",
|
||||
{
|
||||
{"config", {{"soundfont", "data/gm.sf2"}}}
|
||||
});
|
||||
}
|
||||
oFile.close();
|
||||
SOUNDFONT = tomlParse<string>("conf.toml", "config", "soundfont");
|
||||
if (se.init() > 0) {
|
||||
log("Render.cpp", 202, "SoLoud", " has failed to load. Is your dll broken?", ERROR_);
|
||||
|
|
|
@ -279,6 +279,8 @@ namespace Render {
|
|||
return FRAMERATE*time;
|
||||
}
|
||||
|
||||
// Parse a TOML at a given path with a table and key.
|
||||
// Setting table name to "" will look for the key without a table.
|
||||
template <typename T>
|
||||
T tomlParse(string path, string table_name = "", string key = "") {
|
||||
auto parsed = toml::parse(path);
|
||||
|
@ -288,5 +290,30 @@ namespace Render {
|
|||
}
|
||||
return toml::find<T>(parsed, key);
|
||||
}
|
||||
|
||||
// "touch" a file at a given path.
|
||||
// Returns false if file is empty, returns true if not
|
||||
inline bool touchFile(string path) {
|
||||
std::ofstream file;
|
||||
file.open(path, std::ios::app);
|
||||
file.close();
|
||||
fstream oFile(path);
|
||||
oFile.seekg(0,std::ios::end);
|
||||
unsigned int size = oFile.tellg();
|
||||
if (!size) {
|
||||
oFile.close();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Export a TOML file to a path from a toml::value.
|
||||
inline void tomlExport(string path, toml::value values) {
|
||||
string export_ = toml::format(values, 0, 2);
|
||||
std::ofstream config;
|
||||
config.open(path, std::ofstream::out | std::ofstream::trunc);
|
||||
config << export_ << endl;
|
||||
config.close();
|
||||
}
|
||||
}
|
||||
#endif
|
Loading…
Reference in a new issue