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; }
bool CSLevel::savePrefab(CSObject* obj, stringc filename) { CS_LOG(CSLOGTYPE::CSL_DEBUG, "Saving prefab to file %s", filename.c_str()); CS_CHECK_BOOL(obj, CSLOGTYPE::CSL_WARNING, "Warning unable to save prefab. obj is not valid"); _chdir(getApplication()->getDirectory("PrefabDirectory").c_str()); IXMLWriter* writer = getDevice()->getFileSystem()->createXMLWriter(filename); if (!writer) { CS_LOG(CSLOGTYPE::CSL_WARNING, "Warning! unable to create prefab file %s", filename.c_str()); return false; } writer->writeXMLHeader(); stringw name("CSOBJECT"); writer->writeElement(name.c_str(), false, L"TYPE", stringw(obj->getInfo()->getName()).c_str()); writer->writeLineBreak(); IAttributes* attr = getDevice()->getFileSystem()->createEmptyAttributes(getDriver()); SAttributeReadWriteOptions options; obj->serializeAttributes(attr, &options); if (attr->getAttributeCount() != 0) { attr->write(writer); writer->writeLineBreak(); } attr->drop(); writer->writeClosingTag(name.c_str()); writer->writeLineBreak(); writer->writeLineBreak(); writer->drop(); return true; }
//----------------------------------------------------------------------- // i n i t i a l i z e //----------------------------------------------------------------------- int initialize() { IAttributes* oattr = getConfigOverride(); stringc scriptFileName="", option; int c; while ((c = getopt(m_argc,m_argv, "?hi:o:")) != EOF) { switch (c) { case 'i': scriptFileName = optarg; break; case 'o': { option = optarg; s32 sep = option.findFirstChar("=:", 2); if(sep > 0) { stringc opt = option.subString(0, sep); stringc val = option.subString(sep+1, option.size()); oattr->addString(opt.c_str(), val.c_str()); } } break; case 'h': case '?': usage(); return 1; break; } } if(TApplication::initialize()) return 1; if(!scriptFileName.size()) { if(optind < m_argc) { scriptFileName = m_argv[optind++]; } } // script passed on command line? if(!scriptFileName.size()) scriptFileName = m_configScript->getString("script.filepath"); if(scriptFileName.size()) { if(m_scriptManager->loadScript(scriptFileName)) return 1; } // // create and initialize the application/game states // logMessage(LOG_INFO, "Initialize States..."); m_scriptManager->createStates(); TStateMapItr sit; sit = m_states.getIterator(); for(sit = m_states.getIterator();!sit.atEnd(); sit++) { TState* state = sit->getValue(); if(state != this) if(state->initialize()) return 1; } // // add text to the help panel // addHelpText("wasd -", "Camera movement"); addHelpText(" i -", "Invert mouse"); addHelpText(" prt -", "Screen capture"); addHelpText(" F1 -", "Toggle help"); addHelpText(" F2 -", "Toggle debug"); addHelpText(" F3 -", "Cycle wire/pts"); addHelpText(" F4 -", "Toggle Phys dbg"); addHelpText(" F5 -", "Cycle dbg data"); addHelpText(" F6 -", "Toggle Xform"); addHelpText(" F7 -", "Toggle Cursor"); acceptEvent("help",EVENT_DELEGATE(TSE::toggleHelp)); acceptEvent("idbg",EVENT_DELEGATE(TSE::toggleDebug)); acceptEvent("wire",EVENT_DELEGATE(TSE::toggleWire)); acceptEvent("pdbg",EVENT_DELEGATE(TSE::togglePhysicsDebug)); acceptEvent("cdbg",EVENT_DELEGATE(TSE::cycleDebug)); acceptEvent("sprt",EVENT_DELEGATE(TSE::captureScreen)); toggleHelpGUI(); return 0; }
// 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; }
// save the level objects to disk file bool CSLevel::saveToDisk(stringc filename) { // log this event CS_LOG(CSLOGTYPE::CSL_DEBUG, "Saving game data - %s", filename.c_str()); // creat ethe xml writer IXMLWriter* writer = getDevice()->getFileSystem()->createXMLWriter(filename); if (!writer) { CS_LOG(CSLOGTYPE::CSL_WARNING, "Warning! unable to create save file %s", filename.c_str()); return false; } // write the xml header writer->writeXMLHeader(); vector3df pos(0, 0, 0); vector3df tar(0, 0, 0); if (getCamera()) { pos = getCamera()->getPosition(); tar = getCamera()->getTarget(); } // write the camera position and target writer->writeLineBreak(); writer->writeElement(L"CAMERA", false, L"POSITION", stringw(vector3dfToStringc(pos)).c_str(), L"TARGET", stringw(vector3dfToStringc(tar)).c_str() ); writer->writeLineBreak(); writer->writeLineBreak(); // run through thte list of objects CSObject* obj = getObjectFactory()->getObjectManager()->getNextObject(true); while (obj) { // if this is not a debug object, then save it to disk if (!obj->getDebugObject()) { // write the node type stringw name("CSOBJECT"); writer->writeElement(name.c_str(), false, L"TYPE", stringw(obj->getInfo()->getName()).c_str()); writer->writeLineBreak(); // let the object serialize itself into our attributes structure IAttributes* attr = getDevice()->getFileSystem()->createEmptyAttributes(getDriver()); SAttributeReadWriteOptions options; obj->serializeAttributes(attr, &options); // if there are attributes if (attr->getAttributeCount() != 0) { // write the attributes to the xml file attr->write(writer); // make the file pretty writer->writeLineBreak(); } // drop the pointer attr->drop(); // finish writing the xml header / footer writer->writeClosingTag(name.c_str()); writer->writeLineBreak(); writer->writeLineBreak(); } // get the next object obj = getObjectFactory()->getObjectManager()->getNextObject(false); } // drop the pointer writer->drop(); // everything went fine return true; }
bool BinaryToBinaryHandler::startElement(unsigned int tagID, IAttributes& attr, bool empty) { vector<unsigned int> tags = attr.getTags(); for (unsigned int i=0; i<tags.size(); i++) { IAttributes::Types type = attr.getType(tags[i]); switch (type) { case IAttributes::BOOLEAN: writer.setAttribute(tags[i], attr.getBooleanValue(tags[i])); break; case IAttributes::BYTE: writer.setAttribute(tags[i], attr.getByteValue(tags[i])); break; case IAttributes::CHAR: writer.setAttribute(tags[i], attr.getCharValue(tags[i])); break; case IAttributes::DOUBLE: writer.setAttribute(tags[i], attr.getDoubleValue(tags[i])); break; case IAttributes::FLOAT: writer.setAttribute(tags[i], attr.getFloatValue(tags[i])); break; case IAttributes::INT: writer.setAttribute(tags[i], attr.getIntValue(tags[i])); break; case IAttributes::LONG: writer.setAttribute(tags[i], attr.getLongValue(tags[i])); break; case IAttributes::SHORT: writer.setAttribute(tags[i], attr.getShortValue(tags[i])); break; case IAttributes::STRING: writer.setAttribute(tags[i], attr.getStringValue(tags[i])); break; default: cout << "ERROR - Unknown Value for Unknown Type: " << type; break; } } if (empty) { writer.printTag(tagID); } else { writer.openTag(tagID); } return true; }