Added initial JSON configuration support
This commit is contained in:
@@ -3,12 +3,14 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace std;
|
||||
using nlohmann::json;
|
||||
|
||||
map<pair<string, int>, TTF_Font*> font_map;
|
||||
|
||||
@@ -74,3 +76,49 @@ TTF_Font* get_font(const string& filename, int size)
|
||||
return font;
|
||||
}
|
||||
}
|
||||
|
||||
bool read_config_json(json& cfg, const string& filename, ostream* log)
|
||||
{
|
||||
ifstream cfg_file(filename);
|
||||
|
||||
if (!cfg_file.is_open())
|
||||
{
|
||||
if (nullptr != log)
|
||||
{
|
||||
*log << "Could not open config.json" << endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse with comments
|
||||
try
|
||||
{
|
||||
cfg = json::parse(cfg_file, nullptr, true, true);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
if (nullptr != log)
|
||||
{
|
||||
*log << e.what() << endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void json_extract(const json& j, const string& key, string& out)
|
||||
{
|
||||
if (j.contains(key) && j[key].is_string())
|
||||
{
|
||||
out = j[key];
|
||||
}
|
||||
}
|
||||
|
||||
void json_extract(const nlohmann::json& j, const string& key, int& out)
|
||||
{
|
||||
if (j.contains(key) && j[key].is_number_integer())
|
||||
{
|
||||
out = j[key];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user