From cc47f46e8a0902909b13a0885623882de0801c63 Mon Sep 17 00:00:00 2001 From: danij Date: Wed, 2 Jan 2013 12:23:00 +0000 Subject: [PATCH] Refactor|MaterialBind: Minor MaterialBind cleanup refactorings --- .../engine/include/resource/materialbind.h | 21 +++++++- doomsday/engine/src/resource/materialbind.cpp | 39 +++++++++++++- doomsday/engine/src/resource/materials.cpp | 52 ++++--------------- 3 files changed, 66 insertions(+), 46 deletions(-) diff --git a/doomsday/engine/include/resource/materialbind.h b/doomsday/engine/include/resource/materialbind.h index 37b72b7c8f..762bdcf79e 100644 --- a/doomsday/engine/include/resource/materialbind.h +++ b/doomsday/engine/include/resource/materialbind.h @@ -37,7 +37,8 @@ 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 { @@ -45,6 +46,24 @@ namespace de { 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: diff --git a/doomsday/engine/src/resource/materialbind.cpp b/doomsday/engine/src/resource/materialbind.cpp index cfd49bcbcf..f83fa73203 100644 --- a/doomsday/engine/src/resource/materialbind.cpp +++ b/doomsday/engine/src/resource/materialbind.cpp @@ -18,6 +18,7 @@ */ #include "de_base.h" +#include "de_defs.h" #include #include "uri.hh" @@ -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. @@ -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; } @@ -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; diff --git a/doomsday/engine/src/resource/materials.cpp b/doomsday/engine/src/resource/materials.cpp index 62a4bae394..01d8eb2631 100644 --- a/doomsday/engine/src/resource/materials.cpp +++ b/doomsday/engine/src/resource/materials.cpp @@ -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 @@ -44,9 +41,6 @@ #include #include -#include - -#include "resource/materialbind.h" #include "resource/materials.h" #include "resource/materialsnapshot.h" @@ -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 { @@ -421,7 +384,10 @@ void Materials::clearDefinitionLinks() PathTreeIterator iter((*i)->index().leafNodes()); while(iter.hasNext()) { - clearBindingDefinitionLinks(iter.next()); + if(MaterialBind::Info *info = iter.next().info()) + { + info->clearDefinitionLinks(); + } } } }