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

3
.gitignore vendored
View File

@@ -39,4 +39,5 @@ CMakeError.log
*.swp *.swp
*.swo *.swo
*~ *~
.DS_Store .DS_Store
assets/config.toml

View File

@@ -87,6 +87,7 @@ add_executable(${PROJECT_NAME}
src/app.cpp src/app.cpp
src/debug_collector.cpp src/debug_collector.cpp
src/camera.cpp src/camera.cpp
src/config.cpp
src/gameplay/biome.cpp src/gameplay/biome.cpp
src/gameplay/chunk.cpp src/gameplay/chunk.cpp
src/gameplay/player.cpp src/gameplay/player.cpp

View File

@@ -27,6 +27,7 @@ public:
void update_move_camera(); void update_move_camera();
void camera_init(Player* player); void camera_init(Player* player);
void hot_reload();
void reset_camera(); void reset_camera();
void update_cursor_position_camera(double xpos, double ypos); void update_cursor_position_camera(double xpos, double ypos);

View File

@@ -1,5 +1,8 @@
#pragma once #pragma once
#include <array> #include <array>
#include <toml++/toml.hpp>
#include <Cubed/tools/cubed_assert.hpp>
namespace Cubed { namespace Cubed {
@@ -8,149 +11,118 @@ constexpr int MAX_BLOCK_NUM = 7;
constexpr int MAX_UI_NUM = 1; constexpr int MAX_UI_NUM = 1;
constexpr int CHUCK_SIZE = 16; constexpr int CHUCK_SIZE = 16;
constexpr int DISTANCE = 24; constexpr int PRE_LOAD_DISTANCE = 24;
constexpr int MAX_DISTANCE = 128;
constexpr int MAX_BLOCK_STATUS = 1; constexpr int MAX_BLOCK_STATUS = 1;
constexpr int MAX_CHARACTER = 128; constexpr int MAX_CHARACTER = 128;
constexpr float NORMAL_FOV = 70.0f; constexpr float NORMAL_FOV = 70.0f;
constexpr int MAX_BIOME_SUM = 4; constexpr int MAX_BIOME_SUM = 4;
using HeightMapArray = std::array<std::array<float, CHUCK_SIZE>, CHUCK_SIZE>; using HeightMapArray = std::array<std::array<float, CHUCK_SIZE>, CHUCK_SIZE>;
constexpr float VERTICES_POS[6][6][3] = {
// ===== front (z = +1) =====
0.0f, 0.0f, 1.0f, // bottom left
0.0f, 1.0f, 1.0f, // top left
1.0f, 1.0f, 1.0f, // top right
1.0f, 1.0f, 1.0f, // top right
1.0f, 0.0f, 1.0f, // bottom right
0.0f, 0.0f, 1.0f, // bottom left
// ===== right (x = +1) =====
1.0f, 0.0f, 1.0f, // bottom front
1.0f, 0.0f, 0.0f, // bottom back
1.0f, 1.0f, 0.0f, // top back
1.0f, 1.0f, 0.0f, // top back
1.0f, 1.0f, 1.0f, // top front
1.0f, 0.0f, 1.0f, // bottom front
// ===== back (z = -1) =====
0.0f, 0.0f, 0.0f, // bottom left
1.0f, 0.0f, 0.0f, // bottom right
1.0f, 1.0f, 0.0f, // top right
1.0f, 1.0f, 0.0f, // top right
0.0f, 1.0f, 0.0f, // top left
0.0f, 0.0f, 0.0f, // bottom left
// ===== left (x = -1) =====
0.0f, 0.0f, 0.0f, // bottom back
0.0f, 0.0f, 1.0f, // bottom front
0.0f, 1.0f, 1.0f, // top front
0.0f, 1.0f, 1.0f, // top front
0.0f, 1.0f, 0.0f, // top back
0.0f, 0.0f, 0.0f, // bottom back
// ===== top (y = +1) =====
0.0f, 1.0f, 0.0f, // back left
1.0f, 1.0f, 0.0f, // back right
1.0f, 1.0f, 1.0f, // front right
1.0f, 1.0f, 1.0f, // front right
0.0f, 1.0f, 1.0f, // front left
0.0f, 1.0f, 0.0f, // back left
// ===== bottom (y = -1) =====
0.0f, 0.0f, 1.0f, // front left
1.0f, 0.0f, 1.0f, // front right
1.0f, 0.0f, 0.0f, // back right
1.0f, 0.0f, 0.0f, // back right
0.0f, 0.0f, 0.0f, // back left
0.0f, 0.0f, 1.0f // front left
};
constexpr float TEX_COORDS[6][6][2] = {
// ===== front (z = +1) =====
0.0f, 1.0f, // bottom left
0.0f, 0.0f, // top left
1.0f, 0.0f, // top right
1.0f, 0.0f, // top right
1.0f, 1.0f, // bottom right
0.0f, 1.0f, // bottom left
// ===== right (x = +1) =====
0.0f, 1.0f, // bottom front
1.0f, 1.0f, // bottom back
1.0f, 0.0f, // top back
1.0f, 0.0f, // top back
0.0f, 0.0f, // top front
0.0f, 1.0f, // bottom front
// ===== back (z = -1) =====
1.0f, 1.0f, // bottom left
0.0f, 1.0f, // bottom right
0.0f, 0.0f, // top right
0.0f, 0.0f, // top right
1.0f, 0.0f, // top left
1.0f, 1.0f, // bottom left
// ===== left (x = -1) =====
1.0f, 1.0f, // bottom back
0.0f, 1.0f, // bottom front
0.0f, 0.0f, // top front
0.0f, 0.0f, // top front
1.0f, 0.0f, // top back
1.0f, 1.0f, // bottom back
// ===== top (y = +1) =====
0.0f, 0.0f, // back left
1.0f, 0.0f, // back right
1.0f, 1.0f, // front right
1.0f, 1.0f, // front right
0.0f, 1.0f, // front left
0.0f, 0.0f, // back left
// ===== bottom (y = -1) =====
0.0f, 0.0f, // front left
1.0f, 0.0f, // front right
1.0f, 1.0f, // back right
1.0f, 1.0f, // back right
0.0f, 1.0f, // back left
0.0f, 0.0f, // front left
};
constexpr float CUBE_VER[24] = {
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 1.0,
1.0, 1.0, 1.0,
0.0, 1.0, 1.0
};
constexpr int OUTLINE_CUBE_INDICES[24] = { template <typename T>
0,1, 1,2, 2,3, 3,0, concept TomlValueType =
4,5, 5,6, 6,7, 7,4, std::same_as<T, int> ||
0,4, 1,5, 2,6, 3,7 std::same_as<T, bool> ||
}; std::same_as<T, double> ||
std::same_as<T, const char*> ||
std::same_as<T, toml::date> ||
std::same_as<T, toml::time> ||
std::same_as<T, toml::date_time> ||
std::same_as<T, std::string>
;
constexpr float SQUARE_VERTICES[6][2] = { class Config {
-0.5f, -0.5f, // bottom left public:
-0.5f, 0.5f, // top left Config();
0.5f, 0.5f, // top right ~Config();
0.5f, 0.5f, // top right
0.5f, -0.5f, // bottom right
-0.5f, -0.5f // bottom left
};
constexpr float SQUARE_TEXTURE_POS[6][2] = { static Config& get();
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
};
struct Vertex { toml::table& table();
float x = 0.0f, y = 0.0f, z = 0.0f;
float s = 0.0f, t = 0.0f; void load_or_create_config();
float layer = 0.0f; void save_to_file();
};
template <TomlValueType T>
T get(std::string_view key) const{
size_t cur = 0;
auto pos = key.find('.');
const toml::table* table = &m_tbl;
while (pos != std::string_view::npos) {
std::string_view s = key.substr(cur, pos - cur);
if (s.empty()) {
Logger::error("Empty key/table name in path '{}'", key);
ASSERT(false);
std::abort();
}
cur = pos + 1;
pos = key.find('.', cur);
if (auto* next = (*table)[s].as_table()) {
table = next;
} else {
Logger::error("Can't find table {}", s);
ASSERT(false);
std::abort();
}
}
std::string_view n_key = key.substr(cur);
if (n_key.empty()) {
Logger::error("Trailing dot in path '{}'", key);
ASSERT(false);
std::abort();
}
auto opt = (*table)[n_key].value<T>();
if (opt){
return *opt;
} else {
Logger::error("Can't find key {}", n_key);
ASSERT(false);
std::abort();
}
}
template <typename T>
void set(std::string_view key, T&& val) {
if constexpr (!TomlValueType<std::decay_t<T>>) {
static_assert(false, "Type Not Support");
}
size_t cur = 0;
auto pos = key.find('.');
toml::table* table = &m_tbl;
while (pos != std::string_view::npos) {
std::string_view s = key.substr(cur, pos - cur);
if (s.empty()) {
Logger::error("Empty key/table name in path '{}'", key);
ASSERT(false);
std::abort();
}
cur = pos + 1;
pos = key.find('.', cur);
if (auto* next = (*table)[s].as_table()) {
table = next;
} else {
auto [it, inserted] = table->insert_or_assign(s, toml::table{});
table = it->second.as_table();
}
}
std::string_view n_key = key.substr(cur);
if (n_key.empty()) {
Logger::error("Trailing dot in path '{}'", key);
ASSERT(false);
std::abort();
}
table->insert_or_assign(n_key, std::forward<T>(val));
save_to_file();
}
private:
toml::table m_tbl;
constexpr static inline std::string_view CONGIF_PATH = ASSETS_PATH"config.toml";
void create_config();
struct Vertex2D {
float x = 0.0f, y = 0.0f;
float s = 0.0f, t = 0.0f;
float layer = 0.0f;
}; };
} }

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <Cubed/primitive_data.hpp>
#include <Cubed/ui/text.hpp> #include <Cubed/ui/text.hpp>
#include <unordered_map> #include <unordered_map>

View File

@@ -4,6 +4,7 @@
#include <cstdint> #include <cstdint>
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/primitive_data.hpp>
#include <Cubed/gameplay/biome.hpp> #include <Cubed/gameplay/biome.hpp>
#include <Cubed/gameplay/chunk_pos.hpp> #include <Cubed/gameplay/chunk_pos.hpp>
#include <Cubed/gameplay/block.hpp> #include <Cubed/gameplay/block.hpp>

View File

@@ -49,7 +49,7 @@ private:
glm::vec3 move_distance {0.0f, 0.0f, 0.0f}; glm::vec3 move_distance {0.0f, 0.0f, 0.0f};
// player is tow block tall, the pos is the lower pos // player is tow block tall, the pos is the lower pos
glm::vec3 m_player_pos {0.0f, 120.0f, 0.0f}; glm::vec3 m_player_pos {0.0f, 255.0f, 0.0f};
ChunkPos m_player_chunk_pos {0, 0}; ChunkPos m_player_chunk_pos {0, 0};
glm::vec3 m_front {0, 0, -1}; glm::vec3 m_front {0, 0, -1};
@@ -84,12 +84,15 @@ public:
const MoveState& get_move_state() const; const MoveState& get_move_state() const;
void change_mode(GameMode mode); void change_mode(GameMode mode);
void hot_reload();
void set_player_pos(const glm::vec3& pos); void set_player_pos(const glm::vec3& pos);
void update(float delta_time); void update(float delta_time);
void update_front_vec(float offset_x, float offset_y); void update_front_vec(float offset_x, float offset_y);
void update_player_move_state(int key, int action); void update_player_move_state(int key, int action);
void update_scroll(double yoffset); void update_scroll(double yoffset);
}; };

View File

@@ -7,6 +7,7 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <Cubed/AABB.hpp> #include <Cubed/AABB.hpp>
#include <Cubed/primitive_data.hpp>
#include <Cubed/gameplay/chunk.hpp> #include <Cubed/gameplay/chunk.hpp>
namespace Cubed { namespace Cubed {
@@ -45,7 +46,7 @@ private:
std::condition_variable m_gen_cv; std::condition_variable m_gen_cv;
std::atomic<bool> m_gen_running{false}; std::atomic<bool> m_gen_running{false};
std::atomic<bool> m_need_gen_chunk{false}; std::atomic<bool> m_need_gen_chunk{false};
std::atomic<int> m_rendering_distance{24};
std::vector<ChunkPos> m_dirty_queue; std::vector<ChunkPos> m_dirty_queue;
std::vector<ChunkRenderSnapshot> m_render_snapshots; std::vector<ChunkRenderSnapshot> m_render_snapshots;
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk; std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk;
@@ -90,6 +91,8 @@ public:
void push_delete_vbo(GLuint vbo); void push_delete_vbo(GLuint vbo);
void hot_reload();
}; };
} }

View File

@@ -0,0 +1,143 @@
#pragma once
namespace Cubed {
constexpr float VERTICES_POS[6][6][3] = {
// ===== front (z = +1) =====
{{0.0f, 0.0f, 1.0f}, // bottom left
{0.0f, 1.0f, 1.0f}, // top left
{1.0f, 1.0f, 1.0f}, // top right
{1.0f, 1.0f, 1.0f}, // top right
{1.0f, 0.0f, 1.0f}, // bottom right
{0.0f, 0.0f, 1.0f}}, // bottom left
// ===== right (x = +1) =====
{{1.0f, 0.0f, 1.0f}, // bottom front
{1.0f, 0.0f, 0.0f}, // bottom back
{1.0f, 1.0f, 0.0f}, // top back
{1.0f, 1.0f, 0.0f}, // top back
{1.0f, 1.0f, 1.0f}, // top front
{1.0f, 0.0f, 1.0f}}, // bottom front
// ===== back (z = -1) =====
{{0.0f, 0.0f, 0.0f}, // bottom left
{1.0f, 0.0f, 0.0f}, // bottom right
{1.0f, 1.0f, 0.0f}, // top right
{1.0f, 1.0f, 0.0f}, // top right
{0.0f, 1.0f, 0.0f}, // top left
{0.0f, 0.0f, 0.0f}}, // bottom left
// ===== left (x = -1) =====
{{0.0f, 0.0f, 0.0f}, // bottom back
{0.0f, 0.0f, 1.0f}, // bottom front
{0.0f, 1.0f, 1.0f}, // top front
{0.0f, 1.0f, 1.0f}, // top front
{0.0f, 1.0f, 0.0f}, // top back
{0.0f, 0.0f, 0.0f}}, // bottom back
// ===== top (y = +1) =====
{{0.0f, 1.0f, 0.0f}, // back left
{1.0f, 1.0f, 0.0f}, // back right
{1.0f, 1.0f, 1.0f}, // front right
{1.0f, 1.0f, 1.0f}, // front right
{0.0f, 1.0f, 1.0f}, // front left
{0.0f, 1.0f, 0.0f}}, // back left
// ===== bottom (y = -1) =====
{{0.0f, 0.0f, 1.0f}, // front left
{1.0f, 0.0f, 1.0f}, // front right
{1.0f, 0.0f, 0.0f}, // back right
{1.0f, 0.0f, 0.0f}, // back right
{0.0f, 0.0f, 0.0f}, // back left
{0.0f, 0.0f, 1.0f}} // front left
};
constexpr float TEX_COORDS[6][6][2] = {
// ===== front (z = +1) =====
{{0.0f, 1.0f}, // bottom left
{0.0f, 0.0f}, // top left
{1.0f, 0.0f}, // top right
{1.0f, 0.0f}, // top right
{1.0f, 1.0f}, // bottom right
{0.0f, 1.0f}}, // bottom left
// ===== right (x = +1) =====
{{0.0f, 1.0f}, // bottom front
{1.0f, 1.0f}, // bottom back
{1.0f, 0.0f}, // top back
{1.0f, 0.0f}, // top back
{0.0f, 0.0f}, // top front
{0.0f, 1.0f}}, // bottom front
// ===== back (z = -1) =====
{{1.0f, 1.0f}, // bottom left
{0.0f, 1.0f}, // bottom right
{0.0f, 0.0f}, // top right
{0.0f, 0.0f}, // top right
{1.0f, 0.0f}, // top left
{1.0f, 1.0f}}, // bottom left
// ===== left (x = -1) =====
{{1.0f, 1.0f}, // bottom back
{0.0f, 1.0f}, // bottom front
{0.0f, 0.0f}, // top front
{0.0f, 0.0f}, // top front
{1.0f, 0.0f}, // top back
{1.0f, 1.0f}}, // bottom back
// ===== top (y = +1) =====
{{0.0f, 0.0f}, // back left
{1.0f, 0.0f}, // back right
{1.0f, 1.0f}, // front right
{1.0f, 1.0f}, // front right
{0.0f, 1.0f}, // front left
{0.0f, 0.0f}}, // back left
// ===== bottom (y = -1) =====
{{0.0f, 0.0f}, // front left
{1.0f, 0.0f}, // front right
{1.0f, 1.0f}, // back right
{1.0f, 1.0f}, // back right
{0.0f, 1.0f}, // back left
{0.0f, 0.0f}} // front left
};
constexpr float CUBE_VER[24] = {
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 1.0,
1.0, 1.0, 1.0,
0.0, 1.0, 1.0
};
constexpr int OUTLINE_CUBE_INDICES[24] = {
0,1, 1,2, 2,3, 3,0,
4,5, 5,6, 6,7, 7,4,
0,4, 1,5, 2,6, 3,7
};
constexpr float SQUARE_VERTICES[6][2] = {
{-0.5f, -0.5f}, // bottom left
{-0.5f, 0.5f}, // top left
{ 0.5f, 0.5f}, // top right
{ 0.5f, 0.5f}, // top right
{ 0.5f, -0.5f}, // bottom right
{-0.5f, -0.5f} // bottom left
};
constexpr float SQUARE_TEXTURE_POS[6][2] = {
{0.0f, 0.0f},
{0.0f, 1.0f},
{1.0f, 1.0f},
{1.0f, 1.0f},
{1.0f, 0.0f},
{0.0f, 0.0f},
};
struct Vertex {
float x = 0.0f, y = 0.0f, z = 0.0f;
float s = 0.0f, t = 0.0f;
float layer = 0.0f;
};
struct Vertex2D {
float x = 0.0f, y = 0.0f;
float s = 0.0f, t = 0.0f;
float layer = 0.0f;
};
}

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/primitive_data.hpp>
#include <Cubed/shader.hpp> #include <Cubed/shader.hpp>
#include <Cubed/ui/text.hpp> #include <Cubed/ui/text.hpp>
@@ -18,6 +19,7 @@ public:
Renderer(const Camera& camera, World& world, const TextureManager& texture_manager); Renderer(const Camera& camera, World& world, const TextureManager& texture_manager);
~Renderer(); ~Renderer();
void hot_reload();
void init(); void init();
const Shader& get_shader(const std::string& name) const; const Shader& get_shader(const std::string& name) const;
void render(); void render();

View File

@@ -9,6 +9,7 @@
#include <unordered_map> #include <unordered_map>
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/primitive_data.hpp>
namespace Cubed { namespace Cubed {

View File

@@ -3,9 +3,9 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <string> #include <string>
#include <vector>
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/primitive_data.hpp>
#include <Cubed/ui/color.hpp> #include <Cubed/ui/color.hpp>
namespace Cubed { namespace Cubed {

View File

@@ -13,6 +13,9 @@ public:
GLFWwindow* get_glfw_window(); GLFWwindow* get_glfw_window();
void init(); void init();
void update_viewport(); void update_viewport();
// end of frame to reload!
void hot_reload();
void toggle_fullscreen(); void toggle_fullscreen();
void toggle_mouse_able(); void toggle_mouse_able();

View File

@@ -21,6 +21,11 @@ void Camera::camera_init(Player* player) {
m_player = player; m_player = player;
update_move_camera(); update_move_camera();
reset_camera(); reset_camera();
hot_reload();
}
void Camera::hot_reload() {
} }
void Camera::reset_camera() { 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/debug_collector.hpp>
#include <Cubed/config.hpp>
#include <Cubed/tools/system_info.hpp> #include <Cubed/tools/system_info.hpp>
#include <Cubed/tools/cubed_hash.hpp> #include <Cubed/tools/cubed_hash.hpp>
@@ -30,7 +31,7 @@ void DebugCollector::init_text() {
.position(0.0f, 100.0f) .position(0.0f, 100.0f)
.scale(0.8f) .scale(0.8f)
.color(Color::WHITE) .color(Color::WHITE)
.text("Version: v0.0.1-Debug"); .text("Version: " + Config::get().get<std::string>("version"));
fps_text fps_text
.position(0.0f, 50.0f) .position(0.0f, 50.0f)
.text("FPS: 0"); .text("FPS: 0");

View File

@@ -13,7 +13,7 @@ Player::Player(World& world, const std::string& name) :
m_name(name), m_name(name),
m_world(world) m_world(world)
{ {
hot_reload();
} }
Player::~Player() { 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) { void Player::set_player_pos(const glm::vec3& pos) {
m_player_pos = pos; m_player_pos = pos;
} }

View File

@@ -74,12 +74,12 @@ Player& World::get_player(const std::string& name){
} }
void World::init_world() { void World::init_world() {
m_chunks.reserve(DISTANCE * DISTANCE); m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE);
auto t1 = std::chrono::system_clock::now(); auto t1 = std::chrono::system_clock::now();
for (int s = 0; s < DISTANCE; s++) { for (int s = 0; s < PRE_LOAD_DISTANCE; s++) {
for (int t = 0; t < DISTANCE; t++) { for (int t = 0; t < PRE_LOAD_DISTANCE; t++) {
int ns = s - DISTANCE / 2; int ns = s - PRE_LOAD_DISTANCE / 2;
int nt = t - DISTANCE / 2; int nt = t - PRE_LOAD_DISTANCE / 2;
ChunkPos pos{ns, nt}; ChunkPos pos{ns, nt};
@@ -97,7 +97,7 @@ void World::init_world() {
Logger::info("TestPlayer Create Finish"); Logger::info("TestPlayer Create Finish");
start_gen_thread(); start_gen_thread();
hot_reload();
} }
/* /*
void World::init_chunks() { 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); auto [chunk_x, chunk_z] = chunk_pos(x, z);
required_chunks.reserve(DISTANCE * DISTANCE); required_chunks.reserve(m_rendering_distance * m_rendering_distance);
int half = DISTANCE / 2; int half = m_rendering_distance / 2;
for (int u = chunk_x - half; u <= chunk_x + half; ++u) { for (int u = chunk_x - half; u <= chunk_x + half; ++u) {
for (int v = chunk_z - half; v <= chunk_z + half; ++v) { for (int v = chunk_z - half; v <= chunk_z + half; ++v) {
required_chunks.emplace(u, v); required_chunks.emplace(u, v);
@@ -664,4 +664,11 @@ void World::push_delete_vbo(GLuint vbo) {
m_pending_delete_vbo.push_back(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/app.hpp>
#include <Cubed/camera.hpp> #include <Cubed/camera.hpp>
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/primitive_data.hpp>
#include <Cubed/debug_collector.hpp> #include <Cubed/debug_collector.hpp>
#include <Cubed/gameplay/player.hpp> #include <Cubed/gameplay/player.hpp>
#include <Cubed/gameplay/world.hpp> #include <Cubed/gameplay/world.hpp>
@@ -37,7 +38,10 @@ Renderer::~Renderer() {
glDeleteVertexArrays(NUM_VAO, m_vao.data()); glDeleteVertexArrays(NUM_VAO, m_vao.data());
} }
void Renderer::hot_reload() {
auto& config = Config::get();
update_fov(config.get<double>("player.fov"));
}
void Renderer::init() { void Renderer::init() {
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
@@ -109,6 +113,7 @@ void Renderer::init() {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
init_text(); init_text();
hot_reload();
} }
const Shader& Renderer::get_shader(const std::string& name) const { const Shader& Renderer::get_shader(const std::string& name) const {

View File

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