From f60c3734e38408b97e10e29a6c1d8780770d5b79 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 27 Feb 2023 07:29:42 +0100 Subject: compat/prelude: add operator""_uz() for size_t --- compat/format.hpp | 2 +- compat/prelude.hpp | 2 ++ draw/anim.cpp | 10 +++++----- editor/imgui-tiles.cpp | 2 +- editor/inspect.cpp | 2 +- entity/erased-constraints.cpp | 10 +++++----- main/draw.cpp | 2 +- serialize/binary-reader.inl | 6 +++--- serialize/binary-writer.inl | 4 ++-- serialize/magnum-vector.hpp | 4 ++-- serialize/world-reader.cpp | 8 ++++---- serialize/world-writer.cpp | 8 ++++---- src/anim-atlas.cpp | 10 +++++----- src/chunk-collision.cpp | 6 +++--- src/chunk-render.cpp | 20 ++++++++++---------- src/chunk.cpp | 2 +- src/random.cpp | 4 ++-- src/tile-atlas.cpp | 2 +- test/serializer.cpp | 2 +- 19 files changed, 54 insertions(+), 52 deletions(-) diff --git a/compat/format.hpp b/compat/format.hpp index 1432d847..bf6e8861 100644 --- a/compat/format.hpp +++ b/compat/format.hpp @@ -27,7 +27,7 @@ struct fmt_string final { template consteval fmt_string(const char (&arr)[N]) noexcept { - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) data[i] = arr[i]; } }; diff --git a/compat/prelude.hpp b/compat/prelude.hpp index ab2fdfd2..5520ded8 100644 --- a/compat/prelude.hpp +++ b/compat/prelude.hpp @@ -47,3 +47,5 @@ namespace floormat { using Error [[maybe_unused]] = ::Corrade::Utility::Error; namespace Path = Corrade::Utility::Path; // NOLINT(misc-unused-alias-decls) } // namespace floormat + +consteval auto operator""_uz(unsigned long long int x) { return ::floormat::types::size_t(x); } diff --git a/draw/anim.cpp b/draw/anim.cpp index 1e0d1bc4..acde5279 100644 --- a/draw/anim.cpp +++ b/draw/anim.cpp @@ -68,12 +68,12 @@ void anim_mesh::draw(tile_shader& shader, chunk& c) Optional last; const auto max_index = std::uint32_t(size*quad_index_count - 1); - std::size_t last_id = 0; - for (std::size_t k = 0; k < size; k++) + auto last_id = 0_uz; + for (auto k = 0_uz; k < size; k++) { auto id = ids[k]; auto [atlas, s] = c[id].scenery(); - for (std::size_t i = last_id+1; i < id; i++) + for (auto i = last_id+1; i < id; i++) if (auto [atlas, s] = c[i].scenery(); atlas && atlas->info().fps > 0) { @@ -103,7 +103,7 @@ void anim_mesh::draw(tile_shader& shader, chunk& c) draw(shader, *atlas, s.r, s.frame, local_coords{i}, s.offset); } else - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) if (auto [atlas, s] = c[i].scenery(); atlas) draw(shader, *atlas, s.r, s.frame, local_coords{i}, s.offset); @@ -120,7 +120,7 @@ void anim_mesh::draw(tile_shader& shader, anim_atlas& atlas, rotation r, std::si const auto& g = atlas.group(r); const auto texcoords = atlas.texcoords_for_frame(r, frame, !g.mirror_from.isEmpty()); quad_data array; - for (std::size_t i = 0; i < 4; i++) + for (auto i = 0_uz; i < 4; i++) array[i] = { pos[i], texcoords[i], depth }; _vertex_buffer.setSubData(0, array); atlas.texture().bind(0); diff --git a/editor/imgui-tiles.cpp b/editor/imgui-tiles.cpp index 9d84027f..57cdecba 100644 --- a/editor/imgui-tiles.cpp +++ b/editor/imgui-tiles.cpp @@ -49,7 +49,7 @@ void app::draw_editor_tile_pane_atlas(tile_editor& ed, StringView name, const st }; const bool perm_selected = ed.is_permutation_selected(atlas); constexpr std::size_t per_row = 8; - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) { const bool selected = ed.is_tile_selected(atlas, i); if (i > 0 && i % per_row == 0) diff --git a/editor/inspect.cpp b/editor/inspect.cpp index 82514cc9..0c45251b 100644 --- a/editor/inspect.cpp +++ b/editor/inspect.cpp @@ -155,7 +155,7 @@ bool do_inspect_field(void* datum, const erased_accessor& accessor, field_repr r ret = ImGui::SliderScalarN(label, igdt, &value, T::Size, &min, &max); break; } - for (std::size_t i = 0; i < T::Size; i++) + for (auto i = 0_uz; i < T::Size; i++) value[i] = std::clamp(value[i], min[i], max[i]); } diff --git a/entity/erased-constraints.cpp b/entity/erased-constraints.cpp index 2ac049d5..52bb784a 100644 --- a/entity/erased-constraints.cpp +++ b/entity/erased-constraints.cpp @@ -24,7 +24,7 @@ template std::pair range::convert() const using U = typename T::Type; constexpr auto Size = T::Size; T a, b; - for (std::size_t i = 0; i < Size; i++) + for (auto i = 0_uz; i < Size; i++) a[i] = std::numeric_limits::min(), b[i] = std::numeric_limits::max(); return {a, b}; } @@ -65,14 +65,14 @@ template std::pair range::convert() const if constexpr(std::is_signed_v) { fm_assert(type == type_int4); - for (std::size_t i = 0; i < Size; i++) + for (auto i = 0_uz; i < Size; i++) a[i] = U(min.i4[i]), b[i] = U(max.i4[i]); } else { if (type == type_int4) { - for (std::size_t i = 0; i < Size; i++) + for (auto i = 0_uz; i < Size; i++) { fm_assert(min.i4[i] >= 0 && max.i4[i] >= 0); a[i] = U(min.i4[i]), b[i] = U(max.i4[i]); @@ -81,7 +81,7 @@ template std::pair range::convert() const else { fm_assert(type == type_uint4); - for (std::size_t i = 0; i < Size; i++) + for (auto i = 0_uz; i < Size; i++) a[i] = U(min.u4[i]), b[i] = U(max.u4[i]); } } @@ -90,7 +90,7 @@ template std::pair range::convert() const { static_assert(std::is_floating_point_v); fm_assert(type == type_float4); - for (std::size_t i = 0; i < Size; i++) + for (auto i = 0_uz; i < Size; i++) a[i] = U(min.f4[i]), b[i] = U(max.f4[i]); } return { a, b }; diff --git a/main/draw.cpp b/main/draw.cpp index 02f58541..62c7fbf7 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -122,7 +122,7 @@ void main_impl::draw_world() noexcept if (check_chunk_visible(_shader.camera_offset(), sz)) { _anim_mesh.draw(_shader, c); - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) { const local_coords xy{i}; if (auto [atlas, s] = c[xy].scenery(); atlas) diff --git a/serialize/binary-reader.inl b/serialize/binary-reader.inl index 7b831f30..1234b08a 100644 --- a/serialize/binary-reader.inl +++ b/serialize/binary-reader.inl @@ -23,7 +23,7 @@ constexpr T binary_reader::read() noexcept(false) fm_soft_assert((std::ptrdiff_t)N <= std::distance(it, end)); num_bytes_read += N; char buf[N]; - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) buf[i] = *it++; return maybe_byteswap(std::bit_cast(buf)); } @@ -37,7 +37,7 @@ constexpr std::array binary_reader::read() noexcept(false) array = {}; fm_soft_assert(N <= (std::size_t)std::distance(it, end)); num_bytes_read += N; - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) array[i] = *it++; return array; } @@ -74,7 +74,7 @@ constexpr auto binary_reader::read_asciiz_string() noexcept(false) }; fixed_string ret; - for (std::size_t i = 0; i < MAX && it != end; i++) + for (auto i = 0_uz; i < MAX && it != end; i++) { const char c = *it++; ret.buf[i] = c; diff --git a/serialize/binary-writer.inl b/serialize/binary-writer.inl index e5a7ba16..a847903d 100644 --- a/serialize/binary-writer.inl +++ b/serialize/binary-writer.inl @@ -17,7 +17,7 @@ constexpr void binary_writer::write(T x) noexcept _bytes_written += sizeof(T); constexpr std::size_t N = sizeof(T); const auto buf = std::bit_cast, T>(maybe_byteswap(x)); - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) *it++ = buf[i]; } @@ -34,7 +34,7 @@ constexpr void binary_writer::write_asciiz_string(StringView str) noexcept fm_debug_assert(str.flags() & StringViewFlag::NullTerminated); const auto sz = str.size(); _bytes_written += sz + 1; - for (std::size_t i = 0; i < sz; i++) + for (auto i = 0_uz; i < sz; i++) *it++ = str[i]; *it++ = '\0'; } diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp index 08997b90..b8383d96 100644 --- a/serialize/magnum-vector.hpp +++ b/serialize/magnum-vector.hpp @@ -16,7 +16,7 @@ template void adl_serializer>::to_json(json& j, const vec& val) { std::array array{}; - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) array[i] = val[i]; using nlohmann::to_json; to_json(j, array); @@ -28,7 +28,7 @@ void adl_serializer>::from_json(const json& j, vec& v std::array array{}; using nlohmann::from_json; from_json(j, array); - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) val[i] = array[i]; } diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index 4f31b88b..3060579f 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -73,14 +73,14 @@ void reader_state::read_sceneries(reader_t& s) fm_soft_assert(sz < scenery_id_max); sceneries.resize(sz); - std::size_t i = 0; + auto i = 0_uz; while (i < sz) { std::uint8_t num; num << s; fm_soft_assert(num > 0); auto str = s.read_asciiz_string(); const auto sc_ = loader.scenery(str); - for (std::size_t n = 0; n < num; n++) + for (auto n = 0_uz; n < num; n++) { atlasid id; id << s; fm_soft_assert(id < sz); @@ -120,7 +120,7 @@ void reader_state::read_chunks(reader_t& s) { const auto N = s.read(); - for (std::size_t k = 0; k < N; k++) + for (auto k = 0_uz; k < N; k++) { std::decay_t magic; magic << s; @@ -130,7 +130,7 @@ void reader_state::read_chunks(reader_t& s) coord.x << s; coord.y << s; auto& chunk = (*_world)[coord]; - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) { const tilemeta flags = s.read(); tile_ref t = chunk[i]; diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index 6bb033ab..2a58a51b 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -256,7 +256,7 @@ void writer_state::serialize_scenery() s << (atlasid)sz; StringView last; - for (std::size_t i = 0; i < sz; i++) + for (auto i = 0_uz; i < sz; i++) { fm_debug_assert(s.bytes_written() + atlasbuf_size1 < atlasbuf_size); const auto& [sc, idx] = vec[i]; @@ -264,8 +264,8 @@ void writer_state::serialize_scenery() { fm_assert(sc->name.size() < atlas_name_max); last = sc->name; - std::size_t num = 1; - for (std::size_t j = i+1; j < sz && vec[j].s->name == sc->name; j++) + auto num = 1_uz; + for (auto j = i+1; j < sz && vec[j].s->name == sc->name; j++) num++; fm_assert(num < int_max); s << (std::uint8_t)num; @@ -292,7 +292,7 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) s << chunk_magic << coord.x << coord.y; - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) { const tile_proto x = c[i]; const auto ground = x.ground(), wall_north = x.wall_north(), wall_west = x.wall_west(); diff --git a/src/anim-atlas.cpp b/src/anim-atlas.cpp index 655bcf5e..ffade2d6 100644 --- a/src/anim-atlas.cpp +++ b/src/anim-atlas.cpp @@ -29,7 +29,7 @@ decltype(anim_atlas::_group_indices) anim_atlas::make_group_indices(const anim_d 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; const auto ngroups = a.groups.size(); - for (std::size_t i = 0; i < ngroups; i++) + for (auto i = 0_uz; i < ngroups; i++) array[rotation_to_index(a.groups[i].name)] = std::uint8_t(i); return array; } @@ -140,10 +140,10 @@ BitArray anim_atlas::make_bitmask(const ImageView2D& tex) fm_assert(tex.pixelSize() == 4); fm_assert(pixels.stride()[1] == 4); - for (std::size_t j = 0; j < height; j++) + for (auto j = 0_uz; j < height; j++) { constexpr unsigned char amin = 32; - std::size_t i = 0; + auto i = 0_uz; for (; i < width0; i += 8) { const auto src_idx = (j*stride + i*4)+3, dst_idx = (height-j-1)*width + i>>3; @@ -179,7 +179,7 @@ rotation anim_atlas::next_rotation_from(rotation r) const noexcept for (auto i = std::size_t(r)+1; i < count; i++) if (_group_indices[i] != 0xff) return rotation(i); - for (std::size_t i = 0; i < count; i++) + for (auto i = 0_uz; i < count; i++) if (_group_indices[i] != 0xff) return rotation(i); fm_abort("where did the rotations go?!"); @@ -206,7 +206,7 @@ bool anim_atlas::check_rotation(rotation r) const noexcept rotation anim_atlas::first_rotation() const noexcept { - for (std::size_t i = 0; i < rot_count; i++) + for (auto i = 0_uz; i < rot_count; i++) if (_group_indices[i] == 0) return rotation(i); fm_abort("unreachable! can't find first rotation"); diff --git a/src/chunk-collision.cpp b/src/chunk-collision.cpp index d41c02c8..5b85de1e 100644 --- a/src/chunk-collision.cpp +++ b/src/chunk-collision.cpp @@ -60,7 +60,7 @@ void chunk::ensure_passability() noexcept _rtree.RemoveAll(); - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) { auto tile = operator[](i); if (auto s = tile.scenery()) @@ -71,7 +71,7 @@ void chunk::ensure_passability() noexcept _rtree.Insert(start.data(), end.data(), id); } } - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) { if (const auto* atlas = ground_atlas_at(i)) if (atlas->pass_mode(pass_mode::pass) != pass_mode::pass) @@ -81,7 +81,7 @@ void chunk::ensure_passability() noexcept _rtree.Insert(start.data(), end.data(), id); } } - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) { auto tile = operator[](i); if (const auto* atlas = tile.wall_north_atlas().get()) diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index 48425f72..e54260dd 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -12,7 +12,7 @@ template static auto make_index_array(std::size_t max) { std::array, N*TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init) - for (std::size_t i = 0; i < max; i++) + for (auto i = 0_uz; i < max; i++) array[i] = tile_atlas::indices(i); return array; } @@ -30,7 +30,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple _ground_modified = false; std::size_t count = 0; - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) if (_ground_atlases[i]) ground_indexes[count++] = std::uint8_t(i); @@ -40,7 +40,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple }); std::array, TILE_COUNT> vertexes; - for (std::size_t k = 0; k < count; k++) + for (auto k = 0_uz; k < count; k++) { const std::uint8_t i = ground_indexes[k]; const auto& atlas = _ground_atlases[i]; @@ -49,7 +49,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple const auto texcoords = atlas->texcoords_for_id(_ground_variants[i]); const float depth = tile_shader::depth_value(pos); auto& v = vertexes[k]; - for (std::size_t j = 0; j < 4; j++) + for (auto j = 0_uz; j < 4; j++) v[j] = { quad[j], texcoords[j], depth }; } @@ -72,7 +72,7 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple _walls_modified = false; std::size_t count = 0; - for (std::size_t i = 0; i < TILE_COUNT*2; i++) + for (auto i = 0_uz; i < TILE_COUNT*2; i++) if (_wall_atlases[i]) wall_indexes[count++] = std::uint16_t(i); @@ -82,7 +82,7 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple }); std::array, TILE_COUNT*2> vertexes; - for (std::size_t k = 0; k < count; k++) + for (auto k = 0_uz; k < count; k++) { const std::uint16_t i = wall_indexes[k]; const auto& atlas = _wall_atlases[i]; @@ -93,7 +93,7 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple const float depth = tile_shader::depth_value(pos); const auto texcoords = atlas->texcoords_for_id(variant); auto& v = vertexes[k]; - for (std::size_t j = 0; j < 4; j++) + for (auto j = 0_uz; j < 4; j++) v[j] = { quad[j], texcoords[j], depth, }; } @@ -116,7 +116,7 @@ auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple _scenery_modified = false; std::size_t count = 0; - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) if (const auto& atlas = _scenery_atlases[i]; atlas && atlas->info().fps == 0) scenery_indexes[count++] = std::uint8_t(i); @@ -127,7 +127,7 @@ auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple }); #endif std::array, TILE_COUNT> vertexes; - for (std::size_t k = 0; k < count; k++) + for (auto k = 0_uz; k < count; k++) { const std::uint8_t i = scenery_indexes[k]; const local_coords pos{i}; @@ -139,7 +139,7 @@ auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple const auto texcoords = atlas->texcoords_for_frame(fr.r, fr.frame, !group.mirror_from.isEmpty()); const float depth = tile_shader::depth_value(pos, tile_shader::scenery_depth_offset); auto& v = vertexes[k]; - for (std::size_t j = 0; j < 4; j++) + for (auto j = 0_uz; j < 4; j++) v[j] = { quad[j], texcoords[j], depth }; } diff --git a/src/chunk.cpp b/src/chunk.cpp index 033e1831..774a874c 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -7,7 +7,7 @@ bool chunk::empty(bool force) const noexcept { if (!force && !_maybe_empty) return false; - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) if (_ground_atlases[i] || _wall_atlases[i*2 + 0] || _wall_atlases[i*2 + 1] || _scenery_atlases[i]) return _maybe_empty = false; return true; diff --git a/src/random.cpp b/src/random.cpp index d3e45292..7888dbaa 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -17,13 +17,13 @@ std::size_t random_engine_impl::operator()() { if constexpr(sizeof(std::size_t) > sizeof(std::uint32_t)) { - constexpr std::size_t N = (sizeof(std::size_t) + sizeof(std::uint32_t)-1) / sizeof(std::uint32_t); + constexpr auto N = (sizeof(std::size_t) + sizeof(std::uint32_t)-1) / sizeof(std::uint32_t); static_assert(N >= 1); union { std::size_t x; std::uint32_t a[N]; } ret; - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) ret.a[i] = g(); return ret.x; } diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp index b13b9ea8..68feb77b 100644 --- a/src/tile-atlas.cpp +++ b/src/tile-atlas.cpp @@ -50,7 +50,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[]>(N); - for (std::size_t i = 0; i < N; i++) + for (auto i = 0_uz; i < N; i++) ptr[i] = make_texcoords(pixel_size, tile_count, i); return ptr; } diff --git a/test/serializer.cpp b/test/serializer.cpp index 3e1e6a97..71b1a3f1 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -37,7 +37,7 @@ chunk test_app::make_test_chunk() static bool chunks_equal(const chunk& a, const chunk& b) { - for (std::size_t i = 0; i < TILE_COUNT; i++) + for (auto i = 0_uz; i < TILE_COUNT; i++) { const auto &a1 = a[i], &b1 = b[i]; if (a1 != b1) -- cgit v1.2.3