summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-28 18:31:18 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-28 18:31:18 +0200
commit1b84fc144f77c4ebef6fdc0a476410420e0a95b3 (patch)
tree62d019065f4e704ca33b8808e6d76b2fec640da1
parent7c02d3b8f1e6ec4ee82bdf1220da431b1cd1231e (diff)
refactoring work
-rw-r--r--draw/wall.hpp5
-rw-r--r--editor/editor.cpp7
-rw-r--r--editor/editor.hpp4
-rw-r--r--editor/update.cpp2
-rw-r--r--serialize/world-writer.cpp11
-rw-r--r--src/local-coords.hpp2
-rw-r--r--src/tile-atlas.cpp3
-rw-r--r--src/tile-image.hpp24
-rw-r--r--src/tile.hpp20
-rw-r--r--test/json.cpp2
10 files changed, 47 insertions, 33 deletions
diff --git a/draw/wall.hpp b/draw/wall.hpp
index b021ef95..801d2c06 100644
--- a/draw/wall.hpp
+++ b/draw/wall.hpp
@@ -1,8 +1,7 @@
#pragma once
-#include "tile.hpp"
+#include "tile-defs.hpp"
#include <array>
-#include <Corrade/Containers/ArrayViewStl.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>
#include <Magnum/GL/Mesh.h>
@@ -10,6 +9,8 @@
namespace floormat {
+struct tile;
+struct tile_image;
struct tile_shader;
struct chunk;
diff --git a/editor/editor.cpp b/editor/editor.cpp
index b3e3aec6..0cd15d17 100644
--- a/editor/editor.cpp
+++ b/editor/editor.cpp
@@ -66,7 +66,7 @@ void tile_editor::select_tile(const std::shared_ptr<tile_atlas>& atlas, std::siz
fm_assert(atlas);
clear_selection();
_selection_mode = sel_tile;
- _selected_tile = { atlas, variant % atlas->num_tiles() };
+ _selected_tile = { atlas, decltype(tile_image::variant)(variant % atlas->num_tiles()) };
}
void tile_editor::select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas)
@@ -118,12 +118,13 @@ void fisher_yates(T begin, T end)
tile_image tile_editor::get_selected_perm()
{
auto& [atlas, vec] = _permutation;
- const std::size_t N = atlas->num_tiles();
+ using variant_t = decltype(tile_image::variant);
+ const auto N = (variant_t)atlas->num_tiles();
if (N == 0)
return {};
if (vec.empty())
{
- for (std::size_t i = 0; i < N; i++)
+ for (variant_t i = 0; i < N; i++)
vec.push_back(i);
fisher_yates(vec.begin(), vec.end());
}
diff --git a/editor/editor.hpp b/editor/editor.hpp
index 4b1ba124..9203cd6e 100644
--- a/editor/editor.hpp
+++ b/editor/editor.hpp
@@ -2,7 +2,7 @@
#include "compat/defs.hpp"
#include "tile-atlas.hpp"
#include "global-coords.hpp"
-#include "tile.hpp"
+#include "tile-image.hpp"
#include <cstdint>
#include <tuple>
@@ -33,7 +33,7 @@ private:
std::string _name;
std::map<std::string, std::shared_ptr<tile_atlas>> _atlases;
tile_image _selected_tile;
- std::tuple<std::shared_ptr<tile_atlas>, std::vector<std::size_t>> _permutation;
+ std::tuple<std::shared_ptr<tile_atlas>, std::vector<decltype(tile_image::variant)>> _permutation;
selection_mode _selection_mode = sel_none;
editor_mode _mode;
rotation _rotation{};
diff --git a/editor/update.cpp b/editor/update.cpp
index d4bfa2d5..f22ad522 100644
--- a/editor/update.cpp
+++ b/editor/update.cpp
@@ -18,7 +18,7 @@ void app::maybe_initialize_chunk_(const chunk_coords& pos, chunk& c)
#else
const auto& atlas = pt.x == N/2 || pt.y == N/2 ? _floor2 : _floor1;
#endif
- x.ground_image = { atlas, k % atlas->num_tiles() };
+ x.ground_image = { atlas, decltype(tile_image::variant)(k % atlas->num_tiles()) };
}
#ifdef FM_NO_BINDINGS
const auto& wall1 = floor1, wall2 = floor1;
diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp
index b28c113b..80c8de4a 100644
--- a/serialize/world-writer.cpp
+++ b/serialize/world-writer.cpp
@@ -88,7 +88,8 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord)
{
const tile& x = c[i];
- [[maybe_unused]] constexpr auto tile_size = sizeof(atlasid)*3 + sizeof(tilemeta);
+ [[maybe_unused]] constexpr auto tile_size = sizeof(tilemeta) + (sizeof(atlasid) + sizeof(imgvar))*3;
+
fm_debug_assert(s.bytes_written() + tile_size <= chunkbuf_size);
auto img_g = maybe_intern_atlas(x.ground_image);
@@ -105,12 +106,14 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord)
s << flags;
+ static_assert(std::is_same_v<imgvar, imgvar>);
+
if (img_g != null_atlas)
- s << img_g;
+ s << img_g << x.ground_image.variant;
if (img_n != null_atlas)
- s << img_n;
+ s << img_n << x.wall_north.variant;
if (img_w != null_atlas)
- s << img_w;
+ s << img_w << x.wall_west.variant;
}
const auto nbytes = s.bytes_written();
diff --git a/src/local-coords.hpp b/src/local-coords.hpp
index 2d449aae..01101ded 100644
--- a/src/local-coords.hpp
+++ b/src/local-coords.hpp
@@ -1,7 +1,7 @@
#pragma once
#include "compat/assert.hpp"
+#include "compat/int-hash.hpp"
#include "tile-defs.hpp"
-#include <cstdint>
#include <concepts>
namespace floormat {
diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp
index 533cc8e9..e6df942f 100644
--- a/src/tile-atlas.cpp
+++ b/src/tile-atlas.cpp
@@ -1,5 +1,7 @@
#include "tile-atlas.hpp"
#include "compat/assert.hpp"
+#include "tile-image.hpp"
+#include <limits>
#include <Corrade/Containers/StringView.h>
#include <Magnum/Math/Color.h>
#include <Magnum/ImageView.h>
@@ -11,6 +13,7 @@ tile_atlas::tile_atlas(StringView name, const ImageView2D& image, Vector2ub tile
texcoords_{make_texcoords_array(Vector2ui(image.size()), tile_count)},
name_{name}, size_{image.size()}, dims_{tile_count}
{
+ fm_assert(num_tiles() <= std::numeric_limits<decltype(tile_image::variant)>::max());
fm_assert(dims_[0] > 0 && dims_[1] > 0);
fm_assert(size_ % Vector2ui{tile_count} == Vector2ui());
tex_.setWrapping(GL::SamplerWrapping::ClampToEdge)
diff --git a/src/tile-image.hpp b/src/tile-image.hpp
new file mode 100644
index 00000000..deb04e9a
--- /dev/null
+++ b/src/tile-image.hpp
@@ -0,0 +1,24 @@
+#pragma once
+#include "compat/integer-types.hpp"
+#include <compare>
+#include <memory>
+
+namespace floormat {
+
+struct tile_atlas;
+
+struct tile_image final
+{
+ std::shared_ptr<tile_atlas> atlas;
+ std::uint8_t variant = (std::uint8_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;
+ }
+};
+
+} // namespace floormat
diff --git a/src/tile.hpp b/src/tile.hpp
index e8bac834..5103479d 100644
--- a/src/tile.hpp
+++ b/src/tile.hpp
@@ -1,27 +1,9 @@
#pragma once
#include "compat/defs.hpp"
-#include "compat/integer-types.hpp"
-#include "tile-defs.hpp"
-#include <memory>
+#include "tile-image.hpp"
namespace floormat {
-struct tile_atlas;
-
-struct tile_image final
-{
- std::shared_ptr<tile_atlas> atlas;
- 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
{
enum pass_mode : std::uint8_t { pass_blocked, pass_ok, pass_shoot_through, };
diff --git a/test/json.cpp b/test/json.cpp
index 8ea2bd09..83854c38 100644
--- a/test/json.cpp
+++ b/test/json.cpp
@@ -20,7 +20,7 @@ static chunk make_test_chunk()
constexpr auto N = TILE_MAX_DIM;
chunk c;
for (auto& [x, k, pt] : c) {
- x.ground_image = { tiles, k % tiles->num_tiles() };
+ x.ground_image = { tiles, decltype(tile_image::variant)(k % tiles->num_tiles()) };
}
constexpr auto K = N/2;
c[{K, K }].wall_north = { metal1, 0 };