ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

model_component.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/grim/debug.h"
00024 #include "engines/grim/model.h"
00025 #include "engines/grim/resource.h"
00026 #include "engines/grim/grim.h"
00027 #include "engines/grim/set.h"
00028 #include "engines/grim/gfx_base.h"
00029 #include "engines/grim/colormap.h"
00030 #include "engines/grim/animation.h"
00031 
00032 #include "engines/grim/costume/model_component.h"
00033 #include "engines/grim/costume/main_model_component.h"
00034 #include "engines/grim/costume/mesh_component.h"
00035 
00036 namespace Grim {
00037 
00038 #define DEFAULT_COLORMAP "item.cmp"
00039 
00040 ModelComponent::ModelComponent(Component *p, int parentID, const char *filename, Component *prevComponent, tag32 t) :
00041         Component(p, parentID, filename, t),
00042         _obj(nullptr), _hier(nullptr), _animation(nullptr), _animated(false) {
00043     const char *comma = strchr(filename, ',');
00044 
00045     // Can be called with a comma and a numeric parameter afterward, but
00046     // the use for this parameter is currently unknown
00047     // Example: At the "scrimshaw parlor" in Rubacava the object
00048     // "manny_cafe.3do,1" is requested
00049     if (comma) {
00050         _name = Common::String(filename, comma);
00051         warning("Comma in model components not supported: %s", filename);
00052     }
00053     _prevComp = prevComponent;
00054 }
00055 
00056 ModelComponent::~ModelComponent() {
00057     if (_hier && _hier->_parent) {
00058         _hier->_parent->removeChild(_hier);
00059     }
00060 
00061     delete _obj;
00062     delete _animation;
00063 }
00064 
00065 void ModelComponent::init() {
00066     if (_prevComp && _prevComp->isComponentType('M','M','D','L')) {
00067         _previousCmap = _prevComp->getCMap();
00068     }
00069     // Skip loading if it was initialized
00070     // by the sharing MainModelComponent
00071     // constructor before
00072     if (!_obj) {
00073         CMapPtr cm = getCMap();
00074 
00075         // Get the default colormap if we haven't found
00076         // a valid colormap
00077         if (!cm && g_grim->getCurrSet())
00078             cm = g_grim->getCurrSet()->getCMap();
00079         if (!cm) {
00080             Debug::warning(Debug::Costumes, "No colormap specified for %s, using %s", _name.c_str(), DEFAULT_COLORMAP);
00081 
00082             cm = g_resourceloader->getColormap(DEFAULT_COLORMAP);
00083         }
00084 
00085         // If we're the child of a mesh component, put our nodes in the
00086         // parent object's tree.
00087         if (_parent) {
00088             MeshComponent *mc = static_cast<MeshComponent *>(_parent);
00089             _obj = g_resourceloader->loadModel(_name, cm, mc->getModel());
00090             _hier = _obj->getHierarchy();
00091             mc->getNode()->addChild(_hier);
00092         } else {
00093             _obj = g_resourceloader->loadModel(_name, cm);
00094             _hier = _obj->getHierarchy();
00095             Debug::warning(Debug::Costumes, "Parent of model %s wasn't a mesh", _name.c_str());
00096         }
00097 
00098         // Use parent availablity to decide whether to default the
00099         // component to being visible
00100         if (_parent)
00101             setKey(0);
00102         else
00103             setKey(1);
00104     }
00105 
00106     if (!_animation) {
00107         _animation = new AnimManager();
00108     }
00109 }
00110 
00111 void ModelComponent::setKey(int val) {
00112     _visible = (val != 0);
00113     _hier->_hierVisible = _visible;
00114 }
00115 
00116 void ModelComponent::reset() {
00117     _visible = false;
00118     _hier->_hierVisible = _visible;
00119 }
00120 
00121 AnimManager *ModelComponent::getAnimManager() const {
00122     return _animation;
00123 }
00124 
00125 int ModelComponent::update(uint time) {
00126     // First reset the current animation.
00127     for (int i = 0; i < getNumNodes(); i++) {
00128         _hier[i]._animPos = _hier[i]._pos;
00129         _hier[i]._animRot = _hier[i]._rot;
00130     }
00131 
00132     _animated = false;
00133     return 0;
00134 }
00135 
00136 void ModelComponent::animate() {
00137     if (_animated) {
00138         return;
00139     }
00140 
00141     _animation->animate(_hier, getNumNodes());
00142     _animated = true;
00143 }
00144 
00145 void ModelComponent::resetColormap() {
00146     CMap *cm;
00147 
00148     cm = getCMap();
00149     if (_obj && cm)
00150         _obj->reload(cm);
00151 }
00152 
00153 void ModelComponent::restoreState(SaveGame *state) {
00154     _hier->_hierVisible = _visible;
00155 }
00156 
00157 int ModelComponent::getNumNodes() {
00158     return _obj->getNumNodes();
00159 }
00160 
00161 void ModelComponent::translateObject(ModelNode *node, bool reset) {
00162     if (node->_parent)
00163         translateObject(node->_parent, reset);
00164 
00165     if (reset) {
00166         node->translateViewpointFinish();
00167     } else {
00168         node->translateViewpointStart();
00169         node->translateViewpoint();
00170     }
00171 }
00172 
00173 void ModelComponent::translateObject(bool res) {
00174     ModelNode *node = _hier->_parent;
00175     if (node) {
00176         translateObject(node, res);
00177     }
00178 }
00179 
00180 void ModelComponent::draw() {
00181     // If the object was drawn by being a component
00182     // of it's parent then don't draw it
00183 
00184     if (_parent && _parent->isVisible())
00185         return;
00186     // Need to translate object to be in accordance
00187     // with the setup of the parent
00188     translateObject(false);
00189 
00190     _hier->draw();
00191 
00192     // Need to un-translate when done
00193     translateObject(true);
00194 }
00195 
00196 void ModelComponent::getBoundingBox(int *x1, int *y1, int *x2, int *y2) {
00197     // If the object was drawn by being a component
00198     // of it's parent then don't draw it
00199 
00200     if (_parent && _parent->isVisible())
00201         return;
00202     // Need to translate object to be in accordance
00203     // with the setup of the parent
00204     translateObject(false);
00205 
00206     _hier->getBoundingBox(x1, y1, x2, y2);
00207 
00208     // Need to un-translate when done
00209     translateObject(true);
00210 }
00211 
00212 } // end of namespace Grim


Generated on Sat Mar 16 2019 05:01:47 for ResidualVM by doxygen 1.7.1
curved edge   curved edge