From b493547fb8c77918306fe56b74b704284cf54931 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 25 May 2023 00:17:53 +0100 Subject: Import fix + ProvinceOverviewPanel close fix --- extension/src/LoadGameCompatibility.cpp | 5 ++--- extension/src/LoadGameOpenVic.cpp | 32 +++++++++++++------------------- extension/src/Utilities.cpp | 16 ++++++++++++++++ extension/src/Utilities.hpp | 4 +++- 4 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 extension/src/Utilities.cpp (limited to 'extension/src') 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 terrain_sheet; - terrain_sheet.instantiate(); - if (terrain_sheet->load(terrain_texturesheet_path) != OK) { + Ref 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 terrain_image; - terrain_image.instantiate(); - const Error err = terrain_image->load(terrain_path); - if (err != OK) { + const Ref 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 province_image, terrain_image; - province_image.instantiate(); - terrain_image.instantiate(); - Error err = province_image->load(province_image_path); - if (err != OK) { + Ref 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 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.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 = load_godot_image(path); + if (image.is_null()) { UtilityFunctions::push_error("Failed to load good icon image: ", path); err = FAILED; continue; } - Ref tex = ImageTexture::create_from_image(image); + const Ref 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 +#include + +using namespace godot; +using namespace OpenVic; + +Ref 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 +#include #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 load_godot_image(godot::String const& path); } -- cgit v1.2.3-56-ga3b1