From ea46b48eee79c73b35eacdd303dc05df8c6a0494 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 21 Apr 2024 14:39:49 +0200 Subject: [PATCH] Properly wait while resource is pending in ResourceManager --- src/game/engine/resource_manager.cpp | 2 +- src/game/engine/texture_atlas.cpp | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/game/engine/resource_manager.cpp b/src/game/engine/resource_manager.cpp index 73d773d..d524e19 100644 --- a/src/game/engine/resource_manager.cpp +++ b/src/game/engine/resource_manager.cpp @@ -196,7 +196,7 @@ polygun::renderer::Surface& ResourceManager::get_surface(const std::string& reso } // Check if resource is loaded - if(m_textures.count(resource_id)) { + if(m_surfaces.count(resource_id)) { LoadedResource& resource = m_surfaces[resource_id]; resource.m_last_used = std::chrono::steady_clock::now(); release(); diff --git a/src/game/engine/texture_atlas.cpp b/src/game/engine/texture_atlas.cpp index 30d5e23..4c27764 100644 --- a/src/game/engine/texture_atlas.cpp +++ b/src/game/engine/texture_atlas.cpp @@ -79,6 +79,19 @@ void TextureAtlas::construct_thread_func() { std::string texture_filename = m_content_registry.get_node_def(i).m_texture_filename; if(texture_filename.empty()) continue; + + m_resource_manager.get_surface(texture_filename); + + const std::chrono::time_point start = std::chrono::system_clock::now(); + while(m_resource_manager.is_pending()) { + const std::chrono::time_point tm = std::chrono::system_clock::now(); + const std::chrono::duration interval = tm-start; + if(interval.count()>30) { + LOG_WARNING("Waiting for node texture '%s' took too long, stopping", texture_filename.c_str()); + break; + } + } + renderer::Surface& surf = m_resource_manager.get_surface(texture_filename); surfaces[i-1] = &surf; if(surf.get_width()>max_texture_width) @@ -86,15 +99,6 @@ void TextureAtlas::construct_thread_func() { if(surf.get_height()>max_texture_height) max_texture_height = surf.get_height(); } - const std::chrono::time_point start = std::chrono::system_clock::now(); - while(m_resource_manager.is_pending()) { - const std::chrono::time_point tm = std::chrono::system_clock::now(); - const std::chrono::duration interval = tm-start; - if(interval.count()>30) { - LOG_WARNING("Waiting for node textures took too long, stopping"); - break; - } - } acquire(); m_atlas_surface.reset(new renderer::Surface(max_texture_width*surface_count, max_texture_height));