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

engines/stark/resources/object.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 AUTHORS
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/stark/resources/object.h"
00024 
00025 #include "common/debug-channels.h"
00026 #include "common/streamdebug.h"
00027 #include "common/util.h"
00028 
00029 #include "engines/stark/debug.h"
00030 #include "engines/stark/formats/xrc.h"
00031 
00032 namespace Stark {
00033 namespace Resources {
00034 
00035 Type::Type(ResourceType type) {
00036     _type = type;
00037 }
00038 
00039 Type::Type() {
00040     _type = kInvalid;
00041 }
00042 
00043 const char *Type::getName() const {
00044     static const struct {
00045         Type::ResourceType type;
00046         const char *name;
00047     } typeNames[] = {
00048             { Type::kInvalid,                "Invalid"                },
00049             { Type::kRoot,                   "Root"                   },
00050             { Type::kLevel,                  "Level"                  },
00051             { Type::kLocation,               "Location"               },
00052             { Type::kLayer,                  "Layer"                  },
00053             { Type::kCamera,                 "Camera"                 },
00054             { Type::kFloor,                  "Floor"                  },
00055             { Type::kFloorFace,              "FloorFace"              },
00056             { Type::kItem,                   "Item"                   },
00057             { Type::kScript,                 "Script"                 },
00058             { Type::kAnimHierarchy,          "AnimHierarchy"          },
00059             { Type::kAnim,                   "Anim"                   },
00060             { Type::kDirection,              "Direction"              },
00061             { Type::kImage,                  "Image"                  },
00062             { Type::kAnimScript,             "AnimScript"             },
00063             { Type::kAnimScriptItem,         "AnimScriptItem"         },
00064             { Type::kSoundItem,              "SoundItem"              },
00065             { Type::kPath,                   "Path"                   },
00066             { Type::kFloorField,             "FloorField"             },
00067             { Type::kBookmark,               "Bookmark"               },
00068             { Type::kKnowledgeSet,           "KnowledgeSet"           },
00069             { Type::kKnowledge,              "Knowledge"              },
00070             { Type::kCommand,                "Command"                },
00071             { Type::kPATTable,               "PATTable"               },
00072             { Type::kContainer,              "Container"              },
00073             { Type::kDialog,                 "Dialog"                 },
00074             { Type::kSpeech,                 "Speech"                 },
00075             { Type::kLight,                  "Light"                  },
00076             { Type::kCursor,                 "Cursor"                 },
00077             { Type::kBonesMesh,              "BonesMesh"              },
00078             { Type::kScroll,                 "Scroll"                 },
00079             { Type::kFMV,                    "FMV"                    },
00080             { Type::kLipSync,                "LipSynch"               },
00081             { Type::kAnimSoundTrigger,       "AnimSoundTrigger"       },
00082             { Type::kString,                 "String"                 },
00083             { Type::kTextureSet,             "TextureSet"             }
00084     };
00085 
00086     for (uint i = 0; i < ARRAYSIZE(typeNames); i++) {
00087         if (typeNames[i].type == _type) {
00088             return typeNames[i].name;
00089         }
00090     }
00091 
00092     return nullptr;
00093 }
00094 
00095 Type::ResourceType Type::get() const {
00096     return _type;
00097 }
00098 
00099 Object::Object(Object *parent, byte subType, uint16 index, const Common::String &name) :
00100         _parent(parent),
00101         _type(Type::kInvalid),
00102         _subType(subType),
00103         _index(index),
00104         _name(name) {
00105 }
00106 
00107 Object::~Object() {
00108     // Delete the children resources
00109     Common::Array<Object *>::iterator i = _children.begin();
00110     while (i != _children.end()) {
00111         delete *i;
00112         i++;
00113     }
00114 }
00115 
00116 void Object::readData(Formats::XRCReadStream *stream) {
00117 }
00118 
00119 void Object::printData() {
00120 }
00121 
00122 void Object::saveLoad(ResourceSerializer *serializer) {
00123 }
00124 
00125 void Object::saveLoadCurrent(ResourceSerializer *serializer) {
00126 }
00127 
00128 void Object::onPostRead() {
00129 }
00130 
00131 void Object::onAllLoaded() {
00132     Common::Array<Object *>::iterator i = _children.begin();
00133     while (i != _children.end()) {
00134         (*i)->onAllLoaded();
00135         i++;
00136     }
00137 }
00138 
00139 void Object::onEnterLocation() {
00140     Common::Array<Object *>::iterator i = _children.begin();
00141     while (i != _children.end()) {
00142         (*i)->onEnterLocation();
00143         i++;
00144     }
00145 }
00146 
00147 void Object::onGameLoop() {
00148     Common::Array<Object *>::iterator i = _children.begin();
00149     while (i != _children.end()) {
00150         (*i)->onGameLoop();
00151         i++;
00152     }
00153 }
00154 
00155 void Object::onEnginePause(bool pause) {
00156     Common::Array<Object *>::iterator i = _children.begin();
00157     while (i != _children.end()) {
00158         (*i)->onEnginePause(pause);
00159         i++;
00160     }
00161 }
00162 
00163 void Object::onExitLocation() {
00164     Common::Array<Object *>::iterator i = _children.begin();
00165     while (i != _children.end()) {
00166         (*i)->onExitLocation();
00167         i++;
00168     }
00169 }
00170 
00171 void Object::onPreDestroy() {
00172     Common::Array<Object *>::iterator i = _children.begin();
00173     while (i != _children.end()) {
00174         (*i)->onPreDestroy();
00175         i++;
00176     }
00177 }
00178 
00179 void Object::print(uint depth) {
00180     printDescription(depth);
00181     printData();
00182 
00183     // Recursively print the children resources
00184     for (uint i = 0; i < _children.size(); i++) {
00185         _children[i]->print(depth + 1);
00186     }
00187 }
00188 
00189 void Object::printDescription(uint depth) const {
00190     // Build the resource type string
00191     Common::String type(_type.getName());
00192     if (type.empty()) {
00193         type = Common::String::format("%d", _type.get());
00194     }
00195 
00196     // Build the resource description
00197     Common::String description = Common::String::format("%s - %s - (sub=%d, index=%d)", type.c_str(), _name.c_str(), _subType, _index);
00198     printWithDepth(depth, description);
00199 }
00200 
00201 void Object::printWithDepth(uint depth, const Common::String &string) const {
00202     Common::String prefix;
00203     for (uint i = 0; i < depth; i++) {
00204         prefix += "-";
00205     }
00206 
00207     debug("%s %s", prefix.c_str(), string.c_str());
00208 }
00209 
00210 Object *Object::findChildWithIndex(Type type, uint16 index, int subType) const {
00211     for (uint i = 0; i < _children.size(); i++) {
00212         if (_children[i]->getType() == type
00213                 && (_children[i]->getSubType() == subType || subType == -1)
00214                 && _children[i]->getIndex() == index) {
00215             // Found a matching child
00216             return _children[i];
00217         }
00218     }
00219 
00220     return nullptr;
00221 }
00222 
00223 Object *Object::findChildWithOrder(Type type, uint16 order, int subType) const {
00224     uint16 count = 0;
00225     for (uint i = 0; i < _children.size(); i++) {
00226         if (_children[i]->getType() == type
00227             && (_children[i]->getSubType() == subType || subType == -1)) {
00228             if (count == order) {
00229                 // Found a matching child
00230                 return _children[i];
00231             }
00232             count++;
00233         }
00234     }
00235 
00236     return nullptr;
00237 }
00238 
00239 Object *Object::findChildWithName(Type type, const Common::String &name, int subType) const {
00240     for (uint i = 0; i < _children.size(); ++i) {
00241         if (_children[i]->getType() == type
00242                 && (_children[i]->getSubType() == subType || subType == -1)
00243                 && _children[i]->getName() == name) {
00244             // Found a matching child
00245             return _children[i];
00246         }
00247     }
00248 
00249     return nullptr;
00250 }
00251 
00252 template<>
00253 Object *Object::cast<Object>(Object *resource) {
00254     // No type check when asking for the abstract resource
00255     return resource;
00256 }
00257 
00258 template<>
00259 Common::Array<Object *> Object::listChildren<Object>(int subType) const {
00260     assert(subType == -1);
00261 
00262     Common::Array<Object *> list;
00263 
00264     for (uint i = 0; i < _children.size(); i++) {
00265         list.push_back(_children[i]);
00266     }
00267 
00268     return list;
00269 }
00270 
00271 template<>
00272 Object *Object::findParent() {
00273     return _parent;
00274 }
00275 
00276 void Object::addChild(Object *child) {
00277     _children.push_back(child);
00278 }
00279 
00280 UnimplementedResource::UnimplementedResource(Object *parent, Type type, byte subType, uint16 index, const Common::String &name) :
00281         Object(parent, subType, index, name),
00282         _dataLength(0),
00283         _data(nullptr) {
00284     _type = type;
00285 }
00286 
00287 UnimplementedResource::~UnimplementedResource() {
00288     // Delete this resource's data
00289     delete[] _data;
00290 }
00291 
00292 void UnimplementedResource::readData(Formats::XRCReadStream *stream) {
00293     // Read the data
00294     _dataLength = stream->size();
00295     _data = new byte[_dataLength];
00296     uint32 bytesRead = stream->read(_data, _dataLength);
00297 
00298     // Verify the whole array could be read
00299     if (bytesRead != _dataLength) {
00300         error("Stark::UnimplementedResource: data length mismatch (%d != %d)", bytesRead, _dataLength);
00301     }
00302 }
00303 
00304 void UnimplementedResource::printData() {
00305     // Print the resource data
00306     if (_data) {
00307         Common::hexdump(_data, _dataLength);
00308     }
00309 }
00310 
00311 } // End of namespace Resources
00312 } // End of namespace Stark


Generated on Sat Mar 23 2019 05:01:52 for ResidualVM by doxygen 1.7.1
curved edge   curved edge