From 9041dfd60ae8a8056df1a5e7e85609a231406d3d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 9 Dec 2023 05:01:06 +0100 Subject: w --- loader/impl.hpp | 5 ++++- loader/loader.hpp | 2 +- loader/wall-atlas.cpp | 31 +++++++++++++++++++++++++-- loader/wall-info.hpp | 2 +- src/chunk-walls.cpp | 59 ++++++++++++++++++++++++--------------------------- src/chunk.hpp | 2 -- 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 wall_atlas_map; std::vector wall_atlas_array; - const wall_info& wall_atlas(StringView name) override; + Pointer invalid_wall_atlas; + + const wall_info& wall_atlas(StringView name, bool fail_ok = true) override; ArrayView wall_atlas_list() override; void get_wall_atlas_list(); + const wall_info& make_invalid_wall_atlas(); std::shared_ptr 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 tile_atlas(StringView filename) noexcept(false) = 0; virtual ArrayView anim_atlas_list() = 0; virtual std::shared_ptr 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 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 #include #include @@ -49,7 +50,28 @@ std::shared_ptr 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 = ""_s; + constexpr auto size = Vector3ui{iTILE_SIZE}; + constexpr auto frame_size = Vector2ui{size.x(), size.z()}; + + auto a = std::make_shared( + 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{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 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 #include -// +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{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{InPlaceInit}; -} +template constexpr std::array make_wall_vertex_data(float depth); // ----------------------- // corner left -template<> std::array chunk::make_wall_vertex_data(float) +template<> std::array constexpr make_wall_vertex_data(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 chunk::make_wall_vertex_data std::array chunk::make_wall_vertex_data(float) +template<> std::array constexpr make_wall_vertex_data(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 chunk::make_wall_vertex_data std::array chunk::make_wall_vertex_data(float) +template<> std::array constexpr make_wall_vertex_data(float) { return {{ { X, -Y, Z }, @@ -68,30 +63,30 @@ template<> std::array chunk::make_wall_vertex_data std::array chunk::make_wall_vertex_data(float) +template<> std::array constexpr make_wall_vertex_data(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 chunk::make_wall_vertex_data(float depth) +template<> std::array constexpr make_wall_vertex_data(float depth) { return make_wall_vertex_data(depth); } // overlay west -template<> std::array chunk::make_wall_vertex_data(float depth) +template<> std::array constexpr make_wall_vertex_data(float depth) { return make_wall_vertex_data(depth); } // side north -template<> std::array chunk::make_wall_vertex_data(float depth) +template<> std::array constexpr make_wall_vertex_data(float depth) { auto left = Vector2{X, -Y }, right = Vector2{left.x(), left.y() - depth }; @@ -104,7 +99,7 @@ template<> std::array chunk::make_wall_vertex_data std::array chunk::make_wall_vertex_data(float depth) +template<> std::array constexpr make_wall_vertex_data(float depth) { auto right = Vector2{ -X, Y }; auto left = Vector2{ right.x() - depth, right.y() }; @@ -117,7 +112,7 @@ template<> std::array chunk::make_wall_vertex_data std::array chunk::make_wall_vertex_data(float depth) +template<> std::array constexpr make_wall_vertex_data(float depth) { auto top_right = Vector2{X, Y - depth }, bottom_right = Vector2{top_right.x(), Y }, @@ -132,12 +127,12 @@ template<> std::array chunk::make_wall_vertex_data std::array chunk::make_wall_vertex_data(float depth) +template<> std::array constexpr make_wall_vertex_data(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 chunk::make_wall_vertex_data static std::array make_wall_vertex_data(float depth); - Pointer _ground; Pointer _walls; std::vector> _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, -- cgit v1.2.3