Esempio n. 1
0
//===========================================
// Box2dPhysics::Box2dPhysics
//===========================================
Box2dPhysics::Box2dPhysics(Entity* entity, const XmlNode data)
   : EntityPhysics(entity, data),
     m_init(false),
     m_entity(entity),
     m_body(NULL),
     m_opts(false, false, 1.f, 0.3f) {

   init();

   try {
      XML_NODE_CHECK(data, Box2dPhysics);

      XmlAttribute attr = data.firstAttribute();
      XML_ATTR_CHECK(attr, dynamic);
      m_opts.dynamic = attr.getBool();

      attr = attr.nextAttribute();
      XML_ATTR_CHECK(attr, fixedAngle);
      m_opts.fixedAngle = attr.getBool();

      attr = attr.nextAttribute();
      XML_ATTR_CHECK(attr, density);
      m_opts.density = attr.getFloat();

      attr = attr.nextAttribute();
      XML_ATTR_CHECK(attr, friction);
      m_opts.friction = attr.getFloat();
   }
   catch (XmlException& e) {
      e.prepend("Error parsing XML for instance of class Box2dPhysics; ");
      throw;
   }
}
Esempio n. 2
0
//===========================================
// Box2dPhysics::assignData
//===========================================
void Box2dPhysics::assignData(const XmlNode data) {
   if (data.isNull() || data.name() != "Box2dPhysics") return;

   try {
      XmlAttribute attr = data.firstAttribute();
      if (!attr.isNull() && attr.name() == "dynamic") {
         m_opts.dynamic = attr.getBool();
         attr = attr.nextAttribute();
      }

      if (!attr.isNull() && attr.name() == "fixedAngle") {
         m_opts.fixedAngle = attr.getBool();
         attr = attr.nextAttribute();
      }

      if (!attr.isNull() && attr.name() == "density") {
         m_opts.density = attr.getFloat();
         attr = attr.nextAttribute();
      }

      if (!attr.isNull() && attr.name() == "friction") {
         m_opts.friction = attr.getFloat();
      }
   }
   catch (XmlException& e) {
      e.prepend("Error parsing XML for instance of class Box2dPhysics ;");
      throw;
   }
}
Esempio n. 3
0
size_t XmlSchema::getNodeMemberTypeWidth(const XmlNode* node) const
{
	size_t maxWidth = 0;
	size_t width = 0;
	NodeIterator iter;
	for (const XmlNode* child = node->getFirstChild(iter);
		child != NULL;
		child = node->getNextChild(iter))
	{
		XmlAttribute* multiple = child->findAttribute(ATTR_MULTIPLE);

		if (child->isEmpty())
		{
			//simple type
			XmlAttribute* type = child->findAttribute(ATTR_TYPE);
			if (type == NULL)
			{
				continue;
			}
			width = getSimpleTypeString(type).size();
			if (multiple != NULL && multiple->getBool())
			{
				width += Strlen(T("std::vector<>"));
			}
		}
		else
		{
			//struct type
			width = Strlen(child->getName());
			if (multiple != NULL && multiple->getBool())
			{
				width += Strlen(T("std::vector<>"));
			}
		}
		if (width > maxWidth)
		{
			maxWidth = width;
		}
	}
	return maxWidth;
}
Esempio n. 4
0
//===========================================
// Item::assignData
//===========================================
void Item::assignData(const XmlNode data) {
    try {
        XML_NODE_CHECK(data, Item);

        XmlAttribute attr = data.firstAttribute();
        if (!attr.isNull() && attr.name() == "solid")
            m_solid = attr.getBool();
    }
    catch (XmlException& e) {
        e.prepend("Error parsing XML for instance of class Item; ");
        throw;
    }
}
Esempio n. 5
0
//===========================================
// Item::Item
//===========================================
Item::Item(const XmlNode data) {
    try {
        XML_NODE_CHECK(data, Item);

        XmlAttribute attr = data.firstAttribute();
        XML_ATTR_CHECK(attr, solid);
        m_solid = attr.getBool();
    }
    catch (XmlException& e) {
        e.prepend("Error parsing XML for instance of class Item; ");
        throw;
    }
}
Esempio n. 6
0
void XmlSchema::addConstructorItem(const XmlNode* child, String& structDefinition, int& index) const
{
	XmlAttribute* type = child->findAttribute(ATTR_TYPE);
	bool recursive = (child->findAttribute(ATTR_RECURSIVE) != NULL);
	if (type == NULL && !recursive)
	{
		return;
	}
	XmlAttribute* multiple = child->findAttribute(ATTR_MULTIPLE);
	if (multiple != NULL && multiple->getBool())
	{
		//don't need to construct a vector member
		return;
	}
	XmlAttribute* defaultAttribute = child->findAttribute(ATTR_DEFAULT);
	if (type != NULL
		&& Strcmp(type->getString(), T("string")) == 0
		&& defaultAttribute == NULL)
	{
		//for String, need construction only when default value exist
		return;
	}

	if (index == 0)
	{
		structDefinition += T("	");
		structDefinition += child->getParent()->getName();
		structDefinition += T("()\r\n		:	");
	}
	else
	{
		structDefinition += T("		,	");
	}
	if (recursive)
	{
		structDefinition += T("Child");
	}
	else
	{
		structDefinition += child->getName();
	}
	structDefinition += T("(");
	++index;

	if (defaultAttribute != NULL)
	{
		if (Strcmp(type->getString(), T("string")) == 0)
		{
			structDefinition += LEFT_QUOTE;
			structDefinition += defaultAttribute->getString();
			structDefinition += T("\"");
		}
		else
		{
			structDefinition += defaultAttribute->getString();
		}
	}
	else
	{
		if (recursive)
		{
			structDefinition += T("NULL");
		}
		else
		{
			structDefinition += getTypeDefaultValue(type->getString());
		}
	}
	structDefinition += T(")\r\n");
}
Esempio n. 7
0
bool XmlSchema::generateCodeForNode(const XmlNode* node, String& headerCode, String& sourceCode) const
{
	assert(node != NULL);

	if (node->getType() == ELEMENT)
	{
		String structDefinition;
		structDefinition += T("///////////////////////////////////////////////////////////////////////////////////////////////////\r\n");
		structDefinition += T("struct ");
		structDefinition += node->getName();
		structDefinition += T("\r\n{\r\n");
		//constructor
		int index = 0;
		NodeIterator iter;
		for (XmlNode* child = node->getFirstChild(iter);
			  child != NULL;
			  child = node->getNextChild(iter))
		{
			if (child->isEmpty() && child->getType() == ELEMENT)
			{
				//simple type
				addConstructorItem(child, structDefinition, index);
			}
		}
		if (index > 0)
		{
			structDefinition += T("	{\r\n	}\r\n");
		}
		//destructor
		for (XmlNode* child = node->getFirstChild(iter);
			child != NULL;
			child = node->getNextChild(iter))
		{
			if (child->findAttribute(ATTR_RECURSIVE) != NULL
				&& child->findAttribute(ATTR_MULTIPLE) == NULL)
			{
				//recursive pointer, need to delete
				structDefinition += T("	~");
				structDefinition += node->getName();
				structDefinition += T("()\r\n	{\r\n		if (Child != NULL)\r\n		{\r\n			delete Child;\r\n");
				structDefinition += T("			Child = NULL;\r\n		}\r\n	}\r\n");
				break;	//can't have more than one
			}
		}

		structDefinition += T("	void read(const slim::XmlNode* node);\r\n	void write(slim::XmlNode* node) const;\r\n\r\n");

		String readingCode;
		readingCode += T("///////////////////////////////////////////////////////////////////////////////////////////////////\r\n");
		readingCode += T("void ");
		readingCode += node->getName();
		readingCode += T("::read(const XmlNode* node)\r\n{\r\n	assert(node != NULL);\r\n");
		readingCode += T("\r\n	NodeIterator iter;\r\n	const XmlNode* childNode = NULL;\r\n	const XmlAttribute* attribute = NULL;\r\n");

		String writingCode;
		writingCode += T("///////////////////////////////////////////////////////////////////////////////////////////////////\r\n");
		writingCode += T("void ");
		writingCode += node->getName();
		writingCode += T("::write(XmlNode* node) const\r\n{\r\n	assert(node != NULL);\r\n\r\n	node->clearChild();\r\n	node->clearAttribute();");
		writingCode += T("\r\n\r\n	XmlNode* childNode = NULL;\r\n	XmlAttribute* attribute = NULL;\r\n");

		size_t typeWidth = getNodeMemberTypeWidth(node);

		for (const XmlNode* child = node->getFirstChild(iter);
			  child != NULL;
			  child = node->getNextChild(iter))
		{
			if (child->getType() != ELEMENT)
			{
				continue;
			}
			XmlAttribute* multiple = child->findAttribute(ATTR_MULTIPLE);
			bool recursive = (child->findAttribute(ATTR_RECURSIVE) != NULL);
			if (child->isEmpty() && !recursive)
			{
				//simple type
				if (multiple != NULL && multiple->getBool())
				{
					addSimpleVector(child, structDefinition, typeWidth, readingCode, writingCode);
				}
				else
				{
					addSimpleMember(child, structDefinition, typeWidth, readingCode, writingCode);
				}
			}
			else
			{
				//struct type
				if (multiple != NULL && multiple->getBool())
				{
					addStructVector(child, structDefinition, typeWidth, readingCode, writingCode);
				}
				else
				{
					addStructMember(child, structDefinition, typeWidth, readingCode, writingCode);
				}
			}
		}
		structDefinition += T("};\r\n\r\n");

		readingCode += T("}\r\n\r\n");
		writingCode += T("}\r\n\r\n");

		sourceCode += readingCode;
		sourceCode += writingCode;

		//add to front
		headerCode = structDefinition + headerCode;
	}

	NodeIterator iter;
	for (const XmlNode* child = node->getFirstChild(iter);
		  child != NULL;
		  child = node->getNextChild(iter))
	{
		if (child->hasChild())
		{
			if (!generateCodeForNode(child, headerCode, sourceCode))
			{
				return false;
			}
		}
	}
	return true;
}
Esempio n. 8
0
///////////////////////////////////////////////////////////////////////////////////////////////////
//parse data schema from node
//src		data node
//dst		schema node
bool XmlSchema::parseNodeStruct(XmlNode* dst, XmlNode* src)
{
	assert(dst != NULL);
	assert(src != NULL);

	NodeIterator nodeIterator;
	AttributeIterator attriIterator;

	for (XmlAttribute* attribute = src->getFirstAttribute(attriIterator);
		attribute != NULL;
		attribute = src->getNextAttribute(attriIterator))
	{
		XmlNode* structure = dst->findChild(attribute->getName());
		if (structure == NULL)
		{
			//first time show up
			structure = dst->addChild(attribute->getName());
			structure->addAttribute(ATTR_TYPE, guessType(attribute->getString()));
			structure->addAttribute(ATTR_ATTRIBUTE, T("true"));
		}
	}

	for (XmlNode* child = src->getFirstChild(nodeIterator);
		  child != NULL;
		  child = src->getNextChild(nodeIterator))
	{
		if (child->getType() != ELEMENT)
		{
			continue;
		}
		XmlNode* structure = dst->findChild(child->getName());
		if (structure == NULL)
		{
			//first time show up
			bool recursive = false;
			const XmlNode* parent = dst;
			while (parent != NULL)
			{
				if (Strcmp(parent->getName(), child->getName()) == 0)
				{
					recursive = true;
					break;
				}
				parent = parent->getParent();
			}
			structure = dst->addChild(child->getName());
			if (recursive)
			{
				structure->addAttribute(ATTR_RECURSIVE, T("true"));
			}
			else if (!child->hasChild() && !child->hasAttribute())
			{
				//simple type, must have a type attribute
				structure->addAttribute(ATTR_TYPE, guessType(child->getString()));
			}
		}
		else if (structure->findAttribute(ATTR_ATTRIBUTE) != NULL)
		{
			//child and attribute can't have same name
			return false;
		}

		XmlAttribute* multiple = structure->findAttribute(ATTR_MULTIPLE);
		if (multiple == NULL || !multiple->getBool())
		{
			NodeIterator iter;
			if (src->findFirstChild(child->getName(), iter) != NULL
				&& src->findNextChild(child->getName(), iter) != NULL)
			{
				if (multiple == NULL)
				{
					multiple = structure->addAttribute(ATTR_MULTIPLE);
				}
				multiple->setBool(true);
			}
		}

		if (!structure->findAttribute(ATTR_RECURSIVE) && (child->hasChild() || child->hasAttribute()))
		{
			parseNodeStruct(structure, child);
		}
	}

	return true;
}
Esempio n. 9
0
//===========================================
// Application::constructAsset
//===========================================
pAsset_t Application::constructAsset(const XmlNode data) {
   long proto = -1;
   bool addToWorld = false;

   XmlAttribute attr = data.firstAttribute();
   attr = attr.nextAttribute();

   if (!attr.isNull() && attr.name() == "proto") {
      proto = attr.getLong();
      attr = attr.nextAttribute();
   }

   if (!attr.isNull() && attr.name() == "addToWorld") {
      addToWorld = attr.getBool();
   }

   XmlNode node = data.firstChild();


   // Construct non-Entity assets

   if (node.name() == "Texture") return pAsset_t(new Texture(node));
   // ...


   // Construct Entities

   pEntity_t entity;

   // Does the XML description reference a prototype?
   if (proto != -1) {
      entity = pEntity_t(dynamic_cast<Entity*>(m_assetManager.cloneAsset(proto)));

      if (!entity)
         throw Exception("Prototype not found", __FILE__, __LINE__);

      // If the prototype has an Item object (aux data), set it to point at the entity.
      IAuxData* p = entity->getAuxDataPtr();
      if (p) {
         Item* item = dynamic_cast<Item*>(p);
         assert(item);

         item->setEntity(entity.get());
      }

      // If this XML node contains the entity along with an Item
      bool hasAuxData = false;
      if (node.name() == "ExtEntity") {
         node = node.firstChild();
         hasAuxData = true;
      }

      // The Entity node comes before the Item node
      entity->assignData(node);

      // Now we construct the Item
      if (hasAuxData) {
         node = node.nextSibling();

         if (node.name() != "Item")
            throw Exception("Expected Item node", __FILE__, __LINE__);

         Item* item = new Item(node);
         item->setEntity(entity.get());

         entity->attachAuxData(unique_ptr<Item>(item));
      }
   }
   // If XML description does not reference a prototype
   else {
      bool hasAuxData = false;
      if (node.name() == "ExtEntity") {
         node = node.firstChild();
         hasAuxData = true;
      }

      if (node.name() == "Player") entity = pEntity_t(new Player(node));
      if (node.name() == "Soil") entity = pEntity_t(new Soil(node));
      if (node.name() == "ParallaxSprite") entity = pEntity_t(new ParallaxSprite(node));
      if (node.name() == "Entity") entity = pEntity_t(new Entity(node));
      if (node.name() == "Sprite") entity = pEntity_t(new Sprite(node));
      if (node.name() == "PhysicalEntity") entity = pEntity_t(new PhysicalEntity<Box2dPhysics>(node));
      if (node.name() == "PhysicalSprite") entity = pEntity_t(new PhysicalSprite<Box2dPhysics>(node));

      if (!entity) {
         Exception ex("Unrecognised entity type '", __FILE__, __LINE__);
         ex.append(node.name());
         ex.append("'");
         throw ex;
      }

      if (hasAuxData) {
         node = node.nextSibling();

         if (node.name() != "Item")
            throw Exception("Expected Item node", __FILE__, __LINE__);

         Item* item = new Item(node);
         item->setEntity(entity.get());

         entity->attachAuxData(unique_ptr<Item>(item));
      }
   }

   if (addToWorld) {
      entity->addToWorld();
      m_worldSpace.insertAndTrackEntity(entity);
      m_entities[entity->getName()] = entity;
   }

   return entity;
}