refactor: warp everything in Cubed namespace

This commit is contained in:
2026-04-20 22:18:02 +08:00
parent 6c74f4582c
commit c2321a0a6e
49 changed files with 288 additions and 66 deletions

View File

@@ -96,7 +96,6 @@ add_executable(${PROJECT_NAME}
src/tools/math_tools.cpp src/tools/math_tools.cpp
src/tools/shader_tools.cpp src/tools/shader_tools.cpp
src/tools/font.cpp src/tools/font.cpp
src/tools/log.cpp
src/tools/perlin_noise.cpp src/tools/perlin_noise.cpp
src/ui/text.cpp src/ui/text.cpp
src/window.cpp src/window.cpp

View File

@@ -1,5 +1,9 @@
#pragma once #pragma once
#include <glm/glm.hpp> #include <glm/glm.hpp>
namespace Cubed {
struct AABB { struct AABB {
glm::vec3 min{0.0f, 0.0f, 0.0f}; glm::vec3 min{0.0f, 0.0f, 0.0f};
glm::vec3 max{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.y <= other.max.y && max.y >= other.min.y) &&
(min.z <= other.max.z && max.z >= other.min.z); (min.z <= other.max.z && max.z >= other.min.z);
} }
}; };
}

View File

@@ -6,6 +6,8 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace Cubed {
class Player; class Player;
@@ -32,4 +34,6 @@ public:
const glm::vec3& get_camera_pos() const; const glm::vec3& get_camera_pos() const;
}; };
}

View File

@@ -1,4 +1,7 @@
#pragma once #pragma once
namespace Cubed {
constexpr int WORLD_SIZE_Y = 256; constexpr int WORLD_SIZE_Y = 256;
constexpr int MAX_BLOCK_NUM = 7; constexpr int MAX_BLOCK_NUM = 7;
constexpr int MAX_UI_NUM = 1; constexpr int MAX_UI_NUM = 1;
@@ -147,4 +150,6 @@ struct Vertex2D {
float x = 0.0f, y = 0.0f; float x = 0.0f, y = 0.0f;
float s = 0.0f, t = 0.0f; float s = 0.0f, t = 0.0f;
float layer = 0.0f; float layer = 0.0f;
}; };
}

View File

@@ -4,6 +4,9 @@
#include <unordered_map> #include <unordered_map>
namespace Cubed {
class DebugCollector { class DebugCollector {
public: public:
static DebugCollector& get(); static DebugCollector& get();
@@ -17,4 +20,6 @@ public:
private: private:
std::unordered_map<std::size_t, Text> m_texts; std::unordered_map<std::size_t, Text> m_texts;
}; };
}

View File

@@ -2,6 +2,8 @@
#include <array> #include <array>
#include <string> #include <string>
namespace Cubed {
constexpr float BIOME_NOISE_FREQUENCY = 0.003f; constexpr float BIOME_NOISE_FREQUENCY = 0.003f;
constexpr float PLAIN_FREQ = 0.4f; constexpr float PLAIN_FREQ = 0.4f;
@@ -28,3 +30,4 @@ BiomeHeightRange get_biome_height_range(Biome biome);
Biome safe_int_to_biome(int x); Biome safe_int_to_biome(int x);
int get_interpolated_height(float world_x, float world_z, float temp, float humid); int get_interpolated_height(float world_x, float world_z, float temp, float humid);
}

View File

@@ -7,6 +7,10 @@
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
namespace Cubed {
struct BlockTexture { struct BlockTexture {
std::string name; std::string name;
unsigned id; unsigned id;
@@ -62,6 +66,9 @@ const std::array<bool, MAX_BLOCK_NUM> TRANSPARENT_MAP {
}; };
inline bool is_in_transparent_map(unsigned id) { 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]; return TRANSPARENT_MAP[id];
}; };
}

View File

@@ -8,6 +8,9 @@
#include <Cubed/gameplay/chunk_pos.hpp> #include <Cubed/gameplay/chunk_pos.hpp>
#include <Cubed/gameplay/block.hpp> #include <Cubed/gameplay/block.hpp>
namespace Cubed {
class World; class World;
// if want to use, do init_chunk(), gen_vertex_data() and // if want to use, do init_chunk(), gen_vertex_data() and
class Chunk { class Chunk {
@@ -71,4 +74,7 @@ public:
void set_chunk_block(int index, unsigned id); void set_chunk_block(int index, unsigned id);
}; };
}

View File

@@ -4,6 +4,10 @@
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
namespace Cubed {
struct ChunkPos { struct ChunkPos {
int x; int x;
int z; int z;
@@ -28,3 +32,5 @@ struct ChunkPos {
}; };
}; };
}

View File

@@ -2,6 +2,10 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
namespace Cubed {
enum class GameMode { enum class GameMode {
CREATIVE, CREATIVE,
SPECTATOR SPECTATOR
@@ -16,4 +20,7 @@ inline std::string to_str(GameMode mode) {
return {"Spective"}; return {"Spective"};
} }
throw std::invalid_argument{"GameMode is invaild"}; throw std::invalid_argument{"GameMode is invaild"};
}
} }

View File

@@ -11,6 +11,8 @@
#include <optional> #include <optional>
#include <string> #include <string>
namespace Cubed {
enum class Gait{ enum class Gait{
WALK, WALK,
RUN RUN
@@ -88,4 +90,7 @@ public:
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

@@ -2,6 +2,8 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
namespace Cubed {
class Chunk; class Chunk;
struct TreeStructNode { struct TreeStructNode {
@@ -9,4 +11,6 @@ struct TreeStructNode {
unsigned id = 0; unsigned id = 0;
}; };
bool build_tree(Chunk& chunk, const glm::ivec3& pos); bool build_tree(Chunk& chunk, const glm::ivec3& pos);
}

View File

@@ -9,6 +9,8 @@
#include <Cubed/AABB.hpp> #include <Cubed/AABB.hpp>
#include <Cubed/gameplay/chunk.hpp> #include <Cubed/gameplay/chunk.hpp>
namespace Cubed {
struct ChunkRenderSnapshot { struct ChunkRenderSnapshot {
GLuint vbo; GLuint vbo;
size_t vertex_count; size_t vertex_count;
@@ -83,4 +85,6 @@ public:
void push_delete_vbo(GLuint vbo); void push_delete_vbo(GLuint vbo);
}; };
}

View File

@@ -2,6 +2,9 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace Cubed {
struct MoveState { struct MoveState {
bool forward = false; bool forward = false;
bool back = false; bool back = false;
@@ -30,4 +33,7 @@ namespace Input {
InputState& get_input_state(); InputState& get_input_state();
}
} }

View File

@@ -1,6 +1,9 @@
#pragma once #pragma once
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
namespace Cubed {
class MapTable { class MapTable {
private: private:
static std::unordered_map<unsigned, std::string> id_to_name_map; static std::unordered_map<unsigned, std::string> id_to_name_map;
@@ -12,3 +15,5 @@ public:
static void init_map(); static void init_map();
}; };
}

View File

@@ -7,6 +7,7 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <vector> #include <vector>
namespace Cubed {
class Camera; class Camera;
class TextureManager; class TextureManager;
@@ -54,4 +55,6 @@ private:
void render_text(); void render_text();
void render_ui(); void render_ui();
void render_world(); void render_world();
}; };
}

View File

@@ -2,6 +2,10 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <string> #include <string>
namespace Cubed {
class Shader { class Shader {
public: public:
Shader(); Shader();
@@ -23,4 +27,6 @@ private:
std::size_t m_hash = 0; std::size_t m_hash = 0;
std::string m_name = "-1"; std::string m_name = "-1";
}; };
}

View File

@@ -3,6 +3,8 @@
#include <Cubed/gameplay/block.hpp> #include <Cubed/gameplay/block.hpp>
#include <Cubed/tools/shader_tools.hpp> #include <Cubed/tools/shader_tools.hpp>
namespace Cubed {
class TextureManager { class TextureManager {
private: private:
@@ -28,4 +30,7 @@ public:
void need_reload(); void need_reload();
void update(); void update();
}; };
}

View File

@@ -1,5 +1,9 @@
#pragma once #pragma once
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
namespace Cubed {
namespace Assert { namespace Assert {
inline void msg(const char* condition, const char* file, inline void msg(const char* condition, const char* file,
int line, const char* func, int line, const char* func,
@@ -17,20 +21,22 @@ namespace Assert {
} }
#ifdef DEBUG_MODE #ifdef DEBUG_MODE
#define CUBED_ASSERT(cond) \ #define ASSERT(cond) \
do { \ do { \
if (!(cond)) { \ if (!(cond)) { \
::Assert::msg(#cond, __FILE__, __LINE__, __func__); \ ::Cubed::Assert::msg(#cond, __FILE__, __LINE__, __func__); \
} \ } \
} while (0) } while (0)
#define CUBED_ASSERT_MSG(cond, message) \ #define ASSERT_MSG(cond, message) \
do { \ do { \
if (!(cond)) { \ if (!(cond)) { \
::Assert::msg(#cond, __FILE__, __LINE__, __func__, message); \ ::Cubed::Assert::msg(#cond, __FILE__, __LINE__, __func__, message); \
} \ } \
} while (0) } while (0)
#else #else
#define CUBED_ASSERT(cond) ((void)0) #define ASSERT(cond) ((void)0)
#define CUBED_ASSERT_MSG(cond, message) ((void)0) #define ASSERT_MSG(cond, message) ((void)0)
#endif #endif
}

View File

@@ -1,8 +1,14 @@
#pragma once #pragma once
#include <string_view> #include <string_view>
namespace Cubed {
namespace HASH { namespace HASH {
inline std::size_t str(std::string_view value) { inline std::size_t str(std::string_view value) {
return std::hash<std::string_view>{}(value); return std::hash<std::string_view>{}(value);
} }
}
} }

View File

@@ -10,6 +10,9 @@
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
namespace Cubed {
struct Character { struct Character {
glm::vec2 uv_min; glm::vec2 uv_min;
glm::vec2 uv_max; glm::vec2 uv_max;
@@ -41,4 +44,6 @@ private:
void setup_font_character(); void setup_font_character();
}; };
}

View File

@@ -6,6 +6,8 @@
#include <source_location> #include <source_location>
#include <string> #include <string>
namespace Cubed {
namespace Logger { namespace Logger {
enum class Level { enum class Level {
TRACE, TRACE,
@@ -94,3 +96,7 @@ namespace Logger {
} }
} }
}

View File

@@ -1,5 +1,11 @@
#pragma once #pragma once
#include <glm/glm.hpp> #include <glm/glm.hpp>
namespace Cubed {
namespace Math { namespace Math {
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes); void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes);
}
} }

View File

@@ -2,6 +2,9 @@
#include <atomic> #include <atomic>
#include <vector> #include <vector>
namespace Cubed {
class PerlinNoise { class PerlinNoise {
public: public:
static void init(); static void init();
@@ -13,4 +16,6 @@ private:
static float fade(float t); static float fade(float t);
static float lerp(float t, float a, float b); static float lerp(float t, float a, float b);
static float grad(int hash, float x, float y, float z); static float grad(int hash, float x, float y, float z);
}; };
}

View File

@@ -4,6 +4,9 @@
#include <SOIL2.h> #include <SOIL2.h>
#include <string> #include <string>
namespace Cubed {
namespace Tools { namespace Tools {
GLuint create_shader_program(const std::string& v_shader_path, const std::string& f_shader_path); GLuint create_shader_program(const std::string& v_shader_path, const std::string& f_shader_path);
void print_shader_log(GLuint shader); void print_shader_log(GLuint shader);
@@ -14,3 +17,5 @@ namespace Tools {
unsigned char* load_image_data(const std::string& tex_image_path); unsigned char* load_image_data(const std::string& tex_image_path);
} }
}

View File

@@ -14,6 +14,9 @@ typedef LONG (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
#include <fstream> #include <fstream>
#endif #endif
namespace Cubed {
namespace Tools { namespace Tools {
inline bool get_os_version(std::string& str) { inline bool get_os_version(std::string& str) {
@@ -142,3 +145,4 @@ inline std::string get_cpu_info() {
} }
}

View File

@@ -2,6 +2,9 @@
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <glm/glm.hpp> #include <glm/glm.hpp>
namespace Cubed {
enum class Color { enum class Color {
BLACK, BLACK,
WHITE, WHITE,
@@ -49,7 +52,7 @@ inline constexpr glm::vec4 color_value(Color color) {
case Color::BROWN: case Color::BROWN:
return vec4{0.647f, 0.165f, 0.165f, 1.0f}; return vec4{0.647f, 0.165f, 0.165f, 1.0f};
default: default:
CUBED_ASSERT_MSG(false, "Unknown Color"); ASSERT_MSG(false, "Unknown Color");
return vec4{1.0f, 1.0f, 1.0f, 1.0f}; 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}; return glm::vec4{nr, ng, nb, na};
} }
}

View File

@@ -7,6 +7,8 @@
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/ui/color.hpp> #include <Cubed/ui/color.hpp>
namespace Cubed {
class Shader; class Shader;
@@ -49,4 +51,6 @@ private:
void update_vertices(); void update_vertices();
void upload_to_gpu(); void upload_to_gpu();
}; };
}

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace Cubed{
class Renderer; class Renderer;
class Window { class Window {
public: public:
@@ -22,4 +24,6 @@ private:
int m_height; int m_height;
Renderer& m_renderer; Renderer& m_renderer;
}; };
}

View File

@@ -22,7 +22,7 @@ App::~App() {
} }
void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) { void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window)); App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr"); ASSERT_MSG(app, "nullptr");
if (!app->m_window.is_mouse_enable()) { if (!app->m_window.is_mouse_enable()) {
app->m_camera.update_cursor_position_camera(xpos, ypos); 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) { void App::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window)); App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr"); ASSERT_MSG(app, "nullptr");
auto& input = Input::get_input_state(); auto& input = Input::get_input_state();
switch(key) { switch(key) {
case GLFW_KEY_Q: 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) { void App::window_focus_callback(GLFWwindow* window, int focused) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window)); App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr"); ASSERT_MSG(app, "nullptr");
if (focused) { if (focused) {
app->m_camera.reset_camera(); 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) { void App::window_reshape_callback(GLFWwindow* window, int new_width, int new_height) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window)); App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr"); ASSERT_MSG(app, "nullptr");
app->m_window.update_viewport(); app->m_window.update_viewport();
} }

View File

@@ -2,12 +2,15 @@
#include <Cubed/gameplay/player.hpp> #include <Cubed/gameplay/player.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
namespace Cubed {
Camera::Camera() { Camera::Camera() {
} }
void Camera::update_move_camera() { void Camera::update_move_camera() {
CUBED_ASSERT_MSG(m_player, "nullptr"); ASSERT_MSG(m_player, "nullptr");
auto pos = m_player->get_player_pos(); auto pos = m_player->get_player_pos();
// pos.y need to add 1.6f to center // pos.y need to add 1.6f to center
m_camera_pos = glm::vec3(pos.x, pos.y + 1.6f, pos.z); 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_x = xpos;
m_last_mouse_y = ypos; 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); m_player->update_front_vec(offset_x, offset_y);
} }
const glm::mat4 Camera::get_camera_lookat() const{ 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)); 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 { const glm::vec3& Camera::get_camera_pos() const {
return m_camera_pos; return m_camera_pos;
} }
}

View File

@@ -3,6 +3,9 @@
#include <Cubed/tools/system_info.hpp> #include <Cubed/tools/system_info.hpp>
#include <Cubed/tools/cubed_hash.hpp> #include <Cubed/tools/cubed_hash.hpp>
namespace Cubed {
DebugCollector::DebugCollector() { DebugCollector::DebugCollector() {
} }
@@ -98,11 +101,13 @@ std::unordered_map<std::size_t, Text>& DebugCollector::all_texts() {
Text& DebugCollector::text(std::string_view name) { Text& DebugCollector::text(std::string_view name) {
std::size_t id = HASH::str(name); std::size_t id = HASH::str(name);
auto it = m_texts.find(id); 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; return it->second;
} }
void DebugCollector::report(std::string_view name, std::string_view content) { void DebugCollector::report(std::string_view name, std::string_view content) {
auto& t = text(name); auto& t = text(name);
t.text(content); t.text(content);
}
} }

View File

@@ -6,6 +6,8 @@
#include <cmath> #include <cmath>
#include <unordered_map> #include <unordered_map>
namespace Cubed {
std::string get_biome_str(Biome biome) { std::string get_biome_str(Biome biome) {
std::string str; std::string str;
using enum Biome; using enum Biome;
@@ -86,7 +88,7 @@ Biome safe_int_to_biome(int x) {
}; };
auto it = INT_TO_BIOME_MAP.find(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; return it->second;
} }
@@ -131,4 +133,5 @@ int get_interpolated_height(float world_x, float world_z, float temp, float humi
return static_cast<int>(h); return static_cast<int>(h);
} }
}

View File

@@ -9,6 +9,9 @@
#include <numeric> #include <numeric>
#include <utility> #include <utility>
namespace Cubed {
Chunk::Chunk(World& world, ChunkPos chunk_pos) : Chunk::Chunk(World& world, ChunkPos chunk_pos) :
m_world(world), m_world(world),
m_chunk_pos(chunk_pos) m_chunk_pos(chunk_pos)
@@ -61,10 +64,10 @@ const std::vector<uint8_t>& Chunk::get_chunk_blocks() const{
} }
int Chunk::get_index(int x, int y, int z) { 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) { 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); 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; return (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z;
} }
@@ -208,7 +211,7 @@ void Chunk::init_chunk() {
void Chunk::upload_to_gpu() { void Chunk::upload_to_gpu() {
CUBED_ASSERT(is_need_upload()); ASSERT(is_need_upload());
if (m_vbo == 0) { if (m_vbo == 0) {
glGenBuffers(1, &m_vbo); glGenBuffers(1, &m_vbo);
} }
@@ -333,3 +336,4 @@ void Chunk::resolve_blocks() {
mark_dirty(); mark_dirty();
} }
}

View File

@@ -7,6 +7,8 @@
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace Cubed {
Player::Player(World& world, const std::string& name) : Player::Player(World& world, const std::string& name) :
m_world(world), m_world(world),
m_name(name) m_name(name)
@@ -529,4 +531,7 @@ void Player::update_scroll(double yoffset) {
} }
} }
} }
} }
}

View File

@@ -4,6 +4,9 @@
#include <array> #include <array>
namespace Cubed {
using glm::ivec3; using glm::ivec3;
static constexpr std::array<TreeStructNode, 62> TREE {{ static constexpr std::array<TreeStructNode, 62> 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); chunk.set_chunk_block(Chunk::get_index(tree_node), d.id);
} }
return true; return true;
}
} }

View File

@@ -9,6 +9,9 @@
#include <execution> #include <execution>
namespace Cubed {
static constexpr ChunkPos CHUNK_DIR[] { static constexpr ChunkPos CHUNK_DIR[] {
{1, 0}, {-1, 0}, {0, 1}, {0, -1} {1, 0}, {-1, 0}, {0, 1}, {0, -1}
}; };
@@ -44,7 +47,7 @@ const std::optional<LookBlock>& World::get_look_block_pos(const std::string& nam
auto it = m_players.find(HASH::str(name)); auto it = m_players.find(HASH::str(name));
if (it == m_players.end()) { if (it == m_players.end()) {
Logger::error("Can't find player {}", name); Logger::error("Can't find player {}", name);
CUBED_ASSERT(0); ASSERT(0);
return null_look_block; return null_look_block;
} }
@@ -65,7 +68,7 @@ Player& World::get_player(const std::string& name){
auto it = m_players.find(HASH::str(name)); auto it = m_players.find(HASH::str(name));
if (it == m_players.end()) { if (it == m_players.end()) {
Logger::error("Can't find player {}", name); Logger::error("Can't find player {}", name);
CUBED_ASSERT(0); ASSERT(0);
} }
return it->second; return it->second;
@@ -205,7 +208,7 @@ void World::gen_chunks_internal() {
ChunkPosSet required_chunks; ChunkPosSet required_chunks;
compute_required_chunks(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<ChunkPos> need_gen_chunks_pos; std::vector<ChunkPos> need_gen_chunks_pos;
sync_and_collect_missing_chunks(need_gen_chunks_pos, required_chunks); 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) { void World::push_delete_vbo(GLuint vbo) {
std::lock_guard lk(m_delete_vbo_mutex); std::lock_guard lk(m_delete_vbo_mutex);
m_pending_delete_vbo.push_back(vbo); m_pending_delete_vbo.push_back(vbo);
}
} }

View File

@@ -1,5 +1,8 @@
#include <Cubed/input.hpp> #include <Cubed/input.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
namespace Cubed {
static InputState input_state; static InputState input_state;
namespace Input { namespace Input {
@@ -16,4 +19,7 @@ namespace Input {
}
} }

View File

@@ -3,18 +3,20 @@
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/cubed_hash.hpp> #include <Cubed/tools/cubed_hash.hpp>
namespace Cubed {
std::unordered_map<unsigned, std::string> MapTable::id_to_name_map; std::unordered_map<unsigned, std::string> MapTable::id_to_name_map;
std::unordered_map<size_t, unsigned> MapTable::name_to_id_map; std::unordered_map<size_t, unsigned> MapTable::name_to_id_map;
const std::string& MapTable::get_name_from_id(unsigned id) { const std::string& MapTable::get_name_from_id(unsigned id) {
auto it = id_to_name_map.find(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; return it->second;
} }
const unsigned MapTable::get_id_from_name(const std::string& name) { const unsigned MapTable::get_id_from_name(const std::string& name) {
auto it = name_to_id_map.find(HASH::str(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; return it->second;
} }
@@ -29,4 +31,5 @@ void MapTable::init_map() {
} }
}

View File

@@ -16,6 +16,8 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <format> #include <format>
namespace Cubed {
Renderer::Renderer(const Camera& camera, World& world, const TextureManager& texture_manager): Renderer::Renderer(const Camera& camera, World& world, const TextureManager& texture_manager):
m_camera(camera), m_camera(camera),
m_texture_manager(texture_manager), m_texture_manager(texture_manager),
@@ -111,7 +113,7 @@ void Renderer::init() {
const Shader& Renderer::get_shader(const std::string& name) const { const Shader& Renderer::get_shader(const std::string& name) const {
auto it = m_shaders.find(HASH::str(name)); 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; return it->second;
} }
@@ -273,4 +275,6 @@ void Renderer::render_world() {
glUniformMatrix4fv(m_proj_loc, 1 ,GL_FALSE, glm::value_ptr(m_p_mat)); glUniformMatrix4fv(m_proj_loc, 1 ,GL_FALSE, glm::value_ptr(m_p_mat));
m_mvp_mat = m_p_mat * m_mv_mat; m_mvp_mat = m_p_mat * m_mv_mat;
m_world.render(m_mvp_mat); m_world.render(m_mvp_mat);
}
} }

View File

@@ -4,6 +4,9 @@
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
#include <Cubed/tools/shader_tools.hpp> #include <Cubed/tools/shader_tools.hpp>
namespace Cubed {
Shader::Shader() { Shader::Shader() {
} }
@@ -57,11 +60,11 @@ std::size_t Shader::hash() const {
} }
GLuint Shader::loc(const std::string& loc) 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()); GLint pos = glGetUniformLocation(m_program, loc.c_str());
if (pos == -1) { if (pos == -1) {
Logger::info("Shader name {}, loc name {}, pos {}", m_name, loc, pos); 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<GLuint>(pos); return static_cast<GLuint>(pos);
} }
@@ -75,6 +78,8 @@ const std::string& Shader::name() const {
} }
void Shader::use() 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); glUseProgram(m_program);
}
} }

View File

@@ -3,6 +3,10 @@
#include <Cubed/texture_manager.hpp> #include <Cubed/texture_manager.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
namespace Cubed {
TextureManager::TextureManager() { TextureManager::TextureManager() {
} }
@@ -31,7 +35,7 @@ GLuint TextureManager::get_ui_array() const{
void TextureManager::load_block_status(unsigned id) { 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"; std::string path = "texture/status/" + std::to_string(id) + ".png";
unsigned char* image_data = nullptr; unsigned char* image_data = nullptr;
image_data = (Tools::load_image_data(path)); 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) { 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); const std::string& name = MapTable::get_name_from_id(id);
// air don`t need texture // air don`t need texture
if (id == 0) { if (id == 0) {
@@ -78,7 +82,7 @@ void TextureManager::load_block_texture(unsigned id) {
} }
void TextureManager::load_ui_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"; std::string path = "texture/ui/" + std::to_string(id) + ".png";
unsigned char* image_data = nullptr; unsigned char* image_data = nullptr;
@@ -194,4 +198,6 @@ void TextureManager::hot_reload() {
delet_texture(); delet_texture();
init_texture(); init_texture();
m_need_reload = false; m_need_reload = false;
}
} }

View File

@@ -4,6 +4,8 @@
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
#include <Cubed/tools/shader_tools.hpp> #include <Cubed/tools/shader_tools.hpp>
namespace Cubed {
Font::Font() { Font::Font() {
@@ -121,4 +123,6 @@ std::vector<Vertex2D> Font::vertices(const std::string &text, float x, float y,
GLuint Font::text_texture() { GLuint Font::text_texture() {
return m_text_texture; return m_text_texture;
}
} }

View File

@@ -1,6 +0,0 @@
#include <Cubed/tools/log.hpp>
namespace Logger {
}

View File

@@ -2,6 +2,9 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
namespace Cubed {
namespace Math { namespace Math {
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) { void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) {
if (planes.size() != 6) { if (planes.size() != 6) {
@@ -29,4 +32,7 @@ namespace Math {
} }
}
} }

View File

@@ -7,6 +7,9 @@
#include <numeric> #include <numeric>
#include <random> #include <random>
namespace Cubed {
void PerlinNoise::init() { void PerlinNoise::init() {
p.resize(256); p.resize(256);
std::iota(p.begin(), p.end(), 0); std::iota(p.begin(), p.end(), 0);
@@ -19,7 +22,7 @@ void PerlinNoise::init() {
} }
float PerlinNoise::noise(float x, float y, float z) { 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<int>(std::floor(x)) & 255; int ix = static_cast<int>(std::floor(x)) & 255;
int iy = static_cast<int>(std::floor(y)) & 255; int iy = static_cast<int>(std::floor(y)) & 255;
int iz = static_cast<int>(std::floor(z)) & 255; int iz = static_cast<int>(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; float v = h < 4 ? y : h == 12 || h == 14 ? x : z;
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}
} }

View File

@@ -6,6 +6,9 @@
#include <Cubed/tools/shader_tools.hpp> #include <Cubed/tools/shader_tools.hpp>
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
namespace Cubed {
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace Tools { namespace Tools {
@@ -29,7 +32,7 @@ namespace Tools {
if (vc != 1) { if (vc != 1) {
Logger::error("vertex compilation failed"); Logger::error("vertex compilation failed");
Tools::print_shader_log(v_shader); Tools::print_shader_log(v_shader);
CUBED_ASSERT(0); ASSERT(0);
} }
glCompileShader(f_shader); glCompileShader(f_shader);
Tools::check_opengl_error(); Tools::check_opengl_error();
@@ -37,7 +40,7 @@ namespace Tools {
if (fc != 1) { if (fc != 1) {
Logger::error("vertex compilation failed"); Logger::error("vertex compilation failed");
Tools::print_shader_log(f_shader); Tools::print_shader_log(f_shader);
CUBED_ASSERT(0); ASSERT(0);
} }
GLuint vf_program = glCreateProgram(); GLuint vf_program = glCreateProgram();
glAttachShader(vf_program, v_shader); glAttachShader(vf_program, v_shader);
@@ -50,7 +53,7 @@ namespace Tools {
if (linked != 1) { if (linked != 1) {
Logger::error("linking failed"); Logger::error("linking failed");
Tools::print_program_info(vf_program); Tools::print_program_info(vf_program);
CUBED_ASSERT(0); ASSERT(0);
} }
glDeleteShader(v_shader); glDeleteShader(v_shader);
glDeleteShader(f_shader); glDeleteShader(f_shader);
@@ -123,13 +126,15 @@ namespace Tools {
unsigned char* load_image_data(const std::string& tex_image_path) { unsigned char* load_image_data(const std::string& tex_image_path) {
fs::path path = ASSETS_PATH + 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; unsigned char* data = nullptr;
int width, height, channels; int width, height, channels;
data = SOIL_load_image(path.string().c_str(), &width, &height, &channels, SOIL_LOAD_AUTO); 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; return data;
} }
} }
}

View File

@@ -6,6 +6,10 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
namespace Cubed {
Text::Text(std::string_view name) : Text::Text(std::string_view name) :
NAME(name), NAME(name),
UUID(HASH::str(name)) UUID(HASH::str(name))
@@ -74,11 +78,11 @@ Text& Text::text(std::string_view str) {
} }
void Text::render() { void Text::render() {
CUBED_ASSERT_MSG(m_vbo != 0,"VBO not initialized!"); ASSERT_MSG(m_vbo != 0,"VBO not initialized!");
CUBED_ASSERT_MSG(!m_vertices.empty(), "Text String Not Set"); ASSERT_MSG(!m_vertices.empty(), "Text String Not Set");
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, Font::text_texture()); 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)) * 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)); 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) { if (m_vbo == 0) {
glGenBuffers(1, &m_vbo); 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); glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex2D), m_vertices.data(), GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex2D), m_vertices.data(), GL_DYNAMIC_DRAW);
} }
bool Text::operator==(const Text& other) const { bool Text::operator==(const Text& other) const {
return UUID == other.uuid(); return UUID == other.uuid();
}
} }

View File

@@ -3,6 +3,7 @@
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
#include <Cubed/window.hpp> #include <Cubed/window.hpp>
namespace Cubed {
Window::Window(Renderer& renderer) : Window::Window(Renderer& renderer) :
m_renderer(renderer) m_renderer(renderer)
@@ -111,4 +112,6 @@ void Window::toggle_mouse_able() {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
m_mouse_enable = true; m_mouse_enable = true;
} }
}
} }