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 // Convert to GBA-friendly charmap data
{ {
std::vector<uint16_t> charDat; std::vector<uint16_t> charDat;
if (!convert::ConvertCharmap(charDat, p.offset, p.palette, tmx)) if (!convert::ConvertCharmap(charDat, p.offset, p.palette, tmx)) { return 1; }
return 1;
// Write out charmap // Write out charmap
outH.WriteSize(tmx.GetSize().width, tmx.GetSize().height); 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))); 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(); 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 x = FloatFromStr<float>(it.attribute("x").value()).value_or(0.0f);
auto y = FloatFromStr<float>(it.attribute("y").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) bool TmxMap::Load(const std::string& inPath)
@@ -217,7 +229,7 @@ bool TmxMap::Load(const std::string& inPath)
std::string_view name(it.name()); std::string_view name(it.name());
if (!name.compare("layer")) { ReadLayer(it); } if (!name.compare("layer")) { ReadLayer(it); }
else if (!name.compare("tileset")) { ReadTileset(it); } else if (!name.compare("tileset")) { ReadTileset(it); }
else if (!name.compare("objectgroup")) { ReadObjects(it); } else if (!name.compare("objectgroup")) { ReadObjectGroup(it); }
} }
return true; return true;

View File

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

View File

@@ -5,22 +5,39 @@
#define TMXOBJECT_HPP #define TMXOBJECT_HPP
#include <string> #include <string>
#include <string_view>
#include <vector>
#include <utility> #include <utility>
class TmxObject class TmxObject
{ {
public: public:
TmxObject(std::string_view name, float x, float y) : mName(name), mPos{ x, y } {}
template <typename T> 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; } 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: private:
int mId;
std::string mName; 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 #endif//TMXOBJECT_HPP

View File

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