mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 09:47:01 +08:00
refactor: warp everything in Cubed namespace
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
class Player;
|
||||
|
||||
|
||||
@@ -32,4 +34,6 @@ public:
|
||||
const glm::vec3& get_camera_pos() const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
class DebugCollector {
|
||||
public:
|
||||
static DebugCollector& get();
|
||||
@@ -17,4 +20,6 @@ public:
|
||||
|
||||
private:
|
||||
std::unordered_map<std::size_t, Text> m_texts;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
struct BlockTexture {
|
||||
std::string name;
|
||||
unsigned id;
|
||||
@@ -62,6 +66,9 @@ const std::array<bool, MAX_BLOCK_NUM> 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];
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -8,6 +8,9 @@
|
||||
#include <Cubed/gameplay/chunk_pos.hpp>
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
|
||||
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);
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -4,6 +4,10 @@
|
||||
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
struct ChunkPos {
|
||||
int x;
|
||||
int z;
|
||||
@@ -28,3 +32,5 @@ struct ChunkPos {
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
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"};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,6 +11,8 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
class Chunk;
|
||||
|
||||
struct TreeStructNode {
|
||||
@@ -9,4 +11,6 @@ struct TreeStructNode {
|
||||
unsigned id = 0;
|
||||
};
|
||||
|
||||
bool build_tree(Chunk& chunk, const glm::ivec3& pos);
|
||||
bool build_tree(Chunk& chunk, const glm::ivec3& pos);
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <Cubed/AABB.hpp>
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
struct ChunkRenderSnapshot {
|
||||
GLuint vbo;
|
||||
size_t vertex_count;
|
||||
@@ -83,4 +85,6 @@ public:
|
||||
|
||||
void push_delete_vbo(GLuint vbo);
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
struct MoveState {
|
||||
bool forward = false;
|
||||
bool back = false;
|
||||
@@ -30,4 +33,7 @@ namespace Input {
|
||||
InputState& get_input_state();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
class MapTable {
|
||||
private:
|
||||
static std::unordered_map<unsigned, std::string> id_to_name_map;
|
||||
@@ -12,3 +15,5 @@ public:
|
||||
static void init_map();
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
|
||||
class Camera;
|
||||
class TextureManager;
|
||||
@@ -54,4 +55,6 @@ private:
|
||||
void render_text();
|
||||
void render_ui();
|
||||
void render_world();
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Shader();
|
||||
@@ -23,4 +27,6 @@ private:
|
||||
std::size_t m_hash = 0;
|
||||
std::string m_name = "-1";
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
class TextureManager {
|
||||
private:
|
||||
@@ -28,4 +30,7 @@ public:
|
||||
void need_reload();
|
||||
void update();
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
#include <Cubed/tools/log.hpp>
|
||||
|
||||
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
|
||||
#define ASSERT(cond) ((void)0)
|
||||
#define ASSERT_MSG(cond, message) ((void)0)
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
#pragma once
|
||||
#include <string_view>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
namespace HASH {
|
||||
inline std::size_t str(std::string_view value) {
|
||||
return std::hash<std::string_view>{}(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,9 @@
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
struct Character {
|
||||
glm::vec2 uv_min;
|
||||
glm::vec2 uv_max;
|
||||
@@ -41,4 +44,6 @@ private:
|
||||
void setup_font_character();
|
||||
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
namespace Logger {
|
||||
enum class Level {
|
||||
TRACE,
|
||||
@@ -94,3 +96,7 @@ namespace Logger {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
namespace Math {
|
||||
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,9 @@
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
|
||||
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);
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -4,6 +4,9 @@
|
||||
#include <SOIL2.h>
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,9 @@ typedef LONG (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
|
||||
#include <fstream>
|
||||
#endif
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
namespace Tools {
|
||||
|
||||
inline bool get_os_version(std::string& str) {
|
||||
@@ -142,3 +145,4 @@ inline std::string get_cpu_info() {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/ui/color.hpp>
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
class Shader;
|
||||
|
||||
@@ -49,4 +51,6 @@ private:
|
||||
void update_vertices();
|
||||
void upload_to_gpu();
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
#include <GLFW/glfw3.h>
|
||||
namespace Cubed{
|
||||
|
||||
class Renderer;
|
||||
class Window {
|
||||
public:
|
||||
@@ -22,4 +24,6 @@ private:
|
||||
int m_height;
|
||||
Renderer& m_renderer;
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -22,7 +22,7 @@ App::~App() {
|
||||
}
|
||||
void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||
App* app = static_cast<App*>(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<App*>(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<App*>(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<App*>(glfwGetWindowUserPointer(window));
|
||||
CUBED_ASSERT_MSG(app, "nullptr");
|
||||
ASSERT_MSG(app, "nullptr");
|
||||
app->m_window.update_viewport();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
#include <Cubed/tools/system_info.hpp>
|
||||
#include <Cubed/tools/cubed_hash.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
DebugCollector::DebugCollector() {
|
||||
|
||||
}
|
||||
@@ -98,11 +101,13 @@ std::unordered_map<std::size_t, Text>& 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <cmath>
|
||||
#include <unordered_map>
|
||||
|
||||
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<int>(h);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
Chunk::Chunk(World& world, ChunkPos chunk_pos) :
|
||||
m_world(world),
|
||||
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) {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
using glm::ivec3;
|
||||
|
||||
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);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,9 @@
|
||||
|
||||
#include <execution>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
static constexpr ChunkPos CHUNK_DIR[] {
|
||||
{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));
|
||||
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<ChunkPos> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
#include <Cubed/input.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
static InputState input_state;
|
||||
|
||||
namespace Input {
|
||||
@@ -16,4 +19,7 @@ namespace Input {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,18 +3,20 @@
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/cubed_hash.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
std::unordered_map<unsigned, std::string> MapTable::id_to_name_map;
|
||||
std::unordered_map<size_t, unsigned> 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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <format>
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,9 @@
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
|
||||
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<GLuint>(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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,10 @@
|
||||
#include <Cubed/texture_manager.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
Font::Font() {
|
||||
|
||||
@@ -121,4 +123,6 @@ std::vector<Vertex2D> Font::vertices(const std::string &text, float x, float y,
|
||||
|
||||
GLuint Font::text_texture() {
|
||||
return m_text_texture;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#include <Cubed/tools/log.hpp>
|
||||
|
||||
namespace Logger {
|
||||
|
||||
|
||||
}
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
namespace Math {
|
||||
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) {
|
||||
if (planes.size() != 6) {
|
||||
@@ -29,4 +32,7 @@ namespace Math {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -7,6 +7,9 @@
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
|
||||
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<int>(std::floor(x)) & 255;
|
||||
int iy = static_cast<int>(std::floor(y)) & 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;
|
||||
|
||||
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,9 @@
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,10 @@
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/window.hpp>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user