Fix initialization of change_layers (#907)

This commit is contained in:
nnn27 2024-09-14 15:07:34 +02:00 committed by GitHub
parent c924457e28
commit 1ea6db6045
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,7 +21,12 @@ static std::vector<ImGui::Layer*> layers;
// Update layers before rendering to allow layer changes to be applied during rendering. // Update layers before rendering to allow layer changes to be applied during rendering.
// Using deque to keep the order of changes in case a Layer is removed then added again between // Using deque to keep the order of changes in case a Layer is removed then added again between
// frames. // frames.
static std::deque<std::pair<bool, ImGui::Layer*>> change_layers; std::deque<std::pair<bool, ImGui::Layer*>>& GetChangeLayers() {
static std::deque<std::pair<bool, ImGui::Layer*>>* change_layers =
new std::deque<std::pair<bool, ImGui::Layer*>>;
return *change_layers;
}
static std::mutex change_layers_mutex{}; static std::mutex change_layers_mutex{};
namespace ImGui { namespace ImGui {
@ -113,15 +118,15 @@ bool ProcessEvent(SDL_Event* event) {
void NewFrame() { void NewFrame() {
{ {
std::scoped_lock lock{change_layers_mutex}; std::scoped_lock lock{change_layers_mutex};
while (!change_layers.empty()) { while (!GetChangeLayers().empty()) {
const auto [to_be_added, layer] = change_layers.front(); const auto [to_be_added, layer] = GetChangeLayers().front();
if (to_be_added) { if (to_be_added) {
layers.push_back(layer); layers.push_back(layer);
} else { } else {
const auto [begin, end] = std::ranges::remove(layers, layer); const auto [begin, end] = std::ranges::remove(layers, layer);
layers.erase(begin, end); layers.erase(begin, end);
} }
change_layers.pop_front(); GetChangeLayers().pop_front();
} }
} }
@ -184,12 +189,12 @@ void Render(const vk::CommandBuffer& cmdbuf, ::Vulkan::Frame* frame) {
void Layer::AddLayer(Layer* layer) { void Layer::AddLayer(Layer* layer) {
std::scoped_lock lock{change_layers_mutex}; std::scoped_lock lock{change_layers_mutex};
change_layers.emplace_back(true, layer); GetChangeLayers().emplace_back(true, layer);
} }
void Layer::RemoveLayer(Layer* layer) { void Layer::RemoveLayer(Layer* layer) {
std::scoped_lock lock{change_layers_mutex}; std::scoped_lock lock{change_layers_mutex};
change_layers.emplace_back(false, layer); GetChangeLayers().emplace_back(false, layer);
} }
} // namespace ImGui } // namespace ImGui