Skip to content

Commit e4ea548

Browse files
committed
add parsing infinite maps with csv encoding
1 parent d221503 commit e4ea548

2 files changed

Lines changed: 71 additions & 17 deletions

File tree

ParseTest/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main()
3636
{
3737
tmx::Map map;
3838

39-
if (map.load("maps/platform.tmx"))
39+
if (map.load("maps/csv-untitled.tmx"))
4040
{
4141
std::cout << "Loaded Map version: " << map.getVersion().upper << ", " << map.getVersion().lower << std::endl;
4242
if (map.isInfinite())

tmxlite/src/TileLayer.cpp

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,84 @@ void TileLayer::parseBase64(const pugi::xml_node& node)
174174

175175
void TileLayer::parseCSV(const pugi::xml_node& node)
176176
{
177+
auto processDataString = [](const std::string dataString)->std::vector<std::uint32_t>
178+
{
179+
std::vector<std::uint32_t> IDs;
180+
181+
std::stringstream dataStream(dataString);
182+
std::uint32_t i = 0;
183+
while (dataStream >> i)
184+
{
185+
IDs.push_back(i);
186+
//TODO this shouldn't assume the first character
187+
//is a valid value, and it should ignore anything non-numeric.
188+
if (dataStream.peek() == ',')
189+
{
190+
dataStream.ignore();
191+
}
192+
}
193+
194+
return IDs;
195+
};
196+
177197
std::string data = node.text().as_string();
178198
if (data.empty())
179199
{
180-
Logger::log("Layer " + getName() + " has no layer data. Layer skipped.", Logger::Type::Error);
181-
return;
182-
}
200+
//check for chunk nodes
201+
auto dataCount = 0;
202+
for (const auto& childNode : node.children())
203+
{
204+
std::string childName = childNode.name();
205+
if (childName == "chunk")
206+
{
207+
std::string dataString = childNode.text().as_string();
208+
if (!dataString.empty())
209+
{
210+
Chunk chunk;
211+
chunk.position.x = childNode.attribute("x").as_int();
212+
chunk.position.y = childNode.attribute("y").as_int();
183213

184-
std::vector<std::uint32_t> IDs;
185-
IDs.reserve(m_tileCount);
214+
chunk.size.x = childNode.attribute("width").as_int();
215+
chunk.size.y = childNode.attribute("height").as_int();
186216

187-
std::stringstream dataStream(data);
188-
std::uint32_t i;
189-
while (dataStream >> i)
190-
{
191-
IDs.push_back(i);
192-
//TODO this shouldn't assume the first character
193-
//is a valid value, and it should ignore anything non-numeric.
194-
if (dataStream.peek() == ',')
217+
auto IDs = processDataString(dataString);
218+
219+
if (!IDs.empty())
220+
{
221+
createTiles(IDs, chunk.tiles);
222+
m_chunks.push_back(chunk);
223+
dataCount++;
224+
}
225+
}
226+
}
227+
}
228+
229+
if (dataCount == 0)
195230
{
196-
dataStream.ignore();
231+
Logger::log("Layer " + getName() + " has no layer data. Layer skipped.", Logger::Type::Error);
232+
return;
197233
}
198234
}
199-
200-
createTiles(IDs, m_tiles);
235+
else
236+
{
237+
//std::vector<std::uint32_t> IDs;
238+
//IDs.reserve(m_tileCount);
239+
240+
//std::stringstream dataStream(data);
241+
//std::uint32_t i;
242+
//while (dataStream >> i)
243+
//{
244+
// IDs.push_back(i);
245+
// //TODO this shouldn't assume the first character
246+
// //is a valid value, and it should ignore anything non-numeric.
247+
// if (dataStream.peek() == ',')
248+
// {
249+
// dataStream.ignore();
250+
// }
251+
//}
252+
253+
createTiles(processDataString(data), m_tiles);
254+
}
201255
}
202256

203257
void TileLayer::parseUnencoded(const pugi::xml_node& node)

0 commit comments

Comments
 (0)