Skip to content

Commit b770192

Browse files
author
Dan Liebault
committed
Refactor Chunk and ChunkManager method signatures
Modifications des signatures des méthodes `UpdateChunk` pour utiliser des références d'objet `GameObject` au lieu de pointeurs partagés, améliorant ainsi la gestion de la mémoire. Les méthodes `GetVoxel` et `GetPosition` ont été rendues constantes pour permettre leur utilisation sur des instances constantes. Ajout d'une méthode `mesh_id` pour récupérer l'identifiant du maillage. La méthode `UpdateWorld` dans `ChunkManager` retourne maintenant un booléen pour indiquer si une mise à jour de la scène est nécessaire, simplifiant ainsi l'interface. Ajustements pour assurer la cohérence des appels de méthode.
1 parent fb2965e commit b770192

4 files changed

Lines changed: 29 additions & 24 deletions

File tree

R3DVoxel/VoxelEngine/Chunk.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void Chunk::BuildChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_m
8585
world->bindMatToMesh(m_mesh_id, mat);
8686
}
8787

88-
void Chunk::UpdateChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map, std::shared_ptr<GameObject> world)
88+
void Chunk::UpdateChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map, GameObject& world)
8989
{
9090
// mesh should be updated only m_mesh_id != -1 otherwise, it should be built
9191
if (m_active_voxel == false || m_mesh_id == -1)
@@ -158,7 +158,7 @@ void Chunk::UpdateChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_
158158
}
159159
}
160160

161-
world->UpdateMesh(m_mesh_id, mesh.vertices, mesh.indices);
161+
world.UpdateMesh(m_mesh_id, mesh.vertices, mesh.indices);
162162
}
163163

164164
void Chunk::DeleteChunk(std::shared_ptr<GameObject> world)
@@ -177,16 +177,21 @@ void Chunk::SetBlockType(const uint32_t x, const uint32_t y, const uint32_t z, c
177177
m_blocktypes.at(x + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR) = type;
178178
}
179179

180-
bool Chunk::GetVoxel(const uint32_t x, const uint32_t y, const uint32_t z)
180+
bool Chunk::GetVoxel(const uint32_t x, const uint32_t y, const uint32_t z) const
181181
{
182182
return m_active_voxel[x + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR];
183183
}
184184

185-
glm::vec3 Chunk::GetPosition()
185+
glm::vec3 Chunk::GetPosition() const
186186
{
187187
return m_position;
188188
}
189189

190+
int32_t Chunk::mesh_id() const
191+
{
192+
return m_mesh_id;
193+
}
194+
190195
void Chunk::CreateCube(Geometry& mesh, uint32_t x, uint32_t y, uint32_t z)
191196
{
192197
//vertex

R3DVoxel/VoxelEngine/Chunk.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ class Chunk
3939
Chunk(const int32_t posx, const int32_t posy, const int32_t posz);
4040

4141
void BuildChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map, std::shared_ptr<GameObject> world, std::shared_ptr<Material> mat);
42-
void UpdateChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map, std::shared_ptr<GameObject> world);
42+
void UpdateChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map, GameObject& world);
4343
void DeleteChunk(std::shared_ptr<GameObject> world);
4444

4545
void SetVoxel(const uint32_t x, const uint32_t y, const uint32_t z);
4646
void SetBlockType(const uint32_t x, const uint32_t y, const uint32_t z, const TBlock type);
4747

48-
bool GetVoxel(const uint32_t x, const uint32_t y, const uint32_t z);
48+
bool GetVoxel(const uint32_t x, const uint32_t y, const uint32_t z) const;
4949

50-
glm::vec3 GetPosition();
50+
glm::vec3 GetPosition() const;
51+
int32_t mesh_id() const;
5152

5253
private:
5354
void CreateCube(Geometry& mesh, uint32_t x, uint32_t y, uint32_t z);

R3DVoxel/VoxelEngine/ChunkManager.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ void ChunkManager::CreateWorld()
6464
}
6565
}
6666

67-
void ChunkManager::UpdateWorld(std::shared_ptr<Scene> p_scene, std::shared_ptr<Camera> p_camera)
67+
bool ChunkManager::UpdateWorld(const Camera& camera)
6868
{
6969
bool scene_x_need_update = false;
7070

7171
int32_t create_x = -1;
7272
int32_t update_xplus = -1;
7373
int32_t update_xmin = -1;
74-
if (p_camera->GetPosition().x > m_render_position.x + Voxel::CHUNK_SIZE)
74+
if (camera.GetPosition().x > m_render_position.x + Voxel::CHUNK_SIZE)
7575
{
76-
m_render_position.x = p_camera->GetPosition().x;
76+
m_render_position.x = camera.GetPosition().x;
7777

7878
m_render_max.x = m_render_max.x + 1;
7979

@@ -93,9 +93,9 @@ void ChunkManager::UpdateWorld(std::shared_ptr<Scene> p_scene, std::shared_ptr<C
9393
update_xmin = m_render_min.x;
9494
scene_x_need_update = true;
9595
}
96-
else if (p_camera->GetPosition().x < m_render_position.x - Voxel::CHUNK_SIZE)
96+
else if (camera.GetPosition().x < m_render_position.x - Voxel::CHUNK_SIZE)
9797
{
98-
m_render_position.x = p_camera->GetPosition().x;
98+
m_render_position.x = camera.GetPosition().x;
9999

100100
m_render_min.x = m_render_min.x - 1;
101101

@@ -121,9 +121,9 @@ void ChunkManager::UpdateWorld(std::shared_ptr<Scene> p_scene, std::shared_ptr<C
121121
int32_t create_z = -1;
122122
int32_t update_zplus = -1;
123123
int32_t update_zmin = -1;
124-
if (p_camera->GetPosition().z > m_render_position.z + Voxel::CHUNK_SIZE)
124+
if (camera.GetPosition().z > m_render_position.z + Voxel::CHUNK_SIZE)
125125
{
126-
m_render_position.z = p_camera->GetPosition().z;
126+
m_render_position.z = camera.GetPosition().z;
127127

128128
m_render_max.z = m_render_max.z + 1;
129129

@@ -143,9 +143,9 @@ void ChunkManager::UpdateWorld(std::shared_ptr<Scene> p_scene, std::shared_ptr<C
143143
update_zmin = m_render_min.z;
144144
scene_z_need_update = true;
145145
}
146-
else if (p_camera->GetPosition().z < m_render_position.z - Voxel::CHUNK_SIZE)
146+
else if (camera.GetPosition().z < m_render_position.z - Voxel::CHUNK_SIZE)
147147
{
148-
m_render_position.z = p_camera->GetPosition().z;
148+
m_render_position.z = camera.GetPosition().z;
149149

150150
m_render_min.z = m_render_min.z - 1;
151151

@@ -173,8 +173,8 @@ void ChunkManager::UpdateWorld(std::shared_ptr<Scene> p_scene, std::shared_ptr<C
173173
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
174174
{
175175
m_chunk_map.at({ create_x, y, z })->BuildChunk(m_chunk_map, mp_world, mp_world_mat);
176-
m_chunk_map.at({ update_xplus, y, z })->UpdateChunk(m_chunk_map, mp_world);
177-
m_chunk_map.at({ update_xmin, y, z })->UpdateChunk(m_chunk_map, mp_world);
176+
m_chunk_map.at({ update_xplus, y, z })->UpdateChunk(m_chunk_map, *mp_world);
177+
m_chunk_map.at({ update_xmin, y, z })->UpdateChunk(m_chunk_map, *mp_world);
178178
}
179179
}
180180
}
@@ -186,18 +186,17 @@ void ChunkManager::UpdateWorld(std::shared_ptr<Scene> p_scene, std::shared_ptr<C
186186
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
187187
{
188188
m_chunk_map.at({ x, y, create_z })->BuildChunk(m_chunk_map, mp_world, mp_world_mat);
189-
m_chunk_map.at({ x, y, update_zplus })->UpdateChunk(m_chunk_map, mp_world);
190-
m_chunk_map.at({ x, y, update_zmin })->UpdateChunk(m_chunk_map, mp_world);
189+
m_chunk_map.at({ x, y, update_zplus })->UpdateChunk(m_chunk_map, *mp_world);
190+
m_chunk_map.at({ x, y, update_zmin })->UpdateChunk(m_chunk_map, *mp_world);
191191
}
192192
}
193193
}
194194

195-
if (scene_x_need_update || scene_z_need_update)
196-
p_scene->ToUpdate();
197-
198195
Watcher::WatchPosition("render position", m_render_position);
199196
Watcher::WatchPosition("render max", m_render_max);
200197
Watcher::WatchPosition("render min", m_render_min);
198+
199+
return scene_x_need_update || scene_z_need_update;
201200
}
202201

203202
void ChunkManager::CreateNewChunk(int32_t x, int32_t y, int32_t z)

R3DVoxel/VoxelEngine/ChunkManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ChunkManager
1616
~ChunkManager() = default;
1717

1818
void CreateWorld();
19-
void UpdateWorld(std::shared_ptr<Scene> p_scene, std::shared_ptr<Camera> p_camera);
19+
bool UpdateWorld(const Camera& camera);
2020

2121
void CreateNewChunk(int32_t x, int32_t y, int32_t z);
2222
void DestroyChunk(const int32_t x, const int32_t y, const int32_t z);

0 commit comments

Comments
 (0)