feat: add Config class

This commit is contained in:
2026-04-24 17:08:06 +08:00
parent 2409734e89
commit 106cc3d398
20 changed files with 465 additions and 154 deletions

View File

@@ -21,6 +21,11 @@ void Camera::camera_init(Player* player) {
m_player = player;
update_move_camera();
reset_camera();
hot_reload();
}
void Camera::hot_reload() {
}
void Camera::reset_camera() {

83
src/config.cpp Normal file
View File

@@ -0,0 +1,83 @@
#include <Cubed/config.hpp>
#include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp>
#include <filesystem>
#include <fstream>
namespace fs = std::filesystem;
using namespace std::string_view_literals;
namespace Cubed {
Config::Config() {
load_or_create_config();
}
Config::~Config() {
save_to_file();
}
Config& Config::get() {
static Config instance;
return instance;
}
toml::table& Config::table() {
return m_tbl;
}
void Config::create_config() {
static constexpr auto SOURCE = R"(
version = "0.0.1"
[window]
width = 800
height = 600
fullscreen = false
V-Sync = true
[player]
fov = 70.0
mouse_sensitivity = 0.15
[world]
rendering_distance = 24
)"sv;
try {
m_tbl = toml::parse(SOURCE);
} catch (const toml::parse_error& err) {
Logger::error("Load Config Error {}", err.what());
ASSERT(false);
std::abort();
}
Logger::info("Create New Config File Success");
}
void Config::load_or_create_config() {
fs::path config_path {CONGIF_PATH};
if (!fs::is_regular_file(config_path)) {
create_config();
} else try {
m_tbl = toml::parse_file(config_path.string());
} catch (const toml::parse_error& err) {
Logger::error("Load Config Error: \"{}\"", err.what());
create_config();
}
Logger::info("Load Config File Success");
}
void Config::save_to_file() {
fs::path config_path {CONGIF_PATH};
std::ofstream file{config_path};
file << m_tbl;
Logger::info("Save File Success");
}
}

View File

@@ -1,5 +1,6 @@
#include <Cubed/debug_collector.hpp>
#include <Cubed/config.hpp>
#include <Cubed/tools/system_info.hpp>
#include <Cubed/tools/cubed_hash.hpp>
@@ -30,7 +31,7 @@ void DebugCollector::init_text() {
.position(0.0f, 100.0f)
.scale(0.8f)
.color(Color::WHITE)
.text("Version: v0.0.1-Debug");
.text("Version: " + Config::get().get<std::string>("version"));
fps_text
.position(0.0f, 50.0f)
.text("FPS: 0");

View File

@@ -13,7 +13,7 @@ Player::Player(World& world, const std::string& name) :
m_name(name),
m_world(world)
{
hot_reload();
}
Player::~Player() {
@@ -144,6 +144,12 @@ void Player::change_mode(GameMode mode) {
}
}
void Player::hot_reload() {
auto& config = Config::get();
m_sensitivity = static_cast<float>(config.get<double>("player.mouse_sensitivity"));
}
void Player::set_player_pos(const glm::vec3& pos) {
m_player_pos = pos;
}

View File

@@ -74,12 +74,12 @@ Player& World::get_player(const std::string& name){
}
void World::init_world() {
m_chunks.reserve(DISTANCE * DISTANCE);
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE);
auto t1 = std::chrono::system_clock::now();
for (int s = 0; s < DISTANCE; s++) {
for (int t = 0; t < DISTANCE; t++) {
int ns = s - DISTANCE / 2;
int nt = t - DISTANCE / 2;
for (int s = 0; s < PRE_LOAD_DISTANCE; s++) {
for (int t = 0; t < PRE_LOAD_DISTANCE; t++) {
int ns = s - PRE_LOAD_DISTANCE / 2;
int nt = t - PRE_LOAD_DISTANCE / 2;
ChunkPos pos{ns, nt};
@@ -97,7 +97,7 @@ void World::init_world() {
Logger::info("TestPlayer Create Finish");
start_gen_thread();
hot_reload();
}
/*
void World::init_chunks() {
@@ -385,8 +385,8 @@ void World::compute_required_chunks(ChunkPosSet& required_chunks) {
auto [chunk_x, chunk_z] = chunk_pos(x, z);
required_chunks.reserve(DISTANCE * DISTANCE);
int half = DISTANCE / 2;
required_chunks.reserve(m_rendering_distance * m_rendering_distance);
int half = m_rendering_distance / 2;
for (int u = chunk_x - half; u <= chunk_x + half; ++u) {
for (int v = chunk_z - half; v <= chunk_z + half; ++v) {
required_chunks.emplace(u, v);
@@ -664,4 +664,11 @@ void World::push_delete_vbo(GLuint vbo) {
m_pending_delete_vbo.push_back(vbo);
}
void World::hot_reload() {
auto & config = Config::get();
int dist = config.get<int>("world.rendering_distance");
m_rendering_distance = dist <= MAX_DISTANCE ? dist : MAX_DISTANCE;
need_gen();
}
}

View File

@@ -1,6 +1,7 @@
#include <Cubed/app.hpp>
#include <Cubed/camera.hpp>
#include <Cubed/config.hpp>
#include <Cubed/primitive_data.hpp>
#include <Cubed/debug_collector.hpp>
#include <Cubed/gameplay/player.hpp>
#include <Cubed/gameplay/world.hpp>
@@ -37,7 +38,10 @@ Renderer::~Renderer() {
glDeleteVertexArrays(NUM_VAO, m_vao.data());
}
void Renderer::hot_reload() {
auto& config = Config::get();
update_fov(config.get<double>("player.fov"));
}
void Renderer::init() {
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
@@ -109,6 +113,7 @@ void Renderer::init() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
init_text();
hot_reload();
}
const Shader& Renderer::get_shader(const std::string& name) const {

View File

@@ -5,6 +5,9 @@
#include <Cubed/window.hpp>
namespace Cubed {
static int windowed_xpos = 0, windowed_ypos = 0;
static int windowed_width = 800, windowed_height = 600;
Window::Window(Renderer& renderer) :
m_renderer(renderer)
{
@@ -37,7 +40,10 @@ void Window::update_viewport() {
m_aspect = (float)m_width / (float)m_height;
glViewport(0, 0, m_width, m_height);
m_renderer.update_proj_matrix(m_aspect, m_width, m_height) ;
auto& config = Config::get();
config.set("window.width", windowed_width);
config.set("window.height", windowed_height);
}
void Window::init() {
@@ -49,11 +55,23 @@ void Window::init() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
m_window = glfwCreateWindow(800, 600, "Cubed", NULL, NULL);
auto& config= Config::get();
m_width = config.get<int>("window.width");
m_height = config.get<int>("window.height");
if (config.get<bool>("window.fullscreen")) {
GLFWmonitor* primary_monitor = glfwGetPrimaryMonitor();
m_window = glfwCreateWindow(m_width, m_height, "Cubed", primary_monitor, NULL);
} else {
m_window = glfwCreateWindow(m_width, m_height, "Cubed", NULL, NULL);
}
glfwMakeContextCurrent(m_window);
glfwSwapInterval(1);
if (config.get<bool>("window.V-Sync")) {
glfwSwapInterval(1);
} else {
glfwSwapInterval(0);
}
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glfwRawMouseMotionSupported()) {
@@ -69,12 +87,21 @@ void Window::init() {
}
void Window::toggle_fullscreen() {
static int windowed_xpos = 0, windowed_ypos = 0;
static int windowed_width = 800, windowed_height = 600;
void Window::hot_reload() {
auto& config= Config::get();
// V-Sync
if (config.get<bool>("window.V-Sync")) {
glfwSwapInterval(1);
} else {
glfwSwapInterval(0);
}
// Window
windowed_width = config.get<int>("window.width");
windowed_height = config.get<int>("window.height");
GLFWmonitor* monitor = glfwGetWindowMonitor(m_window);
if (monitor != nullptr) {
if (config.get<bool>("window.fullscreen.V-Sync")) {
GLFWmonitor* monitor = glfwGetWindowMonitor(m_window);
if (monitor != nullptr) {
glfwSetWindowMonitor(
m_window,
nullptr,
@@ -84,6 +111,10 @@ void Window::toggle_fullscreen() {
windowed_height,
0
);
} else {
}
config.set("window.fullscreen", false);
} else {
glfwGetWindowPos(m_window, &windowed_xpos, &windowed_ypos);
glfwGetWindowSize(m_window, &windowed_width, &windowed_height);
@@ -100,6 +131,47 @@ void Window::toggle_fullscreen() {
mode->height,
GL_DONT_CARE
);
config.set("window.fullscreen", true);
}
update_viewport();
}
void Window::toggle_fullscreen() {
auto& config = Config::get();
GLFWmonitor* monitor = glfwGetWindowMonitor(m_window);
if (monitor != nullptr) {
glfwSetWindowMonitor(
m_window,
nullptr,
windowed_xpos,
windowed_ypos,
windowed_width,
windowed_height,
0
);
config.set("window.fullscreen", false);
} else {
glfwGetWindowPos(m_window, &windowed_xpos, &windowed_ypos);
glfwGetWindowSize(m_window, &windowed_width, &windowed_height);
GLFWmonitor* primary = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(primary);
glfwSetWindowMonitor(
m_window,
primary,
0,
0,
mode->width,
mode->height,
GL_DONT_CARE
);
config.set("window.fullscreen", true);
}
update_viewport();
}