티스토리 뷰

XMLDeclaration : XML의 버전, 즉 처음 생기는 <?xml version="1.0" encoding="UTF-8"?>를 생성하는 클래스 인스턴스

XMLElement : 실질적으로 각 노드 및 데이터 입력 공간을 생성할 수 있다. 이 공간에 데이터를 넣거나, 텍스트를 넣을 수 있다.

 

파일 생성하는 부분

void ResourcesManager::WriteResource(wstring filePath)
{
	MakeRandomBallers();

	wstring finalPath = _resourcePath + filePath + L".xml";
	auto path = filesystem::path(finalPath);

	filesystem::create_directory(path.parent_path());

	string forder = path.parent_path().string();

	shared_ptr<tinyxml2::XMLDocument> document = make_shared<tinyxml2::XMLDocument>();

	XMLDeclaration* decl = document->NewDeclaration();
	document->LinkEndChild(decl);

	XMLElement* root = document->NewElement("Ballers");
	document->LinkEndChild(root);

	for(ResourceRef resouce : _resources){
		XMLElement* node = document->NewElement("Baller");
		root->LinkEndChild(node);

		XMLElement* element = document->NewElement("Type");
		element->SetAttribute("Type", ResourceType::BALLER);
		node->LinkEndChild(element);

		element = document->NewElement("Position");
		Vec3 transform = resouce->GetTransform();
		element->SetAttribute("X", transform.x);
		element->SetAttribute("Y", transform.y);
		element->SetAttribute("Z", transform.z);
		node->LinkEndChild(element);
	}

	cout << "Success" << endl;

	document->SaveFile(Utils::ToString(finalPath).c_str());
}

 

생성하는 파일을 읽어들이는 부분

void ResourcesManager::ReadResource(wstring filePath)
{
	wstring finalPath = _resourcePath + filePath + L".xml";
	auto path = filesystem::path(finalPath);

	shared_ptr<tinyxml2::XMLDocument> document = make_shared<tinyxml2::XMLDocument>();

	XMLError error = document->LoadFile(path.string().c_str());
	if (error != XML_SUCCESS)
		cout << "Load Failed" << endl;

	//NewDeclaration
	XMLElement* root = document->FirstChildElement();
	XMLElement* bellerNode = root->FirstChildElement();

	while (bellerNode)
	{
		XMLElement* node = nullptr;
		ResourceRef resource = nullptr;

		node = bellerNode->FirstChildElement();
		{
			int32 type = node->IntAttribute("Type");
			switch (type)
			{
			case ResourceType::BALLER:
				resource = make_shared<Baller>();
				break;
			default:
				break;
			}
		}
		if (resource != nullptr) {
			node = node->NextSiblingElement();
			{
				Vec3 pos;
				pos.x = node->FloatAttribute("X");
				pos.y = node->FloatAttribute("Y");
				pos.z = node->FloatAttribute("Z");

				resource->SetTransform(pos);
			}
			_resources.push_back(resource);
		}
		bellerNode = bellerNode->NextSiblingElement();
	}
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함