aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons/AssetManager.cpp
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-12-29 12:04:03 +0100
committer GitHub <noreply@github.com>2023-12-29 12:04:03 +0100
commit62b0e39e7cd294cab9c428d15f1be97a8c39eecc (patch)
treecb3671592c2d1e7697371510f455de7e99f75a12 /extension/src/openvic-extension/singletons/AssetManager.cpp
parentc21e9b7a529aebcf0148a6671377c3e8478fafdf (diff)
parentd04d0247479b229407048a37cd0ad60933aa3bf1 (diff)
Merge pull request #184 from OpenVicProject/error-macros
Use `ERR_FAIL_*` macros where possible
Diffstat (limited to 'extension/src/openvic-extension/singletons/AssetManager.cpp')
-rw-r--r--extension/src/openvic-extension/singletons/AssetManager.cpp72
1 files changed, 27 insertions, 45 deletions
diff --git a/extension/src/openvic-extension/singletons/AssetManager.cpp b/extension/src/openvic-extension/singletons/AssetManager.cpp
index 581c7fa..a81a0a2 100644
--- a/extension/src/openvic-extension/singletons/AssetManager.cpp
+++ b/extension/src/openvic-extension/singletons/AssetManager.cpp
@@ -32,66 +32,50 @@ AssetManager::~AssetManager() {
_singleton = nullptr;
}
-Ref<Image> AssetManager::_load_image(StringName path) {
+Ref<Image> AssetManager::_load_image(StringName const& path) {
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());
- if (lookedup_path.is_empty()) {
- UtilityFunctions::push_error("Failed to look up image: ", path);
- return nullptr;
- }
+ 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);
- if (image.is_null() || image->is_empty()) {
- UtilityFunctions::push_error("Failed to load image: ", lookedup_path, " (looked up from ", path, ")");
- return nullptr;
- } else {
- return image;
- }
+ ERR_FAIL_COND_V_MSG(
+ image.is_null() || image->is_empty(), nullptr, vformat("Failed to load image: %s (looked up: %s)", path, lookedup_path)
+ );
+ return image;
}
-AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName path) {
+AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName const& path) {
const image_asset_map_t::iterator it = image_assets.find(path);
if (it != image_assets.end()) {
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 image_assets.end();
- }
+ ERR_FAIL_NULL_V(image, image_assets.end());
+ return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first;
}
-Ref<Image> AssetManager::get_image(StringName path, bool cache) {
+Ref<Image> AssetManager::get_image(StringName const& 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;
- }
+ ERR_FAIL_COND_V(it == image_assets.end(), nullptr);
+ return it->second.image;
} else {
return _load_image(path);
}
}
-Ref<ImageTexture> AssetManager::get_texture(StringName path) {
+Ref<ImageTexture> AssetManager::get_texture(StringName const& path) {
const image_asset_map_t::iterator it = _get_image_asset(path);
- if (it != image_assets.end()) {
- if (it->second.texture.is_null()) {
- it->second.texture = ImageTexture::create_from_image(it->second.image);
- if (it->second.texture.is_null()) {
- UtilityFunctions::push_error("Failed to turn image into texture: ", path);
- }
- }
- return it->second.texture;
- } else {
- return nullptr;
+ ERR_FAIL_COND_V(it == image_assets.end(), nullptr);
+ if (it->second.texture.is_null()) {
+ it->second.texture = ImageTexture::create_from_image(it->second.image);
+ ERR_FAIL_NULL_V_MSG(it->second.texture, nullptr, vformat("Failed to turn image into texture: %s", path));
}
+ return it->second.texture;
}
-Ref<Font> AssetManager::get_font(StringName name) {
+Ref<Font> AssetManager::get_font(StringName const& name) {
const font_map_t::const_iterator it = fonts.find(name);
if (it != fonts.end()) {
return it->second;
@@ -101,21 +85,19 @@ Ref<Font> AssetManager::get_font(StringName name) {
static const String font_ext = ".fnt";
static const String image_ext = ".tga";
- const String image_path = font_dir + name + image_ext;
+ const StringName image_path = font_dir + name + image_ext;
const Ref<Image> image = get_image(image_path);
- if (image.is_null()) {
- UtilityFunctions::push_error("Failed to load font image: ", image_path, " for the font named ", name);
- return nullptr;
- }
+ ERR_FAIL_NULL_V_MSG(image, 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_dir + name + font_ext)).string());
+ std_to_godot_string(game_singleton->get_dataloader().lookup_file(godot_to_std_string(font_path)).string());
const Ref<Font> font = Utilities::load_godot_font(lookedup_font_path, image);
- if (font.is_null()) {
- UtilityFunctions::push_error("Failed to load font file ", lookedup_font_path, " for the font named ", name);
- return nullptr;
- }
+ 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);
return font;
}