diff options
author | Hop311 <Hop3114@gmail.com> | 2023-12-29 12:04:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-29 12:04:03 +0100 |
commit | 62b0e39e7cd294cab9c428d15f1be97a8c39eecc (patch) | |
tree | cb3671592c2d1e7697371510f455de7e99f75a12 /extension/src/openvic-extension/utility/Utilities.cpp | |
parent | c21e9b7a529aebcf0148a6671377c3e8478fafdf (diff) | |
parent | d04d0247479b229407048a37cd0ad60933aa3bf1 (diff) |
Merge pull request #184 from OpenVicProject/error-macros
Use `ERR_FAIL_*` macros where possible
Diffstat (limited to 'extension/src/openvic-extension/utility/Utilities.cpp')
-rw-r--r-- | extension/src/openvic-extension/utility/Utilities.cpp | 28 |
1 files changed, 9 insertions, 19 deletions
diff --git a/extension/src/openvic-extension/utility/Utilities.cpp b/extension/src/openvic-extension/utility/Utilities.cpp index 8293e70..4cf515f 100644 --- a/extension/src/openvic-extension/utility/Utilities.cpp +++ b/extension/src/openvic-extension/utility/Utilities.cpp @@ -64,31 +64,23 @@ Ref<Resource> Utilities::load_resource(String const& path, String const& type_hi static Ref<Image> load_dds_image(String const& path) { gli::texture2d texture { gli::load_dds(Utilities::godot_to_std_string(path)) }; - if (texture.empty()) { - UtilityFunctions::push_error("Failed to load DDS file: ", path); - return nullptr; - } + ERR_FAIL_COND_V_MSG(texture.empty(), nullptr, vformat("Failed to load DDS file: %s", path)); static constexpr gli::format expected_format = gli::FORMAT_BGRA8_UNORM_PACK8; const bool needs_bgr_to_rgb = texture.format() == expected_format; if (!needs_bgr_to_rgb) { texture = gli::convert(texture, expected_format); - if (texture.empty()) { - UtilityFunctions::push_error("Failed to convert DDS file: ", path); - return nullptr; - } + ERR_FAIL_COND_V_MSG(texture.empty(), nullptr, vformat("Failed to convert DDS file: %s", path)); } const gli::texture2d::extent_type extent { texture.extent() }; - const int width = extent.x, height = extent.y, size = width * height * 4; + const int32_t width = extent.x, height = extent.y, size = width * height * 4; /* Only fail if there aren't enough bytes, everything seems to work fine if there are extra bytes and we ignore them */ - if (size > texture.size()) { - UtilityFunctions::push_error( - "Texture size ", static_cast<int64_t>(texture.size()), " mismatched with dims-based size ", size, " for ", path - ); - return nullptr; - } + ERR_FAIL_COND_V_MSG( + size > texture.size(), nullptr, + vformat("Texture size %d mismatched with dims-based size %d for %s", static_cast<int64_t>(texture.size()), size, path) + ); PackedByteArray pixels; pixels.resize(size); @@ -113,13 +105,11 @@ Ref<Image> Utilities::load_godot_image(String const& path) { } Ref<FontFile> Utilities::load_godot_font(String const& fnt_path, Ref<Image> const& image) { + ERR_FAIL_NULL_V(image, nullptr); Ref<FontFile> font; font.instantiate(); - const Error err = font->load_bitmap_font(fnt_path); + ERR_FAIL_COND_V_MSG(font->load_bitmap_font(fnt_path) != OK, nullptr, vformat("Failed to load font: %s", fnt_path)); font->set_texture_image(0, { font->get_fixed_size(), 0 }, 0, image); - if (err != OK) { - UtilityFunctions::push_error("Failed to load font (error ", err, "): ", fnt_path); - } return font; } |