diff --git a/include/Cubed/dev_panel.hpp b/include/Cubed/dev_panel.hpp index 7993746..e56956d 100644 --- a/include/Cubed/dev_panel.hpp +++ b/include/Cubed/dev_panel.hpp @@ -48,6 +48,7 @@ private: int m_pre_set_tick_speed = 1; bool m_tick_frezze = false; int m_samples_idx = 1; + int m_threads = 1; void show_about_table_bar(); void show_biome_table_bar(); void show_time_table_bar(); diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index 5be6032..075fd2e 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -82,7 +82,8 @@ private: std::atomic m_tick_running{true}; std::atomic m_rendering_distance{24}; std::atomic m_chunk_gen_fraction{0.0f}; - + std::atomic m_pool_threads{1}; + std::atomic m_max_threads{1}; std::atomic m_game_ticks{0}; std::vector m_dirty_queue; @@ -159,7 +160,8 @@ public: bool is_tick_running() const; void tick_running(bool run); - + int pool_threads() const; + int max_threads() const; void change_pool_threads(int threads); }; diff --git a/src/dev_panel.cpp b/src/dev_panel.cpp index f765419..c68484c 100644 --- a/src/dev_panel.cpp +++ b/src/dev_panel.cpp @@ -463,6 +463,23 @@ void DevPanel::show_world_tab_item() { if (ImGui::SliderInt("Render Distance", &rendering_distance, 2, 128)) { m_app.world().rendering_distance(rendering_distance); } + ImGui::Text( + "Pool Threads %d Max Support Threads %d Reserved Threads %d", + m_app.world().pool_threads(), m_app.world().max_threads(), + RESERVED_THREADS); + ImGui::SliderInt("Set Pool Threads", &m_threads, 1, + m_app.world().max_threads()); + ImGui::SameLine(); + if (ImGui::Button("Set")) { + m_app.world().change_pool_threads(m_threads); + } + if (m_threads > m_app.world().max_threads() - RESERVED_THREADS) { + ImGui::TextColored( + ImVec4(1.0f, 1.0f, 0.0f, 1.0f), + "Waring: When the threads in the thread pool exceed \n(maximum " + "threads minus reserved threads), \nit may cause stuttering."); + } + if (ImGui::Button("Rebuild World")) { m_app.world().rebuild_world(); } diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index 62321d7..ef73553 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -657,13 +657,17 @@ void World::per_tick_time(int ms) { m_per_tick_time = ms; } bool World::is_tick_running() const { return m_tick_running.load(); } void World::tick_running(bool run) { m_tick_running = run; } +int World::pool_threads() const { return m_pool_threads.load(); } +int World::max_threads() const { return m_max_threads.load(); } void World::change_pool_threads(int threads) { - int max_thread = std::thread::hardware_concurrency(); - if (max_thread < 1) { - max_thread = 4; + m_max_threads = std::thread::hardware_concurrency(); + if (m_max_threads < 1) { + Logger::warn("Can't Get Max Support Threads, Set Max Threads to 4"); + m_max_threads = 4; } - int used_thread = std::clamp(threads, 1, max_thread); + int used_thread = std::clamp(threads, 1, m_max_threads.load()); Logger::info("Create New Thread Pool Use {} Threads", used_thread); m_gen_thread_pool.store(std::make_shared(used_thread)); + m_pool_threads = used_thread; } } // namespace Cubed \ No newline at end of file