mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 06:53:41 +00:00
started work on config-system
added nlohmann_json library as submodule and to CMakeLists.txt created basic config.json file in root folder created ConfigLoader.h and added basic functionalities to it
This commit is contained in:
parent
7c50c8d1fb
commit
9247b8df8a
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -17,3 +17,6 @@
|
|||||||
path = docs/doxygen-awesome-css
|
path = docs/doxygen-awesome-css
|
||||||
url = https://github.com/jothepro/doxygen-awesome-css.git
|
url = https://github.com/jothepro/doxygen-awesome-css.git
|
||||||
|
|
||||||
|
[submodule "extern/nlohmann_json"]
|
||||||
|
path = extern/nlohmann_json
|
||||||
|
url = https://github.com/nlohmann/json.git
|
||||||
|
|||||||
@ -24,10 +24,13 @@ add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL)
|
|||||||
add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL)
|
add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL)
|
||||||
add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL)
|
add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL)
|
||||||
add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL)
|
add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL)
|
||||||
|
add_subdirectory(extern/nlohmann_json EXCLUDE_FROM_ALL)
|
||||||
|
|
||||||
|
|
||||||
file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp)
|
file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp)
|
||||||
add_library(${PROJECT_NAME} ${SOURCES})
|
add_library(${PROJECT_NAME} ${SOURCES}
|
||||||
|
include/ConfigLoader.h
|
||||||
|
src/ConfigLoader.cpp)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR})
|
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR})
|
||||||
|
|
||||||
@ -37,6 +40,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL fu
|
|||||||
SDL3_mixer::SDL3_mixer-static
|
SDL3_mixer::SDL3_mixer-static
|
||||||
SDL3_ttf::SDL3_ttf-static
|
SDL3_ttf::SDL3_ttf-static
|
||||||
tmxlite
|
tmxlite
|
||||||
|
nlohmann_json::nlohmann_json
|
||||||
)
|
)
|
||||||
|
|
||||||
if(CMAKE_BUILD_TYPE MATCHES "Debug")
|
if(CMAKE_BUILD_TYPE MATCHES "Debug")
|
||||||
|
|||||||
3
config.json
Normal file
3
config.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"fullscreen": false
|
||||||
|
}
|
||||||
1
extern/nlohmann_json
vendored
Submodule
1
extern/nlohmann_json
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit a006a7a48bb30a247f0344b788c62c2806edd90b
|
||||||
24
include/ConfigLoader.h
Normal file
24
include/ConfigLoader.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
class ConfigLoader {
|
||||||
|
|
||||||
|
public:
|
||||||
|
ConfigLoader();
|
||||||
|
~ConfigLoader();
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
|
void setCustomConfig(std::string path);
|
||||||
|
|
||||||
|
private:
|
||||||
|
json baseConfig;
|
||||||
|
std::optional<json> customConfig;
|
||||||
|
json finalConfig;
|
||||||
|
|
||||||
|
json loadConfigFromJSON(const std::string& path);
|
||||||
|
json mergeConfigs(json baseConfig, json customConfig); //<! Merges 2 config.json files, prioritising the custom to the base one
|
||||||
|
};
|
||||||
41
src/ConfigLoader.cpp
Normal file
41
src/ConfigLoader.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "ConfigLoader.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
ConfigLoader::ConfigLoader() {
|
||||||
|
baseConfig = loadConfigFromJSON("config.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigLoader::init() {
|
||||||
|
if (!customConfig.has_value()) {
|
||||||
|
finalConfig = baseConfig;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
finalConfig = mergeConfigs(baseConfig, customConfig.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
json ConfigLoader::loadConfigFromJSON(const std::string& path) {
|
||||||
|
std::ifstream config_file(path);
|
||||||
|
json config;
|
||||||
|
|
||||||
|
if (!config_file.is_open()) {
|
||||||
|
throw std::runtime_error(std::string("Could not load config file at: " + path));
|
||||||
|
}
|
||||||
|
|
||||||
|
config_file >> config;
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ConfigLoader::setCustomConfig(std::string path) {
|
||||||
|
customConfig.emplace(loadConfigFromJSON(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) {
|
||||||
|
for (auto& entry : customConfig.items()) {
|
||||||
|
baseConfig[entry.key()] = entry.value();
|
||||||
|
}
|
||||||
|
return baseConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user