diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | compat/assert.hpp | 39 | ||||
-rw-r--r-- | main/main.cpp | 2 | ||||
-rw-r--r-- | serialize/anim.cpp | 4 | ||||
-rw-r--r-- | serialize/json-helper.hpp (renamed from serialize/helper.hpp) | 2 | ||||
-rw-r--r-- | serialize/magnum-vector.hpp | 51 | ||||
-rw-r--r-- | serialize/magnum-vector2i.hpp | 42 | ||||
-rw-r--r-- | serialize/tile-atlas.cpp | 2 | ||||
-rw-r--r-- | serialize/tile-atlas.hpp | 1 | ||||
-rw-r--r-- | src/tile-atlas.cpp | 1 | ||||
-rw-r--r-- | src/tile-atlas.hpp | 3 | ||||
-rw-r--r-- | src/wall-mesh.cpp | 2 | ||||
-rw-r--r-- | test/CMakeLists.txt | 8 | ||||
-rw-r--r-- | test/main.cpp | 69 |
14 files changed, 165 insertions, 62 deletions
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 <bitset> @@ -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 <tuple> diff --git a/serialize/helper.hpp b/serialize/json-helper.hpp index f16ed60c..af6fd211 100644 --- a/serialize/helper.hpp +++ b/serialize/json-helper.hpp @@ -41,7 +41,7 @@ template<typename t> bool json_helper<t>::to_json(const t& self, const std::filesystem::path& pathname) noexcept { using Corrade::Utility::Error; try { - nlohmann::json j = self; + nlohmann::json j(self); std::ofstream s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); 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 <cstdio> -#include <string> -#include <exception> +#pragma once #include <Magnum/Magnum.h> -#include <Magnum/Math/Vector2.h> +#include <Magnum/Math/Vector.h> #include <nlohmann/json.hpp> namespace nlohmann { -template<typename t> -requires std::is_integral_v<t> -struct adl_serializer<Magnum::Math::Vector2<t>> final { - static void to_json(json& j, const Magnum::Math::Vector2<t>& x); - static void from_json(const json& j, Magnum::Math::Vector2<t>& x); -}; - -template<typename t> -requires std::is_integral_v<t> -void adl_serializer<Magnum::Math::Vector2<t>>::to_json(json& j, const Magnum::Math::Vector2<t>& val) -{ - char buf[64]; - snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]); - j = buf; -} - -template<typename t> -requires std::is_integral_v<t> -void adl_serializer<Magnum::Math::Vector2<t>>::from_json(const json& j, Magnum::Math::Vector2<t>& 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<std::size_t N, typename T> +struct adl_serializer<Magnum::Math::Vector<N, T>> final { + static void to_json(json& j, const Magnum::Math::Vector2<T>& 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<T, N> 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<T>& val) + { + std::array<T, N> 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 <cstdio> +#include <string> +#include <limits> +#include <exception> +#include <Magnum/Magnum.h> +#include <Magnum/Math/Vector2.h> +#include <nlohmann/json.hpp> + +namespace nlohmann { + +template<typename t> +requires std::is_integral_v<t> +struct adl_serializer<Magnum::Math::Vector2<t>> final { + static void to_json(json& j, const Magnum::Math::Vector2<t>& val) + { + char buf[64]; + using type = std::conditional_t<std::is_signed_v<t>, std::intmax_t, std::uintmax_t>; + constexpr auto format_string = std::is_signed_v<t> ? "%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<t>& val) + { + std::string str = j; + using type = std::conditional_t<std::is_signed_v<t>, std::intmax_t, std::uintmax_t>; + constexpr auto format_string = std::is_signed_v<t> ? "%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 <nlohmann/json.hpp> 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 <memory> #include <nlohmann/json_fwd.hpp> 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 <Corrade/Containers/StringView.h> #include <Magnum/ImageView.h> #include <Magnum/GL/TextureFormat.h> 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 <Magnum/Magnum.h> #include <Magnum/GL/RectangleTexture.h> #include <array> #include <string> +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 <Magnum/Magnum.h> +#include <Magnum/Platform/Sdl2Application.h> +#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<std::shared_ptr<tile_atlas>>::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 <cstdlib> +# 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 |