diff options
Diffstat (limited to 'extension/src')
-rw-r--r-- | extension/src/LoadLocalisation.cpp | 8 | ||||
-rw-r--r-- | extension/src/LoadLocalisation.hpp | 8 | ||||
-rw-r--r-- | extension/src/MapSingleton.cpp | 18 | ||||
-rw-r--r-- | extension/src/MapSingleton.hpp | 3 | ||||
-rw-r--r-- | extension/src/TestSingleton.cpp | 4 | ||||
-rw-r--r-- | extension/src/TestSingleton.hpp | 4 | ||||
-rw-r--r-- | extension/src/openvic2/Map.cpp | 43 | ||||
-rw-r--r-- | extension/src/openvic2/Map.hpp | 17 | ||||
-rw-r--r-- | extension/src/register_types.cpp | 9 |
9 files changed, 69 insertions, 45 deletions
diff --git a/extension/src/LoadLocalisation.cpp b/extension/src/LoadLocalisation.cpp index c95c08b..8698bb2 100644 --- a/extension/src/LoadLocalisation.cpp +++ b/extension/src/LoadLocalisation.cpp @@ -8,7 +8,7 @@ using namespace godot; using namespace OpenVic2; -LoadLocalisation *LoadLocalisation::singleton = nullptr; +LoadLocalisation* LoadLocalisation::singleton = nullptr; void LoadLocalisation::_bind_methods() { ClassDB::bind_method(D_METHOD("load_file", "file_path", "locale"), &LoadLocalisation::load_file); @@ -16,7 +16,7 @@ void LoadLocalisation::_bind_methods() { ClassDB::bind_method(D_METHOD("load_localisation_dir", "dir_path"), &LoadLocalisation::load_localisation_dir); } -LoadLocalisation *LoadLocalisation::get_singleton() { +LoadLocalisation* LoadLocalisation::get_singleton() { return singleton; } @@ -54,7 +54,7 @@ Error LoadLocalisation::_load_file_into_translation(String const& file_path, Ref } Ref<Translation> LoadLocalisation::_get_translation(String const& locale) { - TranslationServer *server = TranslationServer::get_singleton(); + TranslationServer* server = TranslationServer::get_singleton(); Ref<Translation> translation = server->get_translation_object(locale); if (translation.is_null() || translation->get_locale() != locale) { translation.instantiate(); @@ -93,7 +93,7 @@ Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& lo */ Error LoadLocalisation::load_localisation_dir(String const& dir_path) { if (DirAccess::dir_exists_absolute(dir_path)) { - TranslationServer *server = TranslationServer::get_singleton(); + TranslationServer* server = TranslationServer::get_singleton(); Error err = OK; for (String const& locale_name : DirAccess::get_directories_at(dir_path)) { if (locale_name == server->standardize_locale(locale_name)) { diff --git a/extension/src/LoadLocalisation.hpp b/extension/src/LoadLocalisation.hpp index 90f3158..f54a025 100644 --- a/extension/src/LoadLocalisation.hpp +++ b/extension/src/LoadLocalisation.hpp @@ -4,11 +4,11 @@ #include <godot_cpp/classes/translation.hpp> namespace OpenVic2 { - class LoadLocalisation : public godot::Object - { + class LoadLocalisation : public godot::Object { + GDCLASS(LoadLocalisation, godot::Object) - static LoadLocalisation *singleton; + static LoadLocalisation* singleton; godot::Error _load_file_into_translation(godot::String const& file_path, godot::Ref<godot::Translation> translation); godot::Ref<godot::Translation> _get_translation(godot::String const& locale); @@ -17,7 +17,7 @@ namespace OpenVic2 { static void _bind_methods(); public: - static LoadLocalisation *get_singleton(); + static LoadLocalisation* get_singleton(); LoadLocalisation(); ~LoadLocalisation(); diff --git a/extension/src/MapSingleton.cpp b/extension/src/MapSingleton.cpp index 9f76508..9d13496 100644 --- a/extension/src/MapSingleton.cpp +++ b/extension/src/MapSingleton.cpp @@ -13,7 +13,7 @@ void MapSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("load_province_identifier_file", "file_path"), &MapSingleton::load_province_identifier_file); ClassDB::bind_method(D_METHOD("load_province_shape_file", "file_path"), &MapSingleton::load_province_shape_file); ClassDB::bind_method(D_METHOD("get_province_shape_image"), &MapSingleton::get_province_shape_image); - ClassDB::bind_method(D_METHOD("get_province_id"), &MapSingleton::get_province_id); + ClassDB::bind_method(D_METHOD("get_province_identifier_from_colour", "colour"), &MapSingleton::get_province_identifier_from_colour); } MapSingleton* MapSingleton::get_singleton() { @@ -64,9 +64,9 @@ Error MapSingleton::load_province_identifier_file(String const& file_path) { err = FAILED; continue; } - // static const String prov_prefix = "prov_"; - // if (!identifier.begins_with(prov_prefix)) - // UtilityFunctions::push_warning("Province identifier missing prefix: ", identifier); + static const String prov_prefix = "prov_"; + if (!identifier.begins_with(prov_prefix)) + UtilityFunctions::push_warning("Province identifier missing prefix: ", identifier); type = colour_var.get_type(); Province::colour_t colour = Province::NULL_COLOUR; if (type == Variant::ARRAY) { @@ -115,12 +115,10 @@ Error MapSingleton::load_province_identifier_file(String const& file_path) { return err; } -godot::String MapSingleton::get_province_id(godot::String const& hex_str) { - UtilityFunctions::print(hex_str); - int64_t colour_string = hex_str.hex_to_int(); - godot::String province_id = map.get_province(colour_string).identifier.c_str(); - UtilityFunctions::print("Returning: ",map.get_province(colour_string).to_string().c_str()); - return province_id; +String MapSingleton::get_province_identifier_from_colour(Province::colour_t colour) { + const Province* province = map.get_province_by_colour(colour); + if (province) return province->get_identifier().c_str(); + else return String{}; } Error MapSingleton::load_province_shape_file(String const& file_path) { diff --git a/extension/src/MapSingleton.hpp b/extension/src/MapSingleton.hpp index fd549eb..fb62e11 100644 --- a/extension/src/MapSingleton.hpp +++ b/extension/src/MapSingleton.hpp @@ -1,6 +1,5 @@ #pragma once -#include <godot_cpp/core/class_db.hpp> #include <godot_cpp/classes/image.hpp> #include "openvic2/Map.hpp" @@ -25,7 +24,7 @@ namespace OpenVic2 { godot::Error load_province_identifier_file(godot::String const& file_path); godot::Error load_province_shape_file(godot::String const& file_path); - godot::String get_province_id(godot::String const& hex_str); + godot::String get_province_identifier_from_colour(Province::colour_t colour); godot::Ref<godot::Image> get_province_shape_image() const; }; } diff --git a/extension/src/TestSingleton.cpp b/extension/src/TestSingleton.cpp index 0855a30..d9e2b03 100644 --- a/extension/src/TestSingleton.cpp +++ b/extension/src/TestSingleton.cpp @@ -6,14 +6,14 @@ using namespace godot; using namespace OpenVic2; -TestSingleton *TestSingleton::singleton = nullptr; +TestSingleton* TestSingleton::singleton = nullptr; void TestSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("hello_singleton"), &TestSingleton::hello_singleton); } -TestSingleton *TestSingleton::get_singleton() +TestSingleton* TestSingleton::get_singleton() { return singleton; } diff --git a/extension/src/TestSingleton.hpp b/extension/src/TestSingleton.hpp index de27589..d140516 100644 --- a/extension/src/TestSingleton.hpp +++ b/extension/src/TestSingleton.hpp @@ -8,13 +8,13 @@ namespace OpenVic2 { { GDCLASS(TestSingleton, godot::Object) - static TestSingleton *singleton; + static TestSingleton* singleton; protected: static void _bind_methods(); public: - static TestSingleton *get_singleton(); + static TestSingleton* get_singleton(); TestSingleton(); ~TestSingleton(); diff --git a/extension/src/openvic2/Map.cpp b/extension/src/openvic2/Map.cpp index 40c421a..4815dff 100644 --- a/extension/src/openvic2/Map.cpp +++ b/extension/src/openvic2/Map.cpp @@ -5,14 +5,33 @@ using namespace OpenVic2; -std::string Province::to_string() const { +Province::Province(std::string const& new_identifier, colour_t new_colour) : + identifier(new_identifier), colour(new_colour) {} + +std::string Province::colour_to_hex_string(colour_t colour) { std::ostringstream stream; - stream << "(" << identifier << ", " << std::hex << std::setfill('0') << std::setw(6) << colour << ")"; + stream << std::hex << std::setfill('0') << std::setw(6) << colour; return stream.str(); } +std::string const& Province::get_identifier() const { + return identifier; +} + +Province::colour_t Province::get_colour() const { + return colour; +} + +std::string Province::to_string() const { + return "(" + std::to_string(index) + ", " + identifier + ", " + colour_to_hex_string(colour) + ")"; +} + bool Map::add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message) { - Province new_province = { identifier, colour }; + if (colour == Province::NULL_COLOUR || colour > Province::MAX_COLOUR) { + error_message = "Invalid province colour: " + Province::colour_to_hex_string(colour); + return false; + } + Province new_province{ identifier, colour }; for (Province const& province : provinces) { if (province.identifier == identifier) { error_message = "Duplicate province identifiers: " + province.to_string() + " and " + new_province.to_string(); @@ -28,10 +47,14 @@ bool Map::add_province(std::string const& identifier, Province::colour_t colour, return true; } -Province Map::get_province(Province::colour_t colour) { - for(Province const& province : provinces) { - if (province.colour == colour) { - return province; - } - } -}
\ No newline at end of file +Province* Map::get_province_by_identifier(std::string const& identifier) { + for (Province& province : provinces) + if (province.identifier == identifier) return &province; + return nullptr; +} + +Province* Map::get_province_by_colour(Province::colour_t colour) { + for (Province& province : provinces) + if (province.colour == colour) return &province; + return nullptr; +} diff --git a/extension/src/openvic2/Map.hpp b/extension/src/openvic2/Map.hpp index 6d8482e..f3fd1ef 100644 --- a/extension/src/openvic2/Map.hpp +++ b/extension/src/openvic2/Map.hpp @@ -8,12 +8,18 @@ namespace OpenVic2 { struct Province { using colour_t = uint32_t; - - static const colour_t NULL_COLOUR = 0; - + friend struct Map; + static const colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; + private: std::string identifier; colour_t colour; - + + Province(std::string const& identifier, colour_t colour); + public: + static std::string colour_to_hex_string(colour_t colour); + + std::string const& get_identifier() const; + colour_t get_colour() const; std::string to_string() const; }; @@ -23,7 +29,8 @@ namespace OpenVic2 { public: bool add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message); - Province get_province(Province::colour_t colour); + Province* get_province_by_identifier(std::string const& identifier); + Province* get_province_by_colour(Province::colour_t colour); }; } diff --git a/extension/src/register_types.cpp b/extension/src/register_types.cpp index ef5428c..3ef0c24 100644 --- a/extension/src/register_types.cpp +++ b/extension/src/register_types.cpp @@ -20,8 +20,7 @@ static Checksum* _checksum; static LoadLocalisation* _load_localisation; static MapSingleton* _map_singleton; -void initialize_openvic2_types(ModuleInitializationLevel p_level) -{ +void initialize_openvic2_types(ModuleInitializationLevel p_level) { if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { return; } @@ -68,13 +67,11 @@ void uninitialize_openvic2_types(ModuleInitializationLevel p_level) { memdelete(_map_singleton); } -extern "C" -{ +extern "C" { // Initialization. - GDExtensionBool GDE_EXPORT openvic2_library_init(const GDExtensionInterface *p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) - { + GDExtensionBool GDE_EXPORT openvic2_library_init(const GDExtensionInterface* p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization* r_initialization) { GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization); init_obj.register_initializer(initialize_openvic2_types); |