Skip to content

Instantly share code, notes, and snippets.

@ark-tik
Created July 4, 2022 07:49
Show Gist options
  • Save ark-tik/45d0daf96a600026a378d7823c2d0c40 to your computer and use it in GitHub Desktop.
Save ark-tik/45d0daf96a600026a378d7823c2d0c40 to your computer and use it in GitHub Desktop.
// code truncated...
class TMXMapLoader : public pugi::xml_tree_walker {
// code truncated...
private:
// code truncated...
virtual bool for_each(pugi::xml_node &xmlNode) {
// code truncated...
// only visit tileset/background in first iteration, when visited attribute is not set
if(resType == "background" && xmlNode.attribute("visited").as_bool() == false) {
// code truncated...
}
else if(resType == "room") {
// code truncated...
// correct width and height, convert from no. of tiles to no. of pixels
resNode->mutable_room()->set_width(resNode->room().width() * resNode->room().hsnap());
resNode->mutable_room()->set_height(resNode->room().height() * resNode->room().vsnap());
}
return true;
}
// code truncated...
void PackRes(pugi::xml_node &xmlNode, google::protobuf::Message *m, const std::string& resType, std::string fieldPrefix = "") {
const google::protobuf::Descriptor *desc = m->GetDescriptor();
const google::protobuf::Reflection *refl = m->GetReflection();
for (int i = 0; i < desc->field_count(); i++) {
// code truncated...
else if(field->is_repeated()) {
switch(field->cpp_type()) {
case CppType::CPPTYPE_MESSAGE: {
// only visit repeated message "tiles/tile" when first traversal is done, as we need to create backgrounds before referencing them
if(resType == "room" && xmlNode.attribute("visited").as_bool() == true && field->name() == "tiles") {
// code truncated...
}
break;
}
}
}
// code truncated...
}
}
// code truncated...
};
// code truncated...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment