Skip to content

Commit

Permalink
Refactor: Relocated MaterialBind into new source files
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Dec 29, 2012
1 parent 5827a8f commit b7a6ad6
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 159 deletions.
4 changes: 3 additions & 1 deletion doomsday/engine/engine.pro
Expand Up @@ -298,6 +298,7 @@ DENG_HEADERS += \
include/resource/image.h \
include/resource/lumpcache.h \
include/resource/material.h \
include/resource/materialbind.h \
include/resource/materials.h \
include/resource/materialsnapshot.h \
include/resource/materialvariant.h \
Expand Down Expand Up @@ -585,8 +586,9 @@ SOURCES += \
src/resource/image.cpp \
src/resource/material.cpp \
src/resource/materialarchive.c \
src/resource/materialsnapshot.cpp \
src/resource/materialbind.cpp \
src/resource/materials.cpp \
src/resource/materialsnapshot.cpp \
src/resource/materialvariant.cpp \
src/resource/models.cpp \
src/resource/patch.cpp \
Expand Down
124 changes: 124 additions & 0 deletions doomsday/engine/include/resource/materialbind.h
@@ -0,0 +1,124 @@
/** @file materialbind.h Material Bind.
*
* @author Copyright &copy; 2011-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCE_MATERIALBIND_H
#define LIBDENG_RESOURCE_MATERIALBIND_H

#ifdef __cplusplus

#include "def_data.h"
#include "resource/materials.h"

namespace de {

/**
* Contains extended info about a material binding (see MaterialBind).
* @note POD object.
*/
struct MaterialBindInfo
{
ded_decor_t *decorationDefs[2];
ded_detailtexture_t *detailtextureDefs[2];
ded_ptcgen_t *ptcgenDefs[2];
ded_reflection_t *reflectionDefs[2];
};

class MaterialBind
{
public:
MaterialBind(MaterialScheme::Index::Node &_direcNode, materialid_t id)
: direcNode(&_direcNode), asocMaterial(0), guid(id), extInfo(0)
{}

~MaterialBind()
{
MaterialBindInfo *detachedInfo = detachInfo();
if(detachedInfo) M_Free(detachedInfo);
}

/// @return Unique identifier associated with this.
materialid_t id() const { return guid; }

/// @return Index node associated with this.
MaterialScheme::Index::Node &directoryNode() const { return *direcNode; }

/// @return Material associated with this else @c NULL.
material_t *material() const { return asocMaterial; }

/// @return Extended info owned by this else @c NULL.
MaterialBindInfo *info() const { return extInfo; }

/**
* Attach extended info data to this. If existing info is present it is replaced.
* MaterialBind is given ownership of the info.
* @param info Extended info data to attach.
*/
MaterialBind &attachInfo(MaterialBindInfo &info);

/**
* Detach any extended info owned by this and relinquish ownership to the caller.
* @return Extended info or else @c NULL if not present.
*/
MaterialBindInfo *detachInfo();

/**
* Change the Material associated with this binding.
*
* @note Only the relationship from MaterialBind to @a material changes!
*
* @post If @a material differs from that currently associated with this, any
* MaterialBindInfo presently owned by this will destroyed (its invalid).
*
* @param material New Material to associate with this.
* @return This instance.
*/
MaterialBind &setMaterial(material_t *material);

/// @return Detail texture definition associated with this else @c NULL
ded_detailtexture_t *detailTextureDef() const;

/// @return Decoration definition associated with this else @c NULL
ded_decor_t *decorationDef() const;

/// @return Particle generator definition associated with this else @c NULL
ded_ptcgen_t *ptcGenDef() const;

/// @return Reflection definition associated with this else @c NULL
ded_reflection_t *reflectionDef() const;

private:
/// This binding's node in the directory.
MaterialScheme::Index::Node *direcNode;

/// Material associated with this.
material_t *asocMaterial;

/// Unique identifier.
materialid_t guid;

/// Extended info about this binding. Will be attached upon successfull preparation
/// of the first derived variant of the associated Material.
MaterialBindInfo *extInfo;
};

} // namespace de

#endif

#endif /* LIBDENG_RESOURCE_MATERIALBIND_H */
89 changes: 89 additions & 0 deletions doomsday/engine/src/resource/materialbind.cpp
@@ -0,0 +1,89 @@
/** @file materialbind.cpp Material Bind.
*
* @author Copyright &copy; 2011-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

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

#include "uri.hh"
#include "resource/materialbind.h"

namespace de {

MaterialBind &MaterialBind::setMaterial(material_t *newMaterial)
{
if(asocMaterial != newMaterial)
{
// Any extended info will be invalid after this op, so destroy it
// (it will automatically be rebuilt later, if subsequently needed).
MaterialBindInfo *detachedInfo = detachInfo();
if(detachedInfo) M_Free(detachedInfo);

// Associate with the new Material.
asocMaterial = newMaterial;
}
return *this;
}

MaterialBind &MaterialBind::attachInfo(MaterialBindInfo &info)
{
LOG_AS("MaterialBind::attachInfo");
if(extInfo)
{
#if _DEBUG
Uri uri = App_Materials()->composeUri(guid);
LOG_DEBUG("Info already present for \"%s\", will replace.") << uri;
#endif
M_Free(extInfo);
}
extInfo = &info;
return *this;
}

MaterialBindInfo *MaterialBind::detachInfo()
{
MaterialBindInfo *retInfo = extInfo;
extInfo = 0;
return retInfo;
}

ded_detailtexture_t *MaterialBind::detailTextureDef() const
{
if(!extInfo || !asocMaterial || !Material_Prepared(asocMaterial)) return 0;
return extInfo->detailtextureDefs[Material_Prepared(asocMaterial)-1];
}

ded_decor_t *MaterialBind::decorationDef() const
{
if(!extInfo || !asocMaterial || !Material_Prepared(asocMaterial)) return 0;
return extInfo->decorationDefs[Material_Prepared(asocMaterial)-1];
}

ded_ptcgen_t *MaterialBind::ptcGenDef() const
{
if(!extInfo || !asocMaterial || !Material_Prepared(asocMaterial)) return 0;
return extInfo->ptcgenDefs[Material_Prepared(asocMaterial)-1];
}

ded_reflection_t *MaterialBind::reflectionDef() const
{
if(!extInfo || !asocMaterial || !Material_Prepared(asocMaterial)) return 0;
return extInfo->reflectionDefs[Material_Prepared(asocMaterial)-1];
}

} // namespace de

0 comments on commit b7a6ad6

Please sign in to comment.