diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-12-09 05:01:06 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-12-09 05:12:05 +0100 |
commit | 9041dfd60ae8a8056df1a5e7e85609a231406d3d (patch) | |
tree | 6d32fefa3cb7624d8f47ee53c68c55ee1a91ebbf | |
parent | 07a0e09c5927478cf2efee663aeec5dbaf3645f1 (diff) |
w
-rw-r--r-- | loader/impl.hpp | 5 | ||||
-rw-r--r-- | loader/loader.hpp | 2 | ||||
-rw-r--r-- | loader/wall-atlas.cpp | 31 | ||||
-rw-r--r-- | loader/wall-info.hpp | 2 | ||||
-rw-r--r-- | src/chunk-walls.cpp | 59 | ||||
-rw-r--r-- | src/chunk.hpp | 2 | ||||
-rw-r--r-- | src/wall-atlas.hpp | 2 |
7 files changed, 64 insertions, 39 deletions
diff --git a/loader/impl.hpp b/loader/impl.hpp index a5a197c9..c5fe83b2 100644 --- a/loader/impl.hpp +++ b/loader/impl.hpp @@ -49,9 +49,12 @@ struct loader_impl final : loader_ tsl::robin_map<StringView, wall_info*> wall_atlas_map; std::vector<wall_info> wall_atlas_array; - const wall_info& wall_atlas(StringView name) override; + Pointer<wall_info> invalid_wall_atlas; + + const wall_info& wall_atlas(StringView name, bool fail_ok = true) override; ArrayView<const wall_info> wall_atlas_list() override; void get_wall_atlas_list(); + const wall_info& make_invalid_wall_atlas(); std::shared_ptr<class wall_atlas> get_wall_atlas(StringView name, StringView path); // >-----> tile >-----> diff --git a/loader/loader.hpp b/loader/loader.hpp index ac99cfb5..a7dd8a7f 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -33,7 +33,7 @@ struct loader_ virtual std::shared_ptr<class tile_atlas> tile_atlas(StringView filename) noexcept(false) = 0; virtual ArrayView<const String> anim_atlas_list() = 0; virtual std::shared_ptr<class anim_atlas> anim_atlas(StringView name, StringView dir = ANIM_PATH) noexcept(false) = 0; - virtual const wall_info& wall_atlas(StringView name) = 0; + virtual const wall_info& wall_atlas(StringView name, bool fail_ok = true) = 0; virtual ArrayView<const wall_info> wall_atlas_list() = 0; static void destroy(); static loader_& default_loader() noexcept; diff --git a/loader/wall-atlas.cpp b/loader/wall-atlas.cpp index 1b10740c..23eb6e53 100644 --- a/loader/wall-atlas.cpp +++ b/loader/wall-atlas.cpp @@ -7,6 +7,7 @@ #include "wall-info.hpp" #include "serialize/json-helper.hpp" #include "serialize/corrade-string.hpp" +#include "src/tile-defs.hpp" #include <Corrade/Containers/Array.h> #include <Corrade/Containers/ArrayViewStl.h> #include <Corrade/Containers/StringIterable.h> @@ -49,7 +50,28 @@ std::shared_ptr<wall_atlas> loader_impl::get_wall_atlas(StringView name, StringV return atlas; } -const wall_info& loader_impl::wall_atlas(StringView name) +const wall_info& loader_impl::make_invalid_wall_atlas() +{ + if (invalid_wall_atlas) [[likely]] + return *invalid_wall_atlas; + + constexpr auto name = "<invalid>"_s; + constexpr auto size = Vector3ui{iTILE_SIZE}; + constexpr auto frame_size = Vector2ui{size.x(), size.z()}; + + auto a = std::make_shared<class wall_atlas>( + wall_atlas_def { + {.name = name, .depth = 8}, + { {{}, frame_size},}, + { { {.index = 0, .count = 1, .pixel_size = frame_size, } } }, + {{ {.val = 0}, {}, {}, {} }}, + {1u}, + }, name, make_error_texture()); + invalid_wall_atlas = Pointer<wall_info>{InPlaceInit, wall_info{ .name = name, .atlas = a } }; + return *invalid_wall_atlas; +} + +const wall_info& loader_impl::wall_atlas(StringView name, bool fail_ok) { fm_soft_assert(check_atlas_name(name)); char buf[FILENAME_MAX]; @@ -57,7 +79,12 @@ const wall_info& loader_impl::wall_atlas(StringView name) auto it = wall_atlas_map.find(name); if (it == wall_atlas_map.end()) - fm_throw("no such wall atlas '{}'"_cf, name); + { + if (!fail_ok) + fm_throw("no such wall atlas '{}'"_cf, name); + else + return make_invalid_wall_atlas(); + } fm_assert(it->second != nullptr); if (!it->second->atlas) it->second->atlas = get_wall_atlas(it->second->name, path); diff --git a/loader/wall-info.hpp b/loader/wall-info.hpp index 570f436d..acd5cdc4 100644 --- a/loader/wall-info.hpp +++ b/loader/wall-info.hpp @@ -8,7 +8,7 @@ class wall_atlas; struct wall_info { - String name, descr; + String name, descr{}; std::shared_ptr<wall_atlas> atlas; }; diff --git a/src/chunk-walls.cpp b/src/chunk-walls.cpp index 8cfdaf64..59c20434 100644 --- a/src/chunk-walls.cpp +++ b/src/chunk-walls.cpp @@ -7,35 +7,30 @@ #include <Corrade/Containers/PairStl.h> #include <algorithm> -// +x +y +z -// +x +y -z -// -x -y +z -// -x +y -z - namespace floormat { -using namespace floormat::Quads; -using Wall::Group_; +void chunk::ensure_alloc_walls() +{ + if (!_walls) [[unlikely]] + _walls = Pointer<wall_stuff>{InPlaceInit}; +} namespace { constexpr Vector2 half_tile = TILE_SIZE2*.5f; constexpr float X = half_tile.x(), Y = half_tile.y(), Z = TILE_SIZE.z(); -} // namespace +using namespace floormat::Quads; +using Wall::Group_; -void chunk::ensure_alloc_walls() -{ - if (!_walls) [[unlikely]] - _walls = Pointer<wall_stuff>{InPlaceInit}; -} +template<Group_ G, bool IsWest> constexpr std::array<Vector3, 4> make_wall_vertex_data(float depth); // ----------------------- // corner left -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::corner_L, false>(float) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::corner_L, false>(float) { - constexpr float x_offset = (float)((unsigned)X)/2u; + constexpr float x_offset = (float)(unsigned)X; return {{ { -X + x_offset, -Y, Z }, { -X + x_offset, -Y, 0 }, @@ -45,9 +40,9 @@ template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::corner_L, } // corner right -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::corner_R, true>(float) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::corner_R, true>(float) { - constexpr float y_offset = Y - (float)((unsigned)Y)/2u; + constexpr float y_offset = TILE_SIZE.y() - (float)(unsigned)Y; return {{ {-X, -Y, Z }, {-X, -Y, 0 }, @@ -57,7 +52,7 @@ template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::corner_R, } // wall north -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::wall, false>(float) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::wall, false>(float) { return {{ { X, -Y, Z }, @@ -68,30 +63,30 @@ template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::wall, fal } // wall west -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::wall, true>(float) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::wall, true>(float) { return {{ - {-X, Y, Z }, - {-X, Y, 0 }, {-X, -Y, Z }, {-X, -Y, 0 }, + {-X, Y, Z }, + {-X, Y, 0 }, }}; } // overlay north -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::overlay, false>(float depth) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::overlay, false>(float depth) { return make_wall_vertex_data<Wall::Group_::wall, false>(depth); } // overlay west -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::overlay, true>(float depth) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::overlay, true>(float depth) { return make_wall_vertex_data<Wall::Group_::wall, true>(depth); } // side north -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::side, false>(float depth) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::side, false>(float depth) { auto left = Vector2{X, -Y }, right = Vector2{left.x(), left.y() - depth }; @@ -104,7 +99,7 @@ template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::side, fal } // side west -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::side, true>(float depth) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::side, true>(float depth) { auto right = Vector2{ -X, Y }; auto left = Vector2{ right.x() - depth, right.y() }; @@ -117,7 +112,7 @@ template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::side, tru } // top north -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::top, false>(float depth) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::top, false>(float depth) { auto top_right = Vector2{X, Y - depth }, bottom_right = Vector2{top_right.x(), Y }, @@ -132,12 +127,12 @@ template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::top, fals } // top west -template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::top, true>(float depth) +template<> std::array<Vector3, 4> constexpr make_wall_vertex_data<Group_::top, true>(float depth) { - auto top_right = Vector2{-X, -Y }, - top_left = Vector2{top_right.x() - depth, top_right.y() }, - bottom_right = Vector2{-X, Y }, - bottom_left = Vector2{bottom_right.x() - depth, bottom_right.y() }; + auto top_right = Vector2{-X, -Y }, + top_left = Vector2{top_right.x() - depth, top_right.y() }, + bottom_right = Vector2{top_right.x(), Y }, + bottom_left = Vector2{top_left.x(), bottom_right.y() }; return {{ { bottom_right.x(), bottom_right.y(), Z }, @@ -149,6 +144,8 @@ template<> std::array<Vector3, 4> chunk::make_wall_vertex_data<Group_::top, true // ----------------------- +} // namespace + fm_noinline GL::Mesh chunk::make_wall_mesh(size_t count) { diff --git a/src/chunk.hpp b/src/chunk.hpp index 243230df..dc80d42e 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -135,8 +135,6 @@ private: bool empty() const { return count_N == 0 && count_W == 0; } }; - template<Wall::Group_ G, bool IsWest> static std::array<Vector3, 4> make_wall_vertex_data(float depth); - Pointer<ground_stuff> _ground; Pointer<wall_stuff> _walls; std::vector<std::shared_ptr<object>> _objects; diff --git a/src/wall-atlas.hpp b/src/wall-atlas.hpp index c08a53e7..c84bac1b 100644 --- a/src/wall-atlas.hpp +++ b/src/wall-atlas.hpp @@ -27,7 +27,7 @@ struct Group uint32_t index = (uint32_t)-1, count = 0; Vector2ui pixel_size; Color4 tint_mult{1,1,1,1}; - Color3 tint_add; + Color3 tint_add{}; uint8_t from_rotation = (uint8_t)-1; // applies only to images; todo remove it? bool mirrored : 1 = false, default_tint : 1 = true, |