diff options
author | Hop311 <Hop3114@gmail.com> | 2023-12-04 10:47:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-04 10:47:51 +0100 |
commit | e01d332f407420db84fbfd207f18c3fec1c1b6a3 (patch) | |
tree | b963c671239ecd693d4162afe4b1b9d3b064554e /extension/src/openvic-extension/singletons/AssetManager.cpp | |
parent | 9165f5980c5cfe75b3bad4303a5822340f6adcfc (diff) | |
parent | 6e350a3dc0b596b1f76fab3b943b67b7713ea4fa (diff) |
Merge pull request #168 from OpenVicProject/update-sim
Sim submodule update + compatibility fixes
Diffstat (limited to 'extension/src/openvic-extension/singletons/AssetManager.cpp')
-rw-r--r-- | extension/src/openvic-extension/singletons/AssetManager.cpp | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/extension/src/openvic-extension/singletons/AssetManager.cpp b/extension/src/openvic-extension/singletons/AssetManager.cpp index b50cae8..8e9eb41 100644 --- a/extension/src/openvic-extension/singletons/AssetManager.cpp +++ b/extension/src/openvic-extension/singletons/AssetManager.cpp @@ -37,33 +37,47 @@ AssetManager::~AssetManager() { _singleton = nullptr; } -AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName path) { - const image_asset_map_t::iterator it = image_assets.find(path); - if (it != image_assets.end()) { - return it; - } +Ref<Image> AssetManager::_load_image(StringName path) { GameSingleton* game_singleton = GameSingleton::get_singleton(); - ERR_FAIL_NULL_V(game_singleton, image_assets.end()); + ERR_FAIL_NULL_V(game_singleton, nullptr); const String lookedup_path = - std_to_godot_string(game_singleton->get_dataloader().lookup_image_file_or_dds(godot_to_std_string(path)).string()); + std_to_godot_string(game_singleton->get_dataloader().lookup_image_file(godot_to_std_string(path)).string()); if (lookedup_path.is_empty()) { UtilityFunctions::push_error("Failed to look up image: ", path); - return image_assets.end(); + return nullptr; } const Ref<Image> image = Utilities::load_godot_image(lookedup_path); if (image.is_null() || image->is_empty()) { UtilityFunctions::push_error("Failed to load image: ", lookedup_path, " (looked up from ", path, ")"); - return image_assets.end(); + return nullptr; + } else { + return image; } - return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first; } -Ref<Image> AssetManager::get_image(StringName path) { - const image_asset_map_t::const_iterator it = _get_image_asset(path); +AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName path) { + const image_asset_map_t::iterator it = image_assets.find(path); if (it != image_assets.end()) { - return it->second.image; + return it; + } + const Ref<Image> image = _load_image(path); + if (image.is_valid()) { + return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first; } else { - return nullptr; + return image_assets.end(); + } +} + +Ref<Image> AssetManager::get_image(StringName path, bool cache) { + if (cache) { + const image_asset_map_t::const_iterator it = _get_image_asset(path); + if (it != image_assets.end()) { + return it->second.image; + } else { + return nullptr; + } + } else { + return _load_image(path); } } |