diff options
author | George L. Albany <Megacake1234@gmail.com> | 2023-05-26 00:23:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 00:23:57 +0200 |
commit | ba3e974f30b8a2e7611b753ddc274dec8ff28283 (patch) | |
tree | 31d37e00cf6c010180fb3212b45ac5f70728cc8f /extension/src | |
parent | 0a1a50cde54fee469bc0069978cd27a2bd1a2fb0 (diff) | |
parent | 9843807154aa70324c84692ce9d3b54414e2b5e0 (diff) |
Merge pull request #125 from OpenVicProject/import-fix
Diffstat (limited to 'extension/src')
-rw-r--r-- | extension/src/LoadGameCompatibility.cpp | 5 | ||||
-rw-r--r-- | extension/src/LoadGameOpenVic.cpp | 41 | ||||
-rw-r--r-- | extension/src/Utilities.cpp | 16 | ||||
-rw-r--r-- | extension/src/Utilities.hpp | 4 |
4 files changed, 39 insertions, 27 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..27fb265 100644 --- a/extension/src/LoadGameOpenVic.cpp +++ b/extension/src/LoadGameOpenVic.cpp @@ -19,14 +19,15 @@ static Error _load_json_file(String const& file_description, String const& file_ return err == OK ? FAILED : err; } const String json_string = file->get_as_text(); - JSON json; - err = json.parse(json_string); + Ref<JSON> json; + json.instantiate(); + err = json->parse(json_string); if (err != OK) { UtilityFunctions::push_error("Failed to parse ", file_description, " file as JSON: ", file_path, - "\nError at line ", json.get_error_line(), ": ", json.get_error_message()); + "\nError at line ", json->get_error_line(), ": ", json->get_error_message()); return err; } - result = json.get_data(); + result = json->get_data(); return err; } @@ -175,12 +176,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 +228,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 +245,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 +375,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); } |