Added initial JSON configuration support

This commit is contained in:
2025-12-04 15:04:39 +02:00
parent 34ce515521
commit 6cad6428a4
6 changed files with 24709 additions and 7 deletions

View File

@@ -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];
}
}