bool CGWIC_Cell::LoadObjectStates() { path filenm = GWIC_CELLSTORE_DIR; filenm += GetCellFileSuffix(); filenm += ".xml"; IXMLReader* xml = graphics->getFileSystem()->createXMLReader(filenm); if (!xml) { std::cerr << "LoadObjectStates(): can't create xml reader for " << filenm.c_str() << std::endl; return false; } const stringw tg_obj(L"object"); const stringw tg_pos(L"position"); const stringw tg_opt(L"options"); stringw cur_tag; path cfile; CIrrStrParser pos,rot,scl; CGWIC_GameObject* optr = NULL; while (xml->read()) { switch (xml->getNodeType()) { case EXN_ELEMENT: if ((cur_tag.empty()) && (tg_obj.equals_ignore_case(xml->getNodeName()))) { cur_tag = tg_obj; cfile = xml->getAttributeValueSafe(L"file"); optr = new CGWIC_GameObject(cfile,GetCoord(),graphics,physics); if (!optr) std::cerr << "Failed to create object from " << cfile.c_str() << std::endl; else objects.push_back(optr); } else if ((cur_tag == tg_obj) && (optr)) { if (tg_pos.equals_ignore_case(xml->getNodeName())) { pos = xml->getAttributeValueSafe(L"pos"); rot = xml->getAttributeValueSafe(L"rot"); scl = xml->getAttributeValueSafe(L"scale"); optr->SetPos(pos.ToVector3f()); optr->SetScale(scl.ToVector3f()); optr->SetRot(rot.ToVector3f()); } else if (tg_pos.equals_ignore_case(xml->getNodeName())) { optr->SetPhysical(xml->getAttributeValueAsInt(L"physical")); optr->SetEnabled(xml->getAttributeValueAsInt(L"enabled")); } } break; case EXN_ELEMENT_END: cur_tag = L""; optr = NULL; break; default: break; } } xml->drop(); return false; }
int CSLevel::loadPrefab(stringc filename) { CS_LOG(CSLOGTYPE::CSL_DEBUG, "Loading prefab %s", filename.c_str()); int id = 0; stringc dir(getApplication()->getDirectory("PrefabDirectory")); dir += filename; IXMLReader* reader = getDevice()->getFileSystem()->createXMLReader(filename); if (!reader) { CS_LOG(CSLOGTYPE::CSL_WARNING, "Warning! unable to open prefab file %s", filename.c_str()); return -1; } // read file while (reader->read()) { switch (reader->getNodeType()) { case io::EXN_ELEMENT: stringw name = reader->getNodeName(); if (stringw("CSOBJECT") == name) { stringw type = reader->getAttributeValueSafe(L"TYPE"); CSObject* obj = getObjectFactory()->createObjectByType(stringc(type)); if (obj) { id = obj->getId(); IAttributes* attr = getDevice()->getFileSystem()->createEmptyAttributes(getDriver()); attr->read(reader, false); obj->deserializeAttributes(attr); obj->setId(id); obj->reCreate(); attr->drop(); } } } } reader->drop(); return id; }
int CSObject::loadFromPreFab(stringc filename) { int id = 0; stringc dir(getLevel()->getApplication()->getDirectory("PrefabDirectory")); dir += filename; CS_LOG(CSLOGTYPE::CSL_DEBUG, "Loading from prefab directory %s", dir.c_str()); IXMLReader* reader = getDevice()->getFileSystem()->createXMLReader(dir); CS_CHECK_BOOL(reader, CSLOGTYPE::CSL_DEBUG, "Warning! unable to open prefab file"); // read file while (reader->read()) { switch (reader->getNodeType()) { case io::EXN_ELEMENT: { stringw name = reader->getNodeName(); if (stringw("CSOBJECT") == name) { stringw type = reader->getAttributeValueSafe(L"TYPE"); IAttributes* attr = getDevice()->getFileSystem()->createEmptyAttributes(getDriver()); attr->read(reader, false); deserializeAttributes(attr); setId(getLevel()->getObjectFactory()->getObjectManager()->getUniqueId()); attr->drop(); } } } } reader->drop(); return id; }
// load the level objects from disk file bool CSLevel::loadFromDisk(stringc filename, bool destroyOld) { CS_LOG(CSLOGTYPE::CSL_DEBUG, "*************************** Loading level from file %s ***************************", filename.c_str()); if (destroyOld) clear(); // attempt to open the file IXMLReader* reader = getDevice()->getFileSystem()->createXMLReader(filename); if (!reader) { CS_LOG(CSLOGTYPE::CSL_WARNING, "Warning! unable to open file %s", filename.c_str()); return false; } // read file while (reader->read()) { // based on the node type switch (reader->getNodeType()) { case io::EXN_ELEMENT: { // get the node name stringw name = reader->getNodeName(); // if this is an object definition if (stringw("CAMERA") == name) { stringw pos = reader->getAttributeValueSafe(L"POSITION"); stringw tar = reader->getAttributeValueSafe(L"TARGET"); if (getCamera()) { getCamera()->setPosition(stringcToVector3df(stringc(pos))); getCamera()->setTarget(stringcToVector3df(stringc(tar))); } else CS_LOG(CSLOGTYPE::CSL_WARNING, "no camera in game save file"); } // if this is an object definition if (stringw("CSOBJECT") == name) { // get the object type stringw type = reader->getAttributeValueSafe(L"TYPE"); // attempt to create the object CSObject* obj = getObjectFactory()->createObjectByType(stringc(type)); if (obj) { // load the attributes from the file IAttributes* attr = getDevice()->getFileSystem()->createEmptyAttributes(getDriver()); attr->read(reader, false); // let the object deserialize from the attributes obj->deserializeAttributes(attr); // recreate the object using the new attribtues obj->reCreate(); // drop the pointer attr->drop(); } } } } } // drop the reader reader->drop(); CS_LOG(CSLOGTYPE::CSL_DEBUG, "*************************** finished Loading level from file %s ***************************", filename.c_str()); // everything went fine return true; }
bool CGWIC_BodyPart::LoadModelFile(irr::io::path fname) { IXMLReader* mio = irDevice->getFileSystem()->createXMLReader(fname); if (!mio) return false; std::cout << "Reading model XML: " << fname.c_str() << std::endl; const stringw mt_model(L"Model"); const stringw mt_inslot(L"InSlot"); const stringw mt_outslot(L"OutSlot"); const stringw mt_collision(L"Collision"); const stringw mt_colbox(L"StandardBox"); const stringw mt_colsph(L"StandardSphere"); const stringw mt_coltri(L"TriMeshShape"); const stringw mt_colcvx(L"ConvexHull"); const stringw mt_colgim(L"GImpact"); GWIC_BPSlot cslot; CIrrStrParser strparse; nocollision = true; //in case we can't load or found collision shape model while (mio->read()) { if (mt_model.equals_ignore_case(mio->getNodeName())) { mesh = scManager->getMesh(GWIC_BPARTS_DIR+mio->getAttributeValueSafe(L"file")); if (mesh) root = scManager->addAnimatedMeshSceneNode( mesh,parent,GWIC_PICKABLE_MASK | GWIC_ACTOR_MASK); if (root) { root->updateAbsolutePosition(); root->setMaterialFlag(EMF_NORMALIZE_NORMALS,true); ITriangleSelector* sel = scManager->createTriangleSelector(mesh,root); root->setTriangleSelector(sel); sel->drop(); } //TODO: texturing } else if (mt_inslot.equals_ignore_case(mio->getNodeName())) { strparse = mio->getAttributeValueSafe(L"position"); slot_in.posit = strparse.ToVector3f(); strparse = mio->getAttributeValueSafe(L"rotation"); slot_in.rotat = strparse.ToVector3f(); strparse = mio->getAttributeValueSafe(L"axis"); slot_in.axis = strparse.ToVector3f(); } else if (mt_outslot.equals_ignore_case(mio->getNodeName())) { cslot.ID = mio->getAttributeValueAsInt(L"ID"); strparse = mio->getAttributeValueSafe(L"position"); cslot.posit = strparse.ToVector3f(); strparse = mio->getAttributeValueSafe(L"rotation"); cslot.rotat = strparse.ToVector3f(); strparse = mio->getAttributeValueSafe(L"axis"); cslot.axis = strparse.ToVector3f(); slot_outs.push_back(cslot); } else if (mt_collision.equals_substring_ignore_case(mio->getNodeName())) { mass = mio->getAttributeValueAsFloat(L"mass"); stringw data = mio->getAttributeValueSafe(L"type"); nocollision = false; if (data.equals_substring_ignore_case(mt_colbox)) coltype = ECST_BOX; else if (data.equals_substring_ignore_case(mt_colsph)) coltype = ECST_SPHERE; else if (data.equals_substring_ignore_case(mt_coltri)) coltype = ECST_BVHTRIMESH; else if (data.equals_substring_ignore_case(mt_colcvx)) coltype = ECST_CONVEXHULL; else if (data.equals_substring_ignore_case(mt_colgim)) coltype = ECST_GIMPACT; else { std::cerr << "Collision model unknown: " << data.c_str() << std::endl; nocollision = true; } //colmesh = scManager->getMesh(mio->getAttributeValueSafe(L"file")); } } mio->drop(); return true; }