diff --git a/include/chunkmanager.hpp b/include/chunkmanager.hpp index 24b729d..6c7d319 100644 --- a/include/chunkmanager.hpp +++ b/include/chunkmanager.hpp @@ -7,12 +7,17 @@ namespace chunkmanager { std::thread initGenThread(); std::thread initMeshThread(); + void stopGenThread(); + void stopMeshThread(); + void update(float deltaTime); + void updateChunk(uint32_t, uint16_t, uint16_t, uint16_t); void destroy(); void mesh(); void generate(); + } -#endif \ No newline at end of file +#endif diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index da3ea6e..c15ff95 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -8,6 +8,7 @@ #include "chunkmesher.hpp" #include "globals.hpp" +#include #include #include #include @@ -27,10 +28,12 @@ namespace chunkmanager std::set to_mesh; std::set to_mesh_shared; + std::atomic_bool mesh_should_run; + std::atomic_bool generate_should_run; + void mesh() { - while (true) - { + while (mesh_should_run) if (mutex_queue_mesh.try_lock()) { for (const auto &c : to_mesh) @@ -45,33 +48,11 @@ namespace chunkmanager to_mesh.clear(); mutex_queue_mesh.unlock(); } - - // while (!to_mesh.empty()) - // { - - // Chunk::Chunk *c = to_mesh.front(); - - // if (c->mutex_state.try_lock()) - // { - // chunkmesher::mesh(c); - // // std::cout << "Meshing chunk at " << c->getPosition().x << ", " << c->getPosition().y << ", " << c->getPosition().z << "\n"; - // c->setState(Chunk::CHUNK_STATE_MESHED, true); - // c->mutex_state.unlock(); - // to_mesh.pop(); - // } - // } - // if(mutex_queue_mesh.try_lock()){ - // to_mesh = to_mesh_shared; - // mutex_queue_mesh.unlock(); - // } - // std::cout << "To mesh empty\n"; - } } void generate() { - while (true) - { + while (generate_should_run) if (mutex_queue_generate.try_lock()) { for (const auto &c : to_generate) @@ -86,20 +67,29 @@ namespace chunkmanager to_generate.clear(); mutex_queue_generate.unlock(); } - } } std::thread initMeshThread() { + mesh_should_run = true; std::thread mesh_thread(mesh); return mesh_thread; } std::thread initGenThread() { + generate_should_run = true; std::thread gen_thread(generate); return gen_thread; } + void stopGenThread(){ + generate_should_run = false; + } + + void stopMeshThread(){ + mesh_should_run = false; + } + int total{0}, toGpu{0}; int rr{RENDER_DISTANCE * RENDER_DISTANCE}; uint8_t f = 0; diff --git a/src/main.cpp b/src/main.cpp index ad1f5f5..1b9e42e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ - #include #include @@ -94,12 +93,17 @@ int main() glfwPollEvents(); } - delete theShader; + chunkmanager::stopGenThread(); + chunkmanager::stopMeshThread(); genThread.join(); meshThread.join(); + chunkmanager::destroy(); + delete theShader; + + glfwTerminate(); return 0; } @@ -119,4 +123,4 @@ void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); -} \ No newline at end of file +}