summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-08 01:34:44 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-08 01:34:44 +0200
commit41f4f4b6f9ba5fa20bb41967dc8ef8020081e38e (patch)
treef06241318bf19d07329b4130fc7e92d4b75dc234
parentf845dbb21799d7b2879ba1ea9761fd3e68303ae8 (diff)
a
-rw-r--r--compat/defs.hpp2
-rw-r--r--src/floor-mesh.cpp4
-rw-r--r--src/tile-atlas.cpp2
-rw-r--r--src/tile-iterator.cpp30
-rw-r--r--src/tile-iterator.hpp50
-rw-r--r--src/wall-mesh.cpp2
-rw-r--r--test/app.hpp1
-rw-r--r--test/json.cpp3
-rw-r--r--test/main.cpp1
-rw-r--r--test/tile-iter.cpp39
10 files changed, 86 insertions, 48 deletions
diff --git a/compat/defs.hpp b/compat/defs.hpp
index 36c6a034..818e004b 100644
--- a/compat/defs.hpp
+++ b/compat/defs.hpp
@@ -9,7 +9,7 @@
#define progn(...) [&]{__VA_ARGS__;}()
#define DECLARE_DEPRECATED_COPY_ASSIGNMENT(type) \
- [[deprecated]] type(const type&) = default; \
+ [[deprecated]] type(const type&) = default; \
[[deprecated]] type& operator=(const type&) = default
#define DECLARE_DELETED_COPY_ASSIGNMENT(type) \
diff --git a/src/floor-mesh.cpp b/src/floor-mesh.cpp
index fdace9d8..3698bc6e 100644
--- a/src/floor-mesh.cpp
+++ b/src/floor-mesh.cpp
@@ -33,14 +33,14 @@ void floor_mesh::set_tile(quad_data& data, tile& x)
void floor_mesh::draw(tile_shader& shader, chunk& c)
{
std::array<quad_data, TILE_COUNT> data;
- for (auto [x, idx, pt] : c) {
+ for (auto& [x, idx, pt] : c) {
set_tile(data[idx], x);
}
_vertex_buffer.setSubData(0, data);
Magnum::GL::MeshView mesh{_mesh};
mesh.setCount(quad_index_count);
const tile_atlas* last_tile_atlas = nullptr;
- for (auto [x, i, pt] : c) {
+ for (auto& [x, i, pt] : c) {
if (!x.ground_image)
continue;
mesh.setIndexRange((int)(i*quad_index_count), 0, quad_index_count*TILE_COUNT - 1);
diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp
index d086c31b..08bc2296 100644
--- a/src/tile-atlas.cpp
+++ b/src/tile-atlas.cpp
@@ -26,7 +26,7 @@ std::array<Vector2, 4> tile_atlas::texcoords_for_id(std::size_t id_) const
{
const auto sz = size_/dims_;
ASSERT(id_ < sz.product());
- const Vector2ui id = { (unsigned)id_ % dims_[0], (unsigned)id_ / dims_[0] };
+ const Vector2ui id = { (UnsignedInt)id_ % dims_[0], (UnsignedInt)id_ / dims_[0] };
const Vector2 p0(id * sz), p1(sz);
const auto x0 = p0.x(), x1 = p1.x(), y0 = p0.y(), y1 = p1.y();
return {{
diff --git a/src/tile-iterator.cpp b/src/tile-iterator.cpp
index d2dbf98d..3e7586dd 100644
--- a/src/tile-iterator.cpp
+++ b/src/tile-iterator.cpp
@@ -3,36 +3,6 @@
namespace Magnum::Examples {
-template <typename T>
-void basic_tile_iterator<T>::swap(basic_tile_iterator<T>& other)
-{
- using std::swap;
- swap(ptr, other.ptr);
- swap(pos, other.pos);
-}
-
-template <typename T>
-template <std::size_t N>
-typename std::tuple_element<N, basic_tile_iterator<T>>::type basic_tile_iterator<T>::get()
-{
- if constexpr(N == 0)
- return pos == TILE_COUNT ? ptr[0] : ptr[pos];
- else if constexpr(N == 1)
- return pos;
- else if constexpr(N == 2)
- return local_coords{pos};
- else
- return std::void_t<std::integral_constant<int, N>>();
-}
-
-template typename std::tuple_element<0, basic_tile_iterator<tile>>::type basic_tile_iterator<tile>::get<0>();
-template typename std::tuple_element<1, basic_tile_iterator<tile>>::type basic_tile_iterator<tile>::get<1>();
-template typename std::tuple_element<2, basic_tile_iterator<tile>>::type basic_tile_iterator<tile>::get<2>();
-
-template typename std::tuple_element<0, basic_tile_iterator<const tile>>::type basic_tile_iterator<const tile>::get<0>();
-template typename std::tuple_element<1, basic_tile_iterator<const tile>>::type basic_tile_iterator<const tile>::get<1>();
-template typename std::tuple_element<2, basic_tile_iterator<const tile>>::type basic_tile_iterator<const tile>::get<2>();
-
template class basic_tile_iterator<tile>;
template class basic_tile_iterator<const tile>;
diff --git a/src/tile-iterator.hpp b/src/tile-iterator.hpp
index 1439eb4f..0fa5049d 100644
--- a/src/tile-iterator.hpp
+++ b/src/tile-iterator.hpp
@@ -18,10 +18,12 @@ template<typename T>
void swap(Magnum::Examples::basic_tile_iterator<T>& lhs,
Magnum::Examples::basic_tile_iterator<T>& rhs) noexcept;
-template<typename T> struct tuple_size<Magnum::Examples::basic_tile_iterator<T>> : std::integral_constant<std::size_t, 3> {};
template<typename T> struct tuple_element<0, Magnum::Examples::basic_tile_iterator<T>> { using type = T&; };
+template<typename T> struct tuple_element<0, const Magnum::Examples::basic_tile_iterator<T>> { using type = const T&; };
+
template<typename T> struct tuple_element<1, Magnum::Examples::basic_tile_iterator<T>> { using type = std::size_t; };
template<typename T> struct tuple_element<2, Magnum::Examples::basic_tile_iterator<T>> { using type = Magnum::Examples::local_coords; };
+template<typename T> struct tuple_size<Magnum::Examples::basic_tile_iterator<T>> : std::integral_constant<std::size_t, 3> {};
} // namespace std
@@ -41,24 +43,48 @@ public:
basic_tile_iterator<T>& operator=(const basic_tile_iterator<T>&) = default;
basic_tile_iterator<T>& operator++() { pos++; return *this; }
basic_tile_iterator<T> operator++(int) { auto tmp = *this; operator++(); return tmp; }
- basic_tile_iterator<T>* operator->() { return this; }
- basic_tile_iterator<T>& operator*() { return *this; }
+ basic_tile_iterator<T>* operator->() & { return this; }
+ basic_tile_iterator<T>& operator*() & { return *this; }
+ basic_tile_iterator<T> const* operator->() const& { return this; }
+ basic_tile_iterator<T> const& operator*() const& { return *this; }
auto operator<=>(const basic_tile_iterator<T>&) const noexcept = default;
void swap(basic_tile_iterator<T>& other);
- template<std::size_t N> typename std::tuple_element<N, basic_tile_iterator<T>>::type get();
+ template<std::size_t N> typename std::tuple_element<N, basic_tile_iterator<T>>::type get() &;
+ template<std::size_t N> typename std::tuple_element<N, basic_tile_iterator<T>>::type get() const&;
};
+template <typename T>
+void basic_tile_iterator<T>::swap(basic_tile_iterator<T>& other)
+{
+ using std::swap;
+ swap(ptr, other.ptr);
+ swap(pos, other.pos);
+}
+
+template <typename T>
+template <std::size_t N>
+typename std::tuple_element<N, basic_tile_iterator<T>>::type basic_tile_iterator<T>::get() &
+{
+ if constexpr(N == 0)
+ return pos == TILE_COUNT ? ptr[0] : ptr[pos];
+ else if constexpr(N == 1)
+ return pos;
+ else if constexpr(N == 2)
+ return local_coords{pos};
+ else
+ return std::void_t<std::integral_constant<int, N>>();
+}
+
+template <typename T>
+template <std::size_t N>
+typename std::tuple_element<N, basic_tile_iterator<T>>::type basic_tile_iterator<T>::get() const&
+{
+ return const_cast<basic_tile_iterator<T>&>(*this).get<N>();
+}
+
extern template class basic_tile_iterator<tile>;
extern template class basic_tile_iterator<const tile>;
-extern template typename std::tuple_element<0, basic_tile_iterator<tile>>::type basic_tile_iterator<tile>::get<0>();
-extern template typename std::tuple_element<1, basic_tile_iterator<tile>>::type basic_tile_iterator<tile>::get<1>();
-extern template typename std::tuple_element<2, basic_tile_iterator<tile>>::type basic_tile_iterator<tile>::get<2>();
-
-extern template typename std::tuple_element<0, basic_tile_iterator<const tile>>::type basic_tile_iterator<const tile>::get<0>();
-extern template typename std::tuple_element<1, basic_tile_iterator<const tile>>::type basic_tile_iterator<const tile>::get<1>();
-extern template typename std::tuple_element<2, basic_tile_iterator<const tile>>::type basic_tile_iterator<const tile>::get<2>();
-
} // namespace Magnum::Examples
namespace std {
diff --git a/src/wall-mesh.cpp b/src/wall-mesh.cpp
index dce86dc1..d698b508 100644
--- a/src/wall-mesh.cpp
+++ b/src/wall-mesh.cpp
@@ -42,7 +42,7 @@ void wall_mesh::draw(tile_shader& shader, chunk& c)
texture_array textures = {};
{
vertex_array data;
- for (auto [x, idx, pt] : c) {
+ for (auto& [x, idx, pt] : c) {
maybe_add_tile(data, textures, x, idx);
}
_vertex_buffer.setSubData(0, data);
diff --git a/test/app.hpp b/test/app.hpp
index c77c3369..44fbed11 100644
--- a/test/app.hpp
+++ b/test/app.hpp
@@ -8,5 +8,6 @@ struct app final : Platform::WindowlessWglApplication // NOLINT(cppcoreguideline
~app();
int exec() override;
static bool test_json();
+ static bool test_tile_iter();
};
} // namespace Magnum::Examples
diff --git a/test/json.cpp b/test/json.cpp
index 3ce3c9d0..46bdf528 100644
--- a/test/json.cpp
+++ b/test/json.cpp
@@ -18,7 +18,7 @@ static chunk make_test_chunk()
metal3 = loader.tile_atlas("share/game/images/metal3.tga", {2, 2});
constexpr auto N = TILE_MAX_DIM;
chunk c;
- for (auto [x, k, pt] : c) {
+ for (auto& [x, k, pt] : c) {
const auto& atlas = pt.x > N/2 && pt.y >= N/2 ? metal1 : metal2;
x.ground_image = { atlas, (std::uint8_t)(k % atlas->num_tiles().product()) };
}
@@ -52,6 +52,7 @@ bool app::test_json() // NOLINT(readability-convert-member-functions-to-static)
const auto chunk = make_test_chunk();
ret &= json_helper::to_json(chunk, output_dir/"zzz_chunk-1.json");
}
+
return ret;
}
diff --git a/test/main.cpp b/test/main.cpp
index d190c27b..06a2e64d 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -25,6 +25,7 @@ int app::exec()
{
bool ret = true;
ret &= test_json();
+ ret &= test_tile_iter();
return !ret;
}
diff --git a/test/tile-iter.cpp b/test/tile-iter.cpp
new file mode 100644
index 00000000..aea6b8b5
--- /dev/null
+++ b/test/tile-iter.cpp
@@ -0,0 +1,39 @@
+#include "app.hpp"
+#include "chunk.hpp"
+namespace Magnum::Examples {
+
+static inline bool always_false()
+{
+ volatile bool ret = false;
+ return ret;
+}
+
+bool app::test_tile_iter() // NOLINT(readability-function-size)
+{
+ if (always_false())
+ {
+ const chunk c;
+ for (const auto& [x, k, pt] : c)
+ static_assert(std::is_same_v<decltype(x), const tile&>);
+ for (auto& [x, k, pt] : c)
+ static_assert(std::is_same_v<decltype(x), const tile&>);
+ for (auto [x, k, pt] : c)
+ static_assert(std::is_same_v<decltype(x), const tile&>);
+ }
+ if (always_false())
+ {
+ chunk c;
+ for (auto& [x, k, pt] : c)
+ static_assert(std::is_same_v<decltype(x), tile&>);
+ for (const auto& [x, k, pt] : c)
+ static_assert(std::is_same_v<decltype(x), const tile&>);
+#if 1
+ for (auto [x, k, pt] : c)
+ static_assert(std::is_same_v<decltype(x), tile&>);
+#endif
+ }
+ return true;
+}
+
+} // namespace Magnum::Examples
+