Skip to content

Commit

Permalink
Emit tilesets as embedded resources in map scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Oct 7, 2024
1 parent bfa1323 commit a8225c0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ struct Gd3TileAtlas final

struct Gd3Tileset final
{
SubResourceId id;
Gd3Metadata meta;
Gd3Resources resources;
std::vector<Gd3TileAtlas> atlases;
};

Expand Down
24 changes: 7 additions & 17 deletions source/godot_tscn_format/lib/src/gd3_document_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ auto _approximate_ellipse_as_polygon(const Float2 radius, const std::size_t poin
const auto n = static_cast<float>(point_count);

for (auto index = 0_uz; index < point_count; ++index) {
const auto theta = static_cast<float>(index) / n * std::numbers::pi * 2.0f;
const auto theta = static_cast<float>(index) / n * std::numbers::pi_v<float> * 2.0f;

auto& point = points.emplace_back();
point.set_x(radius.x() * std::cos(theta));
Expand Down Expand Up @@ -263,20 +263,13 @@ auto Gd3DocumentConverter::visit(const IMapView& map) -> std::expected<void, std
m_map.resources.next_ext_resource_id = 1;
m_map.resources.next_sub_resource_id = 1;

m_map.tileset.resources.next_ext_resource_id = 1;
m_map.tileset.resources.next_sub_resource_id = 1;

m_map.tileset.id = m_map.resources.next_sub_resource_id++;
m_map.sprite_frames.id = m_map.resources.next_sub_resource_id++;

m_map.tile_size = map.get_tile_size();
m_map.tileset_id = m_map.resources.next_ext_resource_id++;
m_map.meta = _convert_meta(map.get_meta());

m_map.resources.ext_resources[m_map.tileset_id] = Gd3ExtResource {
.path = "res://tileset.tres", // TODO setting
.type = "TileSet",
};

return {};
}

Expand Down Expand Up @@ -392,27 +385,24 @@ auto Gd3DocumentConverter::visit(const ITilesetView& tileset)
{
const auto& source_image_path = tileset.get_image_path();

const auto texture_id = m_map.tileset.resources.next_ext_resource_id++;
const auto texture_id = m_map.resources.next_ext_resource_id++;
const auto texture_image_name = source_image_path.filename().string();

const Gd3ExtResource texture_resource {
.path = std::format("res://{}", texture_image_name),
.type = "Texture",
};

const auto texture_res_id_in_map = m_map.resources.next_ext_resource_id++;
m_map.resources.ext_resources[texture_res_id_in_map] = texture_resource;
m_map.tileset_texture_ids[tileset.get_first_tile_id()] = texture_res_id_in_map;

m_map.tileset.resources.ext_resources[texture_id] = texture_resource;
m_map.resources.ext_resources[texture_id] = texture_resource;
m_map.tileset_texture_ids[tileset.get_first_tile_id()] = texture_id;

auto& gd_tile_atlas = m_map.tileset.atlases.emplace_back();
gd_tile_atlas.name = _escape_name(tileset.get_meta().get_name());
gd_tile_atlas.image_path = source_image_path;
gd_tile_atlas.texture_id = texture_id;
gd_tile_atlas.first_tile_id = tileset.get_first_tile_id();
gd_tile_atlas.tile_count = tileset.tile_count();
gd_tile_atlas.column_count = tileset.column_count();
gd_tile_atlas.tile_count = static_cast<std::int32_t>(tileset.tile_count());
gd_tile_atlas.column_count = static_cast<std::int32_t>(tileset.column_count());
gd_tile_atlas.tile_size = tileset.get_tile_size();
gd_tile_atlas.image_size = tileset.get_image_size();

Expand Down
61 changes: 19 additions & 42 deletions source/godot_tscn_format/lib/src/gd3_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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<void>
{
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<std::size_t>(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<void>
Expand Down Expand Up @@ -526,10 +504,9 @@ auto _save_tileset_images(const Gd3Tileset& tileset,
auto save_godot3_scene(const Gd3Map& map,
const SaveFormatWriteOptions& options) -> Result<void>
{
// 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

0 comments on commit a8225c0

Please sign in to comment.