diff options
Diffstat (limited to 'extension')
-rw-r--r-- | extension/src/LoadGameCompatibility.cpp | 5 | ||||
-rw-r--r-- | extension/src/LoadGameOpenVic.cpp | 32 | ||||
-rw-r--r-- | extension/src/Utilities.cpp | 16 | ||||
-rw-r--r-- | extension/src/Utilities.hpp | 4 |
4 files changed, 34 insertions, 23 deletions
diff --git a/extension/src/LoadGameCompatibility.cpp b/extension/src/LoadGameCompatibility.cpp index 96c165b..ddde5b8 100644 --- a/extension/src/LoadGameCompatibility.cpp +++ b/extension/src/LoadGameCompatibility.cpp @@ -88,9 +88,8 @@ Error GameSingleton::_load_terrain_variants_compatibility_mode(String const& ter } // Load the terrain texture sheet and prepare to slice it up - Ref<Image> terrain_sheet; - terrain_sheet.instantiate(); - if (terrain_sheet->load(terrain_texturesheet_path) != OK) { + Ref<Image> terrain_sheet = load_godot_image(terrain_texturesheet_path); + if (terrain_sheet.is_null()) { UtilityFunctions::push_error("Failed to load terrain texture sheet: ", terrain_texturesheet_path); return FAILED; } diff --git a/extension/src/LoadGameOpenVic.cpp b/extension/src/LoadGameOpenVic.cpp index f856fd6..ce1bf39 100644 --- a/extension/src/LoadGameOpenVic.cpp +++ b/extension/src/LoadGameOpenVic.cpp @@ -175,12 +175,10 @@ Error GameSingleton::_parse_terrain_entry(String const& identifier, Variant cons return FAILED; } const String terrain_path = terrain_texture_dir_path + identifier; - Ref<Image> terrain_image; - terrain_image.instantiate(); - const Error err = terrain_image->load(terrain_path); - if (err != OK) { + const Ref<Image> terrain_image = load_godot_image(terrain_path); + if (terrain_image.is_null()) { UtilityFunctions::push_error("Failed to load terrain image: ", terrain_path); - return err; + return FAILED; } return ERR(terrain_variants.add_item({ godot_to_std_string(identifier), colour, terrain_image })); } @@ -229,18 +227,15 @@ Error GameSingleton::_load_map_images(String const& province_image_path, String } // Load images - Ref<Image> province_image, terrain_image; - province_image.instantiate(); - terrain_image.instantiate(); - Error err = province_image->load(province_image_path); - if (err != OK) { + Ref<Image> province_image = load_godot_image(province_image_path); + if (province_image.is_null()) { UtilityFunctions::push_error("Failed to load province image: ", province_image_path); - return err; + return FAILED; } - err = terrain_image->load(terrain_image_path); - if (err != OK) { + Ref<Image> terrain_image = load_godot_image(terrain_image_path); + if (terrain_image.is_null()) { UtilityFunctions::push_error("Failed to load terrain image: ", terrain_image_path); - return err; + return FAILED; } if (flip_vertical) { @@ -249,6 +244,7 @@ Error GameSingleton::_load_map_images(String const& province_image_path, String } // Validate dimensions and format + Error err = OK; const Vector2i province_dims = province_image->get_size(), terrain_dims = terrain_image->get_size(); if (province_dims.x < 1 || province_dims.y < 1) { UtilityFunctions::push_error("Invalid dimensions (", province_dims.x, "x", province_dims.y, ") for province image: ", province_image_path); @@ -378,16 +374,14 @@ Error GameSingleton::_load_goods(String const& defines_path, String const& icons }); game_manager.good_manager.lock_goods(); for (Good const& good : game_manager.good_manager.get_goods()) { - Ref<Image> image; - image.instantiate(); const String path = icons_dir_path + String { "/" } + std_to_godot_string(good.get_identifier()) + ".png"; - const Error good_err = image->load(path); - if (good_err || image.is_null()) { + const Ref<Image> image = load_godot_image(path); + if (image.is_null()) { UtilityFunctions::push_error("Failed to load good icon image: ", path); err = FAILED; continue; } - Ref<Texture> tex = ImageTexture::create_from_image(image); + const Ref<Texture> tex = ImageTexture::create_from_image(image); if (tex.is_null()) { UtilityFunctions::push_error("Failed to generate good icon texture: ", path); err = FAILED; diff --git a/extension/src/Utilities.cpp b/extension/src/Utilities.cpp new file mode 100644 index 0000000..a912490 --- /dev/null +++ b/extension/src/Utilities.cpp @@ -0,0 +1,16 @@ +#include "Utilities.hpp" + +#include <godot_cpp/classes/resource_loader.hpp> +#include <godot_cpp/variant/utility_functions.hpp> + +using namespace godot; +using namespace OpenVic; + +Ref<Image> OpenVic::load_godot_image(String const& path) { + if (path.begins_with("res://")) { + ResourceLoader* loader = ResourceLoader::get_singleton(); + return loader ? loader->load(path) : nullptr; + } else { + return Image::load_from_file(path); + } +} diff --git a/extension/src/Utilities.hpp b/extension/src/Utilities.hpp index 75e873f..681b893 100644 --- a/extension/src/Utilities.hpp +++ b/extension/src/Utilities.hpp @@ -1,6 +1,6 @@ #pragma once -#include <godot_cpp/variant/string.hpp> +#include <godot_cpp/classes/image.hpp> #include "openvic/Types.hpp" @@ -15,4 +15,6 @@ namespace OpenVic { inline godot::String std_to_godot_string(std::string const& str) { return str.c_str(); } + + godot::Ref<godot::Image> load_godot_image(godot::String const& path); } |