Skip to content

Commit

Permalink
ResourceSystem: Model and ModelDef searches return references
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Dec 4, 2013
1 parent c985395 commit 5140981
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 108 deletions.
7 changes: 5 additions & 2 deletions doomsday/client/include/resource/abstractfont.h
Expand Up @@ -25,12 +25,13 @@
#include <de/Vector>
#include <QFlags>

#define MAX_CHARS 256 // Normal 256 ANSI characters.

namespace de {
class FontManifest;
}

/// Special value used to signify an invalid font id.
#define NOFONTID 0

/**
* Abstract font resource.
*
Expand All @@ -54,6 +55,8 @@ class AbstractFont
};
Q_DECLARE_FLAGS(Flags, Flag)

static int const MAX_CHARS = 256; // Normal 256 ANSI characters.

public:
/// Resource manifest for the font.
de::FontManifest &_manifest;
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/include/resource/compositebitmapfont.h
Expand Up @@ -39,7 +39,7 @@ class CompositeBitmapFont : public AbstractFont
{
de::Rectanglei geometry;
patchid_t patch;
de::Texture::Variant *tex;
de::TextureVariant *tex;
uint8_t border;
bool haveSourceImage;
};
Expand Down Expand Up @@ -74,7 +74,7 @@ class CompositeBitmapFont : public AbstractFont

patchid_t glyphPatch(uchar ch);
void glyphSetPatch(uchar ch, de::String encodedPatchName);
de::Texture::Variant *glyphTexture(uchar ch);
de::TextureVariant *glyphTexture(uchar ch);
uint glyphTextureBorder(uchar ch);

private:
Expand Down
48 changes: 29 additions & 19 deletions doomsday/client/include/resource/resourcesystem.h
Expand Up @@ -39,11 +39,6 @@
#include <de/String>
#include <de/System>

#ifdef __CLIENT__
/// Special value used to signify an invalid font id.
#define NOFONTID 0
#endif

/**
* Logical resources; materials, packages, textures, etc...
*
Expand Down Expand Up @@ -88,11 +83,8 @@ class ResourceSystem : public de::System
/// The referenced manifest was not found. @ingroup errors
DENG2_ERROR(MissingManifestError);

/// The referenced color palette could not be found. @ingroup errors
DENG2_ERROR(MissingColorPaletteError);

/// The referenced sprite could not be found. @ingroup errors
DENG2_ERROR(MissingSpriteError);
/// The referenced resource was not found. @ingroup errors
DENG2_ERROR(MissingResourceError);

/// An unknown material group was referenced. @ingroup errors
DENG2_ERROR(UnknownMaterialGroupError);
Expand All @@ -101,6 +93,9 @@ class ResourceSystem : public de::System
DENG2_ERROR(UnknownMaterialIdError);

#ifdef __CLIENT__
/// An unknown model def was referenced. @ingroup errors
DENG2_ERROR(UnknownModelDefError);

/// The specified font id was invalid (out of range). @ingroup errors
DENG2_ERROR(UnknownFontIdError);
#endif
Expand Down Expand Up @@ -541,30 +536,38 @@ class ResourceSystem : public de::System
int indexOf(ModelDef const *modelDef);

/**
* Retrieve a model by it's unique @a id. O(1)
* Convenient method of looking up a concrete model resource in the collection
* given it's unique identifier.
*
* @return Pointer to the associated model; otherwise @c 0.
* @return The associated model resource.
*/
Model *model(modelid_t id);
Model &model(modelid_t id);

/**
* Returns the total number of model definitions in the system.
* Determines if a model definition exists with the given @a id.
* @return @c true, if a definition exists; otherwise @a false.
*
* @see modelDef()
*/
int modelDefCount() const;
bool hasModelDef(de::String id) const;

/**
* Retrieve a model definition by it's unique @a index. O(1)
*
* @return Pointer to the associated definition; otherwise @c 0.
* @return Pointer to the associated definition.
*
* @see modelDefCount()
*/
ModelDef *modelDef(int index);
ModelDef &modelDef(int index);

/**
* Lookup a model definition by it's unique @a id. O(n)
*
* @return Found model definition; otherwise @c 0.
* @return Found model definition.
*
* @see hasModelDef()
*/
ModelDef *modelDef(char const *id);
ModelDef &modelDef(de::String id);

/**
* Lookup a model definition for the specified mobj @a stateIndex.
Expand All @@ -585,6 +588,13 @@ class ResourceSystem : public de::System
*/
float modelDefForMobj(struct mobj_s const *mo, ModelDef **mdef, ModelDef **nextmdef);

/**
* Returns the total number of model definitions in the system.
*
* @see modelDef()
*/
int modelDefCount() const;

/// @todo Refactor away. Used for animating particle/sky models.
void setModelDefFrame(ModelDef &modelDef, int frame);

Expand Down
45 changes: 23 additions & 22 deletions doomsday/client/src/def_main.cpp
Expand Up @@ -1875,45 +1875,46 @@ static void initMaterialGroup(ded_group_t &def)
}
}

void Def_PostInit(void)
void Def_PostInit()
{
#ifdef __CLIENT__

// Particle generators: model setup.
ded_ptcgen_t *gen = defs.ptcGens;
for(int i = 0; i < defs.count.ptcGens.num; ++i, gen++)
{
char name[40];
ded_ptcstage_t *st = gen->stages;
for(int k = 0; k < gen->stageCount.num; ++k, st++)
{
if(st->type < PTC_MODEL || st->type >= PTC_MODEL + MAX_PTC_MODELS)
continue;

sprintf(name, "Particle%02i", st->type - PTC_MODEL);

ModelDef *modef = App_ResourceSystem().modelDef(name);
if(!modef || modef->subModelId(0) == NOMODELID)
st->model = -1;
try
{
st->model = -1;
continue;
}
ModelDef &modef = App_ResourceSystem().modelDef(String("Particle%1").arg(st->type - PTC_MODEL, 2, 10, QChar('0')));
if(modef.subModelId(0) == NOMODELID)
{
continue;
}

Model *mdl = App_ResourceSystem().model(modef->subModelId(0));
DENG2_ASSERT(mdl != 0);
Model &mdl = App_ResourceSystem().model(modef.subModelId(0));

st->model = App_ResourceSystem().indexOf(modef);
st->frame = mdl->frameNumber(st->frameName);
if(st->frame < 0) st->frame = 0;
if(st->endFrameName[0])
{
st->endFrame = mdl->frameNumber(st->endFrameName);
if(st->endFrame < 0) st->endFrame = 0;
}
else
{
st->endFrame = -1;
st->model = App_ResourceSystem().indexOf(&modef);
st->frame = mdl.frameNumber(st->frameName);
if(st->frame < 0) st->frame = 0;
if(st->endFrameName[0])
{
st->endFrame = mdl.frameNumber(st->endFrameName);
if(st->endFrame < 0) st->endFrame = 0;
}
else
{
st->endFrame = -1;
}
}
catch(ResourceSystem::UnknownModelDefError const &)
{} // Ignore this error.
}
}

Expand Down
9 changes: 4 additions & 5 deletions doomsday/client/src/render/rend_model.cpp
Expand Up @@ -473,7 +473,7 @@ static ModelFrame &visibleModelFrame(ModelDef &modef, int subnumber, int mobjId)
curFrame += mobjId % sub.frameRange;
}

return App_ResourceSystem().model(sub.modelId)->frame(curFrame);
return App_ResourceSystem().model(sub.modelId).frame(curFrame);
}

/**
Expand Down Expand Up @@ -733,7 +733,7 @@ static int chooseSkin(ModelDef &mf, int submodel, int id, int selector, int tmap
}

SubmodelDef &smf = mf.subModelDef(submodel);
Model *mdl = App_ResourceSystem().model(smf.modelId);
Model &mdl = App_ResourceSystem().model(smf.modelId);
int skin = smf.skin;

// Selskin overrides the skin range.
Expand Down Expand Up @@ -766,8 +766,7 @@ static int chooseSkin(ModelDef &mf, int submodel, int id, int selector, int tmap
skin = tmap;
}

DENG2_ASSERT(mdl != 0);
if(skin < 0 || skin >= mdl->skinCount())
if(skin < 0 || skin >= mdl.skinCount())
{
skin = 0;
}
Expand All @@ -781,7 +780,7 @@ static void drawSubmodel(uint number, drawmodelparams_t const &parm)
ModelDef *mf = parm.mf, *mfNext = parm.nextMF;
SubmodelDef const &smf = mf->subModelDef(number);

Model &mdl = *App_ResourceSystem().model(smf.modelId);
Model &mdl = App_ResourceSystem().model(smf.modelId);

// Do not bother with infinitely small models...
if(mf->scale == Vector3f(0, 0, 0))
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/render/rend_particle.cpp
Expand Up @@ -418,7 +418,7 @@ static void setupModelParamsForParticle(drawmodelparams_t *params,
params->distance = dist;

params->extraScale = size; // Extra scaling factor.
params->mf = App_ResourceSystem().modelDef(dst->model);
params->mf = &App_ResourceSystem().modelDef(dst->model);
params->alwaysInterpolate = true;

int frame;
Expand Down
25 changes: 15 additions & 10 deletions doomsday/client/src/render/sky.cpp
Expand Up @@ -339,19 +339,24 @@ static void setupSkyModels(ded_sky_t *def)
for(int i = 0; i < MAX_SKY_MODELS; ++i, modef++, sm++)
{
// Is the model ID set?
sm->model = App_ResourceSystem().modelDef(modef->id);
if(!sm->model || !sm->model->subCount())
try
{
continue;
}
sm->model = &App_ResourceSystem().modelDef(modef->id);
if(!sm->model->subCount())
{
continue;
}

// There is a model here.
skyModelsInited = true;
// There is a model here.
skyModelsInited = true;

sm->def = modef;
sm->maxTimer = (int) (TICSPERSEC * modef->frameInterval);
sm->yaw = modef->yaw;
sm->frame = sm->model->subModelDef(0).frame;
sm->def = modef;
sm->maxTimer = (int) (TICSPERSEC * modef->frameInterval);
sm->yaw = modef->yaw;
sm->frame = sm->model->subModelDef(0).frame;
}
catch(ResourceSystem::UnknownModelDefError const &)
{} // Ignore this error.
}
}

Expand Down
8 changes: 4 additions & 4 deletions doomsday/client/src/resource/api_resource.cpp
Expand Up @@ -121,7 +121,7 @@ DENG_EXTERN_C colorpaletteid_t R_GetColorPaletteNumForName(char const *name)
{
return App_ResourceSystem().colorPalette(name).id();
}
catch(ResourceSystem::MissingColorPaletteError const &er)
catch(ResourceSystem::MissingResourceError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ", ignoring.");
Expand All @@ -138,7 +138,7 @@ DENG_EXTERN_C char const *R_GetColorPaletteNameForNum(colorpaletteid_t id)
ColorPalette &palette = App_ResourceSystem().colorPalette(id);
return App_ResourceSystem().colorPaletteName(palette).toUtf8().constData();
}
catch(ResourceSystem::MissingColorPaletteError const &er)
catch(ResourceSystem::MissingResourceError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ", ignoring.");
Expand Down Expand Up @@ -174,7 +174,7 @@ DENG_EXTERN_C void R_GetColorPaletteRGBubv(colorpaletteid_t paletteId, int color
rgb[2] = texGammaLut[rgb[2]];
}
}
catch(ResourceSystem::MissingColorPaletteError const &er)
catch(ResourceSystem::MissingResourceError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ", ignoring.");
Expand Down Expand Up @@ -214,7 +214,7 @@ DENG_EXTERN_C void R_GetColorPaletteRGBf(colorpaletteid_t paletteId, int colorId
rgb[2] = palColor.z;
}
}
catch(ResourceSystem::MissingColorPaletteError const &er)
catch(ResourceSystem::MissingResourceError const &er)
{
// Log but otherwise ignore this error.
LOG_WARNING(er.asText() + ", ignoring.");
Expand Down

0 comments on commit 5140981

Please sign in to comment.