diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-04 22:08:26 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-05 01:04:43 +0100 |
commit | 921e0f47a826e88183ba7c328893826e41d36c6c (patch) | |
tree | 73ab796b168ebe308f27c72290d1c14733e3afe7 | |
parent | 3c7cfbc50ce21589b192e1e6d121b2e6df60a7f3 (diff) |
serialize: use c++14 std::string literals
-rw-r--r-- | serialize/anim.cpp | 108 | ||||
-rw-r--r-- | serialize/scenery.cpp | 33 | ||||
-rw-r--r-- | serialize/tile-atlas.cpp | 7 | ||||
-rw-r--r-- | serialize/wall-atlas.cpp | 41 |
4 files changed, 97 insertions, 92 deletions
diff --git a/serialize/anim.cpp b/serialize/anim.cpp index 8d8bc2bb..bea7f3ce 100644 --- a/serialize/anim.cpp +++ b/serialize/anim.cpp @@ -7,6 +7,8 @@ namespace floormat { +using namespace std::string_literals; + //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_frame, ground, offset, size) //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_group, name, frames, ground, offset) //NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_def, object_name, anim_name, pixel_size, nframes, actionframe, fps, groups, scale) @@ -16,106 +18,106 @@ static void to_json(nlohmann::json& j, const anim_frame& val) constexpr anim_frame def; if (val.ground != def.ground) - j["ground"] = val.ground; + j["ground"s] = val.ground; if (val.offset != def.offset) - j["offset"] = val.offset; + j["offset"s] = val.offset; if (val.size != def.size) - j["size"] = val.size; + j["size"s] = val.size; } static void from_json(const nlohmann::json& j, anim_frame& val) { val = {}; - if (j.contains("ground")) - val.ground = j["ground"]; - if (j.contains("offset")) - val.offset = j["offset"]; - if (j.contains("size")) - val.size = j["size"]; + if (j.contains("ground"s)) + val.ground = j["ground"s]; + if (j.contains("offset"s)) + val.offset = j["offset"s]; + if (j.contains("size"s)) + val.size = j["size"s]; } static void to_json(nlohmann::json& j, const anim_group& val) { const anim_group def{}; - j["name"] = val.name; + j["name"s] = val.name; if (val.mirror_from) - j["mirror-from"] = val.mirror_from; + j["mirror-from"s] = val.mirror_from; else - j["frames"] = val.frames; + j["frames"s] = val.frames; if (val.ground != def.ground) - j["ground"] = val.ground; + j["ground"s] = val.ground; if (val.offset != def.offset) - j["offset"] = val.offset; + j["offset"s] = val.offset; if (val.z_offset != def.z_offset) - j["z-offset"] = val.z_offset; + j["z-offset"s] = val.z_offset; if (val.depth_offset != def.depth_offset) - j["depth-offset"] = val.depth_offset; + j["depth-offset"s] = val.depth_offset; } static void from_json(const nlohmann::json& j, anim_group& val) { val = {}; - val.name = j["name"]; + val.name = j["name"s]; fm_soft_assert(!val.name.isEmpty()); - if (j.contains("mirror-from")) + if (j.contains("mirror-from"s)) { - fm_soft_assert(!j.contains("frames")); - val.mirror_from = j["mirror-from"]; + fm_soft_assert(!j.contains("frames"s)); + val.mirror_from = j["mirror-from"s]; } else - val.frames = j["frames"]; - if (j.contains("ground")) - val.ground = j["ground"]; - if (j.contains("offset")) - val.offset = j["offset"]; - if (j.contains("z-offset")) - val.z_offset = j["z-offset"]; - if (j.contains("depth-offset")) - val.depth_offset = j["depth-offset"]; + val.frames = j["frames"s]; + if (j.contains("ground"s)) + val.ground = j["ground"s]; + if (j.contains("offset"s)) + val.offset = j["offset"s]; + if (j.contains("z-offset"s)) + val.z_offset = j["z-offset"s]; + if (j.contains("depth-offset"s)) + val.depth_offset = j["depth-offset"s]; } static void to_json(nlohmann::json& j, const anim_def& val) { const anim_def def{}; - j["object_name"] = val.object_name; + j["object_name"s] = val.object_name; if (val.anim_name != def.anim_name) - j["anim_name"] = val.anim_name; + j["anim_name"s] = val.anim_name; if (val.pixel_size != def.pixel_size) - j["pixel_size"] = val.pixel_size; + j["pixel_size"s] = val.pixel_size; if (val.nframes != def.nframes) - j["nframes"] = val.nframes; + j["nframes"s] = val.nframes; if (val.action_frame != def.action_frame) - j["action-frame"] = val.action_frame; + j["action-frame"s] = val.action_frame; if (val.action_frame2 != def.action_frame2) - j["action-frame-2"] = val.action_frame2; + j["action-frame-2"s] = val.action_frame2; if (val.fps != def.fps) - j["fps"] = val.fps; - j["groups"] = val.groups; - j["scale"] = val.scale; + j["fps"s] = val.fps; + j["groups"s] = val.groups; + j["scale"s] = val.scale; } static void from_json(const nlohmann::json& j, anim_def& val) { val = {}; - val.object_name = j["object_name"]; + val.object_name = j["object_name"s]; fm_soft_assert(!val.object_name.isEmpty()); - if (j.contains("anim_name")) // todo underscore to hyphen - val.anim_name = j["anim_name"]; - if (j.contains("pixel_size")) - val.pixel_size = j["pixel_size"]; - if (j.contains("nframes")) - val.nframes = j["nframes"]; - if (j.contains("action-frame")) - val.action_frame = j["action-frame"]; - if (j.contains("action-frame-2")) - val.action_frame2 = j["action-frame-2"]; - if (j.contains("fps")) - val.fps = j["fps"]; - val.groups = j["groups"]; + if (j.contains("anim_name"s)) // todo underscore to hyphen + val.anim_name = j["anim_name"s]; + if (j.contains("pixel_size"s)) + val.pixel_size = j["pixel_size"s]; + if (j.contains("nframes"s)) + val.nframes = j["nframes"s]; + if (j.contains("action-frame"s)) + val.action_frame = j["action-frame"s]; + if (j.contains("action-frame-2"s)) + val.action_frame2 = j["action-frame-2"s]; + if (j.contains("fps"s)) + val.fps = j["fps"s]; + val.groups = j["groups"s]; fm_soft_assert(!val.groups.empty()); - val.scale = j["scale"]; + val.scale = j["scale"s]; fm_soft_assert(val.scale.type != anim_scale_type::invalid); } diff --git a/serialize/scenery.cpp b/serialize/scenery.cpp index 8519fd49..820c67af 100644 --- a/serialize/scenery.cpp +++ b/serialize/scenery.cpp @@ -13,6 +13,7 @@ namespace { using namespace floormat; +using namespace std::string_literals; constexpr struct { scenery_type value = scenery_type::none; @@ -85,24 +86,24 @@ void adl_serializer<scenery_proto>::to_json(json& j, const scenery_proto& f) fm_assert(f.atlas); const scenery_proto default_scenery; if (f.type != default_scenery.type) - j["type"] = f.type; - j["atlas-name"] = f.atlas->name(); + j["type"s] = f.type; + j["atlas-name"s] = f.atlas->name(); if (f.frame != default_scenery.frame) - j["frame"] = f.frame; + j["frame"s] = f.frame; if (f.r != default_scenery.r) - j["rotation"] = f.r; + j["rotation"s] = f.r; if (f.pass != default_scenery.pass) - j["pass-mode"] = f.pass; + j["pass-mode"s] = f.pass; if (f.active != default_scenery.active) - j["active"] = f.active; + j["active"s] = f.active; if (f.interactive != default_scenery.interactive) - j["interactive"] = f.interactive; + j["interactive"s] = f.interactive; if (f.offset != default_scenery.offset) - j["offset"] = Vector2i(f.offset); + j["offset"s] = Vector2i(f.offset); if (f.bbox_offset != default_scenery.bbox_offset) - j["bbox-offset"] = Vector2i(f.bbox_offset); + j["bbox-offset"s] = Vector2i(f.bbox_offset); if (f.bbox_size != default_scenery.bbox_size) - j["bbox-size"] = Vector2ui(f.bbox_size); + j["bbox-size"s] = Vector2ui(f.bbox_size); } void adl_serializer<scenery_proto>::from_json(const json& j, scenery_proto& f) @@ -114,7 +115,7 @@ void adl_serializer<scenery_proto>::from_json(const json& j, scenery_proto& f) value = j[s]; }; - StringView atlas_name = j["atlas-name"]; + StringView atlas_name = j["atlas-name"s]; fm_soft_assert(!atlas_name.isEmpty()); f = {}; f.atlas = loader.anim_atlas(atlas_name, loader_::SCENERY_PATH); @@ -169,17 +170,17 @@ void adl_serializer<serialized_scenery>::to_json(json& j, const serialized_scene fm_soft_assert(val.proto.atlas); j = val.proto; const auto name = !val.name.isEmpty() ? StringView{val.name} : val.proto.atlas->name(); - j["name"] = name; - j["description"] = val.descr; + j["name"s] = name; + j["description"s] = val.descr; } void adl_serializer<serialized_scenery>::from_json(const json& j, serialized_scenery& val) { val = {}; val.proto = j; - val.name = j["name"]; - if (j.contains("description")) - val.descr = j["description"]; + val.name = j["name"s]; + if (j.contains("description"s)) + val.descr = j["description"s]; } } // namespace nlohmann diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index 57958d63..3f619dd1 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -10,6 +10,7 @@ #include <nlohmann/json.hpp> using namespace floormat; +using namespace std::string_literals; namespace { @@ -34,7 +35,7 @@ void adl_serializer<std::shared_ptr<tile_atlas>>::to_json(json& j, const std::sh { to_json(j, proxy{x->name(), x->num_tiles2(), NullOpt}); if (auto p = x->pass_mode()) - j["pass-mode"] = *p; + j["pass-mode"s] = *p; } } @@ -48,8 +49,8 @@ void adl_serializer<std::shared_ptr<tile_atlas>>::from_json(const json& j, std:: proxy x; from_json(j, x); Optional<pass_mode> p; - if (j.contains("pass-mode")) - p = {InPlaceInit, j["pass-mode"]}; + if (j.contains("pass-mode"s)) + p = {InPlaceInit, j["pass-mode"s]}; val = loader.tile_atlas(x.name, x.size, p); if (auto p2 = val->pass_mode(); p && p2 != p) { diff --git a/serialize/wall-atlas.cpp b/serialize/wall-atlas.cpp index 1bab2eb2..d8fe8f60 100644 --- a/serialize/wall-atlas.cpp +++ b/serialize/wall-atlas.cpp @@ -12,6 +12,7 @@ namespace floormat { namespace { +using namespace std::string_literals; constexpr StringView rotation_names[] = { "n"_s, "e"_s, "s"_s, "w"_s, }; size_t rotation_from_name(StringView s) @@ -27,24 +28,24 @@ size_t rotation_from_name(StringView s) void read_frameset_metadata(const nlohmann::json& j, wall_frames& val) { - if (j.contains("pixel-size")) - val.pixel_size = j["pixel-size"]; - if (j.contains("tint")) + if (j.contains("pixel-size"s)) + val.pixel_size = j["pixel-size"s]; + if (j.contains("tint"s)) { - auto& t = j["tint"]; - fm_soft_assert(t.contains("mult") || t.contains("add")); - if (t.contains("mult")) - val.tint_mult = Vector4(t["mult"]); - if (t.contains("add")) - val.tint_add = Vector3(t["add"]); + auto& t = j["tint"s]; + fm_soft_assert(t.contains("mult"s) || t.contains("add"s)); + if (t.contains("mult"s)) + val.tint_mult = Vector4(t["mult"s]); + if (t.contains("add"s)) + val.tint_add = Vector3(t["add"s]); fm_soft_assert(val.tint_mult >= Color4{0}); } - if (j.contains("from-rotation")) - val.from_rotation = (uint8_t)rotation_from_name(std::string{j["from-rotation"]}); - if (j.contains("mirrored")) - val.mirrored = j["mirrored"]; - if (j.contains("use-default-tint")) - val.use_default_tint = j["use-default-tint"]; + if (j.contains("from-rotation"s)) + val.from_rotation = (uint8_t)rotation_from_name(std::string{j["from-rotation"s]}); + if (j.contains("mirrored"s)) + val.mirrored = j["mirrored"s]; + if (j.contains("use-default-tint"s)) + val.use_default_tint = j["use-default-tint"s]; } void write_frameset_metadata(nlohmann::json& j, const wall_atlas& a, const wall_frames& val) @@ -58,22 +59,22 @@ void write_frameset_metadata(nlohmann::json& j, const wall_atlas& a, const wall_ fm_soft_assert(val.index < a.array().size()); fm_soft_assert(val.count != (uint32_t)-1 && val.count > 0); - j["pixel-size"] = val.pixel_size; + j["pixel-size"s] = val.pixel_size; if (val.tint_mult != default_value.tint_mult || val.tint_add != default_value.tint_add) { auto tint = std::pair<Vector4, Vector3>{{val.tint_mult}, {val.tint_add}}; - j["tint"] = tint; + j["tint"s] = tint; } if (val.from_rotation != default_value.from_rotation) { fm_soft_assert(val.from_rotation != (uint8_t)-1 && val.from_rotation < 4); - j["from-rotation"] = val.from_rotation; + j["from-rotation"s] = val.from_rotation; } if (val.mirrored != default_value.mirrored) - j["mirrored"] = val.mirrored; + j["mirrored"s] = val.mirrored; if (val.use_default_tint) if (val.tint_mult != default_value.tint_mult || val.tint_add != default_value.tint_add) - j["use-default-tint"] = true; + j["use-default-tint"s] = true; } } // namespace |