Skip to content

Commit

Permalink
Refactor|MaterialManifest: Added MaterialManifest::hasMaterial()
Browse files Browse the repository at this point in the history
MaterialManifest's material() method now returns a reference to the
associated material. Use hasMaterial() to determine if a material
is presently associated.
  • Loading branch information
danij-deng committed Jan 18, 2013
1 parent eb5e771 commit 78ed650
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 85 deletions.
18 changes: 10 additions & 8 deletions doomsday/engine/include/resource/materialmanifest.h
Expand Up @@ -69,19 +69,21 @@ class MaterialManifest : public PathTree::Node
/// @return Unique identifier associated with this.
materialid_t id() const;

/// @return @c true if the material manifest is not derived from an original game resource.
/// @return @c true if the manifest is not derived from an original game resource.
bool isCustom() const;

/// @return Material associated with the manifest; otherwise @c NULL.
Material *material() const;
/// @return @c true if the manifest has an associated material.
bool hasMaterial() const;

/**
* Change the material associated with this manifest.
*
* @post If @a material differs from that currently associated with this,
* any Info presently owned by this will destroyed (its invalid).
* Returns the material associated with the manifest.
*/
Material &material() const;

/**
* Change the material associated with the manifest.
*
* @param material New material to associate with this.
* @param material New material to associate with.
*/
void setMaterial(Material *material);

Expand Down
19 changes: 10 additions & 9 deletions doomsday/engine/src/def_main.cpp
Expand Up @@ -1202,13 +1202,12 @@ void Def_Read()

try
{
MaterialManifest &bind = App_Materials().find(*reinterpret_cast<de::Uri *>(def->uri));
if(Material *mat = bind.material())
{
// Update existing.
App_Materials().rebuild(*mat, def);
}
// Update existing.
Material &material = App_Materials().find(*reinterpret_cast<de::Uri *>(def->uri)).material();
App_Materials().rebuild(material, def);
}
catch(Materials::Manifest::MissingMaterialError const&)
{} // Ignore this error.
catch(Materials::NotFoundError const &)
{
// A new Material.
Expand Down Expand Up @@ -1436,7 +1435,7 @@ static void initMaterialGroup(ded_group_t *def)

try
{
Material *mat = App_Materials().find(*reinterpret_cast<de::Uri *>(gm->material)).material();
Material &material = App_Materials().find(*reinterpret_cast<de::Uri *>(gm->material)).material();

if(def->flags & AGF_PRECACHE) // A precache group.
{
Expand All @@ -1446,7 +1445,7 @@ static void initMaterialGroup(ded_group_t *def)
groupNumber = App_Materials().newGroup();
}

App_Materials().group(groupNumber).addMaterial(*mat);
App_Materials().group(groupNumber).addMaterial(material);
}
#if 0 /// @todo $revise-texture-animation
else // An animation group.
Expand All @@ -1457,10 +1456,12 @@ static void initMaterialGroup(ded_group_t *def)
animNumber = App_Materials().newAnimGroup(def->flags & ~AGF_PRECACHE);
}

App_Materials().animGroup(animNumber).addFrame(*mat, gm->tics, gm->randomTics);
App_Materials().animGroup(animNumber).addFrame(material, gm->tics, gm->randomTics);
}
#endif
}
catch(Materials::Manifest::MissingMaterialError const &)
{} // Ignore this error.
catch(Materials::NotFoundError const &er)
{
// Log but otherwise ignore this error.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/src/edit_map.cpp
Expand Up @@ -1809,15 +1809,15 @@ static void assignSurfaceMaterial(Surface *suf, ddstring_t const *materialUriStr
// First try the preferred scheme, then any.
try
{
material = App_Materials().find(materialUri).material();
material = &App_Materials().find(materialUri).material();
}
catch(Materials::NotFoundError const &)
{
// Try any scheme.
try
{
materialUri.setScheme("");
material = App_Materials().find(materialUri).material();
material = &App_Materials().find(materialUri).material();
}
catch(Materials::NotFoundError const &)
{}
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/src/map/p_dmu.cpp
Expand Up @@ -362,7 +362,7 @@ void *P_ToPtr(int type, uint index)
case DMU_MATERIAL:
if(Materials::Manifest *manifest = App_Materials().toManifest(index))
{
return manifest->material();
return &manifest->material();
}
/// @todo Throw exception?
return 0;
Expand Down Expand Up @@ -529,7 +529,7 @@ int P_Callback(int type, uint index, void *context, int (*callback)(void *p, voi
case DMU_MATERIAL:
if(Materials::Manifest *manifest = App_Materials().toManifest(index))
{
return callback(manifest->material(), context);
return callback(&manifest->material(), context);
}
break;

Expand Down
4 changes: 3 additions & 1 deletion doomsday/engine/src/map/p_particle.cpp
Expand Up @@ -1395,7 +1395,7 @@ static int findDefForGenerator(ptcgen_t *gen, void *parameters)
{
try
{
Material *defMat = App_Materials().find(*reinterpret_cast<de::Uri const *>(def->material)).material();
Material *defMat = &App_Materials().find(*reinterpret_cast<de::Uri const *>(def->material)).material();

Material *mat = gen->plane->PS_material;
if(def->flags & PGF_FLOOR_SPAWN)
Expand All @@ -1422,6 +1422,8 @@ static int findDefForGenerator(ptcgen_t *gen, void *parameters)
}
#endif
}
catch(Materials::Manifest::MissingMaterialError const &)
{} // Ignore this error.
catch(Materials::NotFoundError const &)
{} // Ignore this error.
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/src/map/r_world.cpp
Expand Up @@ -1409,7 +1409,7 @@ static Material *chooseFixMaterial(SideDef *s, SideDefSection section)
if(choice2) return choice2;

// We'll assign the special "missing" material...
return App_Materials().find(de::Uri("System", Path("missing"))).material();
return &App_Materials().find(de::Uri("System", Path("missing"))).material();
}

static void addMissingMaterial(SideDef *s, SideDefSection section)
Expand Down
9 changes: 6 additions & 3 deletions doomsday/engine/src/render/r_draw.cpp
Expand Up @@ -180,6 +180,7 @@ MaterialVariantSpec const &Ui_MaterialSpec()
0, -3, 0, false, false, false, false);
}

/// @todo Optimize: Do not search for resources (materials, textures) each frame.
void R_DrawViewBorder()
{
DENG_ASSERT(inited);
Expand Down Expand Up @@ -215,16 +216,18 @@ void R_DrawViewBorder()
glColor4f(1, 1, 1, 1);

// View background.
Material *mat = App_Materials().find(*reinterpret_cast<de::Uri *>(borderGraphicsNames[BG_BACKGROUND])).material();
if(mat)
try
{
MaterialSnapshot const &ms = mat->prepare(Ui_MaterialSpec());
MaterialSnapshot const &ms = App_Materials().find(*reinterpret_cast<de::Uri *>(borderGraphicsNames[BG_BACKGROUND]))
.material().prepare(Ui_MaterialSpec());

GL_BindTexture(reinterpret_cast<texturevariant_s *>(&ms.texture(MTU_PRIMARY)));
GL_DrawCutRectf2Tiled(0, 0, port->geometry.size.width, port->geometry.size.height, ms.dimensions().width(), ms.dimensions().height(), 0, 0,
vd->window.origin.x - border, vd->window.origin.y - border,
vd->window.size.width + 2 * border, vd->window.size.height + 2 * border);
}
catch(Materials::Manifest::MissingMaterialError const &)
{} // Ignore this error.

if(border != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/src/render/r_things.cpp
Expand Up @@ -212,7 +212,7 @@ static void buildSprite(TextureManifest &manifest)
link = true;
}

frame->mat = App_Materials().find(de::Uri("Sprites", manifest.path())).material();
frame->mat = &App_Materials().find(de::Uri("Sprites", manifest.path())).material();
frame->frame[0] = frameNumber;
frame->rotation[0] = rotationNumber;

Expand Down
9 changes: 5 additions & 4 deletions doomsday/engine/src/render/rend_console.cpp
Expand Up @@ -290,14 +290,15 @@ void Rend_ConsoleUpdateBackground()
DENG_ASSERT(inited);
if(!consoleBackgroundMaterialUri || Str_IsEmpty(Uri_Path(consoleBackgroundMaterialUri))) return;

consoleBackgroundMaterial = 0;
try
{
consoleBackgroundMaterial = App_Materials().find(*reinterpret_cast<de::Uri *>(consoleBackgroundMaterialUri)).material();
consoleBackgroundMaterial = &App_Materials().find(*reinterpret_cast<de::Uri *>(consoleBackgroundMaterialUri)).material();
}
catch(Materials::Manifest::MissingMaterialError const &)
{} // Ignore this error.
catch(Materials::NotFoundError const &)
{
consoleBackgroundMaterial = 0;
}
{} // Ignore this error.
}

void Rend_ConsoleToggleFullscreen()
Expand Down
12 changes: 6 additions & 6 deletions doomsday/engine/src/render/rend_main.cpp
Expand Up @@ -1443,7 +1443,7 @@ static void renderPlane(BspLeaf* bspLeaf, planetype_t type, coord_t height,
else
{
Surface *suf = &bspLeaf->sector->planes[elmIdx]->surface;
Material *mat = suf->material? suf->material : App_Materials().find(de::Uri("System", Path("missing"))).material();
Material *mat = suf->material? suf->material : &App_Materials().find(de::Uri("System", Path("missing"))).material();

MaterialSnapshot const &ms = mat->prepare(Rend_MapSurfaceMaterialSpec());
params.glowing = ms.glowStrength();
Expand Down Expand Up @@ -1618,13 +1618,13 @@ static boolean rendHEdgeSection(HEdge* hedge, SideDefSection section,
if(renderTextures == 2)
{
// Lighting debug mode; render using System:gray.
mat = App_Materials().find(de::Uri("System", Path("gray"))).material();
mat = &App_Materials().find(de::Uri("System", Path("gray"))).material();
}
else if(!surface->material ||
((surface->inFlags & SUIF_FIX_MISSING_MATERIAL) && devNoTexFix))
{
// Missing material debug mode; render using System:missing.
mat = App_Materials().find(de::Uri("System", Path("missing"))).material();
mat = &App_Materials().find(de::Uri("System", Path("missing"))).material();
}
else
{
Expand Down Expand Up @@ -2741,10 +2741,10 @@ static void Rend_RenderPlanes()
else if(texMode == 1)
// For debug, render the "missing" texture instead of the texture
// chosen for surfaces to fix the HOMs.
mat = App_Materials().find(de::Uri("System", Path("missing"))).material();
mat = &App_Materials().find(de::Uri("System", Path("missing"))).material();
else
// For lighting debug, render all solid surfaces using the gray texture.
mat = App_Materials().find(de::Uri("System", Path("gray"))).material();
mat = &App_Materials().find(de::Uri("System", Path("gray"))).material();

V2f_Copy(texOffset, suf->visOffset);
// Add the Y offset to orient the Y flipped texture.
Expand Down Expand Up @@ -3900,7 +3900,7 @@ static void Rend_RenderBoundingBoxes()
glDisable(GL_CULL_FACE);

MaterialSnapshot const &ms = App_Materials().
find(de::Uri("System", Path("bbox"))).material()->prepare(Rend_SpriteMaterialSpec());
find(de::Uri("System", Path("bbox"))).material().prepare(Rend_SpriteMaterialSpec());

GL_BindTexture(reinterpret_cast<texturevariant_s *>(&ms.texture(MTU_PRIMARY)));
GL_BlendMode(BM_ADD);
Expand Down
6 changes: 2 additions & 4 deletions doomsday/engine/src/render/rend_model.cpp
Expand Up @@ -1136,13 +1136,11 @@ static void Mod_RenderSubModel(uint number, rendmodelparams_t const *parm)
if(renderTextures == 2)
{
// For lighting debug, render all surfaces using the gray texture.
Material *mat = App_Materials().find(de::Uri("System", Path("gray"))).material();
DENG_ASSERT(mat);

MaterialVariantSpec const &spec =
App_Materials().variantSpecForContext(MC_MODELSKIN, 0, 0, 0, 0, GL_REPEAT, GL_REPEAT,
1, -2, -1, true, true, false, false);
MaterialSnapshot const &ms = mat->prepare(spec);
MaterialSnapshot const &ms =
App_Materials().find(de::Uri("System", Path("gray"))).material().prepare(spec);

skinTexture = &ms.texture(MTU_PRIMARY);
}
Expand Down
20 changes: 9 additions & 11 deletions doomsday/engine/src/render/sky.cpp
Expand Up @@ -166,14 +166,15 @@ static void configureDefaultSky()
{
skylayer_t *layer = &skyLayers[i];
layer->flags = (i == 0? SLF_ACTIVE : 0);
layer->material = 0;
try
{
layer->material = App_Materials().find(de::Uri(DEFAULT_SKY_SPHERE_MATERIAL, RC_NULL)).material();
layer->material = &App_Materials().find(de::Uri(DEFAULT_SKY_SPHERE_MATERIAL, RC_NULL)).material();
}
catch(Materials::Manifest::MissingMaterialError const &)
{} // Ignore this error.
catch(Materials::NotFoundError const &)
{
layer->material = 0;
}
{} // Ignore this error.
layer->offset = DEFAULT_SKY_SPHERE_XOFFSET;
layer->fadeoutLimit = DEFAULT_SKY_SPHERE_FADEOUT_LIMIT;
}
Expand Down Expand Up @@ -294,11 +295,8 @@ void Sky_Configure(ded_sky_t *def)
{
try
{
Material *mat = App_Materials().find(*reinterpret_cast<de::Uri *>(sl->material)).material();
if(mat)
{
Sky_LayerSetMaterial(i, mat);
}
Material *mat = &App_Materials().find(*reinterpret_cast<de::Uri *>(sl->material)).material();
Sky_LayerSetMaterial(i, mat);
}
catch(Materials::NotFoundError const &er)
{
Expand Down Expand Up @@ -797,14 +795,14 @@ static void configureRenderHemisphereStateForLayer(int layer, hemispherecap_t se

if(renderTextures == 2)
{
mat = App_Materials().find(de::Uri("System", Path("gray"))).material();
mat = &App_Materials().find(de::Uri("System", Path("gray"))).material();
}
else
{
mat = Sky_LayerMaterial(layer);
if(!mat)
{
mat = App_Materials().find(de::Uri("System", Path("missing"))).material();
mat = &App_Materials().find(de::Uri("System", Path("missing"))).material();
rs.texXFlip = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/src/render/sprite.cpp
Expand Up @@ -390,7 +390,7 @@ void Rend_DrawPSprite(rendpspriteparams_t const *params)
{
// For lighting debug, render all solid surfaces using the gray texture.
MaterialSnapshot const &ms =
App_Materials().find(de::Uri("System", Path("gray"))).material()->prepare(PSprite_MaterialSpec());
App_Materials().find(de::Uri("System", Path("gray"))).material().prepare(PSprite_MaterialSpec());

GL_BindTexture(reinterpret_cast<texturevariant_s *>(&ms.texture(MTU_PRIMARY)));
glEnable(GL_TEXTURE_2D);
Expand Down Expand Up @@ -872,7 +872,7 @@ static Material::Variant *chooseSpriteMaterial(rendspriteparams_t const &p)
if(renderTextures == 2)
{
// For lighting debug, render all solid surfaces using the gray texture.
return App_Materials().find(de::Uri("System", Path("gray"))).material()->chooseVariant(Rend_SpriteMaterialSpec(), true);
return App_Materials().find(de::Uri("System", Path("gray"))).material().chooseVariant(Rend_SpriteMaterialSpec(), true);
}

// Use the pre-chosen sprite.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/src/resource/api_material.cpp
Expand Up @@ -13,7 +13,7 @@ DENG_EXTERN_C Material *DD_MaterialForTextureUri(uri_s const *textureUri)
{
de::Uri uri = App_Textures()->find(reinterpret_cast<de::Uri const &>(*textureUri)).composeUri();
uri.setScheme(DD_MaterialSchemeNameForTextureScheme(uri.scheme()));
return App_Materials().find(uri).material();
return &App_Materials().find(uri).material();
}
catch(de::Materials::UnknownSchemeError const &er)
{
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/src/resource/materialarchive.cpp
Expand Up @@ -100,7 +100,7 @@ static Material *findRecordMaterial(Records &records, SerialId id)
Material *material = 0;
try
{
material = App_Materials().find(Uri(records.stringRef(id), RC_NULL)).material();
material = &App_Materials().find(Uri(records.stringRef(id), RC_NULL)).material();
}
catch(Materials::NotFoundError const &)
{} // Ignore this error.
Expand Down Expand Up @@ -151,7 +151,7 @@ struct MaterialArchive::Instance
{
MaterialManifest *manifest = App_Materials().toManifest(i);
SerialId id = insertRecord(manifest->composeUri());
records.setUserPointer(id, manifest->material());
records.setUserPointer(id, &manifest->material());
records.setUserValue(id, true);
}
}
Expand Down

0 comments on commit 78ed650

Please sign in to comment.