mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
feat: async world generation
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
@@ -7,12 +8,10 @@
|
||||
#include <Cubed/gameplay/block.hpp>
|
||||
|
||||
class World;
|
||||
|
||||
// if want to use, do init_chunk(), gen_vertex_data() and
|
||||
class Chunk {
|
||||
private:
|
||||
|
||||
bool m_is_gened = false;
|
||||
bool m_dirty = false;
|
||||
std::atomic<bool> m_dirty {false};
|
||||
|
||||
static constexpr int SIZE_X = CHUCK_SIZE;
|
||||
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
||||
@@ -28,24 +27,33 @@ private:
|
||||
float frequency = 0.01f;
|
||||
float height = 80;
|
||||
|
||||
void clear_dirty();
|
||||
public:
|
||||
Chunk(World& world, ChunkPos chunk_pos);
|
||||
~Chunk();
|
||||
Chunk(const Chunk&) = delete;
|
||||
Chunk& operator=(const Chunk&) = delete;
|
||||
Chunk(Chunk&&) = default;
|
||||
Chunk& operator=(Chunk&&) = delete;
|
||||
Chunk(Chunk&&);
|
||||
Chunk& operator=(Chunk&&);
|
||||
const std::vector<uint8_t>& get_chunk_blocks() const;
|
||||
|
||||
static int get_index(int x, int y, int z);
|
||||
|
||||
void init_chunk();
|
||||
void gen_vertex_data();
|
||||
// 0 : (1, 0)
|
||||
// 1 : (-1, 0)
|
||||
// 2 : (0, 1)
|
||||
// 3 : (0, -1)
|
||||
void gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neighbor_block);
|
||||
void upload_to_gpu();
|
||||
|
||||
GLuint get_vbo() const;
|
||||
const std::vector<Vertex>& get_vertex_data() const;
|
||||
void init_chunk();
|
||||
|
||||
bool is_dirty() const;
|
||||
void mark_dirty();
|
||||
void clear_dirty();
|
||||
|
||||
void set_chunk_block(int index, unsigned id);
|
||||
|
||||
};
|
||||
@@ -1,20 +1,50 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <Cubed/AABB.hpp>
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
|
||||
struct ChunkRenderSnapshot {
|
||||
GLuint vbo;
|
||||
size_t vertex_count;
|
||||
glm::vec3 center;
|
||||
glm::vec3 half_extents;
|
||||
};
|
||||
|
||||
|
||||
class Player;
|
||||
|
||||
class World {
|
||||
private:
|
||||
bool m_need_gen_chunk;
|
||||
glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f};
|
||||
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
||||
std::unordered_map<std::size_t, Player> m_players;
|
||||
std::vector<glm::vec4> m_planes;
|
||||
|
||||
void gen_chunks();
|
||||
std::thread m_gen_thread;
|
||||
std::mutex m_chunks_mutex;
|
||||
std::mutex m_gen_signal_mutex;
|
||||
std::mutex m_new_chunk_queue_mutex;
|
||||
std::mutex m_delete_vbo_mutex;
|
||||
std::vector<GLuint> m_pending_delete_vbo;
|
||||
std::condition_variable m_gen_cv;
|
||||
std::atomic<bool> m_gen_running{false};
|
||||
std::atomic<bool> m_need_gen_chunk{false};
|
||||
|
||||
std::vector<ChunkPos> m_dirty_queue;
|
||||
std::vector<ChunkRenderSnapshot> m_render_snapshots;
|
||||
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk;
|
||||
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk_queue;
|
||||
|
||||
void gen_chunks_internal();
|
||||
|
||||
void start_gen_thread();
|
||||
void stop_gen_thread();
|
||||
|
||||
public:
|
||||
|
||||
@@ -31,7 +61,7 @@ public:
|
||||
int get_block(const glm::ivec3& block_pos) const;
|
||||
bool is_block(const glm::ivec3& block_pos) const;
|
||||
|
||||
ChunkPos chunk_pos(int world_x, int world_z) const;
|
||||
static ChunkPos chunk_pos(int world_x, int world_z);
|
||||
|
||||
void need_gen();
|
||||
void render(const glm::mat4& mvp_matrix);
|
||||
@@ -39,5 +69,6 @@ public:
|
||||
void set_block(const glm::ivec3& pos, unsigned id);
|
||||
void update(float delta_time);
|
||||
|
||||
void push_delete_vbo(GLuint vbo);
|
||||
|
||||
};
|
||||
@@ -1,68 +0,0 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <Cubed/AABB.hpp>
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
|
||||
struct ChunkRenderSnapshot {
|
||||
GLuint vbo;
|
||||
size_t vertex_count;
|
||||
glm::vec3 center;
|
||||
glm::vec3 half_extents;
|
||||
};
|
||||
|
||||
|
||||
class Player;
|
||||
|
||||
class World {
|
||||
private:
|
||||
glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f};
|
||||
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
||||
std::unordered_map<std::size_t, Player> m_players;
|
||||
std::vector<glm::vec4> m_planes;
|
||||
|
||||
std::thread m_gen_thread;
|
||||
std::mutex m_chunks_mutex;
|
||||
std::mutex m_gen_signal_mutex; ;
|
||||
std::condition_variable m_gen_cv;
|
||||
std::atomic<bool> m_gen_running{false};
|
||||
std::atomic<bool> m_need_gen_chunk{false};
|
||||
|
||||
std::vector<ChunkPos> m_dirty_queue;
|
||||
std::vector<ChunkRenderSnapshot> m_render_snapshots;
|
||||
|
||||
void gen_chunks_internal();
|
||||
|
||||
void start_gen_thread();
|
||||
void stop_gen_thread();
|
||||
|
||||
public:
|
||||
|
||||
World();
|
||||
~World();
|
||||
|
||||
bool can_move(const AABB& player_box) const;
|
||||
//const BlockRenderData& get_block_render_data(int x, int y ,int z);
|
||||
const std::optional<LookBlock>& get_look_block_pos(const std::string& name) const;
|
||||
Player& get_player(const std::string& name);
|
||||
void init_world();
|
||||
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents);
|
||||
|
||||
int get_block(const glm::ivec3& block_pos) const;
|
||||
bool is_block(const glm::ivec3& block_pos) const;
|
||||
|
||||
ChunkPos chunk_pos(int world_x, int world_z) const;
|
||||
|
||||
void need_gen();
|
||||
void render(const glm::mat4& mvp_matrix);
|
||||
|
||||
void set_block(const glm::ivec3& pos, unsigned id);
|
||||
void update(float delta_time);
|
||||
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user