diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-27 05:57:05 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-27 05:57:05 +0100 |
commit | 5f3d184276f6b4e13642018681cf33d2bc5b7638 (patch) | |
tree | c6538eb7ef9c6ab0f361337698f7eee4a9ece819 | |
parent | 143b205fae27ed433fd7cac0e0ab6f0fa4f8b03b (diff) |
a
-rw-r--r-- | compat/format.hpp | 9 | ||||
-rw-r--r-- | editor/imgui-tiles.cpp | 1 | ||||
-rw-r--r-- | src/dijkstra.cpp | 1 | ||||
-rw-r--r-- | src/wall-atlas.hpp | 2 | ||||
-rw-r--r-- | wall-tileset-tool/main.cpp | 55 |
5 files changed, 54 insertions, 14 deletions
diff --git a/compat/format.hpp b/compat/format.hpp index 741b35bd..d0736c97 100644 --- a/compat/format.hpp +++ b/compat/format.hpp @@ -4,7 +4,6 @@ #include <fmt/compile.h> #include <Corrade/Containers/StringView.h> #include <Corrade/Containers/String.h> -#include <Corrade/Utility/Math.h> #include <Corrade/Utility/Move.h> namespace fmt { @@ -54,11 +53,15 @@ size_t snformat(char(&buf)[N], Fmt&& fmt, Xs&&... args) { constexpr size_t n = N > 0 ? N - 1 : 0; auto result = fmt::format_to_n(buf, n, Corrade::Utility::forward<Fmt>(fmt), Corrade::Utility::forward<Xs>(args)...); - const auto len = Utility::min(n, result.size); + const auto len = n < result.size ? n : result.size; fm_assert(len > 0); +#if 0 if constexpr(N > 0) buf[len] = '\0'; - return len; +#else + buf[len] = '\0'; +#endif + return result.size; } } // namespace floormat diff --git a/editor/imgui-tiles.cpp b/editor/imgui-tiles.cpp index 0dbe10d3..b4fc8a9c 100644 --- a/editor/imgui-tiles.cpp +++ b/editor/imgui-tiles.cpp @@ -36,6 +36,7 @@ void app::draw_editor_tile_pane_atlas(tile_editor& ed, StringView name, const st text(" (selected)"); } const auto len = snformat(buf, "{:d}"_cf, N); + fm_assert(len < std::size(buf)); ImGui::SameLine(window_width - ImGui::CalcTextSize(buf).x - style.FramePadding.x - 4*dpi[0]); text({buf, len}); }; diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index c5efbd34..8dd8d97a 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -346,6 +346,7 @@ path_search_result astar::Dijkstra(world& w, const point from, const point to, } if (len) { + len = Math::min(len, std::size(buf)-1); std::fwrite(buf, len, 1, stdout); std::fflush(stdout); } diff --git a/src/wall-atlas.hpp b/src/wall-atlas.hpp index 8afddc24..333dd47c 100644 --- a/src/wall-atlas.hpp +++ b/src/wall-atlas.hpp @@ -44,7 +44,7 @@ enum class Direction_ : uint8_t { N, E, S, W, COUNT }; struct Direction { using memfn_ptr = Group Direction::*; - struct member_tuple { StringView str; memfn_ptr member; Group_ tag; }; + struct member_tuple { StringView name; memfn_ptr member; Group_ tag; }; Group wall{}, overlay{}, side{}, top{}; Group corner_L{}, corner_R{}; diff --git a/wall-tileset-tool/main.cpp b/wall-tileset-tool/main.cpp index 4a64dc82..36493c47 100644 --- a/wall-tileset-tool/main.cpp +++ b/wall-tileset-tool/main.cpp @@ -2,7 +2,7 @@ #include "compat/assert.hpp" #include "compat/sysexits.hpp" #include "compat/fix-argv0.hpp" -//#include "compat/format.hpp" +#include "compat/format.hpp" #include "compat/debug.hpp" #include "src/wall-atlas.hpp" //#include "serialize/wall-atlas.hpp" @@ -11,6 +11,7 @@ #include <utility> #include <Corrade/Containers/StringView.h> #include <Corrade/Containers/String.h> +#include <Corrade/Containers/StringIterable.h> #include <Corrade/Containers/TripleStl.h> #include <Corrade/Utility/Path.h> #include <Corrade/Utility/DebugStl.h> @@ -68,14 +69,47 @@ Direction& get_direction(wall_atlas_def& atlas, size_t i) return const_cast<Direction&>(get_direction(const_cast<const wall_atlas_def&>(atlas), i)); } +template<typename Fmt, typename... Xs> +auto asformat(Fmt&& fmt, Xs&&... args) +{ + auto result = fmt::format_to_n((char*)nullptr, 0, std::forward<Fmt>(fmt), std::forward<Xs>(args)...); + std::string ret; + ret.resize(result.size); + auto result2 = fmt::format_to_n(ret.data(), ret.size(), std::forward<Fmt>(fmt), std::forward<Xs>(args)...); + fm_assert(result2.size == result.size); + return ret; +} + bool do_group(state st, size_t i, size_t j, Group& new_dir) { const wall_atlas_def& old_atlas = st.old_atlas; wall_atlas_def& new_atlas = st.new_atlas; const auto& old_dir = get_direction(old_atlas, (size_t)i); //auto& new_dir = get_direction(new_atlas, (size_t)i); + const auto group_name = Direction::groups[j].name; + const auto dir_name = wall_atlas::directions[i].name; + + DBG << " group" << quoted2(group_name); + size_t fileno = 1; + const auto path = Path::join({ st.opts.input_dir, dir_name, group_name }); - DBG << " group" << quoted2(Direction::groups[j].str); + for (;;) + { + auto filename = asformat("{}/{:04}.png"_cf, path, fileno++); + auto str = StringView{filename.data(), filename.size(), StringViewFlag::NullTerminated}; + Debug{} << str; + if (!Path::exists(filename)) + { + Debug{} << "end"; + break; + } + if (Path::isDirectory(filename)) + { + ERR << "fatal: file" << quoted(filename) << "is a directory"; + return false; + } + cv::Mat mat = cv::imread(cv::String{filename.data(), filename.size()}); + } return true; } @@ -118,7 +152,6 @@ bool do_input_file(state& st) { DBG << "input" << quoted(st.old_atlas.header.name) << colon(',') << quoted(st.opts.input_file); - fm_assert(!st.buffer.empty()); fm_assert(loader.check_atlas_name(st.old_atlas.header.name)); fm_assert(st.old_atlas.direction_mask.any()); @@ -151,7 +184,14 @@ inline String fixsep(String str) return str; } -Triple<options, Arguments, bool> parse_cmdline(int argc, const char* const* argv) noexcept +struct argument_tuple +{ + options opts{}; + Arguments args{}; + bool ok = false; +}; + +argument_tuple parse_cmdline(int argc, const char* const* argv) noexcept { Corrade::Utility::Arguments args{}; args.addOption('o', "output"s, "\0"_s).setHelp("output"s, ""_s, "DIR"s); @@ -183,7 +223,7 @@ Triple<options, Arguments, bool> parse_cmdline(int argc, const char* const* argv fm_assert(opts.output_dir); fm_assert(opts.input_file); fm_assert(opts.input_dir); - return { std::move(opts), std::move(args), true }; + return { .opts = std::move(opts), .args = std::move(args), .ok = true }; } return {}; @@ -204,11 +244,6 @@ using namespace floormat::wall_tool; int main(int argc, char** argv) { - auto s = "foo"_s; - static_assert(std::is_same_v<decltype(quoted(StringView{"foo"}))::type, StringView>); - static_assert(std::is_same_v<decltype(quoted(String{"foo"}))::type, String>); - static_assert(std::is_same_v<decltype(quoted(s))::type, const StringView&>); - argv[0] = fix_argv0(argv[0]); auto [opts, args, opts_ok] = parse_cmdline(argc, argv); if (!opts_ok) |