From ded69f52906990cf975a62c0efbaca4b6cfa5e88 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 6 Oct 2022 15:54:10 +0200 Subject: a --- CMakeLists.txt | 1 + compat/assert.hpp | 39 ++++++++++++------------ main/main.cpp | 2 ++ serialize/anim.cpp | 4 +-- serialize/helper.hpp | 62 -------------------------------------- serialize/json-helper.hpp | 62 ++++++++++++++++++++++++++++++++++++++ serialize/magnum-vector.hpp | 51 ++++++++++---------------------- serialize/magnum-vector2i.hpp | 42 ++++++++++++++++++++++++++ serialize/tile-atlas.cpp | 2 +- serialize/tile-atlas.hpp | 1 - src/tile-atlas.cpp | 1 + src/tile-atlas.hpp | 3 +- src/wall-mesh.cpp | 2 +- test/CMakeLists.txt | 8 +++++ test/main.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 226 insertions(+), 123 deletions(-) delete mode 100644 serialize/helper.hpp create mode 100644 serialize/json-helper.hpp create mode 100644 serialize/magnum-vector2i.hpp create mode 100644 test/CMakeLists.txt create mode 100644 test/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 07bb26ee..eabc854f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ if(NOT BOOTSTRAP_DEPENDS) add_subdirectory(src) add_subdirectory(main) add_subdirectory(anim-crop-tool) + add_subdirectory(test) install(DIRECTORY images DESTINATION "share/${PROJECT_NAME}") endif() diff --git a/compat/assert.hpp b/compat/assert.hpp index a9dbe0bc..0d433274 100644 --- a/compat/assert.hpp +++ b/compat/assert.hpp @@ -23,30 +23,29 @@ constexpr void abort(const char (&fmt)[N], Xs... xs) namespace Magnum::Examples { -#define ABORT(...) \ - do { \ - if (std::is_constant_evaluated()) \ - throw "aborting"; \ - else \ - ::Magnum::Examples::detail:: \ - abort("%s: aborting at %s:%d", FUNCTION_NAME, __FILE__, __LINE__); \ +#define ABORT(fmt, ...) \ + do { \ + if (std::is_constant_evaluated()) \ + throw "aborting"; \ + else \ + ::Magnum::Examples::detail:: abort(fmt, __VA_ARGS__); \ } while (false) -#define ASSERT(expr) \ - do { \ - if (!(expr)) { \ - ::Magnum::Examples::detail:: \ - abort("%s: assertion failed: '%s' in %s:%d", \ - FUNCTION_NAME, #expr, __FILE__, __LINE__); \ - } \ +#define ASSERT(expr) \ + do { \ + if (!(expr)) { \ + ::Magnum::Examples::detail:: \ + abort("assertion failed: '%s' in %s:%d", \ + #expr, __FILE__, __LINE__); \ + } \ } while(false) -#define GAME_DEBUG_OUT(pfx, ...) ([&]() { \ - if constexpr (sizeof((pfx)) > 1) \ - std::fputs((pfx), stderr); \ - std::fprintf(stderr, __VA_ARGS__); \ - std::fputs("\n", stderr); \ - std::fflush(stderr); \ +#define GAME_DEBUG_OUT(pfx, ...) ([&]() { \ + if constexpr (sizeof((pfx)) > 1) \ + std::fputs((pfx), stderr); \ + std::fprintf(stderr, __VA_ARGS__); \ + std::fputs("\n", stderr); \ + std::fflush(stderr); \ }()) #define WARN(...) GAME_DEBUG_OUT("warning: ", __VA_ARGS__) diff --git a/main/main.cpp b/main/main.cpp index 9dc5ce4e..14828cad 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -5,6 +5,7 @@ #include "chunk.hpp" #include "floor-mesh.hpp" #include "wall-mesh.hpp" +#include "compat/defs.hpp" #include @@ -105,6 +106,7 @@ app::app(const Arguments& arguments): .setSize({1024, 768}, dpi_policy::Physical), GLConfiguration{} .setSampleCount(4) + .setFlags(Platform::Sdl2Application::GLConfiguration::Flag::Debug) } { reset_camera_offset(); diff --git a/serialize/anim.cpp b/serialize/anim.cpp index 78c871a8..b7d3ce95 100644 --- a/serialize/anim.cpp +++ b/serialize/anim.cpp @@ -1,5 +1,5 @@ -#include "serialize/magnum-vector.hpp" -#include "serialize/helper.hpp" +#include "serialize/magnum-vector2i.hpp" +#include "serialize/json-helper.hpp" #include "serialize/anim.hpp" #include diff --git a/serialize/helper.hpp b/serialize/helper.hpp deleted file mode 100644 index f16ed60c..00000000 --- a/serialize/helper.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include - -template -struct json_helper final { - [[nodiscard]] static std::tuple from_json(const std::filesystem::path& pathname) noexcept; - [[nodiscard]] static bool to_json(const t& self, const std::filesystem::path& pathname) noexcept; -}; - -template -std::tuple json_helper::from_json(const std::filesystem::path& pathname) noexcept { - using namespace nlohmann; - using Corrade::Utility::Error; - std::ifstream s; - s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::in); - } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "': " << e.what(); - return { {}, false }; - } - t ret; - try { - json j; - s >> j; - using nlohmann::from_json; - from_json(j, ret); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed to parse '" << pathname << "': " << e.what(); - return { {}, false }; - } - return { std::move(ret), true }; -} - -template -bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) noexcept { - using Corrade::Utility::Error; - try { - nlohmann::json j = self; - - std::ofstream s; - s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::out | std::ios_base::trunc); - } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what(); - return false; - } - s << j.dump(4); - s.flush(); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed writing to '" << pathname << "': " << e.what(); - return false; - } - - return true; -} diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp new file mode 100644 index 00000000..af6fd211 --- /dev/null +++ b/serialize/json-helper.hpp @@ -0,0 +1,62 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +template +struct json_helper final { + [[nodiscard]] static std::tuple from_json(const std::filesystem::path& pathname) noexcept; + [[nodiscard]] static bool to_json(const t& self, const std::filesystem::path& pathname) noexcept; +}; + +template +std::tuple json_helper::from_json(const std::filesystem::path& pathname) noexcept { + using namespace nlohmann; + using Corrade::Utility::Error; + std::ifstream s; + s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); + try { + s.open(pathname, std::ios_base::in); + } catch (const std::ios::failure& e) { + Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "': " << e.what(); + return { {}, false }; + } + t ret; + try { + json j; + s >> j; + using nlohmann::from_json; + from_json(j, ret); + } catch (const std::exception& e) { + Error{Error::Flag::NoSpace} << "failed to parse '" << pathname << "': " << e.what(); + return { {}, false }; + } + return { std::move(ret), true }; +} + +template +bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) noexcept { + using Corrade::Utility::Error; + try { + nlohmann::json j(self); + + std::ofstream s; + s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); + try { + s.open(pathname, std::ios_base::out | std::ios_base::trunc); + } catch (const std::ios::failure& e) { + Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what(); + return false; + } + s << j.dump(4); + s.flush(); + } catch (const std::exception& e) { + Error{Error::Flag::NoSpace} << "failed writing to '" << pathname << "': " << e.what(); + return false; + } + + return true; +} diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp index c4a2c36c..3e95c8eb 100644 --- a/serialize/magnum-vector.hpp +++ b/serialize/magnum-vector.hpp @@ -1,44 +1,25 @@ -#include -#include -#include +#pragma once #include -#include +#include #include namespace nlohmann { -template -requires std::is_integral_v -struct adl_serializer> final { - static void to_json(json& j, const Magnum::Math::Vector2& x); - static void from_json(const json& j, Magnum::Math::Vector2& x); -}; - -template -requires std::is_integral_v -void adl_serializer>::to_json(json& j, const Magnum::Math::Vector2& val) -{ - char buf[64]; - snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]); - j = buf; -} - -template -requires std::is_integral_v -void adl_serializer>::from_json(const json& j, Magnum::Math::Vector2& val) -{ - std::string str = j; - long long x = 0, y = 0; int n = 0; - int ret = std::sscanf(str.c_str(), "%lld x %lld%n", &x, &y, &n); - if (ret != 2 || (std::size_t)n != str.size()) +template +struct adl_serializer> final { + static void to_json(json& j, const Magnum::Math::Vector2& val) { - std::string msg; msg.reserve(64 + str.size()); - msg += "failed to parse string '"; - msg += str; - msg += "' as Magnum::Vector2i"; - throw std::invalid_argument(msg); + std::array array{}; + for (std::size_t i; i < std::size(val); i++) + array[i] = val[i]; + j = array; } - val = { (t)x, (t)y }; -} + static void from_json(const json& j, Magnum::Math::Vector2& val) + { + std::array array = j; + for (std::size_t i; i < std::size(val); i++) + val[i] = array[i]; + } +}; } // namespace nlohmann diff --git a/serialize/magnum-vector2i.hpp b/serialize/magnum-vector2i.hpp new file mode 100644 index 00000000..eb445e21 --- /dev/null +++ b/serialize/magnum-vector2i.hpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace nlohmann { + +template +requires std::is_integral_v +struct adl_serializer> final { + static void to_json(json& j, const Magnum::Math::Vector2& val) + { + char buf[64]; + using type = std::conditional_t, std::intmax_t, std::uintmax_t>; + constexpr auto format_string = std::is_signed_v ? "%jd x %jd" : "%ju x %ju"; + snprintf(buf, sizeof(buf), format_string, (type)val[0], (type)val[1]); + j = buf; + } + static void from_json(const json& j, Magnum::Math::Vector2& val) + { + std::string str = j; + using type = std::conditional_t, std::intmax_t, std::uintmax_t>; + constexpr auto format_string = std::is_signed_v ? "%jd x %jd%n" : "%ju x %ju%n"; + type x = 0, y = 0; + int n = 0; + int ret = std::sscanf(str.c_str(), format_string, &x, &y, &n); + if (ret != 2 || (std::size_t)n != str.size() || x != (type)x || y != (type)y) + { + std::string msg; msg.reserve(128); + msg += "failed to parse string '"; + msg += str; + msg += "' as Magnum::Vector2i"; + throw std::invalid_argument(msg); + } + val = { (t)x, (t)y }; + } +}; + +} // namespace nlohmann diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index 1aa0c14d..c7e52057 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -1,6 +1,6 @@ #include "src/tile-atlas.hpp" #include "serialize/tile-atlas.hpp" -#include "serialize/magnum-vector.hpp" +#include "serialize/magnum-vector2i.hpp" #include "loader.hpp" #include diff --git a/serialize/tile-atlas.hpp b/serialize/tile-atlas.hpp index c49b8a72..8dfa8c86 100644 --- a/serialize/tile-atlas.hpp +++ b/serialize/tile-atlas.hpp @@ -1,5 +1,4 @@ #pragma once -#include "loader.hpp" #include #include diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp index bc573134..d086c31b 100644 --- a/src/tile-atlas.cpp +++ b/src/tile-atlas.cpp @@ -1,4 +1,5 @@ #include "tile-atlas.hpp" +#include "compat/assert.hpp" #include #include #include diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp index b32a70d8..af9e505b 100644 --- a/src/tile-atlas.hpp +++ b/src/tile-atlas.hpp @@ -1,10 +1,11 @@ #pragma once -#include "compat/assert.hpp" #include #include #include #include +namespace std::filesystem { class path; } + namespace Magnum::Examples { struct tile_atlas final diff --git a/src/wall-mesh.cpp b/src/wall-mesh.cpp index ee998e63..ebbfee75 100644 --- a/src/wall-mesh.cpp +++ b/src/wall-mesh.cpp @@ -51,7 +51,7 @@ void wall_mesh::draw(tile_shader& shader, chunk& c) c.foreach_tile([&](tile& x, std::size_t, local_coords pt) { maybe_add_tile(data, textures, pos, x, pt); }); - _vertex_buffer.setSubData(0, Containers::arrayView(data.data(), pos)); + _vertex_buffer.setSubData(0, {data.data(), pos}); } const GL::RectangleTexture* last_texture = nullptr; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..2bec4a7c --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,8 @@ +set(self "${PROJECT_NAME}-test") +file(GLOB sources "*.cpp" CONFIGURE_ARGS) + +link_libraries(${PROJECT_NAME}) +link_libraries(Magnum::Application Magnum::Trade) + +add_executable(${self} "${sources}" "../main/loader-impl.cpp") +install(TARGETS ${self} RUNTIME DESTINATION bin) diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 00000000..f7014255 --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,69 @@ +#include +#include +#include "compat/assert.hpp" +#include "tile-atlas.hpp" +#include "serialize/tile-atlas.hpp" +#include "serialize/json-helper.hpp" +#include "loader.hpp" +#include "serialize/magnum-vector.hpp" + +namespace Magnum::Examples { + +struct app final : Platform::Application +{ + using dpi_policy = Platform::Implementation::Sdl2DpiScalingPolicy; + + explicit app(const Arguments& arguments); + ~app(); + void drawEvent() override; + void test(); +}; + +app::app(const Arguments& arguments): + Platform::Application{ + arguments, + Configuration{} + .setTitle("Test") + .setSize({1024, 768}, dpi_policy::Physical), + GLConfiguration{} + .setSampleCount(4) + //.setFlags(Platform::Sdl2Application::GLConfiguration::Flag::Debug) + } +{ +} + +app::~app() +{ + loader_::destroy(); +} + +void app::drawEvent() +{ + test(); + Platform::Sdl2Application::exit(0); +} + +void app::test() // NOLINT(readability-convert-member-functions-to-static) +{ + auto atlas = loader.tile_atlas("../share/game/images/metal1.tga", {2, 2}); + bool ret = json_helper>::to_json(atlas, "f:/dev/game/build/test/atlas.json"); + ASSERT(ret); +} + +} // namespace Magnum::Examples + +using namespace Magnum::Examples; + +MAGNUM_APPLICATION_MAIN(Magnum::Examples::app) + +#ifdef _MSC_VER +# include +# ifdef __clang__ +# pragma clang diagnostic ignored "-Wmissing-prototypes" +# pragma clang diagnostic ignored "-Wmain" +# endif + +extern "C" int __stdcall WinMain(void*, void*, void*, int /* nCmdShow */) { + return main(__argc, __argv); +} +#endif -- cgit v1.2.3