maybe this helps......
objects serialize / deserialize themselves
virtual function allows heirarchy
Code: Select all
//! Writes attributes of the object.
void CSObject::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options)
{
// log this event
CS_LOG(CSL_DEBUG, "CSObject::serialize()");
out->addInt("Id", m_Id);
out->addString("Name", m_Name.c_str());
out->addVector3d("Position", m_Position);
out->addVector3d("Rotation", m_Rotation);
out->addVector3d("Scale", m_Scale);
out->addVector3d("PositionOffset", m_PositionOffset);
out->addVector3d("RotationOffset", m_RotationOffset);
out->addVector3d("ScaleOffset", m_ScaleOffset);
out->addVector3d("BBOffset", m_BBOffset);
out->addFloat("Mass", m_Mass);
}
//! Reads attributes of the object.
void CSObject::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
// log this event
CS_LOG(CSL_DEBUG, "CSObject::deserialize()");
m_Id = in->getAttributeAsInt("Id");
m_Name = in->getAttributeAsString("Name");
m_Position = in->getAttributeAsVector3d("Position");
m_Rotation = in->getAttributeAsVector3d("Rotation");
m_Scale = in->getAttributeAsVector3d("Scale");
m_PositionOffset = in->getAttributeAsVector3d("PositionOffset");
m_RotationOffset = in->getAttributeAsVector3d("RotationOffset");
m_ScaleOffset = in->getAttributeAsVector3d("ScaleOffset");
m_BBOffset = in->getAttributeAsVector3d("BBOffset");
m_Mass = in->getAttributeAsFloat("Mass");
}
level class loads objects from xml file
Code: Select all
bool CSLevel::saveToDisk(stringc filename)
{
CS_LOG(CSL_DEBUG, "%s %s", "saving level to file ", filename);
IXMLWriter* writer = getDevice()->getFileSystem()->createXMLWriter(filename);
if (!writer)
{
CS_LOG(CSL_WARNING, "Warning! unable to create save file");
return false;
}
writer->writeXMLHeader();
vector3df pos(0, 0, 0);
vector3df tar(0, 0, 0);
if (getSmgr()->getActiveCamera())
{
pos = getSmgr()->getActiveCamera()->getPosition();
tar = getSmgr()->getActiveCamera()->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();
writer->writeElement(L"LIGHT", false,
L"USE", stringw(boolToStringc(getUseLight())).c_str(),
L"AMBIENTLIGHT", stringw(SColorfToStringc(m_AmbientLight)).c_str());
writer->writeLineBreak();
writer->writeLineBreak();
writer->writeElement(L"FOG", false,
L"USE", stringw(boolToStringc(getUseFog())).c_str(),
L"COLOR", stringw(SColorToStringc(m_FogParams.m_Color)).c_str(),
L"START", stringw(floatToStringc(m_FogParams.m_Start)).c_str(),
L"END", stringw(floatToStringc(m_FogParams.m_End)).c_str());
writer->writeLineBreak();
writer->writeLineBreak();
writer->writeElement(L"DEBUGINFO", false,
L"USE", stringw(boolToStringc(getUsePhysXDebugRender())).c_str());
writer->writeLineBreak();
writer->writeLineBreak();
writer->writeElement(L"MAINCHARACTERID", false,
L"USE", stringw(intToStringc(getMainCharacterId())).c_str());
writer->writeLineBreak();
writer->writeLineBreak();
CSObject* obj = getObjectFactory()->getObjectManager()->getNextObject(true);
while (obj)
{
if (!obj->getIsDebugObject())
{
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();
}
obj = getObjectFactory()->getObjectManager()->getNextObject(false);
}
writer->drop();
return true;
}
bool CSLevel::loadFromDisk(stringc filename)
{
CS_LOG(CSL_DEBUG, "%s %s", "loading level from file ", filename);
IXMLReader* reader = getDevice()->getFileSystem()->createXMLReader(filename);
if (!reader)
{
CS_LOG(CSL_WARNING, "Warning! unable to open save file");
return false;
}
// read file
while (reader->read())
{
switch (reader->getNodeType())
{
case io::EXN_ELEMENT:
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 (getSmgr()->getActiveCamera())
{
getSmgr()->getActiveCamera()->setPosition(stringcToVector3df(stringc(pos)));
getSmgr()->getActiveCamera()->setTarget(stringcToVector3df(stringc(tar)));
}
else CS_LOG(CSLOGTYPE::CSL_WARNING, "no camera in game save file");
}
// if this is an object definition
// if this is an object definition
if (stringw("LIGHT") == name)
{
stringw use = reader->getAttributeValueSafe(L"USE");
stringw color = reader->getAttributeValueSafe(L"AMBIENTLIGHT");
setUseLight(stringcToBool(use));
setAmbientLight(stringcToSColorf(color));
}
// if this is an object definition
if (stringw("FOG") == name)
{
stringw use = reader->getAttributeValueSafe(L"USE");
stringw color = reader->getAttributeValueSafe(L"COLOR");
stringw start = reader->getAttributeValueSafe(L"START");
stringw end = reader->getAttributeValueSafe(L"END");
m_FogParams.m_Color = stringcToSColor(color);
m_FogParams.m_Start = stringcToFloat(start);
m_FogParams.m_End = stringcToFloat(end);
setFogParams(m_FogParams);
setUseFog(stringcToBool(use));
}
// if this is an object definition
if (stringw("DEBUGINFO") == name)
{
stringw use = reader->getAttributeValueSafe(L"USE");
setUsePhysXDebugRender(stringcToBool(use));
}
// if this is an object definition
if (stringw("MAINCHARACTERID") == name)
{
stringw use = reader->getAttributeValueSafe(L"USE");
setMainCharacterId(stringcToInt(use));
}
if (stringw("CSOBJECT") == name)
{
stringw type = reader->getAttributeValueSafe(L"TYPE");
CSObject* obj = getObjectFactory()->createObjectByType(stringc(type), false, 0, stringc(type), true);
if (obj)
{
IAttributes* attr = getDevice()->getFileSystem()->createEmptyAttributes(getDriver());
attr->read(reader, false);
obj->deserializeAttributes(attr);
obj->reCreate();
attr->drop();
}
else CS_LOG(CSL_WARNING, "Did not create object\n");
}
}
}
reader->drop();
return true;
}