Introduced Content, ContentIndices, no more integer ids hardcoded, common refactor

This commit is contained in:
MihailRis
2023-11-21 11:38:59 +03:00
parent f2f9343737
commit 3b03464d27
35 changed files with 472 additions and 279 deletions
+8 -75
View File
@@ -6,86 +6,19 @@
#include <stdexcept>
#include "definitions.h"
#include "util/platform.h"
#include "engine.h"
#include "util/platform.h"
#include "coders/toml.h"
#include "coders/json.h"
#include "files/files.h"
#include "window/Events.h"
toml::Wrapper create_wrapper(EngineSettings& settings) {
toml::Wrapper wrapper;
toml::Section& display = wrapper.add("display");
display.add("width", &settings.display.width);
display.add("height", &settings.display.height);
display.add("samples", &settings.display.samples);
display.add("swap-interval", &settings.display.swapInterval);
toml::Section& chunks = wrapper.add("chunks");
chunks.add("load-distance", &settings.chunks.loadDistance);
chunks.add("load-speed", &settings.chunks.loadSpeed);
chunks.add("padding", &settings.chunks.padding);
toml::Section& camera = wrapper.add("camera");
camera.add("fov-effects", &settings.camera.fovEvents);
camera.add("shaking", &settings.camera.shaking);
toml::Section& graphics = wrapper.add("graphics");
graphics.add("fog-curve", &settings.graphics.fogCurve);
toml::Section& debug = wrapper.add("debug");
debug.add("generator-test-mode", &settings.debug.generatorTestMode);
return wrapper;
}
std::string write_controls() {
json::JObject* obj = new json::JObject();
for (auto& entry : Events::bindings) {
const auto& binding = entry.second;
json::JObject* jentry = new json::JObject();
switch (binding.type) {
case inputtype::keyboard: jentry->put("type", "keyboard"); break;
case inputtype::mouse: jentry->put("type", "mouse"); break;
default: throw std::runtime_error("unsupported control type");
}
jentry->put("code", binding.code);
obj->put(entry.first, jentry);
}
return json::stringify(obj, true, " ");
}
void load_controls(std::string filename, std::string source) {
json::JObject* obj = json::parse(filename, source);
for (auto& entry : Events::bindings) {
auto& binding = entry.second;
json::JObject* jentry = obj->obj(entry.first);
if (jentry == nullptr)
continue;
inputtype type;
std::string typestr;
jentry->str("type", typestr);
if (typestr == "keyboard") {
type = inputtype::keyboard;
} else if (typestr == "mouse") {
type = inputtype::mouse;
} else {
std::cerr << "unknown input type '" << typestr << "'" << std::endl;
continue;
}
binding.type = type;
jentry->num("code", binding.code);
}
}
#include "files/settings_io.h"
#include "content/Content.h"
int main() {
platform::configure_encoding();
setup_definitions();
ContentBuilder contentBuilder;
setup_definitions(&contentBuilder);
std::unique_ptr<Content> content(contentBuilder.build());
try {
EngineSettings settings;
toml::Wrapper wrapper = create_wrapper(settings);
@@ -98,7 +31,7 @@ int main() {
toml::Reader reader(&wrapper, settings_file.string(), content);
reader.read();
}
Engine engine(settings);
Engine engine(settings, content.get());
setup_bindings();
if (std::filesystem::is_regular_file(controls_file)) {
std::cout << "-- loading controls" << std::endl;