Skip to content

Commit

Permalink
Refactor|Material: Relocated material "isCustom" state to MaterialBind
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jan 10, 2013
1 parent ac3044b commit ff0aea3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 43 deletions.
8 changes: 2 additions & 6 deletions doomsday/engine/include/resource/material.h
Expand Up @@ -84,13 +84,12 @@ extern "C" {
* Construct a new material.
*
* @param flags @see materialFlags
* @param isCustom @c true= the material does not come from the original game.
* @param def Definition for the material, if any.
* @param dimensions Dimensions of the material in map coordinate space units.
* @param envClass Environment class for the material.
*/
material_t *Material_New(short flags, boolean isCustom, ded_material_t *def,
Size2Raw *dimensions, material_env_class_t envClass);
material_t *Material_New(short flags, ded_material_t *def, Size2Raw *dimensions,
material_env_class_t envClass);

void Material_Delete(material_t *mat);

Expand Down Expand Up @@ -154,9 +153,6 @@ void Material_SetFlags(material_t *mat, short flags);
*/
boolean Material_IsValid(material_t const *mat);

/// @return @c true if Material is not derived from an original game resource.
boolean Material_IsCustom(material_t const *mat);

/// @return @c true= the material is animated.
boolean Material_IsAnimated(material_t const *mat);

Expand Down
8 changes: 6 additions & 2 deletions doomsday/engine/include/resource/materialbind.h
Expand Up @@ -55,9 +55,9 @@ class MaterialBind : public PathTree::Node
* - When the bound material is changed/first-configured.
* - When said material's "custom" state changes.
*
* @param mat Material to link the definitions of.
* @param bind MaterialBind to link the definitions of.
*/
void linkDefinitions(material_t *mat);
void linkDefinitions(MaterialBind const &bind);

/**
* Zeroes all links to definitions. Should be called when the
Expand All @@ -72,6 +72,7 @@ class MaterialBind : public PathTree::Node
virtual ~MaterialBind();

void setId(materialid_t newId);
void setCustom(bool yes);

/**
* Returns the owning scheme of the material bind.
Expand All @@ -95,6 +96,9 @@ class MaterialBind : public PathTree::Node
/// @return Unique identifier associated with this.
materialid_t id() const;

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

/// @return Material associated with this; otherwise @c NULL.
material_t *material() const;

Expand Down
23 changes: 7 additions & 16 deletions doomsday/engine/src/resource/material.cpp
Expand Up @@ -58,8 +58,6 @@ struct material_s
bool inAnimGroup;
#endif

bool isCustom;

/// Detail texture layer & properties.
de::Texture *detailTex;
float detailScale;
Expand All @@ -75,14 +73,13 @@ struct material_s
/// Current prepared state.
byte prepared;

material_s(short _flags, bool _isCustom, ded_material_t *_def,
material_s(short _flags, ded_material_t *_def,
Size2Raw &_dimensions, material_env_class_t _envClass)
: def(_def), envClass(_envClass), primaryBind(0),
dimensions(Size2_NewFromRaw(&_dimensions)), flags(_flags),
#ifdef LIBDENG_OLD_MATERIAL_ANIM_METHOD
inAnimGroup(false),
#endif
isCustom(_isCustom),
detailTex(0), detailScale(0), detailStrength(0),
shinyTex(0), shinyBlendmode(BM_ADD), shinyStrength(0), shinyMaskTex(0)
{
Expand Down Expand Up @@ -273,10 +270,10 @@ void MaterialAnim::reset()

using namespace de;

material_t *Material_New(short flags, boolean isCustom, ded_material_t *def,
material_t *Material_New(short flags, ded_material_t *def,
Size2Raw *dimensions, material_env_class_t envClass)
{
return new material_s(flags, CPP_BOOL(isCustom), def, *dimensions, envClass);
return new material_s(flags, def, *dimensions, envClass);
}

void Material_Delete(material_t *mat)
Expand Down Expand Up @@ -323,17 +320,17 @@ void Material_SetDefinition(material_t *mat, struct ded_material_s *def)

if(!mat->def) return;

mat->flags = mat->def->flags;
MaterialBind &bind = *App_Materials()->toMaterialBind(mat->primaryBind);

mat->flags = mat->def->flags;
Size2Raw size(def->width, def->height);
Material_SetDimensions(mat, &size);

Material_SetEnvironmentClass(mat, S_MaterialEnvClassForUri(def->uri));

// Update custom status.
/// @todo This should take into account the whole definition, not just whether
/// the primary layer's first texture is custom or not.
mat->isCustom = false;
bind.setCustom(false);
if(def->layers[0].stageCount.num > 0 && def->layers[0].stages[0].texture)
{
de::Uri *texUri = reinterpret_cast<de::Uri *>(def->layers[0].stages[0].texture);
Expand All @@ -342,7 +339,7 @@ void Material_SetDefinition(material_t *mat, struct ded_material_s *def)
TextureManifest &manifest = App_Textures()->find(*texUri);
if(Texture *tex = manifest.texture())
{
mat->isCustom = tex->flags().testFlag(Texture::Custom);
bind.setCustom(tex->flags().testFlag(Texture::Custom));
}
}
catch(Textures::NotFoundError const &)
Expand Down Expand Up @@ -410,12 +407,6 @@ void Material_SetFlags(material_t *mat, short flags)
mat->flags = flags;
}

boolean Material_IsCustom(material_t const *mat)
{
DENG2_ASSERT(mat);
return boolean( mat->isCustom );
}

boolean Material_IsAnimated(material_t const *mat)
{
#ifdef LIBDENG_OLD_MATERIAL_ANIM_METHOD
Expand Down
34 changes: 22 additions & 12 deletions doomsday/engine/src/resource/materialbind.cpp
Expand Up @@ -32,25 +32,25 @@ MaterialBind::Info::Info()
clearDefinitionLinks();
}

void MaterialBind::Info::linkDefinitions(material_t *mat)
void MaterialBind::Info::linkDefinitions(MaterialBind const &bind)
{
bool isCustom = (mat? Material_IsCustom(mat) : false);
material_t *mat = bind.material();

// Surface decorations (lights and models).
decorationDefs[0] = Def_GetDecoration(mat, 0, isCustom);
decorationDefs[1] = Def_GetDecoration(mat, 1, isCustom);
decorationDefs[0] = Def_GetDecoration(mat, 0, bind.isCustom());
decorationDefs[1] = Def_GetDecoration(mat, 1, bind.isCustom());

// Reflection (aka shiny surface).
reflectionDefs[0] = Def_GetReflection(mat, 0, isCustom);
reflectionDefs[1] = Def_GetReflection(mat, 1, isCustom);
reflectionDefs[0] = Def_GetReflection(mat, 0, bind.isCustom());
reflectionDefs[1] = Def_GetReflection(mat, 1, bind.isCustom());

// Generator (particles).
ptcgenDefs[0] = Def_GetGenerator(mat, 0, isCustom);
ptcgenDefs[1] = Def_GetGenerator(mat, 1, isCustom);
ptcgenDefs[0] = Def_GetGenerator(mat, 0, bind.isCustom());
ptcgenDefs[1] = Def_GetGenerator(mat, 1, bind.isCustom());

// Detail texture.
detailtextureDefs[0] = Def_GetDetailTex(mat, 0, isCustom);
detailtextureDefs[1] = Def_GetDetailTex(mat, 1, isCustom);
detailtextureDefs[0] = Def_GetDetailTex(mat, 0, bind.isCustom());
detailtextureDefs[1] = Def_GetDetailTex(mat, 1, bind.isCustom());
}

void MaterialBind::Info::clearDefinitionLinks()
Expand All @@ -73,8 +73,8 @@ struct MaterialBind::Instance
/// of the first derived variant of the associated Material.
MaterialBind::Info *extInfo;

Instance() : material(0), guid(0), extInfo(0)
{}
/// @c true if the material is not derived from an original game resource.
bool isCustom;
};

MaterialBind::MaterialBind(PathTree::NodeArgs const &args)
Expand All @@ -100,6 +100,11 @@ void MaterialBind::setId(materialid_t id)
d->guid = id;
}

void MaterialBind::setCustom(bool yes)
{
d->isCustom = yes;
}

MaterialScheme &MaterialBind::scheme() const
{
LOG_AS("MaterialBind::scheme");
Expand Down Expand Up @@ -131,6 +136,11 @@ materialid_t MaterialBind::id() const
return d->guid;
}

bool MaterialBind::isCustom() const
{
return d->isCustom;
}

material_t *MaterialBind::material() const
{
return d->material;
Expand Down
15 changes: 8 additions & 7 deletions doomsday/engine/src/resource/materials.cpp
Expand Up @@ -111,7 +111,7 @@ static void updateMaterialBindInfo(MaterialBind &mb, bool canCreateInfo)
mb.attachInfo(*info);
}

info->linkDefinitions(mb.material());
info->linkDefinitions(mb);
}

/// A list of materials.
Expand Down Expand Up @@ -598,18 +598,18 @@ material_t *Materials::newFromDef(ded_material_t &def)
Size2Raw dimensions(MAX_OF(0, def.width), MAX_OF(0, def.height));
material_env_class_t envClass = S_MaterialEnvClassForUri(reinterpret_cast<struct uri_s const *>(&uri));

material_t *mat = Material_New(def.flags, tex->flags().testFlag(Texture::Custom), &def,
&dimensions, envClass);
material_t *mat = Material_New(def.flags, &def, &dimensions, envClass);
d->materials.push_back(mat);

if(!bind)
{
newBind(scheme(uri.scheme()), uri.path(), mat);
bind = &newBind(scheme(uri.scheme()), uri.path(), mat);
}
else
{
bind->setMaterial(mat);
}
bind->setCustom(tex->flags().testFlag(Texture::Custom));

return mat;
}
Expand Down Expand Up @@ -939,13 +939,14 @@ static void printMaterialInfo(material_t &mat)
ded_material_t const *def = Material_Definition(&mat);

/// @todo kludge: Should not use App_Materials() here.
Uri uri = App_Materials()->toMaterialBind(Material_PrimaryBind(&mat))->composeUri();
MaterialBind &bind = *App_Materials()->toMaterialBind(Material_PrimaryBind(&mat));
Uri uri = bind.composeUri();
QByteArray path = uri.asText().toUtf8();

// Print summary:
Con_Printf("Material \"%s\" [%p] x%u origin:%s\n",
path.constData(), (void *) &mat, Material_VariantCount(&mat),
!Material_IsCustom(&mat)? "game" : (def->autoGenerated? "addon" : "def"));
!bind.isCustom()? "game" : (def->autoGenerated? "addon" : "def"));

if(Material_Width(&mat) <= 0 || Material_Height(&mat) <= 0)
Con_Printf("Dimensions: unknown (not yet prepared)\n");
Expand Down Expand Up @@ -1009,7 +1010,7 @@ static void printMaterialSummary(MaterialBind &bind, bool printSchemeName = true

Con_FPrintf(!material? CPF_LIGHT : CPF_WHITE,
"%-*s %-6s x%u\n", printSchemeName? 22 : 14, path.constData(),
!material? "unknown" : (!Material_IsCustom(material) ? "game" : (Material_Definition(material)->autoGenerated? "addon" : "def")),
!material? "unknown" : (!bind.isCustom() ? "game" : (Material_Definition(material)->autoGenerated? "addon" : "def")),
!material? 0 : Material_VariantCount(material));
}

Expand Down

0 comments on commit ff0aea3

Please sign in to comment.