Skip to content

Commit

Permalink
Refactor|MaterialBind: Minor MaterialBind cleanup refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jan 6, 2013
1 parent 14c5797 commit cc47f46
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
21 changes: 20 additions & 1 deletion doomsday/engine/include/resource/materialbind.h
Expand Up @@ -37,14 +37,33 @@ namespace de {
public:
/**
* Contains extended info about a material binding.
* @note POD object.
* There are two links for each definition type, the first (index @c 0)
* for original game data and the second for external data.
*/
struct Info
{
ded_decor_t *decorationDefs[2];
ded_detailtexture_t *detailtextureDefs[2];
ded_ptcgen_t *ptcgenDefs[2];
ded_reflection_t *reflectionDefs[2];

Info();

/**
* Update the info with new linked definitions. Should be called:
*
* - When the bound material is changed/first-configured.
* - When said material's "custom" state changes.
*
* @param mat Material to link the definitions of.
*/
void linkDefinitions(material_t *mat);

/**
* Zeroes all links to definitions. Should be called when the
* definition database is reset.
*/
void clearDefinitionLinks();
};

public:
Expand Down
39 changes: 37 additions & 2 deletions doomsday/engine/src/resource/materialbind.cpp
Expand Up @@ -18,6 +18,7 @@
*/

#include "de_base.h"
#include "de_defs.h"
#include <de/memory.h>

#include "uri.hh"
Expand All @@ -26,6 +27,40 @@

namespace de {

MaterialBind::Info::Info()
{
clearDefinitionLinks();
}

void MaterialBind::Info::linkDefinitions(material_t *mat)
{
bool isCustom = (mat? Material_IsCustom(mat) : false);

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

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

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

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

void MaterialBind::Info::clearDefinitionLinks()
{
decorationDefs[0] = decorationDefs[1] = 0;
detailtextureDefs[0] = detailtextureDefs[1] = 0;
ptcgenDefs[0] = ptcgenDefs[1] = 0;
reflectionDefs[0] = reflectionDefs[1] = 0;
}

struct MaterialBind::Instance
{
/// Material associated with this.
Expand All @@ -51,7 +86,7 @@ MaterialBind::MaterialBind(PathTree::NodeArgs const &args)
MaterialBind::~MaterialBind()
{
Info *detachedInfo = detachInfo();
if(detachedInfo) M_Free(detachedInfo);
if(detachedInfo) delete detachedInfo;
delete d;
}

Expand Down Expand Up @@ -113,7 +148,7 @@ void MaterialBind::setMaterial(material_t *newMaterial)
// Any extended info will be invalid after this op, so destroy it
// (it will automatically be rebuilt later, if subsequently needed).
MaterialBind::Info *detachedInfo = detachInfo();
if(detachedInfo) M_Free(detachedInfo);
if(detachedInfo) delete detachedInfo;

// Associate with the new Material.
d->material = newMaterial;
Expand Down
52 changes: 9 additions & 43 deletions doomsday/engine/src/resource/materials.cpp
Expand Up @@ -25,14 +25,11 @@

#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_filesys.h"
#include "de_network.h"
#include "de_network.h" // playback / clientPaused
#include "de_render.h"
#include "de_graphics.h"
#include "de_misc.h"
#include "de_audio.h" // For texture, environmental audio properties.
#include "de_resource.h"
#include "de_misc.h" // M_NumDigits()
#include "de_audio.h" // For environmental audio properties.

#include "gl/sys_opengl.h" /// @todo: get rid of this -jk

Expand All @@ -44,9 +41,6 @@
#include <de/PathTree>
#include <de/memory.h>

#include <cstring>

#include "resource/materialbind.h"
#include "resource/materials.h"
#include "resource/materialsnapshot.h"

Expand All @@ -61,50 +55,19 @@ D_CMD(PrintMaterialStats);

namespace de {

static void clearBindingDefinitionLinks(MaterialBind &mb)
{
MaterialBind::Info *info = mb.info();
if(info)
{
info->decorationDefs[0] = info->decorationDefs[1] = NULL;
info->detailtextureDefs[0] = info->detailtextureDefs[1] = NULL;
info->ptcgenDefs[0] = info->ptcgenDefs[1] = NULL;
info->reflectionDefs[0] = info->reflectionDefs[1] = NULL;
}
}

static void updateMaterialBindInfo(MaterialBind &mb, bool canCreateInfo)
{
MaterialBind::Info *info = mb.info();
material_t *mat = mb.material();
bool isCustom = (mat? Material_IsCustom(mat) : false);

if(!info)
{
if(!canCreateInfo) return;

// Create new info and attach to this binding.
info = (MaterialBind::Info *) M_Malloc(sizeof *info);
if(!info) Con_Error("Materials::updateMaterialBindInfo: Failed on allocation of %lu bytes for new MaterialBindInfo.", (unsigned long) sizeof *info);

info = new MaterialBind::Info();
mb.attachInfo(*info);
}

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

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

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

// Detail texture.
info->detailtextureDefs[0] = Def_GetDetailTex(mat, 0, isCustom);
info->detailtextureDefs[1] = Def_GetDetailTex(mat, 1, isCustom);
info->linkDefinitions(mb.material());
}

typedef struct materialvariantspecificationlistnode_s {
Expand Down Expand Up @@ -421,7 +384,10 @@ void Materials::clearDefinitionLinks()
PathTreeIterator<MaterialScheme::Index> iter((*i)->index().leafNodes());
while(iter.hasNext())
{
clearBindingDefinitionLinks(iter.next());
if(MaterialBind::Info *info = iter.next().info())
{
info->clearDefinitionLinks();
}
}
}
}
Expand Down

0 comments on commit cc47f46

Please sign in to comment.