Wednesday, December 19, 2007

Example Code

[This is just a temporary theme. I think it's fairly dark so I will either change that or find something to lighten it up.]

I just wanted to post some code to show how working with my properties can work. I've switched them over to use the Any class that is built in to Ogre but originally I was using my own version of it. There is more to a property itself than just the Any instance but a property does have one of them attached called the 'propertyValue'.

Here is the code I use to actually export the properties of *any* SceneObject at all. This works on Entities, ParticleSystems, Lights, and will work on any future thing out there. *All* of the code in the editor is like this. There is no notion of a particular type *anywhere* except inside the actual SceneObject derived classes themselves. So something that works on one SceneObject will work on a completely different type of SceneObject as long as it provides the same properties and commands (commands are 'issued' to the SceneObject through a generic interface). I'll post an example of the translate tool a bit later. But needless to say, I write code once and if it works, it'll work for anything. I know that whenever I implement sounds, the translate tool will work on those without having to touch the code. It's nice being able to expect that.


void XmlSceneExporter::_exportProperties ( SceneObject* rootObject, wxXmlNode* propertiesNode )
{
SceneObjectPropertyList properties;
rootObject->getProperties( &properties );

if( properties.size() == 0 )
return;

wxXmlNode* propertiesNode = new wxXmlNode( node, wxXML_ELEMENT_NODE, "properties" );

size_t numChildren = properties.size();
for( size_t i = 0; i < numChildren; i++ )
{
Any propertyValue = properties[i].getPropertyValue();
if ( propertyValue.isEmpty() )
continue;

String strData;
if( StringConverter::convertToString( propertyvalue, &strData ) == false )
continue;

wxXmlNode* propNode = new wxXmlNode( propertiesNode, wxXML_ELEMENT_NODE, "property" );
propNode->AddProperty( "name", properties[i].getName() );
propNode->AddProperty( "value", strValue );
}
}



And here's the example 'convertToString' that is used. It's all very simplistic. And this will allow future objects to be registered and saved without touching anything else. Just implement a new TypeDescriptor, register it (at run-time) with the TypeDescriptorManager, and you're all set.



bool StringConverter::convertToString( const Any& property, String* strData )
{
String strValue;
const TypeDescriptor* descriptor;
TypeDescriptorManager* mgr = TypeDescriptorManager::getSingletonPtr();

descriptor = mgr->findTypeDescriptor( propertyValue.getType() );
if( descriptor != NULL && descriptor->canConvertToString() )
return descriptor->convertToString( propertyValue, &strValue );

return false;
}




I am rather enjoying generic programming like this :) I haven't found myself going back to rework old code to fit a new object that I didn't expect to use. Anyways. I have another exam in 8 hours so I guess I'll start studying for it now.

2 comments:

Anonymous said...

Interesting to know.

Anonymous said...

Amiable fill someone in on and this post helped me alot in my college assignement. Say thank you you seeking your information.