From c2321a0a6e07d1d9388aa6ae1282eb0232e19d6f Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Mon, 20 Apr 2026 22:18:02 +0800 Subject: [PATCH] refactor: warp everything in Cubed namespace --- CMakeLists.txt | 1 - include/Cubed/AABB.hpp | 8 +++++++- include/Cubed/camera.hpp | 4 ++++ include/Cubed/config.hpp | 7 ++++++- include/Cubed/debug_collector.hpp | 7 ++++++- include/Cubed/gameplay/biome.hpp | 3 +++ include/Cubed/gameplay/block.hpp | 11 +++++++++-- include/Cubed/gameplay/chunk.hpp | 8 +++++++- include/Cubed/gameplay/chunk_pos.hpp | 6 ++++++ include/Cubed/gameplay/game_mode.hpp | 7 +++++++ include/Cubed/gameplay/player.hpp | 7 ++++++- include/Cubed/gameplay/tree.hpp | 6 +++++- include/Cubed/gameplay/world.hpp | 6 +++++- include/Cubed/input.hpp | 6 ++++++ include/Cubed/map_table.hpp | 5 +++++ include/Cubed/renderer.hpp | 5 ++++- include/Cubed/shader.hpp | 8 +++++++- include/Cubed/texture_manager.hpp | 7 ++++++- include/Cubed/tools/cubed_assert.hpp | 20 +++++++++++++------- include/Cubed/tools/cubed_hash.hpp | 6 ++++++ include/Cubed/tools/font.hpp | 7 ++++++- include/Cubed/tools/log.hpp | 6 ++++++ include/Cubed/tools/math_tools.hpp | 6 ++++++ include/Cubed/tools/perlin_noise.hpp | 7 ++++++- include/Cubed/tools/shader_tools.hpp | 5 +++++ include/Cubed/tools/system_info.hpp | 4 ++++ include/Cubed/ui/color.hpp | 7 ++++++- include/Cubed/ui/text.hpp | 6 +++++- include/Cubed/window.hpp | 6 +++++- src/app.cpp | 8 ++++---- src/camera.cpp | 12 +++++++++--- src/debug_collector.cpp | 7 ++++++- src/gameplay/biome.cpp | 5 ++++- src/gameplay/chunk.cpp | 10 +++++++--- src/gameplay/player.cpp | 7 ++++++- src/gameplay/tree.cpp | 5 +++++ src/gameplay/world.cpp | 11 ++++++++--- src/input.cpp | 6 ++++++ src/map_table.cpp | 7 +++++-- src/renderer.cpp | 6 +++++- src/shader.cpp | 11 ++++++++--- src/texture_manager.cpp | 12 +++++++++--- src/tools/font.cpp | 4 ++++ src/tools/log.cpp | 6 ------ src/tools/math_tools.cpp | 6 ++++++ src/tools/perlin_noise.cpp | 7 ++++++- src/tools/shader_tools.cpp | 15 ++++++++++----- src/ui/text.cpp | 14 ++++++++++---- src/window.cpp | 3 +++ 49 files changed, 288 insertions(+), 66 deletions(-) delete mode 100644 src/tools/log.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a40e42e..a9404ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,7 +96,6 @@ add_executable(${PROJECT_NAME} src/tools/math_tools.cpp src/tools/shader_tools.cpp src/tools/font.cpp - src/tools/log.cpp src/tools/perlin_noise.cpp src/ui/text.cpp src/window.cpp diff --git a/include/Cubed/AABB.hpp b/include/Cubed/AABB.hpp index 88dc295..99aa883 100644 --- a/include/Cubed/AABB.hpp +++ b/include/Cubed/AABB.hpp @@ -1,5 +1,9 @@ #pragma once #include + +namespace Cubed { + + struct AABB { glm::vec3 min{0.0f, 0.0f, 0.0f}; glm::vec3 max{0.0f, 0.0f, 0.0f}; @@ -16,4 +20,6 @@ struct AABB { (min.y <= other.max.y && max.y >= other.min.y) && (min.z <= other.max.z && max.z >= other.min.z); } -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/include/Cubed/camera.hpp b/include/Cubed/camera.hpp index 7ea26db..cdfa523 100644 --- a/include/Cubed/camera.hpp +++ b/include/Cubed/camera.hpp @@ -6,6 +6,8 @@ #include #include +namespace Cubed { + class Player; @@ -32,4 +34,6 @@ public: const glm::vec3& get_camera_pos() const; }; +} + diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp index 2e8e32d..6945c96 100644 --- a/include/Cubed/config.hpp +++ b/include/Cubed/config.hpp @@ -1,4 +1,7 @@ #pragma once + +namespace Cubed { + constexpr int WORLD_SIZE_Y = 256; constexpr int MAX_BLOCK_NUM = 7; constexpr int MAX_UI_NUM = 1; @@ -147,4 +150,6 @@ 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/debug_collector.hpp b/include/Cubed/debug_collector.hpp index 45d68d9..b16930f 100644 --- a/include/Cubed/debug_collector.hpp +++ b/include/Cubed/debug_collector.hpp @@ -4,6 +4,9 @@ #include +namespace Cubed { + + class DebugCollector { public: static DebugCollector& get(); @@ -17,4 +20,6 @@ public: private: std::unordered_map m_texts; -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/include/Cubed/gameplay/biome.hpp b/include/Cubed/gameplay/biome.hpp index 54ee2c4..77fc12d 100644 --- a/include/Cubed/gameplay/biome.hpp +++ b/include/Cubed/gameplay/biome.hpp @@ -2,6 +2,8 @@ #include #include +namespace Cubed { + constexpr float BIOME_NOISE_FREQUENCY = 0.003f; constexpr float PLAIN_FREQ = 0.4f; @@ -28,3 +30,4 @@ BiomeHeightRange get_biome_height_range(Biome biome); Biome safe_int_to_biome(int x); int get_interpolated_height(float world_x, float world_z, float temp, float humid); +} diff --git a/include/Cubed/gameplay/block.hpp b/include/Cubed/gameplay/block.hpp index 4d01c35..36436ac 100644 --- a/include/Cubed/gameplay/block.hpp +++ b/include/Cubed/gameplay/block.hpp @@ -7,6 +7,10 @@ #include #include + +namespace Cubed { + + struct BlockTexture { std::string name; unsigned id; @@ -62,6 +66,9 @@ const std::array TRANSPARENT_MAP { }; inline bool is_in_transparent_map(unsigned id) { - CUBED_ASSERT_MSG(id < MAX_BLOCK_NUM, "ID is invaild"); + ASSERT_MSG(id < MAX_BLOCK_NUM, "ID is invaild"); return TRANSPARENT_MAP[id]; -}; \ No newline at end of file +}; + + +} \ No newline at end of file diff --git a/include/Cubed/gameplay/chunk.hpp b/include/Cubed/gameplay/chunk.hpp index 7045eea..ec63845 100644 --- a/include/Cubed/gameplay/chunk.hpp +++ b/include/Cubed/gameplay/chunk.hpp @@ -8,6 +8,9 @@ #include #include +namespace Cubed { + + class World; // if want to use, do init_chunk(), gen_vertex_data() and class Chunk { @@ -71,4 +74,7 @@ public: void set_chunk_block(int index, unsigned id); -}; \ No newline at end of file +}; + + +} \ No newline at end of file diff --git a/include/Cubed/gameplay/chunk_pos.hpp b/include/Cubed/gameplay/chunk_pos.hpp index e16b776..104cc5a 100644 --- a/include/Cubed/gameplay/chunk_pos.hpp +++ b/include/Cubed/gameplay/chunk_pos.hpp @@ -4,6 +4,10 @@ #include #include + +namespace Cubed { + + struct ChunkPos { int x; int z; @@ -28,3 +32,5 @@ struct ChunkPos { }; }; + +} \ No newline at end of file diff --git a/include/Cubed/gameplay/game_mode.hpp b/include/Cubed/gameplay/game_mode.hpp index 57e7f07..bb8d145 100644 --- a/include/Cubed/gameplay/game_mode.hpp +++ b/include/Cubed/gameplay/game_mode.hpp @@ -2,6 +2,10 @@ #include #include + +namespace Cubed { + + enum class GameMode { CREATIVE, SPECTATOR @@ -16,4 +20,7 @@ inline std::string to_str(GameMode mode) { return {"Spective"}; } throw std::invalid_argument{"GameMode is invaild"}; +} + + } \ No newline at end of file diff --git a/include/Cubed/gameplay/player.hpp b/include/Cubed/gameplay/player.hpp index 7868bdd..be45090 100644 --- a/include/Cubed/gameplay/player.hpp +++ b/include/Cubed/gameplay/player.hpp @@ -11,6 +11,8 @@ #include #include +namespace Cubed { + enum class Gait{ WALK, RUN @@ -88,4 +90,7 @@ public: void update_player_move_state(int key, int action); void update_scroll(double yoffset); -}; \ No newline at end of file +}; + + +} diff --git a/include/Cubed/gameplay/tree.hpp b/include/Cubed/gameplay/tree.hpp index 051e0b5..5327c22 100644 --- a/include/Cubed/gameplay/tree.hpp +++ b/include/Cubed/gameplay/tree.hpp @@ -2,6 +2,8 @@ #include +namespace Cubed { + class Chunk; struct TreeStructNode { @@ -9,4 +11,6 @@ struct TreeStructNode { unsigned id = 0; }; -bool build_tree(Chunk& chunk, const glm::ivec3& pos); \ No newline at end of file +bool build_tree(Chunk& chunk, const glm::ivec3& pos); + +} diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index 5545e7a..666f301 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -9,6 +9,8 @@ #include #include +namespace Cubed { + struct ChunkRenderSnapshot { GLuint vbo; size_t vertex_count; @@ -83,4 +85,6 @@ public: void push_delete_vbo(GLuint vbo); -}; \ No newline at end of file +}; + +} diff --git a/include/Cubed/input.hpp b/include/Cubed/input.hpp index f2a2705..7902f2c 100644 --- a/include/Cubed/input.hpp +++ b/include/Cubed/input.hpp @@ -2,6 +2,9 @@ #include +namespace Cubed { + + struct MoveState { bool forward = false; bool back = false; @@ -30,4 +33,7 @@ namespace Input { InputState& get_input_state(); +} + + } \ No newline at end of file diff --git a/include/Cubed/map_table.hpp b/include/Cubed/map_table.hpp index 439d8a5..99cd7f8 100644 --- a/include/Cubed/map_table.hpp +++ b/include/Cubed/map_table.hpp @@ -1,6 +1,9 @@ #pragma once #include #include + +namespace Cubed { + class MapTable { private: static std::unordered_map id_to_name_map; @@ -12,3 +15,5 @@ public: static void init_map(); }; + +} \ No newline at end of file diff --git a/include/Cubed/renderer.hpp b/include/Cubed/renderer.hpp index 1c36173..82186c8 100644 --- a/include/Cubed/renderer.hpp +++ b/include/Cubed/renderer.hpp @@ -7,6 +7,7 @@ #include #include #include +namespace Cubed { class Camera; class TextureManager; @@ -54,4 +55,6 @@ private: void render_text(); void render_ui(); void render_world(); -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/include/Cubed/shader.hpp b/include/Cubed/shader.hpp index 3e74de8..aee1784 100644 --- a/include/Cubed/shader.hpp +++ b/include/Cubed/shader.hpp @@ -2,6 +2,10 @@ #include #include + +namespace Cubed { + + class Shader { public: Shader(); @@ -23,4 +27,6 @@ private: std::size_t m_hash = 0; std::string m_name = "-1"; -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/include/Cubed/texture_manager.hpp b/include/Cubed/texture_manager.hpp index 14d1f51..1453af4 100644 --- a/include/Cubed/texture_manager.hpp +++ b/include/Cubed/texture_manager.hpp @@ -3,6 +3,8 @@ #include #include +namespace Cubed { + class TextureManager { private: @@ -28,4 +30,7 @@ public: void need_reload(); void update(); -}; \ No newline at end of file +}; + + +} \ No newline at end of file diff --git a/include/Cubed/tools/cubed_assert.hpp b/include/Cubed/tools/cubed_assert.hpp index e2ad948..8281b5c 100644 --- a/include/Cubed/tools/cubed_assert.hpp +++ b/include/Cubed/tools/cubed_assert.hpp @@ -1,5 +1,9 @@ #pragma once #include + +namespace Cubed { + + namespace Assert { inline void msg(const char* condition, const char* file, int line, const char* func, @@ -17,20 +21,22 @@ namespace Assert { } #ifdef DEBUG_MODE -#define CUBED_ASSERT(cond) \ +#define ASSERT(cond) \ do { \ if (!(cond)) { \ - ::Assert::msg(#cond, __FILE__, __LINE__, __func__); \ + ::Cubed::Assert::msg(#cond, __FILE__, __LINE__, __func__); \ } \ } while (0) -#define CUBED_ASSERT_MSG(cond, message) \ +#define ASSERT_MSG(cond, message) \ do { \ if (!(cond)) { \ - ::Assert::msg(#cond, __FILE__, __LINE__, __func__, message); \ + ::Cubed::Assert::msg(#cond, __FILE__, __LINE__, __func__, message); \ } \ } while (0) #else -#define CUBED_ASSERT(cond) ((void)0) -#define CUBED_ASSERT_MSG(cond, message) ((void)0) -#endif \ No newline at end of file +#define ASSERT(cond) ((void)0) +#define ASSERT_MSG(cond, message) ((void)0) +#endif + +} \ No newline at end of file diff --git a/include/Cubed/tools/cubed_hash.hpp b/include/Cubed/tools/cubed_hash.hpp index d44c8d3..8a3b6da 100644 --- a/include/Cubed/tools/cubed_hash.hpp +++ b/include/Cubed/tools/cubed_hash.hpp @@ -1,8 +1,14 @@ #pragma once #include + +namespace Cubed { + + namespace HASH { inline std::size_t str(std::string_view value) { return std::hash{}(value); } +} + } \ No newline at end of file diff --git a/include/Cubed/tools/font.hpp b/include/Cubed/tools/font.hpp index 796c9ec..45f330b 100644 --- a/include/Cubed/tools/font.hpp +++ b/include/Cubed/tools/font.hpp @@ -10,6 +10,9 @@ #include +namespace Cubed { + + struct Character { glm::vec2 uv_min; glm::vec2 uv_max; @@ -41,4 +44,6 @@ private: void setup_font_character(); -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/include/Cubed/tools/log.hpp b/include/Cubed/tools/log.hpp index 60607b6..3e8406e 100644 --- a/include/Cubed/tools/log.hpp +++ b/include/Cubed/tools/log.hpp @@ -6,6 +6,8 @@ #include #include +namespace Cubed { + namespace Logger { enum class Level { TRACE, @@ -94,3 +96,7 @@ namespace Logger { } } + + +} + diff --git a/include/Cubed/tools/math_tools.hpp b/include/Cubed/tools/math_tools.hpp index 9935d70..9355f2a 100644 --- a/include/Cubed/tools/math_tools.hpp +++ b/include/Cubed/tools/math_tools.hpp @@ -1,5 +1,11 @@ #pragma once #include + +namespace Cubed { + + namespace Math { void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector& planes); +} + } \ No newline at end of file diff --git a/include/Cubed/tools/perlin_noise.hpp b/include/Cubed/tools/perlin_noise.hpp index 15e421d..871be90 100644 --- a/include/Cubed/tools/perlin_noise.hpp +++ b/include/Cubed/tools/perlin_noise.hpp @@ -2,6 +2,9 @@ #include #include +namespace Cubed { + + class PerlinNoise { public: static void init(); @@ -13,4 +16,6 @@ private: static float fade(float t); static float lerp(float t, float a, float b); static float grad(int hash, float x, float y, float z); -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/include/Cubed/tools/shader_tools.hpp b/include/Cubed/tools/shader_tools.hpp index fe11f89..229107d 100644 --- a/include/Cubed/tools/shader_tools.hpp +++ b/include/Cubed/tools/shader_tools.hpp @@ -4,6 +4,9 @@ #include #include +namespace Cubed { + + namespace Tools { GLuint create_shader_program(const std::string& v_shader_path, const std::string& f_shader_path); void print_shader_log(GLuint shader); @@ -14,3 +17,5 @@ namespace Tools { unsigned char* load_image_data(const std::string& tex_image_path); } + +} diff --git a/include/Cubed/tools/system_info.hpp b/include/Cubed/tools/system_info.hpp index c09a93c..703c708 100644 --- a/include/Cubed/tools/system_info.hpp +++ b/include/Cubed/tools/system_info.hpp @@ -14,6 +14,9 @@ typedef LONG (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW); #include #endif +namespace Cubed { + + namespace Tools { inline bool get_os_version(std::string& str) { @@ -142,3 +145,4 @@ inline std::string get_cpu_info() { } +} diff --git a/include/Cubed/ui/color.hpp b/include/Cubed/ui/color.hpp index d6fb03e..5f8343b 100644 --- a/include/Cubed/ui/color.hpp +++ b/include/Cubed/ui/color.hpp @@ -2,6 +2,9 @@ #include #include +namespace Cubed { + + enum class Color { BLACK, WHITE, @@ -49,7 +52,7 @@ inline constexpr glm::vec4 color_value(Color color) { case Color::BROWN: return vec4{0.647f, 0.165f, 0.165f, 1.0f}; default: - CUBED_ASSERT_MSG(false, "Unknown Color"); + ASSERT_MSG(false, "Unknown Color"); return vec4{1.0f, 1.0f, 1.0f, 1.0f}; } @@ -63,3 +66,5 @@ inline glm::vec4 rgb255_to_float(int r, int g, int b, int a) { return glm::vec4{nr, ng, nb, na}; } + +} \ No newline at end of file diff --git a/include/Cubed/ui/text.hpp b/include/Cubed/ui/text.hpp index 5911220..65258a6 100644 --- a/include/Cubed/ui/text.hpp +++ b/include/Cubed/ui/text.hpp @@ -7,6 +7,8 @@ #include #include +namespace Cubed { + class Shader; @@ -49,4 +51,6 @@ private: void update_vertices(); void upload_to_gpu(); -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/include/Cubed/window.hpp b/include/Cubed/window.hpp index bae4ee4..6defe05 100644 --- a/include/Cubed/window.hpp +++ b/include/Cubed/window.hpp @@ -1,5 +1,7 @@ #pragma once #include +namespace Cubed{ + class Renderer; class Window { public: @@ -22,4 +24,6 @@ private: int m_height; Renderer& m_renderer; -}; \ No newline at end of file +}; + +} \ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp index 8483727..b1a85c4 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -22,7 +22,7 @@ App::~App() { } void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) { App* app = static_cast(glfwGetWindowUserPointer(window)); - CUBED_ASSERT_MSG(app, "nullptr"); + ASSERT_MSG(app, "nullptr"); if (!app->m_window.is_mouse_enable()) { app->m_camera.update_cursor_position_camera(xpos, ypos); } @@ -57,7 +57,7 @@ void App::init() { void App::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { App* app = static_cast(glfwGetWindowUserPointer(window)); - CUBED_ASSERT_MSG(app, "nullptr"); + ASSERT_MSG(app, "nullptr"); auto& input = Input::get_input_state(); switch(key) { case GLFW_KEY_Q: @@ -119,7 +119,7 @@ void App::mouse_button_callback(GLFWwindow* window, int button, int action, int void App::window_focus_callback(GLFWwindow* window, int focused) { App* app = static_cast(glfwGetWindowUserPointer(window)); - CUBED_ASSERT_MSG(app, "nullptr"); + ASSERT_MSG(app, "nullptr"); if (focused) { app->m_camera.reset_camera(); } @@ -128,7 +128,7 @@ void App::window_focus_callback(GLFWwindow* window, int focused) { void App::window_reshape_callback(GLFWwindow* window, int new_width, int new_height) { App* app = static_cast(glfwGetWindowUserPointer(window)); - CUBED_ASSERT_MSG(app, "nullptr"); + ASSERT_MSG(app, "nullptr"); app->m_window.update_viewport(); } diff --git a/src/camera.cpp b/src/camera.cpp index e18b557..5a67e3d 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -2,12 +2,15 @@ #include #include +namespace Cubed { + + Camera::Camera() { } void Camera::update_move_camera() { - CUBED_ASSERT_MSG(m_player, "nullptr"); + ASSERT_MSG(m_player, "nullptr"); auto pos = m_player->get_player_pos(); // pos.y need to add 1.6f to center m_camera_pos = glm::vec3(pos.x, pos.y + 1.6f, pos.z); @@ -37,15 +40,18 @@ void Camera::update_cursor_position_camera(double xpos, double ypos) { m_last_mouse_x = xpos; m_last_mouse_y = ypos; - CUBED_ASSERT_MSG(m_player, "nullptr"); + ASSERT_MSG(m_player, "nullptr"); m_player->update_front_vec(offset_x, offset_y); } const glm::mat4 Camera::get_camera_lookat() const{ - CUBED_ASSERT_MSG(m_player, "nullptr"); + ASSERT_MSG(m_player, "nullptr"); return glm::lookAt(m_camera_pos, m_camera_pos + m_player->get_front(), glm::vec3(0.0f, 1.0f, 0.0f)); } const glm::vec3& Camera::get_camera_pos() const { return m_camera_pos; } + + +} diff --git a/src/debug_collector.cpp b/src/debug_collector.cpp index 8414b5b..0f1b009 100644 --- a/src/debug_collector.cpp +++ b/src/debug_collector.cpp @@ -3,6 +3,9 @@ #include #include +namespace Cubed { + + DebugCollector::DebugCollector() { } @@ -98,11 +101,13 @@ std::unordered_map& DebugCollector::all_texts() { Text& DebugCollector::text(std::string_view name) { std::size_t id = HASH::str(name); auto it = m_texts.find(id); - CUBED_ASSERT_MSG(it != m_texts.end(), "Can't Find Text"); + ASSERT_MSG(it != m_texts.end(), "Can't Find Text"); return it->second; } void DebugCollector::report(std::string_view name, std::string_view content) { auto& t = text(name); t.text(content); +} + } \ No newline at end of file diff --git a/src/gameplay/biome.cpp b/src/gameplay/biome.cpp index d93c020..8826fe4 100644 --- a/src/gameplay/biome.cpp +++ b/src/gameplay/biome.cpp @@ -6,6 +6,8 @@ #include #include +namespace Cubed { + std::string get_biome_str(Biome biome) { std::string str; using enum Biome; @@ -86,7 +88,7 @@ Biome safe_int_to_biome(int x) { }; auto it = INT_TO_BIOME_MAP.find(x); - CUBED_ASSERT_MSG(it != INT_TO_BIOME_MAP.end(), ":Can't Find"); + ASSERT_MSG(it != INT_TO_BIOME_MAP.end(), ":Can't Find"); return it->second; } @@ -131,4 +133,5 @@ int get_interpolated_height(float world_x, float world_z, float temp, float humi return static_cast(h); } +} diff --git a/src/gameplay/chunk.cpp b/src/gameplay/chunk.cpp index b667f7d..88e90e8 100644 --- a/src/gameplay/chunk.cpp +++ b/src/gameplay/chunk.cpp @@ -9,6 +9,9 @@ #include #include + +namespace Cubed { + Chunk::Chunk(World& world, ChunkPos chunk_pos) : m_world(world), m_chunk_pos(chunk_pos) @@ -61,10 +64,10 @@ const std::vector& Chunk::get_chunk_blocks() const{ } int Chunk::get_index(int x, int y, int z) { - CUBED_ASSERT(!(x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE)); + ASSERT(!(x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE)); if ((x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z < 0 || (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z >= CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y) { Logger::error("block pos x {} y {} z {} range error", x, y, z); - CUBED_ASSERT(0); + ASSERT(0); } return (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z; } @@ -208,7 +211,7 @@ void Chunk::init_chunk() { void Chunk::upload_to_gpu() { - CUBED_ASSERT(is_need_upload()); + ASSERT(is_need_upload()); if (m_vbo == 0) { glGenBuffers(1, &m_vbo); } @@ -333,3 +336,4 @@ void Chunk::resolve_blocks() { mark_dirty(); } +} diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 6ab0155..c1cc806 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -7,6 +7,8 @@ #include #include +namespace Cubed { + Player::Player(World& world, const std::string& name) : m_world(world), m_name(name) @@ -529,4 +531,7 @@ void Player::update_scroll(double yoffset) { } } } -} \ No newline at end of file +} + + +} diff --git a/src/gameplay/tree.cpp b/src/gameplay/tree.cpp index 23b361b..64a52d0 100644 --- a/src/gameplay/tree.cpp +++ b/src/gameplay/tree.cpp @@ -4,6 +4,9 @@ #include +namespace Cubed { + + using glm::ivec3; static constexpr std::array TREE {{ @@ -95,4 +98,6 @@ bool build_tree(Chunk& chunk, const glm::ivec3& pos) { chunk.set_chunk_block(Chunk::get_index(tree_node), d.id); } return true; +} + } \ No newline at end of file diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index dbd9459..8515ea8 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -9,6 +9,9 @@ #include +namespace Cubed { + + static constexpr ChunkPos CHUNK_DIR[] { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }; @@ -44,7 +47,7 @@ const std::optional& World::get_look_block_pos(const std::string& nam auto it = m_players.find(HASH::str(name)); if (it == m_players.end()) { Logger::error("Can't find player {}", name); - CUBED_ASSERT(0); + ASSERT(0); return null_look_block; } @@ -65,7 +68,7 @@ Player& World::get_player(const std::string& name){ auto it = m_players.find(HASH::str(name)); if (it == m_players.end()) { Logger::error("Can't find player {}", name); - CUBED_ASSERT(0); + ASSERT(0); } return it->second; @@ -205,7 +208,7 @@ void World::gen_chunks_internal() { ChunkPosSet required_chunks; compute_required_chunks(required_chunks); - CUBED_ASSERT_MSG(!required_chunks.empty(), "required chunks is empty!!"); + ASSERT_MSG(!required_chunks.empty(), "required chunks is empty!!"); std::vector need_gen_chunks_pos; sync_and_collect_missing_chunks(need_gen_chunks_pos, required_chunks); @@ -545,4 +548,6 @@ void World::update(float delta_time) { void World::push_delete_vbo(GLuint vbo) { std::lock_guard lk(m_delete_vbo_mutex); m_pending_delete_vbo.push_back(vbo); +} + } \ No newline at end of file diff --git a/src/input.cpp b/src/input.cpp index 3edfe69..23e6017 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,5 +1,8 @@ #include #include +namespace Cubed { + + static InputState input_state; namespace Input { @@ -16,4 +19,7 @@ namespace Input { +} + + } \ No newline at end of file diff --git a/src/map_table.cpp b/src/map_table.cpp index 986cec4..018d92c 100644 --- a/src/map_table.cpp +++ b/src/map_table.cpp @@ -3,18 +3,20 @@ #include #include +namespace Cubed { + std::unordered_map MapTable::id_to_name_map; std::unordered_map MapTable::name_to_id_map; const std::string& MapTable::get_name_from_id(unsigned id) { auto it = id_to_name_map.find(id); - CUBED_ASSERT_MSG(it != id_to_name_map.end(), "Id: " + std::to_string(id) + " is not exist"); + ASSERT_MSG(it != id_to_name_map.end(), "Id: " + std::to_string(id) + " is not exist"); return it->second; } const unsigned MapTable::get_id_from_name(const std::string& name) { auto it = name_to_id_map.find(HASH::str(name)); - CUBED_ASSERT_MSG(it != name_to_id_map.end(), "Name " + name + " is not exist"); + ASSERT_MSG(it != name_to_id_map.end(), "Name " + name + " is not exist"); return it->second; } @@ -29,4 +31,5 @@ void MapTable::init_map() { } +} diff --git a/src/renderer.cpp b/src/renderer.cpp index ddc4013..7aeeebb 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -16,6 +16,8 @@ #include #include +namespace Cubed { + Renderer::Renderer(const Camera& camera, World& world, const TextureManager& texture_manager): m_camera(camera), m_texture_manager(texture_manager), @@ -111,7 +113,7 @@ void Renderer::init() { const Shader& Renderer::get_shader(const std::string& name) const { auto it = m_shaders.find(HASH::str(name)); - CUBED_ASSERT_MSG(it != m_shaders.end(), "Shader don't find, check the name"); + ASSERT_MSG(it != m_shaders.end(), "Shader don't find, check the name"); return it->second; } @@ -273,4 +275,6 @@ void Renderer::render_world() { glUniformMatrix4fv(m_proj_loc, 1 ,GL_FALSE, glm::value_ptr(m_p_mat)); m_mvp_mat = m_p_mat * m_mv_mat; m_world.render(m_mvp_mat); +} + } \ No newline at end of file diff --git a/src/shader.cpp b/src/shader.cpp index 334fac6..e19633c 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -4,6 +4,9 @@ #include #include +namespace Cubed { + + Shader::Shader() { } @@ -57,11 +60,11 @@ std::size_t Shader::hash() const { } GLuint Shader::loc(const std::string& loc) const { - CUBED_ASSERT_MSG(m_program != 0, "Shader program not created"); + ASSERT_MSG(m_program != 0, "Shader program not created"); GLint pos = glGetUniformLocation(m_program, loc.c_str()); if (pos == -1) { Logger::info("Shader name {}, loc name {}, pos {}", m_name, loc, pos); - CUBED_ASSERT_MSG(pos == -1, "Can't find UniformLocation"); + ASSERT_MSG(pos == -1, "Can't find UniformLocation"); } return static_cast(pos); } @@ -75,6 +78,8 @@ const std::string& Shader::name() const { } void Shader::use() const{ - CUBED_ASSERT_MSG(m_program, "Shader don't create !"); + ASSERT_MSG(m_program, "Shader don't create !"); glUseProgram(m_program); +} + } \ No newline at end of file diff --git a/src/texture_manager.cpp b/src/texture_manager.cpp index 2c1a19b..d52af2d 100644 --- a/src/texture_manager.cpp +++ b/src/texture_manager.cpp @@ -3,6 +3,10 @@ #include #include #include + +namespace Cubed { + + TextureManager::TextureManager() { } @@ -31,7 +35,7 @@ GLuint TextureManager::get_ui_array() const{ void TextureManager::load_block_status(unsigned id) { - CUBED_ASSERT_MSG(id < MAX_BLOCK_STATUS, "Exceed the max status sum limit"); + ASSERT_MSG(id < MAX_BLOCK_STATUS, "Exceed the max status sum limit"); std::string path = "texture/status/" + std::to_string(id) + ".png"; unsigned char* image_data = nullptr; image_data = (Tools::load_image_data(path)); @@ -46,7 +50,7 @@ void TextureManager::load_block_status(unsigned id) { } void TextureManager::load_block_texture(unsigned id) { - CUBED_ASSERT_MSG(id < MAX_BLOCK_NUM, "Exceed the max block sum limit"); + ASSERT_MSG(id < MAX_BLOCK_NUM, "Exceed the max block sum limit"); const std::string& name = MapTable::get_name_from_id(id); // air don`t need texture if (id == 0) { @@ -78,7 +82,7 @@ void TextureManager::load_block_texture(unsigned id) { } void TextureManager::load_ui_texture(unsigned id) { - CUBED_ASSERT_MSG(id < MAX_UI_NUM, "Exceed the max ui sum limit"); + ASSERT_MSG(id < MAX_UI_NUM, "Exceed the max ui sum limit"); std::string path = "texture/ui/" + std::to_string(id) + ".png"; unsigned char* image_data = nullptr; @@ -194,4 +198,6 @@ void TextureManager::hot_reload() { delet_texture(); init_texture(); m_need_reload = false; +} + } \ No newline at end of file diff --git a/src/tools/font.cpp b/src/tools/font.cpp index b909c76..d89eb0c 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -4,6 +4,8 @@ #include #include +namespace Cubed { + Font::Font() { @@ -121,4 +123,6 @@ std::vector Font::vertices(const std::string &text, float x, float y, GLuint Font::text_texture() { return m_text_texture; +} + } \ No newline at end of file diff --git a/src/tools/log.cpp b/src/tools/log.cpp deleted file mode 100644 index 41f3af0..0000000 --- a/src/tools/log.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -namespace Logger { - - -} \ No newline at end of file diff --git a/src/tools/math_tools.cpp b/src/tools/math_tools.cpp index 68ddb04..6339ec8 100644 --- a/src/tools/math_tools.cpp +++ b/src/tools/math_tools.cpp @@ -2,6 +2,9 @@ #include +namespace Cubed { + + namespace Math { void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector& planes) { if (planes.size() != 6) { @@ -29,4 +32,7 @@ namespace Math { } +} + + } \ No newline at end of file diff --git a/src/tools/perlin_noise.cpp b/src/tools/perlin_noise.cpp index be699b3..fbb5205 100644 --- a/src/tools/perlin_noise.cpp +++ b/src/tools/perlin_noise.cpp @@ -7,6 +7,9 @@ #include #include +namespace Cubed { + + void PerlinNoise::init() { p.resize(256); std::iota(p.begin(), p.end(), 0); @@ -19,7 +22,7 @@ void PerlinNoise::init() { } float PerlinNoise::noise(float x, float y, float z) { - CUBED_ASSERT_MSG(is_init, "The PerlinNoise don't init!"); + ASSERT_MSG(is_init, "The PerlinNoise don't init!"); int ix = static_cast(std::floor(x)) & 255; int iy = static_cast(std::floor(y)) & 255; int iz = static_cast(std::floor(z)) & 255; @@ -68,4 +71,6 @@ float PerlinNoise::grad(int hash, float x, float y, float z) { float v = h < 4 ? y : h == 12 || h == 14 ? x : z; return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); +} + } \ No newline at end of file diff --git a/src/tools/shader_tools.cpp b/src/tools/shader_tools.cpp index 27548c2..81127f9 100644 --- a/src/tools/shader_tools.cpp +++ b/src/tools/shader_tools.cpp @@ -6,6 +6,9 @@ #include #include +namespace Cubed { + + namespace fs = std::filesystem; namespace Tools { @@ -29,7 +32,7 @@ namespace Tools { if (vc != 1) { Logger::error("vertex compilation failed"); Tools::print_shader_log(v_shader); - CUBED_ASSERT(0); + ASSERT(0); } glCompileShader(f_shader); Tools::check_opengl_error(); @@ -37,7 +40,7 @@ namespace Tools { if (fc != 1) { Logger::error("vertex compilation failed"); Tools::print_shader_log(f_shader); - CUBED_ASSERT(0); + ASSERT(0); } GLuint vf_program = glCreateProgram(); glAttachShader(vf_program, v_shader); @@ -50,7 +53,7 @@ namespace Tools { if (linked != 1) { Logger::error("linking failed"); Tools::print_program_info(vf_program); - CUBED_ASSERT(0); + ASSERT(0); } glDeleteShader(v_shader); glDeleteShader(f_shader); @@ -123,13 +126,15 @@ namespace Tools { unsigned char* load_image_data(const std::string& tex_image_path) { fs::path path = ASSETS_PATH + tex_image_path; - CUBED_ASSERT_MSG(fs::is_regular_file(path), path.c_str()); + ASSERT_MSG(fs::is_regular_file(path), path.c_str()); unsigned char* data = nullptr; int width, height, channels; data = SOIL_load_image(path.string().c_str(), &width, &height, &channels, SOIL_LOAD_AUTO); - CUBED_ASSERT_MSG(data, "Could not load texture" + path.string()); + ASSERT_MSG(data, "Could not load texture" + path.string()); return data; } } + +} \ No newline at end of file diff --git a/src/ui/text.cpp b/src/ui/text.cpp index 20287bc..4e13e81 100644 --- a/src/ui/text.cpp +++ b/src/ui/text.cpp @@ -6,6 +6,10 @@ #include #include + +namespace Cubed { + + Text::Text(std::string_view name) : NAME(name), UUID(HASH::str(name)) @@ -74,11 +78,11 @@ Text& Text::text(std::string_view str) { } void Text::render() { - CUBED_ASSERT_MSG(m_vbo != 0,"VBO not initialized!"); - CUBED_ASSERT_MSG(!m_vertices.empty(), "Text String Not Set"); + ASSERT_MSG(m_vbo != 0,"VBO not initialized!"); + ASSERT_MSG(!m_vertices.empty(), "Text String Not Set"); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, Font::text_texture()); - CUBED_ASSERT_MSG(m_color_loc, "m_color_loc is null"); + ASSERT_MSG(m_color_loc, "m_color_loc is null"); m_model_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(m_pos.x, m_pos.y, 0.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(m_scale, m_scale, 1.0f)); @@ -109,11 +113,13 @@ void Text::upload_to_gpu() { if (m_vbo == 0) { glGenBuffers(1, &m_vbo); } - CUBED_ASSERT_MSG(m_vbo, "Vbo Is Not Gen"); + ASSERT_MSG(m_vbo, "Vbo Is Not Gen"); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex2D), m_vertices.data(), GL_DYNAMIC_DRAW); } bool Text::operator==(const Text& other) const { return UUID == other.uuid(); +} + } \ No newline at end of file diff --git a/src/window.cpp b/src/window.cpp index 080fa91..f122ab1 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,6 +3,7 @@ #include #include #include +namespace Cubed { Window::Window(Renderer& renderer) : m_renderer(renderer) @@ -111,4 +112,6 @@ void Window::toggle_mouse_able() { glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); m_mouse_enable = true; } +} + } \ No newline at end of file