summaryrefslogtreecommitdiffhomepage
path: root/editor
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-01-16 05:54:05 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-01-16 05:54:05 +0100
commit8cf6c38140e505d787d2db12edbd638a91fa2dfc (patch)
tree1d2538b0998e2c7359d53bfbf859704722a537ba /editor
parent27e78617825a35b1fcb38d6a01b7ad5fbbb9d2d5 (diff)
c
Diffstat (limited to 'editor')
-rw-r--r--editor/ground-editor.cpp34
-rw-r--r--editor/ground-editor.hpp19
2 files changed, 30 insertions, 23 deletions
diff --git a/editor/ground-editor.cpp b/editor/ground-editor.cpp
index e99448fb..82a76405 100644
--- a/editor/ground-editor.cpp
+++ b/editor/ground-editor.cpp
@@ -1,20 +1,30 @@
#include "ground-editor.hpp"
+#include "compat/assert.hpp"
#include "src/ground-atlas.hpp"
#include "src/world.hpp"
#include "src/random.hpp"
-#include "loader/ground-info.hpp"
#include "keys.hpp"
#include "loader/loader.hpp"
#include "compat/exception.hpp"
+#include <memory>
+#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Utility/Path.h>
namespace floormat {
+struct ground_editor::tuple
+{
+ std::shared_ptr<ground_atlas> atlas;
+ Array<decltype(tile_image_proto::variant)> variant;
+};
+
ground_editor::ground_editor()
{
load_atlases();
}
+ground_editor::~ground_editor() noexcept = default;
+
void ground_editor::load_atlases()
{
fm_assert(_atlases.empty());
@@ -48,7 +58,7 @@ StringView ground_editor::name() const noexcept { return "ground"_s; }
void ground_editor::clear_selection()
{
_selected_tile = {};
- _permutation = {};
+ *_permutation = {};
_selection_mode = sel_none;
}
@@ -65,7 +75,7 @@ void ground_editor::select_tile_permutation(const std::shared_ptr<ground_atlas>&
fm_assert(atlas);
clear_selection();
_selection_mode = sel_perm;
- _permutation = { atlas, {} };
+ *_permutation = { atlas, {} };
}
bool ground_editor::is_tile_selected(const std::shared_ptr<const ground_atlas>& atlas, size_t variant) const
@@ -76,7 +86,7 @@ bool ground_editor::is_tile_selected(const std::shared_ptr<const ground_atlas>&
bool ground_editor::is_permutation_selected(const std::shared_ptr<const ground_atlas>& atlas) const
{
- const auto& [perm, _] = _permutation;
+ const auto& [perm, _] = *_permutation;
return atlas && _selection_mode == sel_perm && perm == atlas;
}
@@ -113,18 +123,22 @@ void fisher_yates(T begin, T end)
tile_image_proto ground_editor::get_selected_perm()
{
- auto& [atlas, vec] = _permutation;
- const auto N = (variant_t)atlas->num_tiles();
+ auto& [atlas, vec] = *_permutation;
+ static_assert(sizeof(uint32_t) >= sizeof(variant_t));
+ const auto N = (uint32_t)atlas->num_tiles();
+ fm_assert(N == (uint32_t)(variant_t)N);
if (N == 0)
return {};
- if (vec.empty())
+ arrayReserve(vec, N);
+ if (vec.isEmpty())
{
- for (variant_t i = 0; i < N; i++)
- vec.push_back(i);
+ arrayResize(vec, NoInit, N);
+ for (uint32_t i = 0; i < N; i++)
+ vec[i] = (variant_t)i;
fisher_yates(vec.begin(), vec.end());
}
const auto idx = vec.back();
- vec.pop_back();
+ arrayRemoveSuffix(vec);
return {atlas, idx};
}
diff --git a/editor/ground-editor.hpp b/editor/ground-editor.hpp
index 55afec92..a0d99813 100644
--- a/editor/ground-editor.hpp
+++ b/editor/ground-editor.hpp
@@ -1,11 +1,10 @@
#pragma once
-
+#include "compat/safe-ptr.hpp"
#include "editor-enums.hpp"
#include "src/tile-image.hpp"
#include "src/global-coords.hpp"
-#include <vector>
#include <map>
-#include <memory>
+#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/String.h>
namespace floormat {
@@ -15,19 +14,12 @@ struct ground_info;
class ground_editor final
{
- enum selection_mode : unsigned char {
- sel_none, sel_tile, sel_perm,
- };
-
- struct tuple
- {
- std::shared_ptr<ground_atlas> atlas;
- std::vector<decltype(tile_image_proto::variant)> variant;
- };
+ enum selection_mode : unsigned char { sel_none, sel_tile, sel_perm, };
+ struct tuple;
std::map<StringView, const ground_info*> _atlases;
tile_image_proto _selected_tile;
- tuple _permutation;
+ safe_ptr<tuple> _permutation;
selection_mode _selection_mode = sel_none;
void load_atlases();
@@ -35,6 +27,7 @@ class ground_editor final
public:
ground_editor();
+ ~ground_editor() noexcept;
std::shared_ptr<ground_atlas> maybe_atlas(StringView str);
std::shared_ptr<ground_atlas> atlas(StringView str);
auto cbegin() const noexcept { return _atlases.cbegin(); }