diff --git a/source/godot_tscn_format/lib/inc/tactile/godot_tscn_format/gd3_types.hpp b/source/godot_tscn_format/lib/inc/tactile/godot_tscn_format/gd3_types.hpp index d7e81f9273..6d305ebc5d 100644 --- a/source/godot_tscn_format/lib/inc/tactile/godot_tscn_format/gd3_types.hpp +++ b/source/godot_tscn_format/lib/inc/tactile/godot_tscn_format/gd3_types.hpp @@ -161,8 +161,8 @@ struct Gd3TileAtlas final struct Gd3Tileset final { + SubResourceId id; Gd3Metadata meta; - Gd3Resources resources; std::vector atlases; }; diff --git a/source/godot_tscn_format/lib/src/gd3_document_converter.cpp b/source/godot_tscn_format/lib/src/gd3_document_converter.cpp index 52259f1fd3..e90fe42c8d 100644 --- a/source/godot_tscn_format/lib/src/gd3_document_converter.cpp +++ b/source/godot_tscn_format/lib/src/gd3_document_converter.cpp @@ -71,7 +71,7 @@ auto _approximate_ellipse_as_polygon(const Float2 radius, const std::size_t poin const auto n = static_cast(point_count); for (auto index = 0_uz; index < point_count; ++index) { - const auto theta = static_cast(index) / n * std::numbers::pi * 2.0f; + const auto theta = static_cast(index) / n * std::numbers::pi_v * 2.0f; auto& point = points.emplace_back(); point.set_x(radius.x() * std::cos(theta)); @@ -263,20 +263,13 @@ auto Gd3DocumentConverter::visit(const IMapView& map) -> std::expected(tileset.tile_count()); + gd_tile_atlas.column_count = static_cast(tileset.column_count()); gd_tile_atlas.tile_size = tileset.get_tile_size(); gd_tile_atlas.image_size = tileset.get_image_size(); diff --git a/source/godot_tscn_format/lib/src/gd3_exporter.cpp b/source/godot_tscn_format/lib/src/gd3_exporter.cpp index 05284146a7..0aae890e6c 100644 --- a/source/godot_tscn_format/lib/src/gd3_exporter.cpp +++ b/source/godot_tscn_format/lib/src/gd3_exporter.cpp @@ -411,6 +411,19 @@ void _emit_layer(Gd3SceneWriter& writer, } } +void _emit_tileset(Gd3SceneWriter& writer, const Gd3Tileset& tileset) +{ + writer.newline().sub_resource_header("TileSet", tileset.id); + + _emit_metadata(writer, tileset.meta); + + std::size_t tileset_index = 0; + for (const auto& tile_atlas : tileset.atlases) { + _emit_tile_atlas(writer, tile_atlas, tileset_index); + ++tileset_index; + } +} + void _emit_resources(Gd3SceneWriter& writer, const Gd3Resources& resources) { for (const auto& [id, resource] : resources.ext_resources) { @@ -441,14 +454,15 @@ auto _emit_map_file(const Gd3Map& map, const SaveFormatWriteOptions& options) -> _emit_atlas_texture(writer, id, texture); } - _emit_sprite_frames(writer, map.sprite_frames); - for (const auto& [id, shape] : map.rect_shapes) { writer.newline() .sub_resource_header("RectangleShape2D", id) .vector2_variable("extents", shape.extents); } + _emit_sprite_frames(writer, map.sprite_frames); + _emit_tileset(writer, map.tileset); + writer.newline().node_header("Root", "Node2D"); _emit_metadata(writer, map.meta); @@ -459,42 +473,6 @@ auto _emit_map_file(const Gd3Map& map, const SaveFormatWriteOptions& options) -> return kOK; } -[[nodiscard]] -auto _emit_tileset_file(const Gd3Tileset& tileset, - const SaveFormatWriteOptions& options) -> Result -{ - const auto path = options.base_dir / "tileset.tres"; - log(LogLevel::kDebug, "Generating tileset resource '{}'", path.string()); - - std::ofstream stream {path, std::ios::out | std::ios::trunc}; - if (!stream.good()) { - return std::unexpected {std::make_error_code(std::errc::io_error)}; - } - - Gd3SceneWriter writer {stream}; - - const auto load_steps = - tileset.resources.ext_resources.size() + - saturate_cast(tileset.resources.next_sub_resource_id - 1); - writer.gd_resource_header("TileSet", load_steps); - - _emit_metadata(writer, tileset.meta); - - _emit_resources(writer, tileset.resources); - - writer.newline().resource_header(); - - std::size_t tileset_index = 0; - for (const auto& tile_atlas : tileset.atlases) { - _emit_tile_atlas(writer, tile_atlas, tileset_index); - ++tileset_index; - } - - writer.set_key_prefix(""); - - return kOK; -} - [[nodiscard]] auto _save_tileset_images(const Gd3Tileset& tileset, const SaveFormatWriteOptions& options) -> Result @@ -526,10 +504,9 @@ auto _save_tileset_images(const Gd3Tileset& tileset, auto save_godot3_scene(const Gd3Map& map, const SaveFormatWriteOptions& options) -> Result { - // TODO put tileset in map as subresource - return _save_tileset_images(map.tileset, options) - .and_then([&] { return _emit_tileset_file(map.tileset, options); }) - .and_then([&] { return _emit_map_file(map, options); }); + return _save_tileset_images(map.tileset, options).and_then([&] { + return _emit_map_file(map, options); + }); } } // namespace tactile::godot