1
0
mirror of https://github.com/ScrelliCopter/tmx2gba.git synced 2025-02-21 03:29:25 +11:00

restore object reading

This commit is contained in:
2024-04-11 10:36:16 +10:00
parent 6b786d36fb
commit e6053f9472
5 changed files with 62 additions and 43 deletions

View File

@@ -198,8 +198,7 @@ int main(int argc, char** argv)
// Convert to GBA-friendly charmap data
{
std::vector<uint16_t> charDat;
if (!convert::ConvertCharmap(charDat, p.offset, p.palette, tmx))
return 1;
if (!convert::ConvertCharmap(charDat, p.offset, p.palette, tmx)) { return 1; }
// Write out charmap
outH.WriteSize(tmx.GetSize().width, tmx.GetSize().height);

View File

@@ -180,18 +180,30 @@ void TmxMap::ReadLayer(const pugi::xml_node& xNode)
mLayers.emplace_back(TmxLayer(width, height, name, std::move(tileDat)));
}
void TmxMap::ReadObjects(const pugi::xml_node& xNode)
void TmxMap::ReadObjectGroup(const pugi::xml_node& xNode)
{
for (const auto it : xNode.children("object"))
std::string_view name(xNode.value());
std::vector<TmxObject> objects;
const auto xObjects = xNode.children("object");
//mObjects.reserve(xObjects.size())
for (const auto it : xObjects)
{
int id = IntFromStr<int>(it.attribute("id").value()).value_or(0);
std::string_view name = it.attribute("name").value();
// Read position
// Read axis-aligned bounding box
auto x = FloatFromStr<float>(it.attribute("x").value()).value_or(0.0f);
auto y = FloatFromStr<float>(it.attribute("y").value()).value_or(0.0f);
auto width = FloatFromStr<float>(it.attribute("width").value()).value_or(0.0f);
auto height = FloatFromStr<float>(it.attribute("height").value()).value_or(0.0f);
mObjects.emplace_back(TmxObject(name, x, y));
objects.emplace_back(TmxObject(id, name, { x, y, width, height }));
}
if (objects.empty())
return; //FIXME: log this
mObjectGroups.emplace_back(TmxObjectGroup(name, std::move(objects)));
}
bool TmxMap::Load(const std::string& inPath)
@@ -217,7 +229,7 @@ bool TmxMap::Load(const std::string& inPath)
std::string_view name(it.name());
if (!name.compare("layer")) { ReadLayer(it); }
else if (!name.compare("tileset")) { ReadTileset(it); }
else if (!name.compare("objectgroup")) { ReadObjects(it); }
else if (!name.compare("objectgroup")) { ReadObjectGroup(it); }
}
return true;

View File

@@ -18,20 +18,21 @@ class TmxMap
{
int mWidth = 0, mHeight = 0;
std::vector<TmxLayer> mLayers;
std::vector<TmxTileset> mTilesets;
std::vector<TmxObject> mObjects;
std::vector<TmxLayer> mLayers;
std::vector<TmxTileset> mTilesets;
std::vector<TmxObjectGroup> mObjectGroups;
void ReadTileset(const pugi::xml_node& xNode);
void ReadLayer(const pugi::xml_node& xNode);
void ReadObjects(const pugi::xml_node& xNode);
void ReadObjectGroup(const pugi::xml_node& xNode);
public:
[[nodiscard]] bool Load(const std::string& inPath);
constexpr std::pair<int, int> TileCount() const noexcept { return { mWidth, mHeight }; }
constexpr const std::vector<TmxTileset>& Tilesets() const noexcept { return mTilesets; }
constexpr const std::vector<TmxLayer>& Layers() const noexcept { return mLayers; }
[[nodiscard]] constexpr std::pair<int, int> TileCount() const noexcept { return { mWidth, mHeight }; }
[[nodiscard]] constexpr const std::vector<TmxTileset>& Tilesets() const noexcept { return mTilesets; }
[[nodiscard]] constexpr const std::vector<TmxLayer>& Layers() const noexcept { return mLayers; }
[[nodiscard]] constexpr const std::vector<TmxObjectGroup>& ObjectGroups() const noexcept { return mObjectGroups; }
};
#endif//TMXMAP_HPP

View File

@@ -5,22 +5,39 @@
#define TMXOBJECT_HPP
#include <string>
#include <string_view>
#include <vector>
#include <utility>
class TmxObject
{
public:
TmxObject(std::string_view name, float x, float y) : mName(name), mPos{ x, y } {}
template <typename T>
struct Position { T x, y; };
struct AABB { T x, y, w, h; };
TmxObject(int id, std::string_view name, AABB<float>&& box) : mId(id), mName(name), mBox(std::move(box)) {}
constexpr int Id() const noexcept { return mId; }
const std::string_view Name() const noexcept { return mName; }
constexpr Position<float> Pos() const noexcept { return mPos; }
constexpr const AABB<float>& Box() const noexcept { return mBox; }
private:
int mId;
std::string mName;
Position<float> mPos;
AABB<float> mBox;
};
class TmxObjectGroup
{
std::string mName;
std::vector<TmxObject> mObjects;
public:
TmxObjectGroup(std::string_view name, std::vector<TmxObject>&& objects)
: mName(name), mObjects(std::move(objects)) {}
const std::string_view Name() const noexcept { return mName; }
constexpr const std::vector<TmxObject>& Objects() const noexcept { return mObjects; }
};
#endif//TMXOBJECT_HPP

View File

@@ -21,10 +21,7 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
using std::optional;
using std::reference_wrapper;
optional<reference_wrapper<const TmxLayer>> layerGfx;
optional<reference_wrapper<const TmxLayer>> layerCls;
optional<reference_wrapper<const TmxLayer>> layerPal;
optional<reference_wrapper<std::vector<const TmxObject>>> objGroups;
optional<reference_wrapper<const TmxLayer>> layerGfx, layerCls, layerPal;
// Read layers
for (const auto& layer : map.Layers())
@@ -37,12 +34,6 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
if (!layerGfx.has_value() && (graphicsName.empty() || name == graphicsName)) { layerGfx = layer; }
if (!collisionName.empty() && !layerCls.has_value() && name == collisionName) { layerCls = layer; }
if (!paletteName.empty() && !layerPal.has_value() && name == paletteName) { layerPal = layer; }
/*
else if (!objMapping.empty() && layer->getType() == tmx::Layer::Type::Object)
{
objGroups.emplace_back(layer->getLayerAs<ObjectGroup>());
}
*/
}
// Check layers
@@ -89,31 +80,30 @@ TmxReader::Error TmxReader::Open(const std::string& inPath,
[](const auto& it) { return it.GidRange(); });
// Read objects
/*
if (!objMapping.empty())
if (!map.ObjectGroups().empty())
{
for (const auto& group : objGroups)
std::vector<Object> objs;
for (const auto& group : map.ObjectGroups())
{
const auto& tmxObjects = group.get().Objects();
v.reserve(v.size() + tmxObjects.size());
const auto& tmxObjects = group.Objects();
objs.reserve(objs.size() + tmxObjects.size());
for (const auto& tmxObj : tmxObjects)
{
auto it = objMapping.find(std::string(tmxObj.Name()));
if (it == objMapping.end())
continue;
const auto& pos = tmxObj.Pos();
Object obj;
obj.id = it->second;
obj.x = pos.x;
obj.y = pos.y;
v.emplace_back(obj);
const auto& aabb = tmxObj.Box();
objs.emplace_back(Object
{
.id = it->second,
.x = aabb.x,
.y = aabb.y
});
}
}
mObjects.emplace(v);
mObjects.emplace(objs);
}
*/
return Error::OK;
}