diff options
author | Hop311 <Hop3114@gmail.com> | 2024-04-22 01:25:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-22 01:25:41 +0200 |
commit | 34f161570fd7ca9381675be8a9ec3b9b409929e1 (patch) | |
tree | edba52a563952d2cf14c6d8f2a4995ac8017c147 /extension/src/openvic-extension/classes | |
parent | 00c03db2b10588081548b713a4272854a9051c1c (diff) | |
parent | 5192708bda86625a40ce67ff297dca8138f9cc13 (diff) |
Merge pull request #218 from OpenVicProject/array-dict-tweaks
Pre-allocate and use std::move for Godot Arrays and Dictionaries
Diffstat (limited to 'extension/src/openvic-extension/classes')
-rw-r--r-- | extension/src/openvic-extension/classes/GFXPieChartTexture.hpp | 6 | ||||
-rw-r--r-- | extension/src/openvic-extension/classes/MapMesh.cpp | 22 |
2 files changed, 15 insertions, 13 deletions
diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp index f1fbe1a..8683b10 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp @@ -54,12 +54,14 @@ namespace OpenVic { return lhs.first < rhs.first; }); godot_pie_chart_data_t array; - for (auto const& [key, val] : sorted_dist) { + ERR_FAIL_COND_V(array.resize(sorted_dist.size()) != OK, {}); + for (size_t idx = 0; idx < array.size(); ++idx) { + auto const& [key, val] = sorted_dist[idx]; Dictionary sub_dict; sub_dict[_slice_identifier_key()] = Utilities::std_view_to_godot_string(key->get_identifier()); sub_dict[_slice_colour_key()] = Utilities::to_godot_color(key->get_colour()); sub_dict[_slice_weight_key()] = val.to_float(); - array.push_back(sub_dict); + array[idx] = std::move(sub_dict); } return array; } diff --git a/extension/src/openvic-extension/classes/MapMesh.cpp b/extension/src/openvic-extension/classes/MapMesh.cpp index a557105..8db3b84 100644 --- a/extension/src/openvic-extension/classes/MapMesh.cpp +++ b/extension/src/openvic-extension/classes/MapMesh.cpp @@ -92,7 +92,7 @@ bool MapMesh::is_valid_uv_coord(godot::Vector2 const& uv) const { Array MapMesh::_create_mesh_array() const { Array arr; - arr.resize(Mesh::ARRAY_MAX); + ERR_FAIL_COND_V(arr.resize(Mesh::ARRAY_MAX) != OK, {}); const int32_t vertex_count = (subdivide_w + 2) * (subdivide_d + 2); const int32_t indice_count = (subdivide_w + 1) * (subdivide_d + 1) * 6; @@ -103,11 +103,11 @@ Array MapMesh::_create_mesh_array() const { PackedVector2Array uvs; PackedInt32Array indices; - points.resize(vertex_count); - normals.resize(vertex_count); - tangents.resize(vertex_count * 4); - uvs.resize(vertex_count); - indices.resize(indice_count); + ERR_FAIL_COND_V(points.resize(vertex_count) != OK, {}); + ERR_FAIL_COND_V(normals.resize(vertex_count) != OK, {}); + ERR_FAIL_COND_V(tangents.resize(vertex_count * 4) != OK, {}); + ERR_FAIL_COND_V(uvs.resize(vertex_count) != OK, {}); + ERR_FAIL_COND_V(indices.resize(indice_count) != OK, {}); static const Vector3 normal { 0.0f, 1.0f, 0.0f }; const Size2 uv_size { 1.0f + 2.0f * repeat_proportion, 1.0f }; @@ -153,11 +153,11 @@ Array MapMesh::_create_mesh_array() const { thisrow = point_index; } - arr[Mesh::ARRAY_VERTEX] = points; - arr[Mesh::ARRAY_NORMAL] = normals; - arr[Mesh::ARRAY_TANGENT] = tangents; - arr[Mesh::ARRAY_TEX_UV] = uvs; - arr[Mesh::ARRAY_INDEX] = indices; + arr[Mesh::ARRAY_VERTEX] = std::move(points); + arr[Mesh::ARRAY_NORMAL] = std::move(normals); + arr[Mesh::ARRAY_TANGENT] = std::move(tangents); + arr[Mesh::ARRAY_TEX_UV] = std::move(uvs); + arr[Mesh::ARRAY_INDEX] = std::move(indices); return arr; } |