summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-21 21:11:33 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-21 21:11:33 +0200
commitdf2073a5692c991e4fc43c926334983997d272af (patch)
tree60d959c0e1dd97f0b4e63eacc60c162fc4a3e670
parent04ee43ea683f5b9b591e10409b5e5a622e8a198a (diff)
lots of refactoring types crap
-rw-r--r--main/editor.cpp22
-rw-r--r--main/editor.hpp22
-rw-r--r--main/imgui.cpp6
-rw-r--r--main/update.cpp2
-rw-r--r--src/tile-atlas.cpp4
-rw-r--r--src/tile-atlas.hpp2
-rw-r--r--src/tile.hpp8
-rw-r--r--test/json.cpp2
8 files changed, 38 insertions, 30 deletions
diff --git a/main/editor.cpp b/main/editor.cpp
index 5658a785..709ab13b 100644
--- a/main/editor.cpp
+++ b/main/editor.cpp
@@ -61,7 +61,7 @@ void tile_type::clear_selection()
_selection_mode = sel_none;
}
-void tile_type::select_tile(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant)
+void tile_type::select_tile(const std::shared_ptr<tile_atlas>& atlas, std::size_t variant)
{
fm_assert(atlas);
clear_selection();
@@ -77,13 +77,14 @@ void tile_type::select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas
_permutation = { atlas, {} };
}
-bool tile_type::is_tile_selected(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant) const
+bool tile_type::is_tile_selected(const std::shared_ptr<const tile_atlas>& atlas, std::size_t variant) const
{
fm_assert(atlas);
- return _selection_mode == sel_tile && _selected_tile == std::make_tuple(atlas, variant);
+ return _selection_mode == sel_tile && _selected_tile &&
+ atlas == _selected_tile.atlas && variant == _selected_tile.variant;
}
-bool tile_type::is_permutation_selected(const std::shared_ptr<tile_atlas>& atlas) const
+bool tile_type::is_permutation_selected(const std::shared_ptr<const tile_atlas>& atlas) const
{
fm_assert(atlas);
return _selection_mode == sel_perm && std::get<0>(_permutation) == atlas;
@@ -101,7 +102,7 @@ void fisher_yates(T begin, T end)
}
}
-std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t> tile_type::get_selected_perm()
+tile_image tile_type::get_selected_perm()
{
auto& [atlas, vec] = _permutation;
const std::size_t N = atlas->num_tiles();
@@ -109,7 +110,7 @@ std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t> tile_type::get_selected_pe
return {};
if (vec.empty())
{
- for (std::uint8_t i = 0; i < N; i++)
+ for (std::size_t i = 0; i < N; i++)
vec.push_back(i);
fisher_yates(vec.begin(), vec.end());
}
@@ -118,12 +119,12 @@ std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t> tile_type::get_selected_pe
return {atlas, idx};
}
-std::optional<std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>> tile_type::get_selected()
+tile_image tile_type::get_selected()
{
switch (_selection_mode)
{
case sel_none:
- return std::nullopt;
+ return {};
case sel_tile:
return _selected_tile;
case sel_perm:
@@ -134,8 +135,7 @@ std::optional<std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>> tile_type::
}
}
-void tile_type::place_tile(world& world, global_coords pos,
- const std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>& img)
+void tile_type::place_tile(world& world, global_coords pos, tile_image& img)
{
const auto& [c, t] = world[pos];
const auto& [atlas, variant] = img;
@@ -211,7 +211,7 @@ void editor::on_click(world& world, global_coords pos)
if (opt)
{
_last_pos = pos;
- mode->place_tile(world, pos, *opt);
+ mode->place_tile(world, pos, opt);
}
else
on_release();
diff --git a/main/editor.hpp b/main/editor.hpp
index 34a6671c..a308c437 100644
--- a/main/editor.hpp
+++ b/main/editor.hpp
@@ -2,12 +2,14 @@
#include "compat/defs.hpp"
#include "tile-atlas.hpp"
#include "global-coords.hpp"
+#include "tile.hpp"
+
#include <cstdint>
-#include <map>
-#include <memory>
#include <tuple>
#include <optional>
#include <vector>
+#include <map>
+#include <memory>
#include <Corrade/Containers/StringView.h>
namespace floormat {
@@ -33,12 +35,12 @@ struct tile_type final
editor_mode mode() const { return _mode; }
void clear_selection();
- void select_tile(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant);
+ void select_tile(const std::shared_ptr<tile_atlas>& atlas, std::size_t variant);
void select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas);
- bool is_tile_selected(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant) const;
- bool is_permutation_selected(const std::shared_ptr<tile_atlas>& atlas) const;
- std::optional<std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>> get_selected();
- void place_tile(world& world, global_coords pos, const std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>& img);
+ bool is_tile_selected(const std::shared_ptr<const tile_atlas>& atlas, std::size_t variant) const;
+ bool is_permutation_selected(const std::shared_ptr<const tile_atlas>& atlas) const;
+ tile_image get_selected();
+ void place_tile(world& world, global_coords pos, tile_image& img);
private:
enum selection_mode : std::uint8_t {
@@ -50,14 +52,14 @@ private:
std::string _name;
std::map<std::string, std::shared_ptr<tile_atlas>> _atlases;
- std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t> _selected_tile;
- std::tuple<std::shared_ptr<tile_atlas>, std::vector<std::uint8_t>> _permutation;
+ tile_image _selected_tile;
+ std::tuple<std::shared_ptr<tile_atlas>, std::vector<std::size_t>> _permutation;
selection_mode _selection_mode = sel_none;
editor_mode _mode;
rotation _rotation{};
void load_atlases();
- std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t> get_selected_perm();
+ tile_image get_selected_perm();
};
struct editor final
diff --git a/main/imgui.cpp b/main/imgui.cpp
index 2911d849..ea64cd85 100644
--- a/main/imgui.cpp
+++ b/main/imgui.cpp
@@ -113,7 +113,7 @@ void app::draw_editor_pane(tile_type& type, float main_menu_height)
ImGui::SameLine(window_width - ImGui::CalcTextSize(buf).x - style.FramePadding.x - 4);
ImGui::Text("%s", buf);
};
- const auto N = (std::uint8_t)v->num_tiles();
+ const auto N = v->num_tiles();
if (const auto flags = ImGuiTreeNodeFlags_(ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Framed);
auto b = tree_node(k.data(), flags))
{
@@ -125,7 +125,7 @@ void app::draw_editor_pane(tile_type& type, float main_menu_height)
};
const bool perm_selected = ed ? ed->is_permutation_selected(v) : false;
constexpr std::size_t per_row = 5;
- for (std::uint8_t i = 0; i < N; i++)
+ for (std::size_t i = 0; i < N; i++)
{
const bool selected = ed ? ed->is_tile_selected(v, i) : false;
@@ -139,7 +139,7 @@ void app::draw_editor_pane(tile_type& type, float main_menu_height)
perm_selected ? push_style_color(ImGuiCol_ButtonHovered, color_perm_selected) : raii_wrapper{},
};
- snprintf(buf, sizeof(buf), "##item_%hhu", i);
+ snprintf(buf, sizeof(buf), "##item_%zu", i);
const auto uv = v->texcoords_for_id(i);
ImGui::ImageButton(buf, (void*)&v->texture(), {TILE_SIZE[0]/2, TILE_SIZE[1]/2},
{ uv[3][0], uv[3][1] }, { uv[0][0], uv[0][1] });
diff --git a/main/update.cpp b/main/update.cpp
index fac359e8..00e59434 100644
--- a/main/update.cpp
+++ b/main/update.cpp
@@ -7,7 +7,7 @@ void app::make_test_chunk(chunk& c)
constexpr auto N = TILE_MAX_DIM;
for (auto [x, k, pt] : c) {
const auto& atlas = pt.x != pt.y && (pt.x == N/2 || pt.y == N/2) ? floor2 : floor1;
- x.ground_image = { atlas, (std::uint8_t)(k % atlas->num_tiles()) };
+ x.ground_image = { atlas, k % atlas->num_tiles() };
}
constexpr auto K = N/2;
c[{K, K }].wall_north = { wall1, 0 };
diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp
index 6c2bd746..3f08bc9c 100644
--- a/src/tile-atlas.cpp
+++ b/src/tile-atlas.cpp
@@ -28,7 +28,7 @@ std::array<Vector2, 4> tile_atlas::texcoords_for_id(std::size_t i) const
return texcoords_[i];
}
-auto tile_atlas::make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, std::uint8_t i) -> texcoords
+auto tile_atlas::make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, std::size_t i) -> texcoords
{
const auto sz = pixel_size/Vector2ui{tile_count};
const Vector2ui id = { std::uint32_t(i % tile_count[0]), std::uint32_t(i / tile_count[0]) };
@@ -46,7 +46,7 @@ auto tile_atlas::make_texcoords_array(Vector2ui pixel_size, Vector2ub tile_count
{
const std::size_t N = Vector2ui{tile_count}.product();
auto ptr = std::make_unique<std::array<Vector2, 4>[]>(N);
- for (std::uint8_t i = 0; i < N; i++)
+ for (std::size_t i = 0; i < N; i++)
ptr[i] = make_texcoords(pixel_size, tile_count, i);
return ptr;
}
diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp
index 712c396c..97f9f408 100644
--- a/src/tile-atlas.hpp
+++ b/src/tile-atlas.hpp
@@ -29,7 +29,7 @@ struct tile_atlas final
private:
static std::unique_ptr<const texcoords[]> make_texcoords_array(Vector2ui pixel_size, Vector2ub tile_count);
- static texcoords make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, std::uint8_t i);
+ static texcoords make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, std::size_t i);
std::unique_ptr<const texcoords[]> texcoords_;
GL::Texture2D tex_;
diff --git a/src/tile.hpp b/src/tile.hpp
index 067df3c7..499acd97 100644
--- a/src/tile.hpp
+++ b/src/tile.hpp
@@ -12,9 +12,15 @@ struct tile_atlas;
struct tile_image final
{
std::shared_ptr<tile_atlas> atlas;
- std::uint8_t variant = 0xff;
+ std::size_t variant = (std::size_t)-1;
explicit operator bool() const noexcept { return !!atlas; }
+
+ std::strong_ordering operator<=>(const tile_image& o) const noexcept
+ {
+ const auto ret = atlas.get() <=> o.atlas.get();
+ return ret != std::strong_ordering::equal ? ret : variant <=> o.variant;
+ }
};
struct tile final
diff --git a/test/json.cpp b/test/json.cpp
index afdee3a2..f0a32c1e 100644
--- a/test/json.cpp
+++ b/test/json.cpp
@@ -19,7 +19,7 @@ static chunk make_test_chunk()
constexpr auto N = TILE_MAX_DIM;
chunk c;
for (auto& [x, k, pt] : c) {
- x.ground_image = { tiles, (std::uint8_t)(k % tiles->num_tiles()) };
+ x.ground_image = { tiles, k % tiles->num_tiles() };
}
constexpr auto K = N/2;
c[{K, K }].wall_north = { metal1, 0 };