mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-17 16:17:02 +08:00
feat: add Config class
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -39,4 +39,5 @@ CMakeError.log
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
assets/config.toml
|
||||
@@ -87,6 +87,7 @@ add_executable(${PROJECT_NAME}
|
||||
src/app.cpp
|
||||
src/debug_collector.cpp
|
||||
src/camera.cpp
|
||||
src/config.cpp
|
||||
src/gameplay/biome.cpp
|
||||
src/gameplay/chunk.cpp
|
||||
src/gameplay/player.cpp
|
||||
|
||||
@@ -27,6 +27,7 @@ public:
|
||||
void update_move_camera();
|
||||
|
||||
void camera_init(Player* player);
|
||||
void hot_reload();
|
||||
void reset_camera();
|
||||
void update_cursor_position_camera(double xpos, double ypos);
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <toml++/toml.hpp>
|
||||
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
@@ -8,149 +11,118 @@ constexpr int MAX_BLOCK_NUM = 7;
|
||||
constexpr int MAX_UI_NUM = 1;
|
||||
|
||||
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_CHARACTER = 128;
|
||||
constexpr float NORMAL_FOV = 70.0f;
|
||||
|
||||
constexpr int MAX_BIOME_SUM = 4;
|
||||
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] = {
|
||||
0,1, 1,2, 2,3, 3,0,
|
||||
4,5, 5,6, 6,7, 7,4,
|
||||
0,4, 1,5, 2,6, 3,7
|
||||
};
|
||||
template <typename T>
|
||||
concept TomlValueType =
|
||||
std::same_as<T, int> ||
|
||||
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] = {
|
||||
-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
|
||||
};
|
||||
class Config {
|
||||
public:
|
||||
Config();
|
||||
~Config();
|
||||
|
||||
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,
|
||||
};
|
||||
static Config& get();
|
||||
|
||||
struct Vertex {
|
||||
float x = 0.0f, y = 0.0f, z = 0.0f;
|
||||
float s = 0.0f, t = 0.0f;
|
||||
float layer = 0.0f;
|
||||
};
|
||||
toml::table& table();
|
||||
|
||||
void load_or_create_config();
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <Cubed/primitive_data.hpp>
|
||||
#include <Cubed/ui/text.hpp>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/primitive_data.hpp>
|
||||
#include <Cubed/gameplay/biome.hpp>
|
||||
#include <Cubed/gameplay/chunk_pos.hpp>
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
|
||||
@@ -49,7 +49,7 @@ private:
|
||||
glm::vec3 move_distance {0.0f, 0.0f, 0.0f};
|
||||
// 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};
|
||||
|
||||
glm::vec3 m_front {0, 0, -1};
|
||||
@@ -84,12 +84,15 @@ public:
|
||||
const MoveState& get_move_state() const;
|
||||
|
||||
void change_mode(GameMode mode);
|
||||
void hot_reload();
|
||||
void set_player_pos(const glm::vec3& pos);
|
||||
void update(float delta_time);
|
||||
void update_front_vec(float offset_x, float offset_y);
|
||||
void update_player_move_state(int key, int action);
|
||||
void update_scroll(double yoffset);
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <Cubed/AABB.hpp>
|
||||
#include <Cubed/primitive_data.hpp>
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
@@ -45,7 +46,7 @@ private:
|
||||
std::condition_variable m_gen_cv;
|
||||
std::atomic<bool> m_gen_running{false};
|
||||
std::atomic<bool> m_need_gen_chunk{false};
|
||||
|
||||
std::atomic<int> m_rendering_distance{24};
|
||||
std::vector<ChunkPos> m_dirty_queue;
|
||||
std::vector<ChunkRenderSnapshot> m_render_snapshots;
|
||||
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk;
|
||||
@@ -90,6 +91,8 @@ public:
|
||||
|
||||
void push_delete_vbo(GLuint vbo);
|
||||
|
||||
void hot_reload();
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
143
include/Cubed/primitive_data.hpp
Normal file
143
include/Cubed/primitive_data.hpp
Normal 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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/primitive_data.hpp>
|
||||
#include <Cubed/shader.hpp>
|
||||
#include <Cubed/ui/text.hpp>
|
||||
|
||||
@@ -18,6 +19,7 @@ public:
|
||||
|
||||
Renderer(const Camera& camera, World& world, const TextureManager& texture_manager);
|
||||
~Renderer();
|
||||
void hot_reload();
|
||||
void init();
|
||||
const Shader& get_shader(const std::string& name) const;
|
||||
void render();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/primitive_data.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/primitive_data.hpp>
|
||||
#include <Cubed/ui/color.hpp>
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
@@ -13,6 +13,9 @@ public:
|
||||
GLFWwindow* get_glfw_window();
|
||||
void init();
|
||||
void update_viewport();
|
||||
// end of frame to reload!
|
||||
void hot_reload();
|
||||
|
||||
void toggle_fullscreen();
|
||||
void toggle_mouse_able();
|
||||
|
||||
|
||||
@@ -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
83
src/config.cpp
Normal 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");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user