From 518e457dcf851ed5494c42008563a5e31a7921b6 Mon Sep 17 00:00:00 2001 From: danij Date: Tue, 22 Jan 2013 09:01:45 +0000 Subject: [PATCH] Refactor|Material: Relocated logic from Material::setDefinition() to def_main.cpp Similarly, Material lacks the knowledge of how to update itself after a definition reset so therefore this logic should instead be implemented externally. --- doomsday/client/src/def_main.cpp | 135 ++++++++++++++-------- doomsday/client/src/resource/material.cpp | 37 +----- 2 files changed, 90 insertions(+), 82 deletions(-) diff --git a/doomsday/client/src/def_main.cpp b/doomsday/client/src/def_main.cpp index 803809c1fd..980b134956 100644 --- a/doomsday/client/src/def_main.cpp +++ b/doomsday/client/src/def_main.cpp @@ -1020,6 +1020,93 @@ static void generateMaterialDefs() textures.iterateDeclared("Sprites", generateMaterialDefForTextureWorker); } +static void updateMaterialFromDef(Material &material, ded_material_t &def) +{ + // Nothing to do? + if(material.definition() == &def) return; + + material.setDefinition(&def); + // Textures are updated automatically at prepare-time, so just clear them. + material.setDetailTexture(0); + material.setShinyTexture(0); + material.setShinyMaskTexture(0); + + MaterialManifest &manifest = material.manifest(); + + material.setFlags(def.flags); + material.setDimensions(QSize(def.width, def.height)); + material.setAudioEnvironment(S_AudioEnvironmentForMaterial(def.uri)); + + // Update custom status. + /// @todo This should take into account the whole definition, not just whether + /// the primary layer's first texture is custom or not. + manifest.setCustom(false); + Material::Layer const &layer = *material.layers()[0]; + if(layer.stageCount() > 0 && layer.stages()[0]->texture) + { + try + { + de::Uri *texUri = reinterpret_cast(layer.stages()[0]->texture); + if(Texture *tex = App_Textures()->find(*texUri).texture()) + { + manifest.setCustom(tex->flags().testFlag(Texture::Custom)); + } + } + catch(Textures::NotFoundError const &) + {} // Ignore this error. + } + + // Re-link definitions. + manifest.linkDefinitions(); +} + +static void interpretMaterialDef(ded_material_t &def) +{ + for(int i = 0; i < DED_MAX_MATERIAL_DECORATIONS; ++i) + { + ded_material_decoration_t* lig = &def.decorations[i]; + for(int k = 0; k < lig->stageCount.num; ++k) + { + ded_decorlight_stage_t *stage = &lig->stages[k]; + + if(stage->up) + { + defineLightmap(stage->up); + } + if(stage->down) + { + defineLightmap(stage->down); + } + if(stage->sides) + { + defineLightmap(stage->sides); + } + if(stage->flare) + { + defineFlaremap(stage->flare); + } + } + } + + try + { + // Update existing. + Material &material = App_Materials().find(*reinterpret_cast(def.uri)).material(); + + /// @todo We should be able to rebuild the variants. + material.clearVariants(); + + updateMaterialFromDef(material, def); + } + catch(Materials::Manifest::MissingMaterialError const&) + {} // Ignore this error. + catch(Materials::NotFoundError const &) + { + // A new Material. + App_Materials().newFromDef(def); + } +} + void Def_Read() { if(defsInited) @@ -1171,53 +1258,7 @@ void Def_Read() // Materials. for(int i = 0; i < defs.count.materials.num; ++i) { - ded_material_t *def = &defs.materials[i]; - - for(int k = 0; k < DED_MAX_MATERIAL_DECORATIONS; ++k) - { - ded_material_decoration_t* lig = &def->decorations[k]; - for(int m = 0; m < lig->stageCount.num; ++m) - { - ded_decorlight_stage_t *stage = &lig->stages[m]; - - if(stage->up) - { - defineLightmap(stage->up); - } - if(stage->down) - { - defineLightmap(stage->down); - } - if(stage->sides) - { - defineLightmap(stage->sides); - } - if(stage->flare) - { - defineFlaremap(stage->flare); - } - } - } - - try - { - // Update existing. - Material &material = App_Materials().find(*reinterpret_cast(def->uri)).material(); - - /// @todo We should be able to rebuild the variants. - material.clearVariants(); - material.setDefinition(def); - - // Re-link definitions. - material.manifest().linkDefinitions(); - } - catch(Materials::Manifest::MissingMaterialError const&) - {} // Ignore this error. - catch(Materials::NotFoundError const &) - { - // A new Material. - App_Materials().newFromDef(*def); - } + interpretMaterialDef(defs.materials[i]); } DED_NewEntries((void **) &stateLights, &countStateLights, sizeof(*stateLights), defs.count.states.num); diff --git a/doomsday/client/src/resource/material.cpp b/doomsday/client/src/resource/material.cpp index e11f8a160c..2113301855 100644 --- a/doomsday/client/src/resource/material.cpp +++ b/doomsday/client/src/resource/material.cpp @@ -228,41 +228,8 @@ ded_material_t *Material::definition() const void Material::setDefinition(ded_material_t *def) { - if(d->def != def) - { - d->def = def; - - // Textures are updated automatically at prepare-time, so just clear them. - setDetailTexture(0); - setShinyTexture(0); - setShinyMaskTexture(0); - } - - if(!d->def) return; - - MaterialManifest &_manifest = manifest(); - - d->flags = d->def->flags; - setDimensions(QSize(def->width, def->height)); - setAudioEnvironment(S_AudioEnvironmentForMaterial(def->uri)); - - // Update custom status. - /// @todo This should take into account the whole definition, not just whether - /// the primary layer's first texture is custom or not. - _manifest.setCustom(false); - if(d->layers[0]->stageCount() > 0 && d->layers[0]->stages()[0]->texture) - { - try - { - de::Uri *texUri = reinterpret_cast(d->layers[0]->stages()[0]->texture); - if(Texture *tex = App_Textures()->find(*texUri).texture()) - { - _manifest.setCustom(tex->flags().testFlag(Texture::Custom)); - } - } - catch(Textures::NotFoundError const &) - {} // Ignore this error. - } + if(d->def == def) return; + d->def = def; } QSize const &Material::dimensions() const