Skip to content

Commit 3dc25d0

Browse files
committed
Améliore la gestion des chunks et simplifie Application
Modifications apportées à `Application::Start()` pour se concentrer sur `update_x` et ajouter une méthode `copy_to_render` dans `ChunkManager`. La méthode `compute_world_update_x` a été optimisée pour préallouer de la mémoire et gérer les chunks via `_setup_list`. Normalisation des noms de variables dans `ChunkManager.h` et mise à jour des dépendances de projet dans `R3D_Engine.sln`.
1 parent 500b9b7 commit 3dc25d0

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

R3DVoxel/Application.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ void Application::Start()
6868
{
6969
const auto update_x = chunk_manager.compute_world_update_x(*mp_engine->GetMainCamera());
7070
if (update_x.updated)
71+
{
72+
chunk_manager.copy_to_render();
7173
chunk_manager.update_world_x(update_x.created, update_x.update_plus, update_x.update_min);
74+
}
7275

73-
const auto update_z = chunk_manager.compute_world_update_z(*mp_engine->GetMainCamera());
76+
/* const auto update_z = chunk_manager.compute_world_update_z(*mp_engine->GetMainCamera());
7477
if (update_z.updated)
75-
chunk_manager.update_world_z(update_z.created, update_z.update_plus, update_z.update_min);
78+
chunk_manager.update_world_z(update_z.created, update_z.update_plus, update_z.update_min);*/
7679

77-
if (update_x.updated || update_z.updated)
80+
if (update_x.updated /* || update_z.updated */)
7881
scene->ToUpdate();
7982

8083
Watcher::WatchPosition("camera", mp_engine->GetMainCamera()->GetPosition());

R3DVoxel/VoxelEngine/ChunkManager.cpp

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,22 @@ render_update_t ChunkManager::compute_world_update_x(const Camera& camera)
208208

209209
m_render_max.x = m_render_max.x + 1;
210210

211+
// preallocate memory for chunks
211212
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
212213
{
213214
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
214215
{
215-
CreateNewChunk(m_render_max.x, y, z);
216-
DestroyChunk(m_render_min.x, y, z);
216+
const ChunkKey key = { .x = m_render_max.x, .y = y, .z = z };
217+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, nullptr));
218+
}
219+
}
220+
221+
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
222+
{
223+
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
224+
{
225+
_setup_list.at({ m_render_max.x, y, z }) = create_chunk(m_render_max.x, y, z);
226+
// DestroyChunk(m_render_min.x, y, z);
217227
}
218228
}
219229

@@ -230,12 +240,22 @@ render_update_t ChunkManager::compute_world_update_x(const Camera& camera)
230240

231241
m_render_min.x = m_render_min.x - 1;
232242

243+
// preallocate memory for chunks
233244
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
234245
{
235246
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
236247
{
237-
CreateNewChunk(m_render_min.x, y, z);
238-
DestroyChunk(m_render_max.x, y, z);
248+
const ChunkKey key = { .x = m_render_min.x, .y = y, .z = z };
249+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, nullptr));
250+
}
251+
}
252+
253+
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
254+
{
255+
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
256+
{
257+
_setup_list.at({ m_render_min.x, y, z }) = create_chunk(m_render_min.x, y, z);
258+
// DestroyChunk(m_render_max.x, y, z);
239259
}
240260
}
241261

@@ -328,6 +348,28 @@ void ChunkManager::update_world_z(int32_t create_z, int32_t update_zplus, int32_
328348
}
329349
}
330350

351+
void ChunkManager::copy_to_render()
352+
{
353+
for (auto& [key, chunk] : _setup_list)
354+
{
355+
if (chunk)
356+
{
357+
m_chunk_map.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, std::move(chunk)));
358+
}
359+
}
360+
361+
for (auto& [key, chunk] : _destroy_list)
362+
{
363+
if (chunk)
364+
{
365+
m_chunk_map.erase(key);
366+
}
367+
}
368+
369+
_setup_list.clear();
370+
_destroy_list.clear();
371+
}
372+
331373
void ChunkManager::CreateNewChunk(int32_t x, int32_t y, int32_t z)
332374
{
333375
const ChunkKey key = { x, y, z };

R3DVoxel/VoxelEngine/ChunkManager.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class ChunkManager
3434
void update_world_x(int32_t create_x, int32_t update_xplus, int32_t update_xmin) const;
3535
void update_world_z(int32_t create_z, int32_t update_zplus, int32_t update_zmin) const;
3636

37+
void copy_to_render();
38+
3739
void CreateNewChunk(int32_t x, int32_t y, int32_t z);
3840
void DestroyChunk(const int32_t x, const int32_t y, const int32_t z);
3941

@@ -47,8 +49,10 @@ class ChunkManager
4749
std::shared_ptr<GameObject> mp_world;
4850
std::shared_ptr<Material> mp_world_mat;
4951

50-
std::map<ChunkKey, std::unique_ptr<Chunk>> m_setup_list;
52+
std::map<ChunkKey, std::unique_ptr<Chunk>> _setup_list;
53+
std::map<ChunkKey, std::unique_ptr<Chunk>> _destroy_list;
5154
std::map<ChunkKey, std::unique_ptr<Chunk>> m_chunk_map;
55+
5256
uint8_t m_load_radius = 10;
5357
glm::vec3 m_render_position;
5458

R3D_Engine.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MovingLight", "MovingLight\
2121
EndProjectSection
2222
EndProject
2323
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "R3DVoxel", "R3DVoxel\R3DVoxel.vcxproj", "{E129D7F0-3D7C-4F89-8034-30B8F0E20A14}"
24+
ProjectSection(ProjectDependencies) = postProject
25+
{4401C86B-984C-4184-A0A5-33586F893073} = {4401C86B-984C-4184-A0A5-33586F893073}
26+
EndProjectSection
2427
EndProject
2528
Global
2629
GlobalSection(SolutionConfigurationPlatforms) = preSolution

0 commit comments

Comments
 (0)