aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension
diff options
context:
space:
mode:
Diffstat (limited to 'extension/src/openvic-extension')
-rw-r--r--extension/src/openvic-extension/singletons/AssetManager.cpp28
-rw-r--r--extension/src/openvic-extension/singletons/AssetManager.hpp7
-rw-r--r--extension/src/openvic-extension/singletons/GameSingleton.cpp5
-rw-r--r--extension/src/openvic-extension/singletons/GameSingleton.hpp2
-rw-r--r--extension/src/openvic-extension/utility/UITools.cpp6
5 files changed, 26 insertions, 22 deletions
diff --git a/extension/src/openvic-extension/singletons/AssetManager.cpp b/extension/src/openvic-extension/singletons/AssetManager.cpp
index a81a0a2..546dc9d 100644
--- a/extension/src/openvic-extension/singletons/AssetManager.cpp
+++ b/extension/src/openvic-extension/singletons/AssetManager.cpp
@@ -45,34 +45,34 @@ Ref<Image> AssetManager::_load_image(StringName const& path) {
return image;
}
-AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName const& path) {
- const image_asset_map_t::iterator it = image_assets.find(path);
+AssetManager::image_asset_t* AssetManager::_get_image_asset(StringName const& path) {
+ image_asset_map_t::iterator it = image_assets.find(path);
if (it != image_assets.end()) {
- return it;
+ return &it.value();
}
const Ref<Image> image = _load_image(path);
- ERR_FAIL_NULL_V(image, image_assets.end());
- return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first;
+ ERR_FAIL_NULL_V(image, nullptr);
+ return &image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first.value();
}
Ref<Image> AssetManager::get_image(StringName const& path, bool cache) {
if (cache) {
- const image_asset_map_t::const_iterator it = _get_image_asset(path);
- ERR_FAIL_COND_V(it == image_assets.end(), nullptr);
- return it->second.image;
+ image_asset_t const* asset = _get_image_asset(path);
+ ERR_FAIL_NULL_V(asset, nullptr);
+ return asset->image;
} else {
return _load_image(path);
}
}
Ref<ImageTexture> AssetManager::get_texture(StringName const& path) {
- const image_asset_map_t::iterator it = _get_image_asset(path);
- ERR_FAIL_COND_V(it == image_assets.end(), nullptr);
- if (it->second.texture.is_null()) {
- it->second.texture = ImageTexture::create_from_image(it->second.image);
- ERR_FAIL_NULL_V_MSG(it->second.texture, nullptr, vformat("Failed to turn image into texture: %s", path));
+ image_asset_t* asset = _get_image_asset(path);
+ ERR_FAIL_NULL_V(asset, nullptr);
+ if (asset->texture.is_null()) {
+ asset->texture = ImageTexture::create_from_image(asset->image);
+ ERR_FAIL_NULL_V_MSG(asset->texture, nullptr, vformat("Failed to turn image into texture: %s", path));
}
- return it->second.texture;
+ return asset->texture;
}
Ref<Font> AssetManager::get_font(StringName const& name) {
diff --git a/extension/src/openvic-extension/singletons/AssetManager.hpp b/extension/src/openvic-extension/singletons/AssetManager.hpp
index 40577ad..7f73e4c 100644
--- a/extension/src/openvic-extension/singletons/AssetManager.hpp
+++ b/extension/src/openvic-extension/singletons/AssetManager.hpp
@@ -17,14 +17,15 @@ namespace OpenVic {
godot::Ref<godot::Image> image;
godot::Ref<godot::ImageTexture> texture;
};
- using image_asset_map_t = std::map<godot::StringName, image_asset_t>;
- using font_map_t = std::map<godot::StringName, godot::Ref<godot::Font>>;
+ /* deque_ordered_map to avoid the need to reallocate. */
+ using image_asset_map_t = deque_ordered_map<godot::StringName, image_asset_t>;
+ using font_map_t = deque_ordered_map<godot::StringName, godot::Ref<godot::Font>>;
image_asset_map_t image_assets;
font_map_t fonts;
static godot::Ref<godot::Image> _load_image(godot::StringName const& path);
- image_asset_map_t::iterator _get_image_asset(godot::StringName const& path);
+ image_asset_t* _get_image_asset(godot::StringName const& path);
protected:
static void _bind_methods();
diff --git a/extension/src/openvic-extension/singletons/GameSingleton.cpp b/extension/src/openvic-extension/singletons/GameSingleton.cpp
index e10efb3..8564d54 100644
--- a/extension/src/openvic-extension/singletons/GameSingleton.cpp
+++ b/extension/src/openvic-extension/singletons/GameSingleton.cpp
@@ -609,9 +609,12 @@ Error GameSingleton::_load_flag_images() {
flag_types.emplace_back(std_to_godot_string_name(type));
}
+ flag_image_map.reserve(country_manager.get_countries().size());
+
Error ret = OK;
for (Country const& country : country_manager.get_countries()) {
- std::map<StringName, Ref<Image>>& flag_images = flag_image_map[&country];
+ ordered_map<StringName, Ref<Image>>& flag_images = flag_image_map[&country];
+ flag_images.reserve(flag_types.size());
const String country_name = std_view_to_godot_string(country.get_identifier());
for (StringName const& flag_type : flag_types) {
const StringName flag_path =
diff --git a/extension/src/openvic-extension/singletons/GameSingleton.hpp b/extension/src/openvic-extension/singletons/GameSingleton.hpp
index 5622688..047d14c 100644
--- a/extension/src/openvic-extension/singletons/GameSingleton.hpp
+++ b/extension/src/openvic-extension/singletons/GameSingleton.hpp
@@ -23,7 +23,7 @@ namespace OpenVic {
godot::Ref<godot::ImageTexture> province_colour_texture;
Mapmode::index_t mapmode_index = 0;
godot::Ref<godot::Texture2DArray> terrain_texture;
- std::map<Country const*, std::map<godot::StringName, godot::Ref<godot::Image>>> flag_image_map;
+ ordered_map<Country const*, ordered_map<godot::StringName, godot::Ref<godot::Image>>> flag_image_map;
static godot::StringName const& _signal_gamestate_updated();
static godot::StringName const& _signal_province_selected();
diff --git a/extension/src/openvic-extension/utility/UITools.cpp b/extension/src/openvic-extension/utility/UITools.cpp
index 409a3ba..e1cd873 100644
--- a/extension/src/openvic-extension/utility/UITools.cpp
+++ b/extension/src/openvic-extension/utility/UITools.cpp
@@ -71,7 +71,7 @@ static T* new_control(GUI::Element const& element, String const& name) {
using enum GUI::Element::orientation_t;
using enum Control::LayoutPreset;
- static const std::map<GUI::Element::orientation_t, Control::LayoutPreset> orientation_map {
+ static const ordered_map<GUI::Element::orientation_t, Control::LayoutPreset> orientation_map {
{ UPPER_LEFT, PRESET_TOP_LEFT }, { LOWER_LEFT, PRESET_BOTTOM_LEFT },
{ LOWER_RIGHT, PRESET_BOTTOM_RIGHT }, { UPPER_RIGHT, PRESET_TOP_RIGHT },
{ CENTER, PRESET_CENTER }
@@ -400,7 +400,7 @@ static bool generate_text(generate_gui_args_t&& args) {
godot_label->set_custom_minimum_size(Utilities::to_godot_fvec2(text.get_max_size()));
using enum GUI::AlignedElement::format_t;
- static const std::map<GUI::AlignedElement::format_t, HorizontalAlignment> format_map {
+ static const ordered_map<GUI::AlignedElement::format_t, HorizontalAlignment> format_map {
{ left, HORIZONTAL_ALIGNMENT_LEFT },
{ centre, HORIZONTAL_ALIGNMENT_CENTER },
{ right, HORIZONTAL_ALIGNMENT_RIGHT }
@@ -495,7 +495,7 @@ static bool generate_window(generate_gui_args_t&& args) {
static bool generate_element(GUI::Element const* element, String const& name, AssetManager& asset_manager, Control*& result) {
ERR_FAIL_NULL_V(element, false);
- static const std::map<std::string_view, bool (*)(generate_gui_args_t&&)> type_map {
+ static const ordered_map<std::string_view, bool (*)(generate_gui_args_t&&)> type_map {
{ GUI::Icon::get_type_static(), &generate_icon },
{ GUI::Button::get_type_static(), &generate_button },
{ GUI::Checkbox::get_type_static(), &generate_checkbox },