aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/AssetManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'extension/src/openvic-extension/singletons/AssetManager.cpp')
-rw-r--r--extension/src/openvic-extension/singletons/AssetManager.cpp130
1 files changed, 96 insertions, 34 deletions
diff --git a/extension/src/openvic-extension/singletons/AssetManager.cpp b/extension/src/openvic-extension/singletons/AssetManager.cpp
index 083d934..6646c8b 100644
--- a/extension/src/openvic-extension/singletons/AssetManager.cpp
+++ b/extension/src/openvic-extension/singletons/AssetManager.cpp
@@ -13,9 +13,14 @@ using OpenVic::Utilities::godot_to_std_string;
using OpenVic::Utilities::std_to_godot_string;
void AssetManager::_bind_methods() {
- OV_BIND_METHOD(AssetManager::get_image, { "path", "cache", "flip_y" }, DEFVAL(true), DEFVAL(false));
- OV_BIND_METHOD(AssetManager::get_texture, { "path", "flip_y" }, DEFVAL(false));
+ OV_BIND_METHOD(AssetManager::get_image, { "path", "load_flags" }, DEFVAL(LOAD_FLAG_CACHE_IMAGE));
+ OV_BIND_METHOD(AssetManager::get_texture, { "path", "load_flags" }, DEFVAL(LOAD_FLAG_CACHE_TEXTURE));
OV_BIND_METHOD(AssetManager::get_font, { "name" });
+
+ BIND_ENUM_CONSTANT(LOAD_FLAG_NONE);
+ BIND_ENUM_CONSTANT(LOAD_FLAG_CACHE_IMAGE);
+ BIND_ENUM_CONSTANT(LOAD_FLAG_CACHE_TEXTURE);
+ BIND_ENUM_CONSTANT(LOAD_FLAG_FLIP_Y);
}
AssetManager* AssetManager::get_singleton() {
@@ -32,55 +37,95 @@ AssetManager::~AssetManager() {
_singleton = nullptr;
}
-Ref<Image> AssetManager::_load_image(StringName const& path) {
+Ref<Image> AssetManager::_load_image(StringName const& path, bool flip_y) {
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, nullptr);
+
const String lookedup_path =
std_to_godot_string(game_singleton->get_dataloader().lookup_image_file(godot_to_std_string(path)).string());
ERR_FAIL_COND_V_MSG(lookedup_path.is_empty(), nullptr, vformat("Failed to look up image: %s", path));
+
const Ref<Image> image = Utilities::load_godot_image(lookedup_path);
ERR_FAIL_COND_V_MSG(
- image.is_null() || image->is_empty(), nullptr, vformat("Failed to load image: %s (looked up: %s)", path, lookedup_path)
+ image.is_null() || image->is_empty(), nullptr,
+ vformat("Failed to load image: %s (looked up: %s)", path, lookedup_path)
);
- return image;
-}
-AssetManager::image_asset_t* AssetManager::_get_image_asset(StringName const& path, bool flip_y) {
- image_asset_map_t::iterator it = image_assets.find(path);
- if (it != image_assets.end()) {
- return &it.value();
- }
- const Ref<Image> image = _load_image(path);
- ERR_FAIL_NULL_V(image, nullptr);
if (flip_y) {
image->flip_y();
}
- return &image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first.value();
+
+ return image;
}
-Ref<Image> AssetManager::get_image(StringName const& path, bool cache, bool flip_y) {
- if (cache) {
- image_asset_t const* asset = _get_image_asset(path, flip_y);
- ERR_FAIL_NULL_V(asset, nullptr);
- return asset->image;
+Ref<Image> AssetManager::get_image(StringName const& path, LoadFlags load_flags) {
+ /* Check for an existing image entry indicating a previous load attempt, whether successful or not. */
+ const image_asset_map_t::iterator it = image_assets.find(path);
+ if (it != image_assets.end()) {
+ std::optional<Ref<Image>> const& cached_image = it->second.image;
+
+ if (cached_image.has_value()) {
+ ERR_FAIL_NULL_V_MSG(*cached_image, nullptr, vformat("Failed to load image previously: %s", path));
+
+ return *cached_image;
+ }
+ }
+
+ /* No load attempt has been made yet, so we try now. */
+ const Ref<Image> image = _load_image(path, load_flags & LOAD_FLAG_FLIP_Y);
+
+ if (image.is_valid()) {
+ if (load_flags & LOAD_FLAG_CACHE_IMAGE) {
+ image_assets[path].image = image;
+ }
+
+ return image;
} else {
- return _load_image(path);
+ /* Mark both image and texture as failures, regardless of cache flags, in case of future load/creation attempts. */
+ image_assets[path] = { nullptr, nullptr };
+
+ ERR_FAIL_V_MSG(nullptr, vformat("Failed to load image: %s", path));
}
}
-Ref<ImageTexture> AssetManager::get_texture(StringName const& path, bool flip_y) {
- image_asset_t* asset = _get_image_asset(path, flip_y);
- ERR_FAIL_NULL_V(asset, nullptr);
- if (asset->texture.is_null()) {
- asset->texture = ImageTexture::create_from_image(asset->image);
- ERR_FAIL_NULL_V_MSG(asset->texture, nullptr, vformat("Failed to turn image into texture: %s", path));
+Ref<ImageTexture> AssetManager::get_texture(StringName const& path, LoadFlags load_flags) {
+ /* Check for an existing texture entry indicating a previous creation attempt, whether successful or not. */
+ const image_asset_map_t::const_iterator it = image_assets.find(path);
+ if (it != image_assets.end()) {
+ std::optional<Ref<ImageTexture>> const& cached_texture = it->second.texture;
+
+ if (cached_texture.has_value()) {
+ ERR_FAIL_NULL_V_MSG(*cached_texture, nullptr, vformat("Failed to create texture previously: %s", path));
+
+ return *cached_texture;
+ }
+ }
+
+ /* No creation attempt has yet been made, so we try now starting by finding the corresponding image. */
+ const Ref<Image> image = get_image(path, load_flags);
+ ERR_FAIL_NULL_V_MSG(image, nullptr, vformat("Failed to load image for texture: %s", path));
+
+ const Ref<ImageTexture> texture = ImageTexture::create_from_image(image);
+
+ if (texture.is_valid()) {
+ if (load_flags & LOAD_FLAG_CACHE_TEXTURE) {
+ image_assets[path].texture = texture;
+ }
+
+ return texture;
+ } else {
+ /* Mark texture as a failure, regardless of cache flags, in case of future creation attempts. */
+ image_assets[path].texture = nullptr;
+
+ ERR_FAIL_V_MSG(nullptr, vformat("Failed to create texture: %s", path));
}
- return asset->texture;
}
Ref<Font> AssetManager::get_font(StringName const& name) {
const font_map_t::const_iterator it = fonts.find(name);
if (it != fonts.end()) {
+ ERR_FAIL_NULL_V_MSG(it->second, nullptr, vformat("Failed to load font previously: %s", name));
+
return it->second;
}
@@ -89,18 +134,35 @@ Ref<Font> AssetManager::get_font(StringName const& name) {
static const String image_ext = ".tga";
const StringName image_path = font_dir + name + image_ext;
- const Ref<Image> image = get_image(image_path);
- ERR_FAIL_NULL_V_MSG(image, nullptr, vformat("Failed to load font image %s for the font named %s", image_path, name));
+ const Ref<Image> image = get_image(image_path, LOAD_FLAG_NONE);
+ if (image.is_null()) {
+ fonts.emplace(name, nullptr);
+
+ ERR_FAIL_V_MSG(nullptr, vformat("Failed to load font image %s for the font named %s", image_path, name));
+ }
+
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, nullptr);
+
const String font_path = font_dir + name + font_ext;
const String lookedup_font_path =
std_to_godot_string(game_singleton->get_dataloader().lookup_file(godot_to_std_string(font_path)).string());
+ if (lookedup_font_path.is_empty()) {
+ fonts.emplace(name, nullptr);
+
+ ERR_FAIL_V_MSG(nullptr, vformat("Failed to look up font: %s", font_path));
+ }
+
const Ref<Font> font = Utilities::load_godot_font(lookedup_font_path, image);
- ERR_FAIL_NULL_V_MSG(
- font, nullptr,
- vformat("Failed to load font file %s (looked up: %s) for the font named %s", font_path, lookedup_font_path, name)
- );
- fonts.emplace(std::move(name), font);
+ if (font.is_null()) {
+ fonts.emplace(name, nullptr);
+
+ ERR_FAIL_V_MSG(
+ nullptr,
+ vformat("Failed to load font file %s (looked up: %s) for the font named %s", font_path, lookedup_font_path, name)
+ );
+ }
+
+ fonts.emplace(name, font);
return font;
}