I wanted to store and load additional datas for nodes into an .irr file (loadScene, saveScene)...
during my researches I saw that there are no tutorials on this (at least I found none)...
so maybe this can be used as little tutorial for others who are interested in this topic...
also I'm not sure if this is the best way to do this,
so improvements are realy welcome !!!
Code: Select all
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class mySerializer : public ISceneUserDataSerializer{
private:
IFileSystem* Filesys;
IVideoDriver* Driver;
public:
//! called for each node loaded from the file
void OnReadUserData(ISceneNode* forSceneNode, io::IAttributes* userData){
// here you can save additional data for each node
// maybe get the data from an array and use the node's ID as index ???
switch(forSceneNode->getType()){
case ESNT_ANIMATED_MESH:{
stringc str = userData->getAttributeAsString("TestName");
printf("name: %s\n", str.c_str());
}break;
}
}
//! called for each node saved to the file
IAttributes* createUserData(ISceneNode* forSceneNode){
// here you can load additional data for each node
// maybe store the data in an array and use the node's ID as index ???
switch(forSceneNode->getType()){
case ESNT_ANIMATED_MESH:{
IAttributes* atr = Filesys->createEmptyAttributes(Driver);
atr->addString("TestName","name of the mesh");
return atr;
}break;
}
return 0;
}
//! Constructor
mySerializer(IrrlichtDevice* dev){
if(dev){
Filesys = dev->getFileSystem();
Driver = dev->getVideoDriver();
}
}
};
int main(){
// create Irrlicht
IrrlichtDevice* device = createDevice(EDT_DIRECT3D9);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// create the serializer
mySerializer serializer(device);
// create and save a scene
IAnimatedMesh* mesh = smgr->getMesh("media/dwarf.x");
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
node->setMaterialFlag(EMF_LIGHTING, false);
smgr->saveScene("test.irr", &serializer);
// load the scene
smgr->loadScene("test.irr", &serializer);
// setup and run Irrlicht render loop
smgr->addCameraSceneNodeFPS();
while(device->run()){
driver->beginScene(true, true, video::SColor(0,0,0,0));
smgr->drawAll();
driver->endScene();
}
device->drop();
return 0;
}