Loading and duplicating a material

// Access our resource manager:
RED::Object* resmgr = RED::Factory::CreateInstance( CID_REDResourceManager );
RED::IResourceManager* iresmgr = resmgr->As< RED::IResourceManager >();

// Access the data manager:
RED::Object* datamgr = iresmgr->GetDataManager();
RED::IDataManager* idatamgr = datamgr->As< RED::IDataManager >();

// Load a material from a .red file:
RED::Object* redfile = RED::Factory::CreateInstance( CID_REDFile );
if( redfile == NULL )

RED::IREDFile* ifile = redfile->As< RED::IREDFile >();

RED::StreamingPolicy policy;
RED::FileHeader header;
RED::FileInfo finfo;
RED::Vector< unsigned int > contexts;

RC_TEST( ifile->Load( "MyMaterial.red", iresmgr->GetState(), policy, header, finfo, contexts ) );

// Get the first material (assuming there's one in the file!):
RED::Object* material;
if( contexts.size() != 0 )
  unsigned int mcount = 0;
  RC_TEST( idatamgr->GetMaterialsCount( mcount, contexts[0] ) );

  if( mcount > 0 )
    RC_TEST( idatamgr->GetMaterial( material, contexts[0], 0 ) );

// Now, duplicate the material, without duplicating large images:
RED::Object* material_copy;
RC_TEST( iresmgr->CloneMaterial( material_copy, material, iresmgr->GetState(), false, true ) );

We must pinpoint the two boolean options of RED::IResourceManager::CloneMaterial in this task: A material generally uses a lot of images. Among them, we find tiny 1x1 pixel images used by REDsdk shaders that perform texturing (for consistency, a built-in REDsdk shader generally uses either no texture to source its parameters, or all its parameters are turned into textures; therefore, we have many tiny 1x1 textures that may be used as replacement for colors if we're using texturing); and we have large images that are generally the source images used for the material.

The RED::IResourceManager::CloneMaterial method lets you clone or share all images in the material. This is important from both a memory management perspective and also for edition needs. If a cloned material shares all its images, then any change in the material parameter will probably modify an image that is shared by both materials. If a cloned material duplicates tiny images and share other images, then modifying a small image (generally a color that was set with no texture) will modify only the cloned material.