Skip to content

Commit

Permalink
Refactor|Texture: Cleanup refactorings for Texture
Browse files Browse the repository at this point in the history
The lifetimes of Texture::Variant instances are now managed via
the interface of the 'superior' Texture.
  • Loading branch information
danij-deng committed Jan 17, 2013
1 parent f5c75d4 commit 0c582f7
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 109 deletions.
6 changes: 3 additions & 3 deletions doomsday/engine/include/resource/materialsnapshot.h
Expand Up @@ -66,11 +66,11 @@ class MaterialSnapshot
#endif

public:
/// Invalid texture unit referenced. @ingroup errors
DENG2_ERROR(InvalidUnitError);
/// The referenced (texture) unit does not exist. @ingroup errors
DENG2_ERROR(UnknownUnitError);

#ifdef __CLIENT__
/// Invalid decoration referenced. @ingroup errors
/// The referenced decoration does not exist. @ingroup errors
DENG2_ERROR(UnknownDecorationError);
#endif

Expand Down
158 changes: 80 additions & 78 deletions doomsday/engine/include/resource/texture.h
Expand Up @@ -21,12 +21,6 @@
#ifndef LIBDENG_RESOURCE_TEXTURE_H
#define LIBDENG_RESOURCE_TEXTURE_H

#include "texturevariantspec.h"

#ifdef __cplusplus
extern "C" {
#endif

/// @addtogroup resource
///@{
typedef enum {
Expand All @@ -42,13 +36,9 @@ typedef enum {
} texture_analysisid_t;

#define VALID_TEXTURE_ANALYSISID(id) (\
(id) >= TEXTURE_ANALYSIS_FIRST && (id) < TEXTURE_ANALYSIS_COUNT)
(int)(id) >= TEXTURE_ANALYSIS_FIRST && (int)(id) < TEXTURE_ANALYSIS_COUNT)
///@}

#ifdef __cplusplus
} // extern "C"
#endif

#ifdef __cplusplus

#include <QFlag>
Expand All @@ -57,6 +47,8 @@ typedef enum {
#include <QSize>
#include <de/Error>

struct texturevariantspecification_t;

namespace de {

class TextureManifest;
Expand All @@ -67,6 +59,8 @@ class TextureManifest;
*/
class Texture
{
struct Instance; // Needs to be friended by Variant

public:
/**
* Classification/processing flags
Expand Down Expand Up @@ -107,7 +101,7 @@ class Texture
};
Q_DECLARE_FLAGS(Flags, Flag)

public:
private:
/**
* @param generalCase Texture from which this variant is derived.
* @param spec Specification used to derive this variant.
Expand All @@ -116,6 +110,7 @@ class Texture
Variant(Texture &generalCase, texturevariantspecification_t const &spec);
~Variant();

public:
/**
* Retrieve the general case for this variant. Allows for a variant
* reference to be used in place of a texture (implicit indirection).
Expand Down Expand Up @@ -159,6 +154,9 @@ class Texture

void setGLName(uint glName);

friend class Texture;
friend struct Texture::Instance;

private:
struct Instance;
Instance *d;
Expand Down Expand Up @@ -189,94 +187,46 @@ class Texture
* @param userData User data to associate with the resultant texture.
*/
Texture(TextureManifest &manifest, void *userData = 0);

~Texture();

/// @return Provides access to the classification/processing flags.
Flags const &flags() const;

/// @return Provides access to the classification/processing flags.
Flags &flags();

/**
* Returns the TextureManifest derived to yield the texture.
*/
TextureManifest &manifest() const;

/**
* Retrieve the value of the associated user data pointer.
* @return Associated data pointer value.
*/
void *userDataPointer() const;

/**
* Set the user data pointer value. Ownership of the data is not given to
* this instance.
*
* @note If already set the old value will be replaced (so if it points
* to some dynamically constructed data/resource it is the caller's
* responsibility to release it beforehand).
*
* @param userData User data pointer value.
*/
void setUserDataPointer(void *userData);
/// Returns the dimensions of the texture in map coordinate space units.
QSize const &dimensions() const;

/// Destroy all analyses for the texture.
void clearAnalyses();
/// Returns the world width of the texture in map coordinate space units.
inline int width() const { return dimensions().width(); }

/**
* Retrieve the value of an identified @a analysis data pointer.
* @return Associated data pointer value.
**/
void *analysisDataPointer(texture_analysisid_t analysis) const;
/// Returns the world height of the texture in map coordinate space units.
inline int height() const { return dimensions().height(); }

/**
* Set the value of an identified @a analysis data pointer. Ownership of
* the data is not given to this instance.
*
* @note If already set the old value will be replaced (so if it points
* to some dynamically constructed data/resource it is the caller's
* responsibility to release it beforehand).
* Change the world dimensions of the texture.
* @param newDimensions New dimensions in map coordinate space units.
*
* @param analysis Identifier of the data being attached.
* @param data Data to be attached.
*/
void setAnalysisDataPointer(texture_analysisid_t analysis, void *data);

/**
* Returns the world width of the texture in map coordinate space units.
* @todo Update any Materials (and thus Surfaces) which reference this.
*/
int width() const;

/**
* Returns the world height of the texture in map coordinate space units.
*/
int height() const;

/**
* Returns the world dimensions [width, height] of the texture in map
* coordinate space units.
*/
QSize const &dimensions() const;
void setDimensions(QSize const &newDimensions);

/**
* Change the world width of the texture.
* @param newWidth New width in map coordinate space units.
*
* @todo Update any Materials (and thus Surfaces) which reference this.
*/
void setWidth(int newWidth);

/**
* Change the world height of the texture.
* @param newHeight New height in map coordinate space units.
*
* @todo Update any Materials (and thus Surfaces) which reference this.
*/
void setHeight(int newHeight);

/**
* Change the world dimensions of the texture.
* @param newDimensions New dimensions [width, height] in map coordinate space units.
*/
void setDimensions(QSize const &newDimensions);

/**
* Returns the world origin offset of texture in map coordinate space units.
*/
Expand All @@ -288,10 +238,16 @@ class Texture
*/
void setOrigin(QPoint const &newOrigin);

/// @return Provides access to the classification/processing flags.
Flags const &flags() const;

/// @return Provides access to the classification/processing flags.
Flags &flags();

/**
* Returns the number of variants for the texture.
* Destroys all derived variants for the texture.
*/
uint variantCount() const;
void clearVariants();

/**
* Choose/create a variant of the texture which fulfills @a spec.
Expand All @@ -306,12 +262,58 @@ class Texture
texturevariantspecification_t const &spec, bool canCreate = false);

/**
* Provides access to the list of variant textures for efficent traversals.
* Provides access to the list of variant instances for efficent traversal.
*/
Variants const &variants() const;

/**
* Returns the number of variants for the texture.
*/
uint variantCount() const;

/**
* Destroys all analyses for the texture.
*/
void clearAnalyses();

/**
* Retrieve the value of an identified @a analysis data pointer.
* @return Associated data pointer value.
*/
void *analysisDataPointer(texture_analysisid_t analysis) const;

/**
* Set the value of an identified @a analysis data pointer. Ownership of
* the data is not given to this instance.
*
* @note If already set the old value will be replaced (so if it points
* to some dynamically constructed data/resource it is the caller's
* responsibility to release it beforehand).
*
* @param analysis Identifier of the data being attached.
* @param data Data to be attached.
*/
void setAnalysisDataPointer(texture_analysisid_t analysis, void *data);

/**
* Retrieve the value of the associated user data pointer.
* @return Associated data pointer value.
*/
void *userDataPointer() const;

/**
* Set the user data pointer value. Ownership of the data is not given to
* this instance.
*
* @note If already set the old value will be replaced (so if it points
* to some dynamically constructed data/resource it is the caller's
* responsibility to release it beforehand).
*
* @param userData User data pointer value.
*/
void setUserDataPointer(void *userData);

private:
struct Instance;
Instance *d;
};

Expand Down
8 changes: 4 additions & 4 deletions doomsday/engine/src/resource/materialsnapshot.cpp
Expand Up @@ -164,8 +164,8 @@ Texture::Variant &MaterialSnapshot::texture(int index) const
{
if(!hasTexture(index))
{
/// @throw InvalidUnitError Attempt to dereference with an invalid index.
throw InvalidUnitError("MaterialSnapshot::texture", QString("Invalid texture index %1").arg(index));
/// @throw UnknownUnitError Attempt to dereference with an invalid index.
throw UnknownUnitError("MaterialSnapshot::texture", QString("Invalid texture index %1").arg(index));
}
return *d->stored.textures[index];
}
Expand All @@ -175,8 +175,8 @@ rtexmapunit_t const &MaterialSnapshot::unit(rtexmapunitid_t id) const
{
if(id < 0 || id >= NUM_TEXMAP_UNITS)
{
/// @throw InvalidUnitError Attempt to obtain a reference to a unit with an invalid id.
throw InvalidUnitError("MaterialSnapshot::unit", QString("Invalid unit id %1").arg(id));
/// @throw UnknownUnitError Attempt to obtain a reference to a unit with an invalid id.
throw UnknownUnitError("MaterialSnapshot::unit", QString("Invalid unit id %1").arg(id));
}
return d->stored.units[id];
}
Expand Down
40 changes: 16 additions & 24 deletions doomsday/engine/src/resource/texture.cpp
Expand Up @@ -68,21 +68,21 @@ struct Texture::Instance

void clearVariants()
{
DENG2_FOR_EACH(Variants, i, variants)
while(!variants.isEmpty())
{
Texture::Variant *variant = variants.takeFirst();
#if _DEBUG
uint glName = (*i)->glName();
uint glName = variant->glName();
if(glName)
{
LOG_AS("Texture::clearVariants")
LOG_WARNING("GLName (%i) still set for a variant of \"%s\" [%p]. Perhaps it wasn't released?")
<< glName << manifest.composeUri() << (void *)this;
GL_PrintTextureVariantSpecification(&(*i)->spec());
GL_PrintTextureVariantSpecification(&variant->spec());
}
#endif
delete *i;
delete variant;
}
variants.clear();
}
};

Expand Down Expand Up @@ -143,37 +143,24 @@ uint Texture::variantCount() const
return uint(d->variants.count());
}

int Texture::width() const
{
return d->dimensions.width();
}

void Texture::setWidth(int newWidth)
QSize const &Texture::dimensions() const
{
d->dimensions.setWidth(newWidth);
/// @todo Update any Materials (and thus Surfaces) which reference this.
return d->dimensions;
}

int Texture::height() const
void Texture::setDimensions(QSize const &newDimensions)
{
return d->dimensions.height();
d->dimensions = newDimensions;
}

QSize const &Texture::dimensions() const
void Texture::setWidth(int newWidth)
{
return d->dimensions;
d->dimensions.setWidth(newWidth);
}

void Texture::setHeight(int newHeight)
{
d->dimensions.setHeight(newHeight);
/// @todo Update any Materials (and thus Surfaces) which reference this.
}

void Texture::setDimensions(QSize const &newDimensions)
{
d->dimensions = newDimensions;
/// @todo Update any Materials (and thus Surfaces) which reference this.
}

QPoint const &Texture::origin() const
Expand Down Expand Up @@ -235,6 +222,11 @@ Texture::Variants const &Texture::variants() const
return d->variants;
}

void Texture::clearVariants()
{
d->clearVariants();
}

void *Texture::analysisDataPointer(texture_analysisid_t analysisId) const
{
DENG2_ASSERT(VALID_TEXTURE_ANALYSISID(analysisId));
Expand Down

0 comments on commit 0c582f7

Please sign in to comment.