Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Feb 26, 2013
1 parent a535117 commit ffd1704
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 60 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/include/resource/materials.h
Expand Up @@ -288,7 +288,7 @@ class Materials
inline int groupCount() const { return allGroups().count(); }

///
Manifest &newManifest(Scheme &scheme, Path const &path);
Manifest &newManifest(Uri const &uri);

///
void addMaterial(Material &material);
Expand Down
107 changes: 53 additions & 54 deletions doomsday/client/src/def_main.cpp
Expand Up @@ -1133,79 +1133,78 @@ static void rebuildMaterialDecorations(Material &material, ded_material_t const
}
#endif // __CLIENT__

static void interpretMaterialDef(ded_material_t &def)
static void interpretMaterialDef(ded_material_t const &def)
{
if(!def.uri) return;
LOG_AS("interpretMaterialDef");

// We require a properly formed uri (but not a urn - this is a resource path).
de::Uri &uri = *reinterpret_cast<de::Uri *>(def.uri);
if(!App_Materials().validateUri(uri, 0, (verbose >= 1)))
return;
if(!def.uri) return;

// Have we already created a manifest for this?
MaterialManifest *manifest = &App_Materials().newManifest(App_Materials().scheme(uri.scheme()), uri.path());
if(!manifest)
try
{
LOG_WARNING("Failed declaring material \"%s\", ignoring.") << uri;
return;
}
// Create/retrieve a manifest for the would-be material.
MaterialManifest *manifest = &App_Materials().newManifest(*reinterpret_cast<de::Uri *>(def.uri));

// Update manifest classification flags:
manifest->setFlags(MaterialManifest::AutoGenerated, CPP_BOOL(def.autoGenerated));
manifest->setFlags(MaterialManifest::Custom, false);
if(def.layers[0].stageCount.num > 0)
{
ded_material_layer_t const &layer = def.layers[0];
de::Uri *texUri = reinterpret_cast<de::Uri *>(layer.stages[0].texture);
if(texUri) // Not unused.
// Update manifest classification:
manifest->setFlags(MaterialManifest::AutoGenerated, CPP_BOOL(def.autoGenerated));
manifest->setFlags(MaterialManifest::Custom, false);
if(def.layers[0].stageCount.num > 0)
{
try
{
Texture &texture = App_Textures().find(*texUri).texture();
if(texture.flags().testFlag(Texture::Custom))
manifest->setFlags(MaterialManifest::Custom);
}
catch(Textures::NotFoundError const &er)
ded_material_layer_t const &firstLayer = def.layers[0];
if(firstLayer.stages[0].texture) // Not unused.
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ". Unknown texture \"%s\" in Material \"%s\" (layer %i stage %i), ignoring.")
<< reinterpret_cast<de::Uri *>(layer.stages[0].texture)
<< reinterpret_cast<de::Uri *>(def.uri)
<< 0 << 0;
try
{
Texture &texture = App_Textures().find(*reinterpret_cast<de::Uri *>(firstLayer.stages[0].texture)).texture();
if(texture.flags().testFlag(Texture::Custom))
manifest->setFlags(MaterialManifest::Custom);
}
catch(Textures::NotFoundError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ". Unknown texture \"%s\" in Material \"%s\" (layer %i stage %i), ignoring.")
<< *reinterpret_cast<de::Uri *>(firstLayer.stages[0].texture)
<< *reinterpret_cast<de::Uri *>(def.uri)
<< 0 << 0;
}
}
}
}

// An entirely new material?
if(!manifest->hasMaterial())
{
// Instantiate and associate the new material with the manifest.
manifest->setMaterial(new Material(*manifest));
// An entirely new material?
if(!manifest->hasMaterial())
{
// Instantiate and associate the new material with the manifest.
manifest->setMaterial(new Material(*manifest));

// Include the material in the scheme-agnostic list of instances.
App_Materials().addMaterial(manifest->material());
}
// Include the material in the scheme-agnostic list of instances.
App_Materials().addMaterial(manifest->material());
}

/*
* (Re)configure the material:
*/
Material &material = manifest->material();
/*
* (Re)configure the material:
*/
Material &material = manifest->material();

Material::Flags newFlags;
if(def.flags & MATF_NO_DRAW) newFlags |= Material::NoDraw;
if(def.flags & MATF_SKYMASK) newFlags |= Material::SkyMask;
material.setFlags(newFlags);
Material::Flags newFlags;
if(def.flags & MATF_NO_DRAW) newFlags |= Material::NoDraw;
if(def.flags & MATF_SKYMASK) newFlags |= Material::SkyMask;
material.setFlags(newFlags);

material.setDimensions(Vector2i(def.width, def.height));
material.setDimensions(Vector2i(def.width, def.height));

material.setAudioEnvironment(S_AudioEnvironmentForMaterial(def.uri));
material.setAudioEnvironment(S_AudioEnvironmentForMaterial(def.uri));

rebuildMaterialLayers(material, def);
rebuildMaterialLayers(material, def);
#ifdef __CLIENT__
rebuildMaterialDecorations(material, def);
rebuildMaterialDecorations(material, def);
#endif

material.markValid(true);
material.markValid(true);
}
catch(...)
{
LOG_WARNING("Failed declaring material \"%s\", ignoring.")
<< *reinterpret_cast<de::Uri *>(def.uri);
}
}

static void invalidateAllMaterials()
Expand Down
15 changes: 10 additions & 5 deletions doomsday/client/src/resource/materials.cpp
Expand Up @@ -438,22 +438,25 @@ bool Materials::has(Uri const &path) const
return false;
}

Materials::Manifest &Materials::newManifest(Materials::Scheme &scheme, Path const &path)
Materials::Manifest &Materials::newManifest(de::Uri const &uri)
{
LOG_AS("Materials::newManifest");

// We require a properly formed URI (but not a URN - this is a resource path).
if(!validateUri(uri, 0, (verbose >= 1)))
throw Error("Materials::newManifest", "Invalid URI \"" + uri.asText() + "\"");

// Have we already created a manifest for this?
Manifest *manifest = 0;
try
{
manifest = &find(de::Uri(scheme.name(), path));
return find(uri);
}
catch(NotFoundError const &)
{
// Acquire a new unique identifier for the manifest.
materialid_t const id = ++d->manifestCount;

manifest = &scheme.insertManifest(path, id);
Manifest *manifest = &scheme(uri.scheme()).insertManifest(uri.path(), id);

// Add the new manifest to the id index/map.
if(d->manifestCount > d->manifestIdMapSize)
Expand All @@ -463,9 +466,11 @@ Materials::Manifest &Materials::newManifest(Materials::Scheme &scheme, Path cons
d->manifestIdMap = (Manifest **) M_Realloc(d->manifestIdMap, sizeof *d->manifestIdMap * d->manifestIdMapSize);
}
d->manifestIdMap[d->manifestCount - 1] = manifest; /* 1-based index */

return *manifest;
}

return *manifest;
throw Error("Materials::newManifest", "An unknown error occured declaring the new material");
}

void Materials::addMaterial(Material &material)
Expand Down

0 comments on commit ffd1704

Please sign in to comment.