diff --git a/.gitignore b/.gitignore index e660240..6d5d972 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ CMakeError.log *.swp *.swo *~ -.DS_Store \ No newline at end of file +.DS_Store +assets/config.toml \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 61c7d42..21d6b8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/Cubed/camera.hpp b/include/Cubed/camera.hpp index cdfa523..5676ea4 100644 --- a/include/Cubed/camera.hpp +++ b/include/Cubed/camera.hpp @@ -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); diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp index a20a2da..a80f9ea 100644 --- a/include/Cubed/config.hpp +++ b/include/Cubed/config.hpp @@ -1,5 +1,8 @@ #pragma once #include +#include + +#include 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, 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 +concept TomlValueType = + std::same_as || + std::same_as || + std::same_as || + std::same_as || + std::same_as || + std::same_as || + std::same_as || + std::same_as + ; -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 + 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(); + if (opt){ + return *opt; + } else { + Logger::error("Can't find key {}", n_key); + ASSERT(false); + std::abort(); + } + } + template + void set(std::string_view key, T&& val) { + if constexpr (!TomlValueType>) { + 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(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; }; } diff --git a/include/Cubed/debug_collector.hpp b/include/Cubed/debug_collector.hpp index b16930f..6072896 100644 --- a/include/Cubed/debug_collector.hpp +++ b/include/Cubed/debug_collector.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include diff --git a/include/Cubed/gameplay/chunk.hpp b/include/Cubed/gameplay/chunk.hpp index 9e5aef4..b39a087 100644 --- a/include/Cubed/gameplay/chunk.hpp +++ b/include/Cubed/gameplay/chunk.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/include/Cubed/gameplay/player.hpp b/include/Cubed/gameplay/player.hpp index be45090..1171f75 100644 --- a/include/Cubed/gameplay/player.hpp +++ b/include/Cubed/gameplay/player.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); + + }; diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index 4abbf34..005d362 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace Cubed { @@ -45,7 +46,7 @@ private: std::condition_variable m_gen_cv; std::atomic m_gen_running{false}; std::atomic m_need_gen_chunk{false}; - + std::atomic m_rendering_distance{24}; std::vector m_dirty_queue; std::vector m_render_snapshots; std::vector> m_new_chunk; @@ -90,6 +91,8 @@ public: void push_delete_vbo(GLuint vbo); + void hot_reload(); + }; } diff --git a/include/Cubed/primitive_data.hpp b/include/Cubed/primitive_data.hpp new file mode 100644 index 0000000..84dd58c --- /dev/null +++ b/include/Cubed/primitive_data.hpp @@ -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; +}; + +} \ No newline at end of file diff --git a/include/Cubed/renderer.hpp b/include/Cubed/renderer.hpp index 82186c8..2d4ebb5 100644 --- a/include/Cubed/renderer.hpp +++ b/include/Cubed/renderer.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -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(); diff --git a/include/Cubed/tools/font.hpp b/include/Cubed/tools/font.hpp index 45f330b..6b4437c 100644 --- a/include/Cubed/tools/font.hpp +++ b/include/Cubed/tools/font.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace Cubed { diff --git a/include/Cubed/ui/text.hpp b/include/Cubed/ui/text.hpp index 65258a6..b7533d7 100644 --- a/include/Cubed/ui/text.hpp +++ b/include/Cubed/ui/text.hpp @@ -3,9 +3,9 @@ #include #include #include -#include #include +#include #include namespace Cubed { diff --git a/include/Cubed/window.hpp b/include/Cubed/window.hpp index 6defe05..4ef58e9 100644 --- a/include/Cubed/window.hpp +++ b/include/Cubed/window.hpp @@ -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(); diff --git a/src/camera.cpp b/src/camera.cpp index 5a67e3d..dbf776e 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -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() { diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..adfdd89 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,83 @@ +#include +#include +#include + +#include +#include +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"); +} + + +} \ No newline at end of file diff --git a/src/debug_collector.cpp b/src/debug_collector.cpp index 0f1b009..dfa8005 100644 --- a/src/debug_collector.cpp +++ b/src/debug_collector.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -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("version")); fps_text .position(0.0f, 50.0f) .text("FPS: 0"); diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 9ab1ab1..a0178ba 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -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(config.get("player.mouse_sensitivity")); + +} + void Player::set_player_pos(const glm::vec3& pos) { m_player_pos = pos; } diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index 0e3a571..255278b 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -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("world.rendering_distance"); + m_rendering_distance = dist <= MAX_DISTANCE ? dist : MAX_DISTANCE; + need_gen(); +} + } \ No newline at end of file diff --git a/src/renderer.cpp b/src/renderer.cpp index 7aeeebb..8191fd9 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -37,7 +38,10 @@ Renderer::~Renderer() { glDeleteVertexArrays(NUM_VAO, m_vao.data()); } - +void Renderer::hot_reload() { + auto& config = Config::get(); + update_fov(config.get("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 { diff --git a/src/window.cpp b/src/window.cpp index f122ab1..9be32eb 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -5,6 +5,9 @@ #include 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("window.width"); + m_height = config.get("window.height"); + if (config.get("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("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("window.V-Sync")) { + glfwSwapInterval(1); + } else { + glfwSwapInterval(0); + } + // Window + windowed_width = config.get("window.width"); + windowed_height = config.get("window.height"); - GLFWmonitor* monitor = glfwGetWindowMonitor(m_window); - if (monitor != nullptr) { + if (config.get("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(); }